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-red"
259 def get_tunnel_data():
261 "opendaylight-inventory:nodes": {
265 "ofoverlay:tunnel": [
267 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
268 "node-connector-id": "openflow:1:1",
269 "ip": "192.168.50.70",
273 "tunnel-type": "overlay:tunnel-type-vxlan",
274 "node-connector-id": "openflow:1:2",
275 "ip": "192.168.50.70",
282 "ofoverlay:tunnel": [
284 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
285 "node-connector-id": "openflow:2:1",
286 "ip": "192.168.50.71",
290 "tunnel-type": "overlay:tunnel-type-vxlan",
291 "node-connector-id": "openflow:2:2",
292 "ip": "192.168.50.71",
299 "ofoverlay:tunnel": [
301 "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
302 "node-connector-id": "openflow:3:1",
303 "ip": "192.168.50.72",
307 "tunnel-type": "overlay:tunnel-type-vxlan",
308 "node-connector-id": "openflow:3:2",
309 "ip": "192.168.50.72",
319 def get_tunnel_uri():
320 return "/restconf/config/opendaylight-inventory:nodes"
322 def get_endpoint_data():
326 "endpoint-group": "clients",
328 "network-containment" : "subnet-10.0.35.0/24",
330 "l2-context": "bridge-domain1",
331 "mac-address": "00:00:00:00:35:02",
335 "ip-address": "10.0.35.2",
336 "l3-context": "l3-context-vrf-red"
339 "port-name": "vethl-h35_2",
340 "tenant": "tenant-red"
346 "endpoint-group": "clients",
348 "network-containment" : "subnet-10.0.35.0/24",
350 "l2-context": "bridge-domain1",
351 "mac-address": "00:00:00:00:35:03",
355 "ip-address": "10.0.35.3",
356 "l3-context": "l3-context-vrf-red"
359 "port-name": "vethl-h35_3",
360 "tenant": "tenant-red"
366 "endpoint-group": "clients",
368 "network-containment" : "subnet-10.0.35.0/24",
370 "l2-context": "bridge-domain1",
371 "mac-address": "00:00:00:00:35:04",
375 "ip-address": "10.0.35.4",
376 "l3-context": "l3-context-vrf-red"
379 "port-name": "vethl-h35_4",
380 "tenant": "tenant-red"
386 "endpoint-group": "clients",
388 "network-containment" : "subnet-10.0.35.0/24",
390 "l2-context": "bridge-domain1",
391 "mac-address": "00:00:00:00:35:05",
395 "ip-address": "10.0.35.5",
396 "l3-context": "l3-context-vrf-red"
399 "port-name": "vethl-h35_5",
400 "tenant": "tenant-red"
406 "endpoint-group": "webservers",
408 "network-containment" : "subnet-10.0.36.0/24",
410 "l2-context": "bridge-domain1",
411 "mac-address": "00:00:00:00:36:02",
415 "ip-address": "10.0.36.2",
416 "l3-context": "l3-context-vrf-red"
419 "port-name": "vethl-h36_2",
420 "tenant": "tenant-red"
426 "endpoint-group": "webservers",
428 "network-containment" : "subnet-10.0.36.0/24",
430 "l2-context": "bridge-domain1",
431 "mac-address": "00:00:00:00:36:03",
435 "ip-address": "10.0.36.3",
436 "l3-context": "l3-context-vrf-red"
439 "port-name": "vethl-h36_3",
440 "tenant": "tenant-red"
446 "endpoint-group": "webservers",
448 "network-containment" : "subnet-10.0.36.0/24",
450 "l2-context": "bridge-domain1",
451 "mac-address": "00:00:00:00:36:04",
455 "ip-address": "10.0.36.4",
456 "l3-context": "l3-context-vrf-red"
459 "port-name": "vethl-h36_4",
460 "tenant": "tenant-red"
465 "endpoint-group": "webservers",
467 "network-containment" : "subnet-10.0.36.0/24",
469 "l2-context": "bridge-domain1",
470 "mac-address": "00:00:00:00:36:05",
474 "ip-address": "10.0.36.5",
475 "l3-context": "l3-context-vrf-red"
478 "port-name": "vethl-h36_5",
479 "tenant": "tenant-red"
483 def get_endpoint_uri():
484 return "/restconf/operations/endpoint:register-endpoint"
486 if __name__ == "__main__":
490 # Some sensible defaults
491 controller=os.environ.get('ODL')
492 if controller == None:
493 sys.exit("No controller set.")
497 tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
500 print "sending tenant"
501 put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)
502 print "sending tunnel"
503 put(controller, DEFAULT_PORT, get_tunnel_uri(), get_tunnel_data(), True)
504 print "registering endpoints"
505 for endpoint in get_endpoint_data():
506 post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)