d08aa82c9dde603b5f689907f9e9deda588abbd0
[groupbasedpolicy.git] / demos / gbpsfc-env / demo-gbp2 / rest.py
1 #!/usr/bin/python
2 import argparse
3 import requests,json
4 from requests.auth import HTTPBasicAuth
5 from subprocess import call
6 import time
7 import sys
8 import os
9
10
11 DEFAULT_PORT='8181'
12
13
14 USERNAME='admin'
15 PASSWORD='admin'
16
17
18 OPER_NODES='/restconf/operational/opendaylight-inventory:nodes/'
19 CONF_TENANT='/restconf/config/policy:tenants'
20
21 def get(host, port, uri):
22     url='http://'+host+":"+port+uri
23     #print url
24     r = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD))
25     jsondata=json.loads(r.text)
26     return jsondata
27
28 def put(host, port, uri, data, debug=False):
29     '''Perform a PUT rest operation, using the URL and data provided'''
30
31     url='http://'+host+":"+port+uri
32
33     headers = {'Content-type': 'application/yang.data+json',
34                'Accept': 'application/yang.data+json'}
35     if debug == True:
36         print "PUT %s" % url
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))
39     if debug == True:
40         print r.text
41     r.raise_for_status()
42
43 def post(host, port, uri, data, debug=False):
44     '''Perform a POST rest operation, using the URL and data provided'''
45
46     url='http://'+host+":"+port+uri
47     headers = {'Content-type': 'application/yang.data+json',
48                'Accept': 'application/yang.data+json'}
49     if debug == True:
50         print "POST %s" % url
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))
53     if debug == True:
54         print r.text
55     r.raise_for_status()
56
57
58 def get_tenant_data():
59     return {
60         "policy:tenant": {
61           "id": "tenant-red",
62           "name": "GBPPOC",
63           "forwarding-context": {
64             "l2-bridge-domain": [
65               {
66                 "id": "bridge-domain1",
67                 "parent": "l3-context-vrf-red"
68               }
69             ],
70             "l2-flood-domain": [
71               {
72                 "id": "flood-domain-1",
73                 "parent": "bridge-domain1"
74               },
75               {
76                 "id": "flood-domain1",
77                 "parent": "bridge-domain1"
78               }
79             ],
80             "l3-context": [
81               {
82                 "id": "l3-context-vrf-red"
83               }
84             ],
85             "subnet": [
86               {
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"
91               },
92               {
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"
97               }
98             ]
99           },
100           "policy": {
101             "contract": [
102               {
103                 "clause": [
104                   {
105                     "name": "allow-http-clause",
106                     "subject-refs": [
107                       "allow-http-subject",
108                       "allow-icmp-subject"
109                     ]
110                   }
111                 ],
112                 "id": "icmp-http-contract",
113                 "subject": [
114                   {
115                     "name": "allow-http-subject",
116                     "rule": [
117                       {
118                         "classifier-ref": [
119                           {
120                             "direction": "in",
121                             "name": "http-dest",
122                             "instance-name": "http-dest"
123                           },
124                           {
125                             "direction": "out",
126                             "name": "http-src",
127                             "instance-name": "http-src"
128                           }
129                         ],
130                         "action-ref": [
131                           {
132                             "name": "allow1",
133                             "order": 0
134                           }
135                         ],
136                         "name": "allow-http-rule"
137                       }
138                     ]
139                   },
140                   {
141                     "name": "allow-icmp-subject",
142                     "rule": [
143                       {
144                         "classifier-ref": [
145                           {
146                             "name": "icmp-in",
147                             "instance-name": "icmp",
148                             "direction": "in"
149                           },
150                           {
151                             "name": "icmp-out",
152                             "instance-name": "icmp",
153                             "direction": "out"
154                           }
155                         ],
156                         "action-ref": [
157                           {
158                             "name": "allow1",
159                             "order": 0
160                           }
161                         ],
162                         "name": "allow-icmp-rule"
163                       }
164                     ]
165                   }
166                 ]
167               }
168             ],
169             "endpoint-group": [
170               {
171                 "consumer-named-selector": [
172                   {
173                     "contract": [
174                       "icmp-http-contract"
175                     ],
176                     "name": "webservers-clients-icmp-http-contract"
177                   }
178                 ],
179                 "id": "clients",
180                 "provider-named-selector": []
181               },
182               {
183                 "consumer-named-selector": [],
184                 "id": "webservers1",
185                 "provider-named-selector": [
186                   {
187                     "contract": [
188                       "icmp-http-contract"
189                     ],
190                     "name": "webservers-clients-icmp-http-contract"
191                   }
192                 ]
193               },
194               {
195                 "consumer-named-selector": [],
196                 "id": "webservers2",
197                 "provider-named-selector": [
198                   {
199                     "contract": [
200                       "icmp-http-contract"
201                     ],
202                     "name": "webservers-clients-icmp-http-contract"
203                   }
204                 ]
205               }
206             ],
207             "subject-feature-instances": {
208               "classifier-instance": [
209                 {
210                   "classifier-definition-id": "Classifier-L4",
211                   "name": "http-dest",
212                   "parameter-value": [
213                     {
214                       "int-value": "6",
215                       "name": "proto"
216                     },
217                     {
218                       "int-value": "80",
219                       "name": "destport"
220                     }
221                   ]
222                 },
223                 {
224                   "classifier-definition-id": "Classifier-L4",
225                   "name": "http-src",
226                   "parameter-value": [
227                     {
228                       "int-value": "6",
229                       "name": "proto"
230                     },
231                     {
232                       "int-value": "80",
233                       "name": "sourceport"
234                     }
235                   ]
236                 },
237                 {
238                   "classifier-definition-id": "Classifier-IP-Protocol",
239                   "name": "icmp",
240                   "parameter-value": [
241                     {
242                       "int-value": "1",
243                       "name": "proto"
244                     }
245                   ]
246                 }
247               ],
248               "action-instance": [
249                 {
250                   "name": "allow1",
251                   "action-definition-id": "Action-Allow"
252                 }
253               ]
254             }
255           }
256         }
257     }
258
259 # Main definition - constants
260
261 # =======================
262 #     MENUS FUNCTIONS
263 # =======================
264
265 # Main menu
266
267 # =======================
268 #      MAIN PROGRAM
269 # =======================
270
271 # Main Program
272
273 def get_tenant_uri():
274     return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
275
276
277 def get_tunnel_data():
278     return {
279   "opendaylight-inventory:nodes": {
280     "node": [
281       {
282         "id": "openflow:1",
283         "ofoverlay:tunnel": [
284           {
285             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
286             "node-connector-id": "openflow:1:1",
287             "ip": "192.168.50.70",
288             "port": 6633
289           },
290           {
291             "tunnel-type": "overlay:tunnel-type-vxlan",
292             "node-connector-id": "openflow:1:2",
293             "ip": "192.168.50.70",
294             "port": 4789
295           }
296         ]
297       },
298       {
299         "id": "openflow:2",
300         "ofoverlay:tunnel": [
301           {
302             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
303             "node-connector-id": "openflow:2:1",
304             "ip": "192.168.50.71",
305             "port": 6633
306           },
307           {
308             "tunnel-type": "overlay:tunnel-type-vxlan",
309             "node-connector-id": "openflow:2:2",
310             "ip": "192.168.50.71",
311             "port": 4789
312           }
313         ]
314       },
315                    {
316         "id": "openflow:3",
317         "ofoverlay:tunnel": [
318           {
319             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
320             "node-connector-id": "openflow:3:1",
321             "ip": "192.168.50.72",
322             "port": 6633
323           },
324           {
325             "tunnel-type": "overlay:tunnel-type-vxlan",
326             "node-connector-id": "openflow:3:2",
327             "ip": "192.168.50.72",
328             "port": 4789
329           }
330         ]
331       },
332
333     ]
334   }
335             }
336
337 def get_tunnel_uri():
338     return "/restconf/config/opendaylight-inventory:nodes"
339
340 def get_endpoint_data():
341     return [{
342     "input": {
343
344         "endpoint-group": "clients",
345
346         "network-containment" : "subnet-10.0.35.0/24",
347
348         "l2-context": "bridge-domain1",
349         "mac-address": "00:00:00:00:35:02",
350
351         "l3-address": [
352             {
353                 "ip-address": "10.0.35.2",
354                 "l3-context": "l3-context-vrf-red"
355             }
356         ],
357         "port-name": "vethl-h35_2",
358         "tenant": "tenant-red"
359     }
360 },
361             {
362     "input": {
363
364         "endpoint-group": "clients",
365
366         "network-containment" : "subnet-10.0.35.0/24",
367
368         "l2-context": "bridge-domain1",
369         "mac-address": "00:00:00:00:35:03",
370
371         "l3-address": [
372             {
373                 "ip-address": "10.0.35.3",
374                 "l3-context": "l3-context-vrf-red"
375             }
376         ],
377         "port-name": "vethl-h35_3",
378         "tenant": "tenant-red"
379     }
380 },
381             {
382     "input": {
383
384         "endpoint-group": "clients",
385
386         "network-containment" : "subnet-10.0.35.0/24",
387
388         "l2-context": "bridge-domain1",
389         "mac-address": "00:00:00:00:35:04",
390
391         "l3-address": [
392             {
393                 "ip-address": "10.0.35.4",
394                 "l3-context": "l3-context-vrf-red"
395             }
396         ],
397         "port-name": "vethl-h35_4",
398         "tenant": "tenant-red"
399     }
400 },
401             {
402     "input": {
403
404         "endpoint-group": "clients",
405
406         "network-containment" : "subnet-10.0.35.0/24",
407
408         "l2-context": "bridge-domain1",
409         "mac-address": "00:00:00:00:35:05",
410
411         "l3-address": [
412             {
413                 "ip-address": "10.0.35.5",
414                 "l3-context": "l3-context-vrf-red"
415             }
416         ],
417         "port-name": "vethl-h35_5",
418         "tenant": "tenant-red"
419     }
420 },
421             {
422     "input": {
423
424         "endpoint-group": "webservers1",
425
426         "network-containment" : "subnet-10.0.36.0/24",
427
428         "l2-context": "bridge-domain1",
429         "mac-address": "00:00:00:00:36:02",
430
431         "l3-address": [
432             {
433                 "ip-address": "10.0.36.2",
434                 "l3-context": "l3-context-vrf-red"
435             }
436         ],
437         "port-name": "vethl-h36_2",
438         "tenant": "tenant-red"
439     }
440 },
441             {
442     "input": {
443
444         "endpoint-group": "webservers2",
445
446         "network-containment" : "subnet-10.0.36.0/24",
447
448         "l2-context": "bridge-domain1",
449         "mac-address": "00:00:00:00:36:03",
450
451         "l3-address": [
452             {
453                 "ip-address": "10.0.36.3",
454                 "l3-context": "l3-context-vrf-red"
455             }
456         ],
457         "port-name": "vethl-h36_3",
458         "tenant": "tenant-red"
459     }
460 },
461             {
462     "input": {
463
464         "endpoint-group": "webservers1",
465
466         "network-containment" : "subnet-10.0.36.0/24",
467
468         "l2-context": "bridge-domain1",
469         "mac-address": "00:00:00:00:36:04",
470
471         "l3-address": [
472             {
473                 "ip-address": "10.0.36.4",
474                 "l3-context": "l3-context-vrf-red"
475             }
476         ],
477         "port-name": "vethl-h36_4",
478         "tenant": "tenant-red"
479     }
480 },{
481     "input": {
482
483         "endpoint-group": "webservers2",
484
485         "network-containment" : "subnet-10.0.36.0/24",
486
487         "l2-context": "bridge-domain1",
488         "mac-address": "00:00:00:00:36:05",
489
490         "l3-address": [
491             {
492                 "ip-address": "10.0.36.5",
493                 "l3-context": "l3-context-vrf-red"
494             }
495         ],
496         "port-name": "vethl-h36_5",
497         "tenant": "tenant-red"
498     }
499 }]
500
501 def get_endpoint_uri():
502     return "/restconf/operations/endpoint:register-endpoint"
503
504 if __name__ == "__main__":
505     # Launch main menu
506
507
508     # Some sensible defaults
509     controller=os.environ.get('ODL')
510     if controller == None:
511         sys.exit("No controller set.")
512
513
514     print "tenants"
515     tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
516     print tenants
517
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)
525
526
527
528