Open Kilda Java Documentation
nodes.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Copyright 2017 Telstra Open Source
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 
17 import json
18 import requests
19 
20 class Nodes(object):
21  def toJSON(self):
22  return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=4)
23 
24 class Edge(object):
25  def toJSON(self):
26  return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=4)
27 
28 class Link(object):
29  def toJSON(self):
30  return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=4)
31 
32 
33 
34 
35 data = {'query' : 'MATCH (n) return n'}
36 auth = ('neo4j', 'temppass')
37 result_switches = requests.post('http://localhost:7474/db/data/cypher', data=data, auth=auth)
38 
39 
40 j_switches = json.loads(result_switches.text)
41 nodes = Nodes()
42 nodes.edges = []
43 
44 for n in j_switches['data']:
45  for r in n:
46  result_relationships = requests.get(str(r['outgoing_relationships']), auth=auth)
47  j_paths = json.loads(result_relationships.text)
48  outgoing_relationships = []
49  for j_path in j_paths:
50  target = Link()
51  if j_path['type'] == u'isl':
52  edge = Edge()
53  source = Link()
54  source.label = r['data']['name']
55  source.id = r['metadata']['id']
56 
57  dest_node = requests.get(str(j_path['end']), auth=auth)
58  j_dest_node = json.loads(dest_node.text)
59 
60  target.label = j_path['data']['dst_switch']
61  target.id = j_dest_node['metadata']['id']
62  edge.value = "{} to {}".format(source.label, target.label)
63  edge.source = source
64  edge.target = target
65  nodes.edges.append(edge)
66 print nodes.toJSON()
67 
68 '''
69 nodes = Nodes()
70 nodes.edges = []
71 
72 i = 0
73 
74 while i < 10:
75  edge = Edge()
76  source = Link()
77  target = Link()
78 
79  source.id = i
80  source.label = "s{}".format(str(i))
81 
82  target.id = i + 1
83  target.label = "s{}".format(str(i + 1))
84 
85  edge.value = "link: {}".format(str(i))
86  edge.source = source
87  edge.target = target
88 
89 
90  nodes.edges.append(edge)
91 
92  i += 1
93 
94 #print nodes.toJSON()
95 
96 '''
def toJSON(self)
Definition: nodes.py:21
def toJSON(self)
Definition: nodes.py:25