4 from requests.auth import HTTPBasicAuth
5 from subprocess import call
18 OPER_NODES='/restconf/operational/opendaylight-inventory:nodes/'
19 CONF_TENANT='/restconf/config/policy:tenants'
21 def get(host, port, uri):
22 url='http://'+host+":"+port+uri
24 r = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD))
25 jsondata=json.loads(r.text)
28 def put(host, port, uri, data, debug=False):
29 '''Perform a PUT rest operation, using the URL and data provided'''
31 url='http://'+host+":"+port+uri
33 headers = {'Content-type': 'application/yang.data+json',
34 'Accept': 'application/yang.data+json'}
37 print json.dumps(data, indent=4, sort_keys=True)
38 r = requests.put(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
43 def post(host, port, uri, data, debug=False):
44 '''Perform a POST rest operation, using the URL and data provided'''
46 url='http://'+host+":"+port+uri
47 headers = {'Content-type': 'application/yang.data+json',
48 'Accept': 'application/yang.data+json'}
51 print json.dumps(data, indent=4, sort_keys=True)
52 r = requests.post(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
58 def get_tenant_data():
63 "forwarding-context": {
66 "id": "bridge-domain1",
67 "parent": "l3-context-vrf-red"
72 "id": "flood-domain-1",
73 "parent": "bridge-domain1"
76 "id": "flood-domain1",
77 "parent": "bridge-domain1"
82 "id": "l3-context-vrf-red"
87 "id": "subnet-10.0.35.0/24",
88 "ip-prefix": "10.0.35.1/24",
89 "parent": "flood-domain-1",
90 "virtual-router-ip": "10.0.35.1"
93 "id": "subnet-10.0.36.0/24",
94 "ip-prefix": "10.0.36.1/24",
95 "parent": "flood-domain1",
96 "virtual-router-ip": "10.0.36.1"
105 "name": "allow-http-clause",
107 "allow-http-subject",
112 "id": "icmp-http-contract",
115 "name": "allow-http-subject",
122 "instance-name": "http-dest"
127 "instance-name": "http-src"
136 "name": "allow-http-rule"
141 "name": "allow-icmp-subject",
147 "instance-name": "icmp"
156 "name": "allow-icmp-rule"
165 "consumer-named-selector": [
170 "name": "webservers-clients-icmp-http-contract"
174 "provider-named-selector": []
177 "consumer-named-selector": [],
179 "provider-named-selector": [
184 "name": "webservers-clients-icmp-http-contract"
189 "subject-feature-instances": {
190 "classifier-instance": [
192 "classifier-definition-id": "Classifier-L4",
206 "classifier-definition-id": "Classifier-L4",
220 "classifier-definition-id": "Classifier-IP-Protocol",
233 "action-definition-id": "Action-Allow"
241 # Main definition - constants
243 # =======================
245 # =======================
249 # =======================
251 # =======================
255 def get_tenant_uri():
256 return "/restconf/config/policy:tenants/policy:tenant/tenant-dobre"
258 def get_tunnel_data():
260 "opendaylight-inventory:nodes": {
264 "ofoverlay:tunnel": [
266 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
267 "node-connector-id": "openflow:1:1",
268 "ip": "192.168.50.70",
272 "tunnel-type": "overlay:tunnel-type-vxlan",
273 "node-connector-id": "openflow:1:2",
274 "ip": "192.168.50.70",
281 "ofoverlay:tunnel": [
283 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
284 "node-connector-id": "openflow:2:1",
285 "ip": "192.168.50.71",
289 "tunnel-type": "overlay:tunnel-type-vxlan",
290 "node-connector-id": "openflow:2:2",
291 "ip": "192.168.50.71",
298 "ofoverlay:tunnel": [
300 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
301 "node-connector-id": "openflow:3:1",
302 "ip": "192.168.50.72",
306 "tunnel-type": "overlay:tunnel-type-vxlan",
307 "node-connector-id": "openflow:3:2",
308 "ip": "192.168.50.72",
318 def get_tunnel_uri():
319 return "/restconf/config/opendaylight-inventory:nodes"
321 def get_endpoint_data():
325 "endpoint-group": "clients",
327 "network-containment" : "subnet-10.0.35.0/24",
329 "l2-context": "bridge-domain1",
330 "mac-address": "00:00:00:00:35:02",
334 "ip-address": "10.0.35.2",
335 "l3-context": "l3-context-vrf-red"
338 "port-name": "vethl-h35_2",
339 "tenant": "tenant-dobre"
345 "endpoint-group": "clients",
347 "network-containment" : "subnet-10.0.35.0/24",
349 "l2-context": "bridge-domain1",
350 "mac-address": "00:00:00:00:35:03",
354 "ip-address": "10.0.35.3",
355 "l3-context": "l3-context-vrf-red"
358 "port-name": "vethl-h35_3",
359 "tenant": "tenant-dobre"
365 "endpoint-group": "clients",
367 "network-containment" : "subnet-10.0.35.0/24",
369 "l2-context": "bridge-domain1",
370 "mac-address": "00:00:00:00:35:04",
374 "ip-address": "10.0.35.4",
375 "l3-context": "l3-context-vrf-red"
378 "port-name": "vethl-h35_4",
379 "tenant": "tenant-dobre"
385 "endpoint-group": "clients",
387 "network-containment" : "subnet-10.0.35.0/24",
389 "l2-context": "bridge-domain1",
390 "mac-address": "00:00:00:00:35:05",
394 "ip-address": "10.0.35.5",
395 "l3-context": "l3-context-vrf-red"
398 "port-name": "vethl-h35_5",
399 "tenant": "tenant-dobre"
405 "endpoint-group": "webservers",
407 "network-containment" : "subnet-10.0.36.0/24",
409 "l2-context": "bridge-domain1",
410 "mac-address": "00:00:00:00:36:02",
414 "ip-address": "10.0.36.2",
415 "l3-context": "l3-context-vrf-red"
418 "port-name": "vethl-h36_2",
419 "tenant": "tenant-dobre"
425 "endpoint-group": "webservers",
427 "network-containment" : "subnet-10.0.36.0/24",
429 "l2-context": "bridge-domain1",
430 "mac-address": "00:00:00:00:36:03",
434 "ip-address": "10.0.36.3",
435 "l3-context": "l3-context-vrf-red"
438 "port-name": "vethl-h36_3",
439 "tenant": "tenant-dobre"
445 "endpoint-group": "webservers",
447 "network-containment" : "subnet-10.0.36.0/24",
449 "l2-context": "bridge-domain1",
450 "mac-address": "00:00:00:00:36:04",
454 "ip-address": "10.0.36.4",
455 "l3-context": "l3-context-vrf-red"
458 "port-name": "vethl-h36_4",
459 "tenant": "tenant-dobre"
464 "endpoint-group": "webservers",
466 "network-containment" : "subnet-10.0.36.0/24",
468 "l2-context": "bridge-domain1",
469 "mac-address": "00:00:00:00:36:05",
473 "ip-address": "10.0.36.5",
474 "l3-context": "l3-context-vrf-red"
477 "port-name": "vethl-h36_5",
478 "tenant": "tenant-dobre"
482 def get_endpoint_uri():
483 return "/restconf/operations/endpoint:register-endpoint"
485 if __name__ == "__main__":
489 # Some sensible defaults
490 controller=os.environ.get('ODL')
491 if controller == None:
492 sys.exit("No controller set.")
496 tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
499 print "sending tenant"
500 put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)
501 print "sending tunnel"
502 put(controller, DEFAULT_PORT, get_tunnel_uri(), get_tunnel_data(), True)
503 print "registering endpoints"
504 for endpoint in get_endpoint_data():
505 post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)