35fceb86581f65788556bdad4e62ddaf441bb48e
[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",
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-red"
257
258
259 def get_tunnel_data():
260     return {
261   "opendaylight-inventory:nodes": {
262     "node": [
263       {
264         "id": "openflow:1",
265         "ofoverlay:tunnel": [
266           {
267             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
268             "node-connector-id": "openflow:1:1",
269             "ip": "192.168.50.70",
270             "port": 6633
271           },
272           {
273             "tunnel-type": "overlay:tunnel-type-vxlan",
274             "node-connector-id": "openflow:1:2",
275             "ip": "192.168.50.70",
276             "port": 4789
277           }
278         ]
279       },
280       {
281         "id": "openflow:2",
282         "ofoverlay:tunnel": [
283           {
284             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
285             "node-connector-id": "openflow:2:1",
286             "ip": "192.168.50.71",
287             "port": 6633
288           },
289           {
290             "tunnel-type": "overlay:tunnel-type-vxlan",
291             "node-connector-id": "openflow:2:2",
292             "ip": "192.168.50.71",
293             "port": 4789
294           }
295         ]
296       },
297                    {
298         "id": "openflow:3",
299         "ofoverlay:tunnel": [
300           {
301             "tunnel-type": "overlay:tunnel-type-vxlan-gpe",
302             "node-connector-id": "openflow:3:1",
303             "ip": "192.168.50.72",
304             "port": 6633
305           },
306           {
307             "tunnel-type": "overlay:tunnel-type-vxlan",
308             "node-connector-id": "openflow:3:2",
309             "ip": "192.168.50.72",
310             "port": 4789
311           }
312         ]
313       },
314              
315     ]
316   }
317             }
318     
319 def get_tunnel_uri():
320     return "/restconf/config/opendaylight-inventory:nodes"
321
322 def get_endpoint_data():
323     return [{
324     "input": {
325
326         "endpoint-group": "clients", 
327
328         "network-containment" : "subnet-10.0.35.0/24",
329
330         "l2-context": "bridge-domain1", 
331         "mac-address": "00:00:00:00:35:02", 
332
333         "l3-address": [
334             {
335                 "ip-address": "10.0.35.2", 
336                 "l3-context": "l3-context-vrf-red"
337             }
338         ], 
339         "port-name": "vethl-h35_2", 
340         "tenant": "tenant-red"
341     }
342 },
343             {
344     "input": {
345
346         "endpoint-group": "clients", 
347
348         "network-containment" : "subnet-10.0.35.0/24",
349
350         "l2-context": "bridge-domain1", 
351         "mac-address": "00:00:00:00:35:03", 
352
353         "l3-address": [
354             {
355                 "ip-address": "10.0.35.3", 
356                 "l3-context": "l3-context-vrf-red"
357             }
358         ], 
359         "port-name": "vethl-h35_3", 
360         "tenant": "tenant-red"
361     }
362 },
363             {
364     "input": {
365
366         "endpoint-group": "clients", 
367
368         "network-containment" : "subnet-10.0.35.0/24",
369
370         "l2-context": "bridge-domain1", 
371         "mac-address": "00:00:00:00:35:04", 
372
373         "l3-address": [
374             {
375                 "ip-address": "10.0.35.4", 
376                 "l3-context": "l3-context-vrf-red"
377             }
378         ], 
379         "port-name": "vethl-h35_4", 
380         "tenant": "tenant-red"
381     }
382 },
383             {
384     "input": {
385
386         "endpoint-group": "clients", 
387
388         "network-containment" : "subnet-10.0.35.0/24",
389
390         "l2-context": "bridge-domain1", 
391         "mac-address": "00:00:00:00:35:05", 
392
393         "l3-address": [
394             {
395                 "ip-address": "10.0.35.5", 
396                 "l3-context": "l3-context-vrf-red"
397             }
398         ], 
399         "port-name": "vethl-h35_5", 
400         "tenant": "tenant-red"
401     }
402 },
403             {
404     "input": {
405
406         "endpoint-group": "webservers", 
407
408         "network-containment" : "subnet-10.0.36.0/24",
409
410         "l2-context": "bridge-domain1", 
411         "mac-address": "00:00:00:00:36:02", 
412
413         "l3-address": [
414             {
415                 "ip-address": "10.0.36.2", 
416                 "l3-context": "l3-context-vrf-red"
417             }
418         ], 
419         "port-name": "vethl-h36_2", 
420         "tenant": "tenant-red"
421     }
422 },
423             {
424     "input": {
425
426         "endpoint-group": "webservers", 
427
428         "network-containment" : "subnet-10.0.36.0/24",
429
430         "l2-context": "bridge-domain1", 
431         "mac-address": "00:00:00:00:36:03", 
432
433         "l3-address": [
434             {
435                 "ip-address": "10.0.36.3", 
436                 "l3-context": "l3-context-vrf-red"
437             }
438         ], 
439         "port-name": "vethl-h36_3", 
440         "tenant": "tenant-red"
441     }
442 },
443             {
444     "input": {
445
446         "endpoint-group": "webservers", 
447
448         "network-containment" : "subnet-10.0.36.0/24",
449
450         "l2-context": "bridge-domain1", 
451         "mac-address": "00:00:00:00:36:04", 
452
453         "l3-address": [
454             {
455                 "ip-address": "10.0.36.4", 
456                 "l3-context": "l3-context-vrf-red"
457             }
458         ], 
459         "port-name": "vethl-h36_4", 
460         "tenant": "tenant-red"
461     }
462 },{
463     "input": {
464
465         "endpoint-group": "webservers", 
466
467         "network-containment" : "subnet-10.0.36.0/24",
468
469         "l2-context": "bridge-domain1", 
470         "mac-address": "00:00:00:00:36:05", 
471
472         "l3-address": [
473             {
474                 "ip-address": "10.0.36.5", 
475                 "l3-context": "l3-context-vrf-red"
476             }
477         ], 
478         "port-name": "vethl-h36_5", 
479         "tenant": "tenant-red"
480     }
481 }]
482
483 def get_endpoint_uri():
484     return "/restconf/operations/endpoint:register-endpoint"
485
486 if __name__ == "__main__":
487     # Launch main menu
488
489
490     # Some sensible defaults
491     controller=os.environ.get('ODL')
492     if controller == None:
493         sys.exit("No controller set.")
494     
495
496     print "tenants"
497     tenants=get(controller,DEFAULT_PORT,CONF_TENANT)
498     print tenants
499     
500     print "sending tenant"
501     put(controller, DEFAULT_PORT, get_tenant_uri(), get_tenant_data(),True)
502     print "sending tunnel"
503     put(controller, DEFAULT_PORT, get_tunnel_uri(), get_tunnel_data(), True)
504     print "registering endpoints"
505     for endpoint in get_endpoint_data():
506         post(controller, DEFAULT_PORT, get_endpoint_uri(),endpoint,True)
507         
508         
509     
510