Merge "Add missing license headers"
[groupbasedpolicy.git] / demos / gbpsfc-env / rest-clean.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     r = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD))
24     return r
25
26 def rest_delete(host, port, uri, debug=False):
27     '''Perform a DELETE rest operation, using the URL and data provided'''
28     url='http://'+host+":"+port+uri
29     headers = {'Content-type': 'application/yang.data+json',
30                'Accept': 'application/yang.data+json'}
31     if debug == True:
32         print "DELETE %s" % url
33     r = requests.delete(url, headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
34     if debug == True:
35         print r.text
36     r.raise_for_status()
37
38 def post(host, port, uri, data, debug=False):
39     '''Perform a POST rest operation, using the URL and data provided'''
40     url='http://'+host+":"+port+uri
41     headers = {'Content-type': 'application/yang.data+json',
42                'Accept': 'application/yang.data+json'}
43     if debug == True:
44         print "POST %s" % url
45         print json.dumps(data, indent=4, sort_keys=True)
46     r = requests.post(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
47     if debug == True:
48         print r.text
49     r.raise_for_status()
50
51 def get_service_functions_uri():
52     return "/restconf/config/service-function:service-functions"
53
54 def get_service_function_forwarders_uri():
55     return "/restconf/config/service-function-forwarder:service-function-forwarders"
56
57 def get_service_function_chains_uri():
58     return "/restconf/config/service-function-chain:service-function-chains/"
59
60 def get_service_function_paths_uri():
61     return "/restconf/config/service-function-path:service-function-paths/"
62
63 def get_tenant_uri():
64     return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
65
66 def get_tunnel_uri():
67     return "/restconf/config/opendaylight-inventory:nodes"
68
69 def get_endpoint_uri():
70     return "/restconf/operations/endpoint:unregister-endpoint"
71
72 def get_topology_uri():
73     return "/restconf/config/network-topology:network-topology/topology/ovsdb:1"
74
75 if __name__ == "__main__":
76     # Launch main menu
77
78
79     # Some sensible defaults
80     controller=os.environ.get('ODL')
81     if controller == None:
82         sys.exit("No controller set.")
83     else:
84         print "Contacting controller at %s" % controller
85
86     resp=get(controller,DEFAULT_PORT,'/restconf/operational/endpoint:endpoints')
87     resp_json=json.loads(resp.text)
88     l2_eps=resp_json['endpoints']['endpoint']
89     l3_eps=resp_json['endpoints']['endpoint-l3']
90
91     print "deleting service function paths"
92     rest_delete(controller, DEFAULT_PORT, get_service_function_paths_uri(), True)
93
94     print "deleting service function chains"
95     rest_delete(controller, DEFAULT_PORT, get_service_function_chains_uri(), True)
96
97     print "deleting service functions"
98     rest_delete(controller, DEFAULT_PORT, get_service_functions_uri(), True)
99
100     print "deleting service function forwarders"
101     rest_delete(controller, DEFAULT_PORT, get_service_function_forwarders_uri(), True)
102
103     print "deleting tunnel"
104     rest_delete(controller, DEFAULT_PORT, get_tunnel_uri(), True)
105
106     print "deleting tenant"
107     rest_delete(controller, DEFAULT_PORT, get_tenant_uri(), True)
108
109     print "unregistering L2 endpoints"
110     for endpoint in l2_eps:
111         data={ "input": { "l2": [ { "l2-context": endpoint['l2-context'] ,"mac-address": endpoint['mac-address'] } ] } }
112         post(controller, DEFAULT_PORT, get_endpoint_uri(),data,True)
113
114     print "unregistering L3 endpoints"
115     for endpoint in l3_eps:
116         data={ "input": { "l3": [ { "l3-context": endpoint['l3-context'] ,"ip-address": endpoint['ip-address'] } ] } }
117         post(controller, DEFAULT_PORT, get_endpoint_uri(),data,True)
118
119     print "topology removed check"
120     resp=get(controller, DEFAULT_PORT, get_topology_uri())
121     topology_json=json.loads(resp.text)
122     if resp.status_code == requests.codes.ok:
123         print "WARNING: Topology %s has not been removed! Removing now..." % get_topology_uri()
124         rest_delete(controller, DEFAULT_PORT, get_topology_uri(), True)