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