Bug3738: Coexistence demo environment
[groupbasedpolicy.git] / demos / gbpsfc-env / demo-asymmetric-coexistence / rest.py
1 #!/usr/bin/python\r
2 import argparse\r
3 import requests,json\r
4 from requests.auth import HTTPBasicAuth\r
5 from subprocess import call\r
6 import time\r
7 import sys\r
8 import os\r
9 \r
10 \r
11 DEFAULT_PORT='8181'\r
12 \r
13 \r
14 USERNAME='admin'\r
15 PASSWORD='admin'\r
16 \r
17 \r
18 CONF_TENANT='/restconf/config/policy:tenants'\r
19 \r
20 def get(host, port, uri):\r
21     url='http://'+host+":"+port+uri\r
22     r = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD))\r
23     return r\r
24 \r
25 def put(host, port, uri, data, debug=False):\r
26     '''Perform a PUT rest operation, using the URL and data provided'''\r
27 \r
28     url='http://'+host+":"+port+uri\r
29 \r
30     headers = {'Content-type': 'application/yang.data+json',\r
31                'Accept': 'application/yang.data+json'}\r
32     if debug == True:\r
33         print "PUT %s" % url\r
34         print json.dumps(data, indent=4, sort_keys=True)\r
35     r = requests.put(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))\r
36     if debug == True:\r
37         print r.text\r
38     r.raise_for_status()\r
39 \r
40 def post(host, port, uri, data, debug=False):\r
41     '''Perform a POST rest operation, using the URL and data provided'''\r
42 \r
43     url='http://'+host+":"+port+uri\r
44     headers = {'Content-type': 'application/yang.data+json',\r
45                'Accept': 'application/yang.data+json'}\r
46     if debug == True:\r
47         print "POST %s" % url\r
48         print json.dumps(data, indent=4, sort_keys=True)\r
49     r = requests.post(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))\r
50     if debug == True:\r
51         print r.text\r
52     r.raise_for_status()\r
53 \r
54 def wait_for_sff_in_datastore(url):\r
55     for i in xrange(30):\r
56         resp=get(controller, DEFAULT_PORT, url)\r
57         if ('192.168.50.70' in resp.text) and ('192.168.50.71' in resp.text):\r
58             break\r
59         time.sleep(3)\r
60     if ('192.168.50.70' not in resp.text):\r
61         print "ERROR: SFF1 has not been initialized!"\r
62         sys.exit(1)\r
63     if ('192.168.50.71' not in resp.text):\r
64         print "ERROR: SFF2 has not been initialized!"\r
65         sys.exit(1)\r
66 \r
67 \r
68 \r
69 def get_service_functions_uri():\r
70     return "/restconf/config/service-function:service-functions"\r
71 \r
72 def get_service_functions_data():\r
73     return {\r
74     "service-functions": {\r
75         "service-function": [\r
76             {\r
77                 "name": "firewall-72",\r
78                 "ip-mgmt-address": "192.168.50.72",\r
79                 "type": "service-function-type:firewall",\r
80                 "nsh-aware": "true",\r
81                 "sf-data-plane-locator": [\r
82                     {\r
83                         "name": "2",\r
84                         "port": 6633,\r
85                         "ip": "192.168.50.72",\r
86                         "transport": "service-locator:vxlan-gpe",\r
87                         "service-function-forwarder": "SFF1"\r
88                     }\r
89                 ]\r
90             },\r
91             {\r
92                 "name": "dpi-74",\r
93                 "ip-mgmt-address": "192.168.50.74",\r
94                 "type": "service-function-type:dpi",\r
95                 "nsh-aware": "true",\r
96                 "sf-data-plane-locator": [\r
97                     {\r
98                         "name": "3",\r
99                         "port": 6633,\r
100                         "ip": "192.168.50.74",\r
101                         "transport": "service-locator:vxlan-gpe",\r
102                         "service-function-forwarder": "SFF2"\r
103                     }\r
104                 ]\r
105             }\r
106         ]\r
107     }\r
108 }\r
109 \r
110 def get_service_function_forwarders_uri():\r
111     return "/restconf/config/service-function-forwarder:service-function-forwarders"\r
112 \r
113 def get_service_function_forwarders_data():\r
114     return {\r
115     "service-function-forwarders": {\r
116         "service-function-forwarder": [\r
117             {\r
118                 "name": "SFF1",\r
119                 "service-node": "OVSDB2",\r
120                 "service-function-forwarder-ovs:ovs-bridge": {\r
121                     "bridge-name": "sw1"\r
122                 },\r
123                 "service-function-dictionary": [\r
124                     {\r
125                         "name": "firewall-72",\r
126                         "sff-sf-data-plane-locator": {\r
127                             "sf-dpl-name": "2",\r
128                             "sff-dpl-name": "sfc-tun2"\r
129                         }\r
130                     }\r
131                 ],\r
132                 "sff-data-plane-locator": [\r
133                     {\r
134                         "name": "sfc-tun2",\r
135                         "data-plane-locator": {\r
136                             "transport": "service-locator:vxlan-gpe",\r
137                             "port": 6633,\r
138                             "ip": "192.168.50.70"\r
139                         },\r
140                         "service-function-forwarder-ovs:ovs-options": {\r
141                             "remote-ip": "flow",\r
142                             "dst-port": "6633",\r
143                             "key": "flow",\r
144                             "nsp": "flow",\r
145                             "nsi": "flow",\r
146                             "nshc1": "flow",\r
147                             "nshc2": "flow",\r
148                             "nshc3": "flow",\r
149                             "nshc4": "flow"\r
150                         }\r
151                     }\r
152                 ]\r
153             },\r
154             {\r
155                 "name": "SFF2",\r
156                 "service-node": "OVSDB2",\r
157                 "service-function-forwarder-ovs:ovs-bridge": {\r
158                     "bridge-name": "sw2"\r
159                 },\r
160                 "service-function-dictionary": [\r
161                     {\r
162                         "name": "dpi-74",\r
163                         "sff-sf-data-plane-locator": {\r
164                             "sf-dpl-name": "3",\r
165                             "sff-dpl-name": "sfc-tun4"\r
166                         }\r
167                     }\r
168                 ],\r
169                 "sff-data-plane-locator": [\r
170                     {\r
171                         "name": "sfc-tun4",\r
172                         "data-plane-locator": {\r
173                             "transport": "service-locator:vxlan-gpe",\r
174                             "port": 6633,\r
175                             "ip": "192.168.50.71"\r
176                         },\r
177                         "service-function-forwarder-ovs:ovs-options": {\r
178                             "remote-ip": "flow",\r
179                             "dst-port": "6633",\r
180                             "key": "flow",\r
181                             "nsp": "flow",\r
182                             "nsi": "flow",\r
183                             "nshc1": "flow",\r
184                             "nshc2": "flow",\r
185                             "nshc3": "flow",\r
186                             "nshc4": "flow"\r
187                         }\r
188                     }\r
189                 ]\r
190             }\r
191         ]\r
192     }\r
193 }\r
194 \r
195 def get_service_function_chains_uri():\r
196     return "/restconf/config/service-function-chain:service-function-chains/"\r
197 \r
198 def get_service_function_chains_data():\r
199     return {\r
200     "service-function-chains": {\r
201         "service-function-chain": [\r
202             {\r
203                 "name": "SFCGBP",\r
204                 "symmetric": "false",\r
205                 "sfc-service-function": [\r
206                     {\r
207                         "name": "firewall-abstract1",\r
208                         "type": "service-function-type:firewall"\r
209                     },\r
210                     {\r
211                         "name": "dpi-abstract1",\r
212                         "type": "service-function-type:dpi"\r
213                     }\r
214                 ]\r
215             }\r
216         ]\r
217     }\r
218 }\r
219 \r
220 def get_service_function_paths_uri():\r
221     return "/restconf/config/service-function-path:service-function-paths/"\r
222 \r
223 def get_service_function_paths_data():\r
224     return {\r
225     "service-function-paths": {\r
226         "service-function-path": [\r
227             {\r
228                 "name": "SFCGBP-Path",\r
229                 "service-chain-name": "SFCGBP",\r
230                 "starting-index": 255,\r
231                 "symmetric": "false"\r
232 \r
233             }\r
234         ]\r
235     }\r
236 }\r
237 \r
238 def get_tenant_data():\r
239     return {\r
240         "tenant": [\r
241           {\r
242             "id": "tenant-red",\r
243             "name": "DockerTenant",\r
244             "forwarding-context": {\r
245               "l2-flood-domain": [\r
246                 {\r
247                   "id": "flood-domain-1",\r
248                   "parent": "bridge-domain1"\r
249                 },\r
250                 {\r
251                   "id": "flood-domain-2",\r
252                   "parent": "bridge-domain1"\r
253                 }\r
254               ],\r
255               "l3-context": [\r
256                 {\r
257                   "id": "l3-context-vrf-red"\r
258                 }\r
259               ],\r
260               "l2-bridge-domain": [\r
261                 {\r
262                   "id": "bridge-domain1",\r
263                   "parent": "l3-context-vrf-red"\r
264                 }\r
265               ],\r
266               "subnet": [\r
267                 {\r
268                   "id": "subnet-10.0.36.0/24",\r
269                   "virtual-router-ip": "10.0.36.1",\r
270                   "parent": "flood-domain-2",\r
271                   "ip-prefix": "10.0.36.1/24"\r
272                 },\r
273                 {\r
274                   "id": "subnet-10.0.35.0/24",\r
275                   "virtual-router-ip": "10.0.35.1",\r
276                   "parent": "flood-domain-1",\r
277                   "ip-prefix": "10.0.35.1/24"\r
278                 }\r
279               ]\r
280             },\r
281             "policy": {\r
282               "endpoint-group": [\r
283                 {\r
284                   "id": "webservers",\r
285                   "name": "webservers",\r
286                   "provider-named-selector": [\r
287                     {\r
288                       "name": "webservers-clients-icmp-http-contract",\r
289                       "contract": [\r
290                         "icmp-http-contract"\r
291                       ]\r
292                     }\r
293                   ]\r
294                 },\r
295                 {\r
296                   "id": "clients",\r
297                   "name": "clients",\r
298                   "consumer-named-selector": [\r
299                     {\r
300                       "name": "webservers-clients-icmp-http-contract",\r
301                       "contract": [\r
302                         "icmp-http-contract"\r
303                       ]\r
304                     }\r
305                   ]\r
306                 }\r
307               ],\r
308               "subject-feature-instances": {\r
309                 "classifier-instance": [\r
310                   {\r
311                     "name": "icmp",\r
312                     "classifier-definition-id": "Classifier-IP-Protocol",\r
313                     "parameter-value": [\r
314                       {\r
315                         "name": "proto",\r
316                         "int-value": 1\r
317                       }\r
318                     ]\r
319                   },\r
320                   {\r
321                     "name": "http-dest",\r
322                     "classifier-definition-id": "Classifier-L4",\r
323                     "parameter-value": [\r
324                       {\r
325                         "int-value": "6",\r
326                         "name": "proto"\r
327                       },\r
328                       {\r
329                         "int-value": "80",\r
330                         "name": "destport"\r
331                       }\r
332                     ]\r
333                   },\r
334                   {\r
335                     "name": "http-src",\r
336                     "classifier-definition-id": "Classifier-L4",\r
337                     "parameter-value": [\r
338                       {\r
339                         "int-value": "6",\r
340                         "name": "proto"\r
341                       },\r
342                       {\r
343                         "int-value": "80",\r
344                         "name": "sourceport"\r
345                       }\r
346                     ]\r
347                   }\r
348                 ],\r
349                 "action-instance": [\r
350                   {\r
351                     "name": "chain1",\r
352                     "action-definition-id": "Action-Chain",\r
353                     "parameter-value": [\r
354                       {\r
355                         "name": "sfc-chain-name",\r
356                         "string-value": "SFCGBP"\r
357                       }\r
358                     ]\r
359                   },\r
360                   {\r
361                     "name": "allow1",\r
362                     "action-definition-id": "Action-Allow"\r
363                   }\r
364                 ]\r
365               },\r
366               "contract": [\r
367                 {\r
368                   "id": "icmp-http-contract",\r
369                   "subject": [\r
370                     {\r
371                       "name": "icmp-subject",\r
372                       "rule": [\r
373                         {\r
374                           "name": "allow-icmp-rule",\r
375                           "order": 0,\r
376                           "classifier-ref": [\r
377                             {\r
378                               "name": "icmp",\r
379                               "instance-name": "icmp"\r
380                             }\r
381                           ],\r
382                           "action-ref": [\r
383                             {\r
384                               "name": "allow1",\r
385                               "order": 0\r
386                             }\r
387                           ]\r
388                         }\r
389                       ]\r
390                     },\r
391                     {\r
392                       "name": "http-subject",\r
393                       "rule": [\r
394                         {\r
395                           "name": "http-chain-rule",\r
396                           "classifier-ref": [\r
397                             {\r
398                               "name": "http-dest",\r
399                               "instance-name": "http-dest",\r
400                               "direction": "in"\r
401                             }\r
402                           ],\r
403                           "action-ref": [\r
404                             {\r
405                               "name": "chain1",\r
406                               "order": 0\r
407                             }\r
408                           ]\r
409                         },\r
410                         {\r
411                           "name": "http-out-rule",\r
412                           "classifier-ref": [\r
413                             {\r
414                               "name": "http-src",\r
415                               "instance-name": "http-src",\r
416                               "direction": "out"\r
417                             }\r
418                           ],\r
419                           "action-ref": [\r
420                             {\r
421                               "name": "allow1",\r
422                               "order": 0\r
423                             }\r
424                           ]\r
425                         }\r
426                       ]\r
427                     }\r
428                   ],\r
429                   "clause": [\r
430                     {\r
431                       "name": "icmp-http-clause",\r
432                       "subject-refs": [\r
433                         "icmp-subject",\r
434                         "http-subject"\r
435                       ]\r
436                     }\r
437                   ]\r
438                 }\r
439               ]\r
440             }\r
441           }\r
442         ]\r
443     }\r
444 \r
445 # Main definition - constants\r
446 \r
447 # =======================\r
448 #     MENUS FUNCTIONS\r
449 # =======================\r
450 \r
451 # Main menu\r
452 \r
453 # =======================\r
454 #      MAIN PROGRAM\r
455 # =======================\r
456 \r
457 # Main Program\r
458 \r
459 def get_tenant_uri():\r
460     return "/restconf/config/policy:tenants/policy:tenant/tenant-red"\r
461 \r
462 def get_tunnel_data_1():\r
463     return {\r
464     "node": [\r
465       {\r
466         "id": "openflow:1",\r
467         "ofoverlay:tunnel": [\r
468           {\r
469             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",\r
470             "node-connector-id": "openflow:1:1",\r
471             "ip": "192.168.50.70",\r
472             "port": 6633\r
473           },\r
474           {\r
475             "tunnel-type": "overlay:tunnel-type-vxlan",\r
476             "node-connector-id": "openflow:1:2",\r
477             "ip": "192.168.50.70",\r
478             "port": 4789\r
479           }\r
480         ]\r
481       }\r
482     ]\r
483   }\r
484 \r
485 def get_tunnel_uri_1():\r
486     return "/restconf/config/opendaylight-inventory:nodes/node/openflow:1"\r
487 \r
488 def get_tunnel_data_6():\r
489     return {\r
490     "node": [\r
491       {\r
492         "id": "openflow:6",\r
493         "ofoverlay:tunnel": [\r
494           {\r
495             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",\r
496             "node-connector-id": "openflow:6:1",\r
497             "ip": "192.168.50.75",\r
498             "port": 6633\r
499           },\r
500           {\r
501             "tunnel-type": "overlay:tunnel-type-vxlan",\r
502             "node-connector-id": "openflow:6:2",\r
503             "ip": "192.168.50.75",\r
504             "port": 4789\r
505           }\r
506         ]\r
507       }\r
508     ]\r
509   }\r
510 \r
511 def get_tunnel_uri_6():\r
512     return "/restconf/config/opendaylight-inventory:nodes/node/openflow:6"\r
513 \r
514 def get_endpoint_data():\r
515     return [\r
516 {\r
517 "input": {\r
518 \r
519     "endpoint-group": "webservers",\r
520 \r
521     "network-containment" : "subnet-10.0.36.0/24",\r
522 \r
523     "l2-context": "bridge-domain1",\r
524     "mac-address": "00:00:00:00:36:02",\r
525 \r
526     "l3-address": [\r
527         {\r
528             "ip-address": "10.0.36.2",\r
529             "l3-context": "l3-context-vrf-red"\r
530         }\r
531     ],\r
532     "port-name": "vethl-h36_2",\r
533     "tenant": "tenant-red"\r
534 }\r
535 },\r
536 {\r
537 "input": {\r
538     "endpoint-group": "clients",\r
539 "network-containment" : "subnet-10.0.35.0/24",\r
540 "l2-context": "bridge-domain1",\r
541 "mac-address": "00:00:00:00:35:02",\r
542 "l3-address": [\r
543     {\r
544         "ip-address": "10.0.35.2",\r
545         "l3-context": "l3-context-vrf-red"\r
546     }\r
547 ],\r
548 "port-name": "vethl-h35_2",\r
549 "tenant": "tenant-red"\r
550 }\r
551 },\r
552 {\r
553 "input": {\r
554 \r
555     "endpoint-group": "clients",\r
556 \r
557     "network-containment" : "subnet-10.0.35.0/24",\r
558 \r
559     "l2-context": "bridge-domain1",\r
560     "mac-address": "00:00:00:00:35:03",\r
561 \r
562     "l3-address": [\r
563         {\r
564             "ip-address": "10.0.35.3",\r
565             "l3-context": "l3-context-vrf-red"\r
566         }\r
567     ],\r
568     "port-name": "vethl-h35_3",\r
569     "tenant": "tenant-red"\r
570 }\r
571 },\r
572 {\r
573 "input": {\r
574 \r
575     "endpoint-group": "webservers",\r
576 \r
577     "network-containment" : "subnet-10.0.36.0/24",\r
578 \r
579     "l2-context": "bridge-domain1",\r
580     "mac-address": "00:00:00:00:36:03",\r
581 \r
582     "l3-address": [\r
583         {\r
584             "ip-address": "10.0.36.3",\r
585             "l3-context": "l3-context-vrf-red"\r
586         }\r
587     ],\r
588     "port-name": "vethl-h36_3",\r
589     "tenant": "tenant-red"\r
590 }\r
591 },\r
592 {\r
593 "input": {\r
594 \r
595     "endpoint-group": "webservers",\r
596 \r
597     "network-containment" : "subnet-10.0.36.0/24",\r
598 \r
599     "l2-context": "bridge-domain1",\r
600     "mac-address": "00:00:00:00:36:04",\r
601 \r
602     "l3-address": [\r
603         {\r
604             "ip-address": "10.0.36.4",\r
605             "l3-context": "l3-context-vrf-red"\r
606         }\r
607     ],\r
608     "port-name": "vethl-h36_4",\r
609     "tenant": "tenant-red"\r
610 }\r
611 },\r
612 {\r
613 "input": {\r
614 \r
615     "endpoint-group": "clients",\r
616 \r
617     "network-containment" : "subnet-10.0.35.0/24",\r
618 \r
619     "l2-context": "bridge-domain1",\r
620     "mac-address": "00:00:00:00:35:04",\r
621 \r
622     "l3-address": [\r
623         {\r
624             "ip-address": "10.0.35.4",\r
625             "l3-context": "l3-context-vrf-red"\r
626         }\r
627     ],\r
628     "port-name": "vethl-h35_4",\r
629     "tenant": "tenant-red"\r
630 }\r
631 },\r
632 {\r
633 "input": {\r
634 \r
635     "endpoint-group": "clients",\r
636 \r
637     "network-containment" : "subnet-10.0.35.0/24",\r
638 \r
639     "l2-context": "bridge-domain1",\r
640     "mac-address": "00:00:00:00:35:05",\r
641 \r
642     "l3-address": [\r
643         {\r
644             "ip-address": "10.0.35.5",\r
645             "l3-context": "l3-context-vrf-red"\r
646         }\r
647     ],\r
648     "port-name": "vethl-h35_5",\r
649     "tenant": "tenant-red"\r
650 }\r
651 },\r
652 {\r
653 "input": {\r
654 \r
655     "endpoint-group": "webservers",\r
656 \r
657     "network-containment" : "subnet-10.0.36.0/24",\r
658 \r
659     "l2-context": "bridge-domain1",\r
660     "mac-address": "00:00:00:00:36:05",\r
661 \r
662     "l3-address": [\r
663         {\r
664             "ip-address": "10.0.36.5",\r
665             "l3-context": "l3-context-vrf-red"\r
666         }\r
667     ],\r
668     "port-name": "vethl-h36_5",\r
669     "tenant": "tenant-red"\r
670 }\r
671 }]\r
672 \r
673 \r
674 def get_endpoint_uri():\r
675     return "/restconf/operations/endpoint:register-endpoint"\r
676 \r
677 def get_tunnel_oper_uri():\r
678     return "/restconf/operational/opendaylight-inventory:nodes/"\r
679 \r
680 def get_topology_oper_uri():\r
681     return "/restconf/operational/network-topology:network-topology/topology/ovsdb:1/"\r
682 \r
683 if __name__ == "__main__":\r
684     # Launch main menu\r
685 \r
686 \r
687     # Some sensible defaults\r
688     controller=os.environ.get('ODL')\r
689     if controller == None:\r
690         sys.exit("No controller set.")\r
691 \r
692     print "Contacting controller at %s" % controller\r
693     print "waiting for manager on SFFs..."\r
694     wait_for_sff_in_datastore(get_topology_oper_uri())\r
695     print "sending service functions"\r
696     put(controller, DEFAULT_PORT, get_service_functions_uri(), get_service_functions_data(), True)\r
697     print "sending service function forwarders"\r
698     put(controller, DEFAULT_PORT, get_service_function_forwarders_uri(), get_service_function_forwarders_data(), True)\r
699     print "waiting for switches on SFFs..."\r
700     wait_for_sff_in_datastore(get_tunnel_oper_uri())\r
701     print "sending service function chains"\r
702     put(controller, DEFAULT_PORT, get_service_function_chains_uri(), get_service_function_chains_data(), True)\r
703     print "sending service function paths"\r
704     put(controller, DEFAULT_PORT, get_service_function_paths_uri(), get_service_function_paths_data(), True)\r
705     print "sending tunnel"\r
706     put(controller, DEFAULT_PORT, get_tunnel_uri_1(), get_tunnel_data_1(), True)\r
707     print "sending tenant"\r
708     put(controller, DEFAULT_PORT, get_tunnel_uri_6(), get_tunnel_data_6(), True)\r
709     print "sending tenant"\r
710     put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)\r
711     print "registering endpoints"\r
712     for endpoint in get_endpoint_data():\r
713         post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)\r