4 from requests.auth import HTTPBasicAuth
5 from subprocess import call
18 OPER_NODES='/restconf/operational/opendaylight-inventory:nodes/'
19 CONF_TENANT='/restconf/config/policy:tenants'
21 def get(host, port, uri):
22 url='http://'+host+":"+port+uri
23 r = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD))
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'}
32 print "DELETE %s" % url
33 r = requests.delete(url, headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
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'}
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))
51 def get_service_functions_uri():
52 return "/restconf/config/service-function:service-functions"
54 def get_service_function_forwarders_uri():
55 return "/restconf/config/service-function-forwarder:service-function-forwarders"
57 def get_service_function_chains_uri():
58 return "/restconf/config/service-function-chain:service-function-chains/"
60 def get_service_function_paths_uri():
61 return "/restconf/config/service-function-path:service-function-paths/"
64 return "/restconf/config/policy:tenants/policy:tenant/tenant-red"
67 return "/restconf/config/opendaylight-inventory:nodes"
69 def get_endpoint_uri():
70 return "/restconf/operations/endpoint:unregister-endpoint"
72 def get_topology_uri():
73 return "/restconf/config/network-topology:network-topology/topology/ovsdb:1"
75 if __name__ == "__main__":
79 # Some sensible defaults
80 controller=os.environ.get('ODL')
81 if controller == None:
82 sys.exit("No controller set.")
84 print "Contacting controller at %s" % controller
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']
91 print "deleting service function paths"
92 rest_delete(controller, DEFAULT_PORT, get_service_function_paths_uri(), True)
94 print "deleting service function chains"
95 rest_delete(controller, DEFAULT_PORT, get_service_function_chains_uri(), True)
97 print "deleting service functions"
98 rest_delete(controller, DEFAULT_PORT, get_service_functions_uri(), True)
100 print "deleting service function forwarders"
101 rest_delete(controller, DEFAULT_PORT, get_service_function_forwarders_uri(), True)
103 print "deleting tunnel"
104 rest_delete(controller, DEFAULT_PORT, get_tunnel_uri(), True)
106 print "deleting tenant"
107 rest_delete(controller, DEFAULT_PORT, get_tenant_uri(), True)
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)
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)
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)