-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdftestnew.py
264 lines (223 loc) · 7.64 KB
/
rdftestnew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import rdflib
import requests
import json
import coreapi
from topology import Topology, Node, Port, IntermediateNode
class RdfDecoder():
SCHEMAS = {
'networkresources': 'http://unis.crest.iu.edu/schema/20151104/networkresource#',
'nodes': 'http://unis.crest.iu.edu/schema/20151104/node#',
'domains': 'http://unis.crest.iu.edu/schema/20151104/domain#',
'ports': 'http://unis.crest.iu.edu/schema/20151104/port#',
'links': 'http://unis.crest.iu.edu/schema/20151104/link#',
'paths': 'http://unis.crest.iu.edu/schema/20151104/path#',
'networks': 'http://unis.crest.iu.edu/schema/20151104/network#',
'topologies': 'http://unis.crest.iu.edu/schema/20151104/topology#',
'services': 'http://unis.crest.iu.edu/schema/20151104/service#',
'blipp': 'http://unis.crest.iu.edu/schema/20151104/blipp#',
'metadata': 'http://unis.crest.iu.edu/schema/20151104/metadata#',
'datum': 'http://unis.crest.iu.edu/schema/20151104/datum#',
'data': 'http://unis.crest.iu.edu/schema/20151104/data#',
'ipports': 'http://unis.crest.iu.edu/schema/ext/ipport/1/ipport#'
}
def __init__(self, rdf_file_name, host_name, port_number='8888'):
self.g=rdflib.Graph()
self.g.load(rdf_file_name)
self.prefix = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ndl: <http://www.science.uva.nl/research/sne/ndl#> """;
self.uri="http://"+host_name+":"+port_number+"/"
print("URI:"+self.uri)
self.inode_object = IntermediateNode()
self.topology_object = Topology()
self.clean_all()
# self.decode()
def clean_all(self):
print("Starting the initial CLEANUP PROCESS")
self.clean_nodes()
self.delete_ports()
def clean_nodes(self):
print("Deleting the nodes")
nodes_uri=self.uri+"nodes"
nodes_list = coreapi.get(nodes_uri)
for node_dict in nodes_list:
print node_dict['selfRef']
# requests.delete(node_dict['selfRef'])
requests.delete(nodes_uri+"/"+node_dict['selfRef'].split("/")[3])
def delete_ports(self):
print("Deleting the ports")
ports_uri=self.uri+"ports"
ports_list = coreapi.get(ports_uri)
for port_dict in ports_list:
print port_dict['selfRef']
# requests.delete(port_dict['selfRef'])
requests.delete(ports_uri+"/"+port_dict['selfRef'].split("/")[3])
def decode(self):
print("Starting the DECODE PROCESS")
self.create_nodes_from_rdf()
self.update_node_refs()
self.create_ports_from_rdf()
self.update_port_refs()
self.build_port_ref_in_nodes()
print("Starting Links creation")
self.create_links_from_rdf()
print("Printing Inodes")
self.inode_object.print_inodes()
def create_nodes_from_rdf(self):
node_only_query=self.prefix+"""
SELECT ?name
WHERE {
?x rdf:type ndl:Device . ?x ndl:name ?name.
OPTIONAL {
?y ndl:connectedTo ?z .
?z rdf:type ndl:Interface .
?z ndl:name ?neighbour
} . OPTIONAL {
?y ndl:capacity ?capacity .
?y ndl:encapsulation ?type
} .
}"""
nodes_uri=self.uri+"nodes"
nodes = []
for row in self.g.query(node_only_query):
node = dict()
node["$schema"]=self.SCHEMAS['nodes']
node["name"]=row.name
# ports = []
# port={'href':'instageni.illinois.edu_authority_cm_slice_idms','rel': 'full'}
# ports.append(port)
# node["ports"]=ports
print "Node::"
print row.name
nodes.append(node)
print "::FINAL JSON::"
json_data = json.dumps(nodes)
print("NODE URI::"+nodes_uri)
print("JSON DATA:"+json_data)
requests.post(nodes_uri, data = json_data)
def update_node_refs(self):
nodes_uri=self.uri+"nodes"
nodes_list = coreapi.get(nodes_uri)
for check_node in nodes_list:
print check_node['name']
print check_node['selfRef']
node_object = Node(check_node['name'])
self.topology_object.add_node(self.getId(check_node['selfRef']), node_object)
print "\n"
print("PRINIIIIIII")
self.topology_object.display_topology()
############# PORTS
def getId(self, ref_url):
"""
http://10.10.0.135:8888/nodes/56f88569e1382308b0b6a2ea will return 56f88569e1382308b0b6a2ea
:param ref_url:
:return:
"""
return ref_url.split("/")[3]
def create_ref_url(self, type, id):
"""
It will build the url from type and id
:param type:
:param id:
:return:
"""
return self.uri+type+"/"+id
def create_ports_from_rdf(self):
interface_query=self.prefix+"""
SELECT ?name ?interface
WHERE {
?x rdf:type ndl:Device . ?x ndl:name ?name .
?x ndl:hasInterface ?interface
OPTIONAL {
?y ndl:connectedTo ?z .
?z rdf:type ndl:Interface .
?z ndl:name ?neighbour
} . OPTIONAL {
?y ndl:capacity ?capacity .
?y ndl:encapsulation ?type
} .
}"""
ports_uri=self.uri+"ports"
ports = []
print("PORTSSSSS!!!!")
for row in self.g.query(interface_query):
print(row.name+" :::"+row.interface)
temp_intf_name=row.interface
intf_name=temp_intf_name.split("#")
port_name_split=intf_name[1].split(":")
port_node_name=port_name_split[0]
port_name=port_name_split[1]
port = dict()
port["$schema"]=self.SCHEMAS['ports']
port["name"]=port_name
port["nodeRef"]=self.topology_object.get_node_id_by_name(port_node_name)
ipv4_addr = dict()
ipv4_addr["type"]="ipv4"
## TODO: Fill the actual IPv4 address probably from the interface table using Router Proxy
ipv4_addr["address"]="1.1.1.1"
port["properties"]={"ipv4":ipv4_addr}
ports.append(port)
print "::FINAL PORTS JSON::"
ports_json_data = json.dumps(ports)
print(ports_json_data)
print("PORTS URI::"+ports_uri)
requests.post(ports_uri, data = ports_json_data)
###### GET PORT REFS
def update_port_refs(self):
ports_uri=self.uri+"ports"
ports_list = coreapi.get(ports_uri)
for check_port in ports_list:
port_name = check_port['name']
port_ref = self.getId(check_port['selfRef'])
node_ref = self.getId(check_port['nodeRef'])
port_object = Port(node_ref, "2.2.2.2")
self.topology_object.nodes[node_ref].add_port(port_ref, port_object)
print "\n"
print("PRINIIIIIII TOPO FINAL")
self.topology_object.display_topology()
######### BUILD PORT REF in NODES
def build_port_ref_in_nodes(self):
for node_id in self.topology_object.nodes.keys():
self.topology_object.nodes[node_id].ports
node = dict()
ports = []
for port_id in self.topology_object.nodes[node_id].ports.keys():
port={'href': self.create_ref_url("ports", port_id),'rel': 'full'}
ports.append(port)
node["ports"]=ports
print "::FINAL JSON::"
json_data = json.dumps(node)
print(json_data)
r=requests.put(node_id, data=json_data)
def create_links_from_rdf(self):
links_query = self.prefix+"""
SELECT ?name ?interface ?connectedTo ?type ?neighbour
WHERE {
?x rdf:type ndl:Device . ?x ndl:name ?name .
?x ndl:hasInterface ?y . ?y rdf:type ndl:Interface .
?y ndl:name ?interface . ?y ndl:connectedTo ?connectedTo .
OPTIONAL {
?y ndl:connectedTo ?z .
?z rdf:type ndl:Interface .
?z ndl:name ?neighbour
} . OPTIONAL {
?y ndl:capacity ?capacity .
?y ndl:encapsulation ?type
} .
}"""
for row in self.g.query(links_query):
print(row['name'], row['interface'], row['connectedTo'])
dest_node_name = row['connectedTo'].split("#")[1]
print("ref::"+row['connectedTo'].split("#")[1])
print("router...name::"+row['interface'].split(":")[0])
node_name = row['interface'].split(":")[0]
print("interface...name::"+row['interface'].split(":")[1])
intf_name = row['interface'].split(":")[1]
self.inode_object.add_link_to_inode(node_name, intf_name, dest_node_name)
def build_links(self):
link = dict()
link["directed"] = "false"
link["$schema"] = self.SCHEMAS['links']
link["name"] = "linkk"
# link["endpoints"] =
rdf = RdfDecoder('mini-topo.rdf', '10.10.0.135', '8888')