7c7f4993e9d11c3e64f5d891a24e1e14674d1546
[groupbasedpolicy.git] / demos / gbpsfc-env / demo-gbp1 / 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": "webservers",
185                 "provider-named-selector": [
186                   {
187                     "contract": [
188                       "icmp-http-contract"
189                     ],
190                     "name": "webservers-clients-icmp-http-contract"
191                   }
192                 ]
193               }
194             ],
195             "subject-feature-instances": {
196               "classifier-instance": [
197                 {
198                   "classifier-definition-id": "Classifier-L4",
199                   "name": "http-dest",
200                   "parameter-value": [
201                     {
202                       "int-value": "6",
203                       "name": "proto"
204                     },
205                     {
206                       "int-value": "80",
207                       "name": "destport"
208                     }
209                   ]
210                 },
211                 {
212                   "classifier-definition-id": "Classifier-L4",
213                   "name": "http-src",
214                   "parameter-value": [
215                     {
216                       "int-value": "6",
217                       "name": "proto"
218                     },
219                     {
220                       "int-value": "80",
221                       "name": "sourceport"
222                     }
223                   ]
224                 },
225                 {
226                   "classifier-definition-id": "Classifier-IP-Protocol",
227                   "name": "icmp",
228                   "parameter-value": [
229                     {
230                       "int-value": "1",
231                       "name": "proto"
232                     }
233                   ]
234                 }
235               ],
236               "action-instance": [
237                 {
238                   "name": "allow1",
239                   "action-definition-id": "Action-Allow"
240                 }
241               ]
242             }
243           }
244         }
245     }
246
247 # Main definition - constants
248
249 # =======================
250 #     MENUS FUNCTIONS
251 # =======================
252
253 # Main menu
254
255 # =======================
256 #      MAIN PROGRAM
257 # =======================
258
259 # Main Program
260
261 def get_tenant_uri():
262     return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
263
264
265 def get_tunnel_data():
266     return {
267   "opendaylight-inventory:nodes": {
268     "node": [
269       {
270         "id": "openflow:1",
271         "ofoverlay:tunnel": [
272           {
273             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
274             "node-connector-id": "openflow:1:1",
275             "ip": "192.168.50.70",
276             "port": 6633
277           },
278           {
279             "tunnel-type": "overlay:tunnel-type-vxlan",
280             "node-connector-id": "openflow:1:2",
281             "ip": "192.168.50.70",
282             "port": 4789
283           }
284         ]
285       },
286       {
287         "id": "openflow:2",
288         "ofoverlay:tunnel": [
289           {
290             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
291             "node-connector-id": "openflow:2:1",
292             "ip": "192.168.50.71",
293             "port": 6633
294           },
295           {
296             "tunnel-type": "overlay:tunnel-type-vxlan",
297             "node-connector-id": "openflow:2:2",
298             "ip": "192.168.50.71",
299             "port": 4789
300           }
301         ]
302       },
303                    {
304         "id": "openflow:3",
305         "ofoverlay:tunnel": [
306           {
307             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
308             "node-connector-id": "openflow:3:1",
309             "ip": "192.168.50.72",
310             "port": 6633
311           },
312           {
313             "tunnel-type": "overlay:tunnel-type-vxlan",
314             "node-connector-id": "openflow:3:2",
315             "ip": "192.168.50.72",
316             "port": 4789
317           }
318         ]
319       },
320
321     ]
322   }
323             }
324
325 def get_tunnel_uri():
326     return "/restconf/config/opendaylight-inventory:nodes"
327
328 def get_endpoint_data():
329     return [{
330     "input": {
331
332         "endpoint-group": "clients",
333
334         "network-containment" : "subnet-10.0.35.0/24",
335
336         "l2-context": "bridge-domain1",
337         "mac-address": "00:00:00:00:35:02",
338
339         "l3-address": [
340             {
341                 "ip-address": "10.0.35.2",
342                 "l3-context": "l3-context-vrf-red"
343             }
344         ],
345         "port-name": "vethl-h35_2",
346         "tenant": "tenant-red"
347     }
348 },
349             {
350     "input": {
351
352         "endpoint-group": "clients",
353
354         "network-containment" : "subnet-10.0.35.0/24",
355
356         "l2-context": "bridge-domain1",
357         "mac-address": "00:00:00:00:35:03",
358
359         "l3-address": [
360             {
361                 "ip-address": "10.0.35.3",
362                 "l3-context": "l3-context-vrf-red"
363             }
364         ],
365         "port-name": "vethl-h35_3",
366         "tenant": "tenant-red"
367     }
368 },
369             {
370     "input": {
371
372         "endpoint-group": "clients",
373
374         "network-containment" : "subnet-10.0.35.0/24",
375
376         "l2-context": "bridge-domain1",
377         "mac-address": "00:00:00:00:35:04",
378
379         "l3-address": [
380             {
381                 "ip-address": "10.0.35.4",
382                 "l3-context": "l3-context-vrf-red"
383             }
384         ],
385         "port-name": "vethl-h35_4",
386         "tenant": "tenant-red"
387     }
388 },
389             {
390     "input": {
391
392         "endpoint-group": "clients",
393
394         "network-containment" : "subnet-10.0.35.0/24",
395
396         "l2-context": "bridge-domain1",
397         "mac-address": "00:00:00:00:35:05",
398
399         "l3-address": [
400             {
401                 "ip-address": "10.0.35.5",
402                 "l3-context": "l3-context-vrf-red"
403             }
404         ],
405         "port-name": "vethl-h35_5",
406         "tenant": "tenant-red"
407     }
408 },
409             {
410     "input": {
411
412         "endpoint-group": "webservers",
413
414         "network-containment" : "subnet-10.0.36.0/24",
415
416         "l2-context": "bridge-domain1",
417         "mac-address": "00:00:00:00:36:02",
418
419         "l3-address": [
420             {
421                 "ip-address": "10.0.36.2",
422                 "l3-context": "l3-context-vrf-red"
423             }
424         ],
425         "port-name": "vethl-h36_2",
426         "tenant": "tenant-red"
427     }
428 },
429             {
430     "input": {
431
432         "endpoint-group": "webservers",
433
434         "network-containment" : "subnet-10.0.36.0/24",
435
436         "l2-context": "bridge-domain1",
437         "mac-address": "00:00:00:00:36:03",
438
439         "l3-address": [
440             {
441                 "ip-address": "10.0.36.3",
442                 "l3-context": "l3-context-vrf-red"
443             }
444         ],
445         "port-name": "vethl-h36_3",
446         "tenant": "tenant-red"
447     }
448 },
449             {
450     "input": {
451
452         "endpoint-group": "webservers",
453
454         "network-containment" : "subnet-10.0.36.0/24",
455
456         "l2-context": "bridge-domain1",
457         "mac-address": "00:00:00:00:36:04",
458
459         "l3-address": [
460             {
461                 "ip-address": "10.0.36.4",
462                 "l3-context": "l3-context-vrf-red"
463             }
464         ],
465         "port-name": "vethl-h36_4",
466         "tenant": "tenant-red"
467     }
468 },{
469     "input": {
470
471         "endpoint-group": "webservers",
472
473         "network-containment" : "subnet-10.0.36.0/24",
474
475         "l2-context": "bridge-domain1",
476         "mac-address": "00:00:00:00:36:05",
477
478         "l3-address": [
479             {
480                 "ip-address": "10.0.36.5",
481                 "l3-context": "l3-context-vrf-red"
482             }
483         ],
484         "port-name": "vethl-h36_5",
485         "tenant": "tenant-red"
486     }
487 }]
488
489 def get_endpoint_uri():
490     return "/restconf/operations/endpoint:register-endpoint"
491
492 if __name__ == "__main__":
493     # Launch main menu
494
495
496     # Some sensible defaults
497     controller=os.environ.get('ODL')
498     if controller == None:
499         sys.exit("No controller set.")
500
501
502     print "tenants"
503     tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
504     print tenants
505
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)
513
514
515
516