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 "consumer-named-selector": [],
197 "provider-named-selector": [
202 "name": "webservers-clients-icmp-http-contract"
207 "subject-feature-instances": {
208 "classifier-instance": [
210 "classifier-definition-id": "Classifier-L4",
224 "classifier-definition-id": "Classifier-L4",
238 "classifier-definition-id": "Classifier-IP-Protocol",
251 "action-definition-id": "Action-Allow"
259 # Main definition - constants
261 # =======================
263 # =======================
267 # =======================
269 # =======================
273 def get_tenant_uri():
274 return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
277 def get_tunnel_data():
279 "opendaylight-inventory:nodes": {
283 "ofoverlay:tunnel": [
285 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
286 "node-connector-id": "openflow:1:1",
287 "ip": "192.168.50.70",
291 "tunnel-type": "overlay:tunnel-type-vxlan",
292 "node-connector-id": "openflow:1:2",
293 "ip": "192.168.50.70",
300 "ofoverlay:tunnel": [
302 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
303 "node-connector-id": "openflow:2:1",
304 "ip": "192.168.50.71",
308 "tunnel-type": "overlay:tunnel-type-vxlan",
309 "node-connector-id": "openflow:2:2",
310 "ip": "192.168.50.71",
317 "ofoverlay:tunnel": [
319 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
320 "node-connector-id": "openflow:3:1",
321 "ip": "192.168.50.72",
325 "tunnel-type": "overlay:tunnel-type-vxlan",
326 "node-connector-id": "openflow:3:2",
327 "ip": "192.168.50.72",
337 def get_tunnel_uri():
338 return "/restconf/config/opendaylight-inventory:nodes"
340 def get_endpoint_data():
344 "endpoint-group": "clients",
346 "network-containment" : "subnet-10.0.35.0/24",
348 "l2-context": "bridge-domain1",
349 "mac-address": "00:00:00:00:35:02",
353 "ip-address": "10.0.35.2",
354 "l3-context": "l3-context-vrf-red"
357 "port-name": "vethl-h35-2",
358 "tenant": "tenant-red"
364 "endpoint-group": "clients",
366 "network-containment" : "subnet-10.0.35.0/24",
368 "l2-context": "bridge-domain1",
369 "mac-address": "00:00:00:00:35:03",
373 "ip-address": "10.0.35.3",
374 "l3-context": "l3-context-vrf-red"
377 "port-name": "vethl-h35-3",
378 "tenant": "tenant-red"
384 "endpoint-group": "clients",
386 "network-containment" : "subnet-10.0.35.0/24",
388 "l2-context": "bridge-domain1",
389 "mac-address": "00:00:00:00:35:04",
393 "ip-address": "10.0.35.4",
394 "l3-context": "l3-context-vrf-red"
397 "port-name": "vethl-h35-4",
398 "tenant": "tenant-red"
404 "endpoint-group": "clients",
406 "network-containment" : "subnet-10.0.35.0/24",
408 "l2-context": "bridge-domain1",
409 "mac-address": "00:00:00:00:35:05",
413 "ip-address": "10.0.35.5",
414 "l3-context": "l3-context-vrf-red"
417 "port-name": "vethl-h35-5",
418 "tenant": "tenant-red"
424 "endpoint-group": "webservers1",
426 "network-containment" : "subnet-10.0.36.0/24",
428 "l2-context": "bridge-domain1",
429 "mac-address": "00:00:00:00:36:02",
433 "ip-address": "10.0.36.2",
434 "l3-context": "l3-context-vrf-red"
437 "port-name": "vethl-h36-2",
438 "tenant": "tenant-red"
444 "endpoint-group": "webservers2",
446 "network-containment" : "subnet-10.0.36.0/24",
448 "l2-context": "bridge-domain1",
449 "mac-address": "00:00:00:00:36:03",
453 "ip-address": "10.0.36.3",
454 "l3-context": "l3-context-vrf-red"
457 "port-name": "vethl-h36-3",
458 "tenant": "tenant-red"
464 "endpoint-group": "webservers1",
466 "network-containment" : "subnet-10.0.36.0/24",
468 "l2-context": "bridge-domain1",
469 "mac-address": "00:00:00:00:36:04",
473 "ip-address": "10.0.36.4",
474 "l3-context": "l3-context-vrf-red"
477 "port-name": "vethl-h36-4",
478 "tenant": "tenant-red"
483 "endpoint-group": "webservers2",
485 "network-containment" : "subnet-10.0.36.0/24",
487 "l2-context": "bridge-domain1",
488 "mac-address": "00:00:00:00:36:05",
492 "ip-address": "10.0.36.5",
493 "l3-context": "l3-context-vrf-red"
496 "port-name": "vethl-h36-5",
497 "tenant": "tenant-red"
501 def get_endpoint_uri():
502 return "/restconf/operations/endpoint:register-endpoint"
504 if __name__ == "__main__":
508 # Some sensible defaults
509 controller=os.environ.get('ODL')
510 if controller == None:
511 sys.exit("No controller set.")
515 tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
518 print "sending tenant"
519 put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)
520 print "sending tunnel"
521 put(controller, DEFAULT_PORT, get_tunnel_uri(), get_tunnel_data(), True)
522 print "registering endpoints"
523 for endpoint in get_endpoint_data():
524 post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)