blob: f5bfcc852f1c9e0d0269eff8b0d6a6671d9d31af [file] [log] [blame]
Boyuan Yanf7f50542019-02-25 12:16:09 -08001#!/usr/bin/python
Andrea Campanella50c0c4c2018-12-21 12:18:21 +01002
3import requests
4import json
Andrea Campanella50c0c4c2018-12-21 12:18:21 +01005
Boyuan Yan2313d7d2019-02-24 16:28:01 -08006
Andrea Campanella50c0c4c2018-12-21 12:18:21 +01007#
Boyuan Yan2313d7d2019-02-24 16:28:01 -08008# Creates client-side connectivity json
Andrea Campanella50c0c4c2018-12-21 12:18:21 +01009#
Boyuan Yan2313d7d2019-02-24 16:28:01 -080010def tapi_client_input(sip_uuids):
Andrea Campanella50c0c4c2018-12-21 12:18:21 +010011 create_input = {
12 "tapi-connectivity:input": {
13 "end-point" : [
14 {
15 "local-id": sip_uuids[0],
16 "service-interface-point": {
17 "service-interface-point-uuid" : sip_uuids[0]
18 }
19 }
20 ,
21 {
22 "local-id": sip_uuids[1],
23 "service-interface-point": {
24 "service-interface-point-uuid" : sip_uuids[1]
25 }
26 }
27 ]
28 }
29 }
30 return create_input
31
Boyuan Yan2313d7d2019-02-24 16:28:01 -080032
33#
34# Creates line-side connectivity json
35#
36def tapi_line_input(sip_uuids):
37 create_input = {
38 "tapi-connectivity:input" : {
39 "end-point" : [
40 {
41 "layer-protocol-qualifier" : "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC",
42 "role" : "UNKNOWN",
43 "local-id" : "Src_end_point",
44 "direction" : "BIDIRECTIONAL",
45 "service-interface-point" : {
46 "service-interface-point-uuid" : sip_uuids[0]
47 },
48 "protection-role" : "WORK",
49 "layer-protocol-name" : "PHOTONIC_MEDIA"
50 },
51 {
52 "direction" : "BIDIRECTIONAL",
53 "service-interface-point" : {
54 "service-interface-point-uuid" : sip_uuids[1]
55 },
56 "protection-role" : "WORK",
57 "layer-protocol-name" : "PHOTONIC_MEDIA",
58 "layer-protocol-qualifier" : "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC",
59 "role" : "UNKNOWN",
60 "local-id" : "Dst_end_point"
61 }
62 ]
63 }
64 }
65 return create_input
66
Boyuan Yanf7f50542019-02-25 12:16:09 -080067
Andrea Campanella50c0c4c2018-12-21 12:18:21 +010068#
69# Obtains TAPI context through restconf
70#
71def get_context(url_context):
72 resp = requests.get(url_context, auth=('onos', 'rocks'))
73 if resp.status_code != 200:
Boyuan Yanf7f50542019-02-25 12:16:09 -080074 raise Exception('GET {}'.format(resp.status_code))
Andrea Campanella50c0c4c2018-12-21 12:18:21 +010075 return resp.json()
76
Boyuan Yan2313d7d2019-02-24 16:28:01 -080077
Andrea Campanella50c0c4c2018-12-21 12:18:21 +010078#
79# Requests a connectivity service
80#
81def request_connection(url_connectivity, context):
82 # All Context SIPs
83 sips = context["tapi-common:context"]["service-interface-point"]
84
85 # Sorted Photonic Media SIPs. filter is an iterable
86 esips = list(filter(is_dsr_media, sorted(sips, key=lambda sip: sip["name"][0]["value"])))
87 endpoints = [esips[0], esips[-1]]
88 sip_uuids = []
89 for sip in endpoints:
90 sip_uuids.append(sip["uuid"])
91 for uuid in sip_uuids:
92 print(uuid)
93
Boyuan Yan2313d7d2019-02-24 16:28:01 -080094 create_input_json = json.dumps(tapi_client_input(sip_uuids))
Andrea Campanella50c0c4c2018-12-21 12:18:21 +010095 print (create_input_json)
96 headers = {'Content-type': 'application/json'}
97 resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks'))
98 if resp.status_code != 200:
99 raise Exception('POST {}'.format(resp.status_code))
100 return resp
101
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800102
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100103#
104# Filter method used to keep only SIPs that are photonic_media
105#
106def is_photonic_media(sip):
Boyuan Yanf7f50542019-02-25 12:16:09 -0800107 return sip["layer-protocol-name"] == "PHOTONIC_MEDIA"
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100108
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800109
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100110#
111# Filter method used to keep only SIPs that are DSR
112#
113def is_dsr_media(sip):
Boyuan Yanf7f50542019-02-25 12:16:09 -0800114 return sip["layer-protocol-name"] == "DSR"
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100115
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800116
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100117#
118# Processes the topology to verify the correctness
119#
120def process_topology():
121 # TODO use method to parse topology
122 # Getting the Topology
123 # topology = context["tapi-common:context"]["tapi-topology:topology-context"]["topology"][0]
124 # nodes = topology["node"];
125 # links = topology["link"];
126 noop
127
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800128
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100129#
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800130# Create a client-side connection. Firstly, get the context, parsing for SIPs that connect
131# with each other in line-side; Secondly, issue the request
132#
133def create_client_connection(url_context, url_connectivity):
134 context = get_context(url_context)
Boyuan Yanf7f50542019-02-25 12:16:09 -0800135 # select the first topo from all topologies
136 topo = context["tapi-common:context"]["tapi-topology:topology-context"]["topology"][0]
137
138 # select the first link from all links of topo
139 nep_pair = topo["link"][0]["node-edge-point"]
140 assert topo["uuid"] == nep_pair[0]["topology-uuid"]
141 assert topo["uuid"] == nep_pair[1]["topology-uuid"]
142 (src_onep, dst_onep) = (find_client_onep(nep_pair[0], topo["node"]),
143 find_client_onep(nep_pair[1], topo["node"]))
Andrea Campanellae22a6b12019-02-27 16:10:08 +0100144 print "client side neps", (src_onep, dst_onep)
145
Boyuan Yanf7f50542019-02-25 12:16:09 -0800146 src_sip_uuid, dst_sip_uuid = \
147 (src_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"],
148 dst_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"])
149 print "\nBuild client-side connectivity:\n|Item|SRC|DST|\n|:--|:--|:--|\n|onos-cp|%s|%s|\n|connection id|%s|%s|\n|sip uuid|%s|%s|" % \
150 (src_onep["name"][2]["value"], dst_onep["name"][2]["value"],
151 src_onep["name"][1]["value"], dst_onep["name"][1]["value"],
152 src_sip_uuid, dst_sip_uuid)
153 create_input_json = json.dumps(tapi_client_input((src_sip_uuid, dst_sip_uuid)))
154 print "\nThe json content of creation operation for client-side connectivity service is \n\t\t%s." % \
155 create_input_json
156 headers = {'Content-type': 'application/json'}
157 resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks'))
158 if resp.status_code != 200:
159 raise Exception('POST {}'.format(resp.status_code))
160 return resp
161
Andrea Campanellae22a6b12019-02-27 16:10:08 +0100162#
163# Parse array structure "name" under structure "owned node edge point"
164#
165def parse_value(arr):
166 rtn = {}
167 for item in arr:
168 rtn[item["value-name"]] = item["value"]
169 return rtn
Boyuan Yanf7f50542019-02-25 12:16:09 -0800170
171#
172# Find node edge point of node structure in topology with client-side port, by using nep with line-side port.
173# The odtn-connection-id should be the same in both line-side nep and client-side nep
174#
175def find_client_onep(line_nep_in_link, nodes):
176 for node in nodes:
177 if node["uuid"] == line_nep_in_link["node-uuid"]:
178 conn_id = None
179 for onep in node["owned-node-edge-point"]:
180 if onep["uuid"] == line_nep_in_link["node-edge-point-uuid"]:
Andrea Campanellae22a6b12019-02-27 16:10:08 +0100181 name = parse_value(onep["name"])
182 if name["odtn-port-type"] == "line":
183 conn_id = name["odtn-connection-id"]
184 break
Boyuan Yanf7f50542019-02-25 12:16:09 -0800185 if conn_id is None:
186 raise AssertionError("Cannot find owned node edge point with node id %s and nep id %s."
187 % (line_nep_in_link["node-uuid"], line_nep_in_link["node-edge-point-uuid"], ))
188 for onep in node["owned-node-edge-point"]:
Andrea Campanellae22a6b12019-02-27 16:10:08 +0100189 name = parse_value(onep["name"])
190 if name["odtn-port-type"] == "client" and name["odtn-connection-id"] == conn_id:
Boyuan Yanf7f50542019-02-25 12:16:09 -0800191 return onep
Andrea Campanellae22a6b12019-02-27 16:10:08 +0100192
Boyuan Yanf7f50542019-02-25 12:16:09 -0800193 return None
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800194
195
196#
197# Create a line-side connection. Firstly, get the context, parsing for SIPs with photonic_media type,
198# and select one pair of them; Secondly, issue the request
199#
200def create_line_connection(url_context, url_connectivity):
201 context = get_context(url_context)
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800202 # select the first topo from all topologies
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800203 topo = context["tapi-common:context"]["tapi-topology:topology-context"]["topology"][0]
204
205 # select the first link from all links of topo
206 nep_pair = topo["link"][0]["node-edge-point"]
207 assert topo["uuid"] == nep_pair[0]["topology-uuid"]
208 assert topo["uuid"] == nep_pair[1]["topology-uuid"]
Boyuan Yanf7f50542019-02-25 12:16:09 -0800209 src_onep, dst_onep = (find_line_onep(nep_pair[0], topo["node"]),
210 find_line_onep(nep_pair[1], topo["node"]))
211 src_sip_uuid, dst_sip_uuid = \
212 (src_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"],
213 dst_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"])
214 print "\nBuild line-side connectivity:\n|Item|SRC|DST|\n|:--|:--|:--|\n|onos-cp|%s|%s|\n|connection id|%s|%s|\n|sip uuid|%s|%s|" % \
215 (src_onep["name"][2]["value"], dst_onep["name"][2]["value"],
216 src_onep["name"][1]["value"], dst_onep["name"][1]["value"],
217 src_sip_uuid, dst_sip_uuid)
218 create_input_json = json.dumps(tapi_line_input((src_sip_uuid, dst_sip_uuid)))
219 print "\nThe json content of creation operation for line-side connectivity service is \n\t\t%s." % \
220 create_input_json
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800221 headers = {'Content-type': 'application/json'}
222 resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks'))
223 if resp.status_code != 200:
224 raise Exception('POST {}'.format(resp.status_code))
225 return resp
226
227
Boyuan Yanf7f50542019-02-25 12:16:09 -0800228def find_line_onep(line_nep_in_link, nodes):
229 for node in nodes:
230 if node["uuid"] == line_nep_in_link["node-uuid"]:
231 for onep in node["owned-node-edge-point"]:
232 if onep["uuid"] == line_nep_in_link["node-edge-point-uuid"]:
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800233 # check the length equals 1 to verify the 1-to-1 mapping relationship
234 assert len(onep["mapped-service-interface-point"]) == 1
Boyuan Yanf7f50542019-02-25 12:16:09 -0800235 return onep
Boyuan Yan2313d7d2019-02-24 16:28:01 -0800236 return None
237
238
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100239#
240# Obtains existing connectivity services
241#
242def get_connection(url_connectivity, uuid):
Boyuan Yan863db912019-02-15 12:24:43 -0800243 # uuid is useless for this method
244 json = '{}'
Andrea Campanella50c0c4c2018-12-21 12:18:21 +0100245 headers = {'Content-type': 'application/json'}
246 resp = requests.post(url_connectivity, data=json, headers=headers, auth=('onos', 'rocks'))
247 if resp.status_code != 200:
248 raise Exception('POST {}'.format(resp.status_code))
249 return resp