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