blob: 9fe3fcc9de623155c785e231da3d78c8a0035db9 [file] [log] [blame]
Boyuan Yan6b5d4fd2019-02-25 12:16:09 -08001#!/usr/bin/python
Andrea Campanella6d774232018-12-21 12:18:21 +01002
3import requests
4import json
Andrea Campanella6d774232018-12-21 12:18:21 +01005
Boyuan Yan41036782019-02-24 16:28:01 -08006
Andrea Campanella6d774232018-12-21 12:18:21 +01007#
Boyuan Yan41036782019-02-24 16:28:01 -08008# Creates client-side connectivity json
Andrea Campanella6d774232018-12-21 12:18:21 +01009#
Boyuan Yan41036782019-02-24 16:28:01 -080010def tapi_client_input(sip_uuids):
Andrea Campanella6d774232018-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 Yan41036782019-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 Yan6b5d4fd2019-02-25 12:16:09 -080067
Andrea Campanella6d774232018-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 Yan6b5d4fd2019-02-25 12:16:09 -080074 raise Exception('GET {}'.format(resp.status_code))
Andrea Campanella6d774232018-12-21 12:18:21 +010075 return resp.json()
76
Boyuan Yan41036782019-02-24 16:28:01 -080077
Andrea Campanella6d774232018-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 Yan41036782019-02-24 16:28:01 -080094 create_input_json = json.dumps(tapi_client_input(sip_uuids))
Andrea Campanella6d774232018-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 Yan41036782019-02-24 16:28:01 -0800102
Andrea Campanella6d774232018-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 Yan6b5d4fd2019-02-25 12:16:09 -0800107 return sip["layer-protocol-name"] == "PHOTONIC_MEDIA"
Andrea Campanella6d774232018-12-21 12:18:21 +0100108
Boyuan Yan41036782019-02-24 16:28:01 -0800109
Andrea Campanella6d774232018-12-21 12:18:21 +0100110#
111# Filter method used to keep only SIPs that are DSR
112#
113def is_dsr_media(sip):
Boyuan Yan6b5d4fd2019-02-25 12:16:09 -0800114 return sip["layer-protocol-name"] == "DSR"
Andrea Campanella6d774232018-12-21 12:18:21 +0100115
Boyuan Yan41036782019-02-24 16:28:01 -0800116
Andrea Campanella6d774232018-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 Yan41036782019-02-24 16:28:01 -0800128
Andrea Campanella6d774232018-12-21 12:18:21 +0100129#
Boyuan Yan41036782019-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 Yan6b5d4fd2019-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"]))
144 src_sip_uuid, dst_sip_uuid = \
145 (src_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"],
146 dst_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"])
147 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|" % \
148 (src_onep["name"][2]["value"], dst_onep["name"][2]["value"],
149 src_onep["name"][1]["value"], dst_onep["name"][1]["value"],
150 src_sip_uuid, dst_sip_uuid)
151 create_input_json = json.dumps(tapi_client_input((src_sip_uuid, dst_sip_uuid)))
152 print "\nThe json content of creation operation for client-side connectivity service is \n\t\t%s." % \
153 create_input_json
154 headers = {'Content-type': 'application/json'}
155 resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks'))
156 if resp.status_code != 200:
157 raise Exception('POST {}'.format(resp.status_code))
158 return resp
159
160
161#
162# Find node edge point of node structure in topology with client-side port, by using nep with line-side port.
163# The odtn-connection-id should be the same in both line-side nep and client-side nep
164#
165def find_client_onep(line_nep_in_link, nodes):
166 for node in nodes:
167 if node["uuid"] == line_nep_in_link["node-uuid"]:
168 conn_id = None
169 for onep in node["owned-node-edge-point"]:
170 if onep["uuid"] == line_nep_in_link["node-edge-point-uuid"]:
171 assert onep["name"][1]["value-name"] == "odtn-connection-id"
172 assert onep["name"][0]["value"] == "line"
173 conn_id = onep["name"][1]["value"]
174 break
175 if conn_id is None:
176 raise AssertionError("Cannot find owned node edge point with node id %s and nep id %s."
177 % (line_nep_in_link["node-uuid"], line_nep_in_link["node-edge-point-uuid"], ))
178 for onep in node["owned-node-edge-point"]:
179 if onep["name"][1]["value"] == conn_id and onep["name"][0]["value"] == "client":
180 return onep
181 return None
Boyuan Yan41036782019-02-24 16:28:01 -0800182
183
184#
185# Create a line-side connection. Firstly, get the context, parsing for SIPs with photonic_media type,
186# and select one pair of them; Secondly, issue the request
187#
188def create_line_connection(url_context, url_connectivity):
189 context = get_context(url_context)
Boyuan Yan41036782019-02-24 16:28:01 -0800190 # select the first topo from all topologies
Boyuan Yan41036782019-02-24 16:28:01 -0800191 topo = context["tapi-common:context"]["tapi-topology:topology-context"]["topology"][0]
192
193 # select the first link from all links of topo
194 nep_pair = topo["link"][0]["node-edge-point"]
195 assert topo["uuid"] == nep_pair[0]["topology-uuid"]
196 assert topo["uuid"] == nep_pair[1]["topology-uuid"]
Boyuan Yan6b5d4fd2019-02-25 12:16:09 -0800197 src_onep, dst_onep = (find_line_onep(nep_pair[0], topo["node"]),
198 find_line_onep(nep_pair[1], topo["node"]))
199 src_sip_uuid, dst_sip_uuid = \
200 (src_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"],
201 dst_onep["mapped-service-interface-point"][0]["service-interface-point-uuid"])
202 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|" % \
203 (src_onep["name"][2]["value"], dst_onep["name"][2]["value"],
204 src_onep["name"][1]["value"], dst_onep["name"][1]["value"],
205 src_sip_uuid, dst_sip_uuid)
206 create_input_json = json.dumps(tapi_line_input((src_sip_uuid, dst_sip_uuid)))
207 print "\nThe json content of creation operation for line-side connectivity service is \n\t\t%s." % \
208 create_input_json
Boyuan Yan41036782019-02-24 16:28:01 -0800209 headers = {'Content-type': 'application/json'}
210 resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks'))
211 if resp.status_code != 200:
212 raise Exception('POST {}'.format(resp.status_code))
213 return resp
214
215
Boyuan Yan6b5d4fd2019-02-25 12:16:09 -0800216def find_line_onep(line_nep_in_link, nodes):
217 for node in nodes:
218 if node["uuid"] == line_nep_in_link["node-uuid"]:
219 for onep in node["owned-node-edge-point"]:
220 if onep["uuid"] == line_nep_in_link["node-edge-point-uuid"]:
Boyuan Yan41036782019-02-24 16:28:01 -0800221 # check the length equals 1 to verify the 1-to-1 mapping relationship
222 assert len(onep["mapped-service-interface-point"]) == 1
Boyuan Yan6b5d4fd2019-02-25 12:16:09 -0800223 return onep
Boyuan Yan41036782019-02-24 16:28:01 -0800224 return None
225
226
Andrea Campanella6d774232018-12-21 12:18:21 +0100227#
228# Obtains existing connectivity services
229#
230def get_connection(url_connectivity, uuid):
Boyuan Yan528fdba2019-02-15 12:24:43 -0800231 # uuid is useless for this method
232 json = '{}'
Andrea Campanella6d774232018-12-21 12:18:21 +0100233 headers = {'Content-type': 'application/json'}
234 resp = requests.post(url_connectivity, data=json, headers=headers, auth=('onos', 'rocks'))
235 if resp.status_code != 200:
236 raise Exception('POST {}'.format(resp.status_code))
237 return resp