blob: aa9abfc8445f026f4bf8c50300229b269e75bfbd [file] [log] [blame]
Boyuan Yandcdbc892019-02-27 15:09:19 -08001#! /usr/bin/env python
2
3import sys
4import tapiHelper
5import json
6import requests
7
8if len(sys.argv) != 3 and len(sys.argv) != 4:
9 print "usage: execute-tapi-delete-call <onos-node> <deletion-type> [uuid]"
10 print "\t- <onos-node> is onos IP. 'localhost' is invalid."
11 print "\t- <deletion-type> is one of {line, client, both}, which mean line-side deletion, " \
12 "client-side deletion, and all deletion respectively."
13 print "\t- [uuid] is the created service uuid, which is optional. If uuid is empty, " \
14 "all connectivity services with <deletion-type> will be deleted."
15 print "\t If [uuid] is not empty, and <deletion-type> is 'both', this script doesn't work."
16 print "\t Otherwise, delete line-side or client-side connectivity with specific uuid."
17 print "For example, if we want to delete all client-side services on local host, the command should be:"
18 print "\t python execute-tapi-delete-call.py 127.0.0.1 client"
19 sys.exit(1)
20
21
22#
23# Define the input json string for service deletion.
24#
25def tapi_deletion_input(service_uuid):
26 delete_input = {
27 "tapi-connectivity:input":
28 {
29 "service-id-or-name": service_uuid
30 }
31 }
32 return delete_input
33
34
35#
36# Return sip uuid pair of service structure
37#
38def parse_sip_uuid_pair(sv): return \
39 (sv["end-point"][0]["service-interface-point"]["service-interface-point-uuid"],
40 sv["end-point"][1]["service-interface-point"]["service-interface-point-uuid"])
41
42
43#
44# Find sip in sip array through sip uuid.
45#
46def find_sip(sip_uuid, sips):
47 for sip in sips:
48 if sip["uuid"] == sip_uuid:
49 return sip
50 return None
51
52
53#
54# Post service deletion request to ONOS.
55#
56def post_deletion(service_uuid, del_request):
57 delete_input_json = json.dumps(tapi_deletion_input(service_uuid))
58 print "\nThe json content of deletion operation for connectivity service is \n\t\t%s." % \
59 delete_input_json
60 headers = {'Content-type': 'application/json'}
61 resp = requests.post(del_request, data=delete_input_json, headers=headers, auth=('onos', 'rocks'))
62 if resp.status_code != 200:
63 raise Exception('POST {}'.format(resp.status_code))
64 return resp
65
66
67# 1. Parse the input params.
68node = sys.argv[1]
69del_type = sys.argv[2]
70serv_uuid = None
71assert del_type in {"line", "client", "both"}
72if len(sys.argv) == 4:
73 serv_uuid = sys.argv[3]
74# 2. Get the subtree of tapi-common:context
75context_request = 'http://' + node + ':8181/onos/restconf/data/tapi-common:context'
76delete_request = 'http://' + node + ':8181/onos/restconf/operations/tapi-connectivity:delete-connectivity-service'
77context = tapiHelper.get_context(context_request)["tapi-common:context"]
78sips = context["service-interface-point"]
79try:
80 services = context["tapi-connectivity:connectivity-context"]["connectivity-service"]
81except KeyError:
82 print "Warning - there is no connectivity service in ONOS (%s)." % node
83 sys.exit(0)
84# 3. handle deletion requests according to <deletion-type> and [uuid]
85if serv_uuid is None:
86 # If [uuid] is empty, traverse all services with <deletion-type>
87 service_map = {}
88 del_num = 0
89 for service in services:
90 src, _ = parse_sip_uuid_pair(service)
91 sip = find_sip(src, sips)
92 if ((del_type == "line" or del_type == "both") and tapiHelper.is_photonic_media(sip)) or \
93 ((del_type == "client" or del_type == "both") and tapiHelper.is_dsr_media(sip)):
94 json_resp = post_deletion(service["uuid"], delete_request)
95 del_num += 1
96 print "Returns json string for deletion operations is\n %s\n" % json_resp
97 if del_num == 0:
98 print "Warning - there is no %s-side connectivity servicein ONOS (%s)." % (del_type, node)
99else:
100 # If [uuid] is not empty, check the <deletion-type>
101 if del_type == "both":
102 print "Error - The option 'both' is illegal when [uuid] is assigned."
103 else:
104 is_del = False
105 for service in services:
106 if service["uuid"] == serv_uuid:
107 src, _ = parse_sip_uuid_pair(service)
108 sip = find_sip(src, sips)
109 if (del_type == "line" and tapiHelper.is_photonic_media(sip)) or \
110 (del_type == "client" and tapiHelper.is_dsr_media(sip)):
111 json_resp = post_deletion(service["uuid"], delete_request)
112 is_del = True
113 print "Returns json string for deletion operations is\n %s\n" % json_resp
114 break
115 if not is_del:
116 print "Warning - Cannot find %s-side connectivity service with service uuid %s." % (del_type, serv_uuid)