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",
152 "instance-name": "icmp",
162 "name": "allow-icmp-rule"
171 "consumer-named-selector": [
176 "name": "webservers-clients-icmp-http-contract"
180 "provider-named-selector": []
183 "consumer-named-selector": [],
185 "provider-named-selector": [
190 "name": "webservers-clients-icmp-http-contract"
195 "subject-feature-instances": {
196 "classifier-instance": [
198 "classifier-definition-id": "Classifier-L4",
212 "classifier-definition-id": "Classifier-L4",
226 "classifier-definition-id": "Classifier-IP-Protocol",
239 "action-definition-id": "Action-Allow"
247 # Main definition - constants
249 # =======================
251 # =======================
255 # =======================
257 # =======================
261 def get_tenant_uri():
262 return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
265 def get_tunnel_data():
267 "opendaylight-inventory:nodes": {
271 "ofoverlay:tunnel": [
273 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
274 "node-connector-id": "openflow:1:1",
275 "ip": "192.168.50.70",
279 "tunnel-type": "overlay:tunnel-type-vxlan",
280 "node-connector-id": "openflow:1:2",
281 "ip": "192.168.50.70",
288 "ofoverlay:tunnel": [
290 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
291 "node-connector-id": "openflow:2:1",
292 "ip": "192.168.50.71",
296 "tunnel-type": "overlay:tunnel-type-vxlan",
297 "node-connector-id": "openflow:2:2",
298 "ip": "192.168.50.71",
305 "ofoverlay:tunnel": [
307 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
308 "node-connector-id": "openflow:3:1",
309 "ip": "192.168.50.72",
313 "tunnel-type": "overlay:tunnel-type-vxlan",
314 "node-connector-id": "openflow:3:2",
315 "ip": "192.168.50.72",
325 def get_tunnel_uri():
326 return "/restconf/config/opendaylight-inventory:nodes"
328 def get_endpoint_data():
332 "endpoint-group": "clients",
334 "network-containment" : "subnet-10.0.35.0/24",
336 "l2-context": "bridge-domain1",
337 "mac-address": "00:00:00:00:35:02",
341 "ip-address": "10.0.35.2",
342 "l3-context": "l3-context-vrf-red"
345 "port-name": "vethl-h35_2",
346 "tenant": "tenant-red"
352 "endpoint-group": "clients",
354 "network-containment" : "subnet-10.0.35.0/24",
356 "l2-context": "bridge-domain1",
357 "mac-address": "00:00:00:00:35:03",
361 "ip-address": "10.0.35.3",
362 "l3-context": "l3-context-vrf-red"
365 "port-name": "vethl-h35_3",
366 "tenant": "tenant-red"
372 "endpoint-group": "clients",
374 "network-containment" : "subnet-10.0.35.0/24",
376 "l2-context": "bridge-domain1",
377 "mac-address": "00:00:00:00:35:04",
381 "ip-address": "10.0.35.4",
382 "l3-context": "l3-context-vrf-red"
385 "port-name": "vethl-h35_4",
386 "tenant": "tenant-red"
392 "endpoint-group": "clients",
394 "network-containment" : "subnet-10.0.35.0/24",
396 "l2-context": "bridge-domain1",
397 "mac-address": "00:00:00:00:35:05",
401 "ip-address": "10.0.35.5",
402 "l3-context": "l3-context-vrf-red"
405 "port-name": "vethl-h35_5",
406 "tenant": "tenant-red"
412 "endpoint-group": "webservers",
414 "network-containment" : "subnet-10.0.36.0/24",
416 "l2-context": "bridge-domain1",
417 "mac-address": "00:00:00:00:36:02",
421 "ip-address": "10.0.36.2",
422 "l3-context": "l3-context-vrf-red"
425 "port-name": "vethl-h36_2",
426 "tenant": "tenant-red"
432 "endpoint-group": "webservers",
434 "network-containment" : "subnet-10.0.36.0/24",
436 "l2-context": "bridge-domain1",
437 "mac-address": "00:00:00:00:36:03",
441 "ip-address": "10.0.36.3",
442 "l3-context": "l3-context-vrf-red"
445 "port-name": "vethl-h36_3",
446 "tenant": "tenant-red"
452 "endpoint-group": "webservers",
454 "network-containment" : "subnet-10.0.36.0/24",
456 "l2-context": "bridge-domain1",
457 "mac-address": "00:00:00:00:36:04",
461 "ip-address": "10.0.36.4",
462 "l3-context": "l3-context-vrf-red"
465 "port-name": "vethl-h36_4",
466 "tenant": "tenant-red"
471 "endpoint-group": "webservers",
473 "network-containment" : "subnet-10.0.36.0/24",
475 "l2-context": "bridge-domain1",
476 "mac-address": "00:00:00:00:36:05",
480 "ip-address": "10.0.36.5",
481 "l3-context": "l3-context-vrf-red"
484 "port-name": "vethl-h36_5",
485 "tenant": "tenant-red"
489 def get_endpoint_uri():
490 return "/restconf/operations/endpoint:register-endpoint"
492 if __name__ == "__main__":
496 # Some sensible defaults
497 controller=os.environ.get('ODL')
498 if controller == None:
499 sys.exit("No controller set.")
503 tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
506 print "sending tenant"
507 put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)
508 print "sending tunnel"
509 put(controller, DEFAULT_PORT, get_tunnel_uri(), get_tunnel_data(), True)
510 print "registering endpoints"
511 for endpoint in get_endpoint_data():
512 post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)