Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 2 | |
| 3 | import requests |
| 4 | import json |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 5 | |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 6 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 7 | # |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 8 | # Creates client-side connectivity json |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 9 | # |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 10 | def tapi_client_input(sip_uuids): |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 11 | 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 32 | |
| 33 | # |
| 34 | # Creates line-side connectivity json |
| 35 | # |
| 36 | def 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 Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 67 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 68 | # |
| 69 | # Obtains TAPI context through restconf |
| 70 | # |
| 71 | def get_context(url_context): |
| 72 | resp = requests.get(url_context, auth=('onos', 'rocks')) |
| 73 | if resp.status_code != 200: |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 74 | raise Exception('GET {}'.format(resp.status_code)) |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 75 | return resp.json() |
| 76 | |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 77 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 78 | # |
| 79 | # Requests a connectivity service |
| 80 | # |
| 81 | def 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 94 | create_input_json = json.dumps(tapi_client_input(sip_uuids)) |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 95 | 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 102 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 103 | # |
| 104 | # Filter method used to keep only SIPs that are photonic_media |
| 105 | # |
| 106 | def is_photonic_media(sip): |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 107 | return sip["layer-protocol-name"] == "PHOTONIC_MEDIA" |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 108 | |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 109 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 110 | # |
| 111 | # Filter method used to keep only SIPs that are DSR |
| 112 | # |
| 113 | def is_dsr_media(sip): |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 114 | return sip["layer-protocol-name"] == "DSR" |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 115 | |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 116 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 117 | # |
| 118 | # Processes the topology to verify the correctness |
| 119 | # |
| 120 | def 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 128 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 129 | # |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 130 | # 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 | # |
| 133 | def create_client_connection(url_context, url_connectivity): |
| 134 | context = get_context(url_context) |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 135 | # 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 Campanella | e22a6b1 | 2019-02-27 16:10:08 +0100 | [diff] [blame] | 144 | print "client side neps", (src_onep, dst_onep) |
| 145 | |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 146 | 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 Campanella | e22a6b1 | 2019-02-27 16:10:08 +0100 | [diff] [blame] | 162 | # |
| 163 | # Parse array structure "name" under structure "owned node edge point" |
| 164 | # |
| 165 | def parse_value(arr): |
| 166 | rtn = {} |
| 167 | for item in arr: |
| 168 | rtn[item["value-name"]] = item["value"] |
| 169 | return rtn |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 170 | |
| 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 | # |
| 175 | def 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 Campanella | e22a6b1 | 2019-02-27 16:10:08 +0100 | [diff] [blame] | 181 | name = parse_value(onep["name"]) |
| 182 | if name["odtn-port-type"] == "line": |
| 183 | conn_id = name["odtn-connection-id"] |
| 184 | break |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 185 | 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 Campanella | e22a6b1 | 2019-02-27 16:10:08 +0100 | [diff] [blame] | 189 | name = parse_value(onep["name"]) |
| 190 | if name["odtn-port-type"] == "client" and name["odtn-connection-id"] == conn_id: |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 191 | return onep |
Andrea Campanella | e22a6b1 | 2019-02-27 16:10:08 +0100 | [diff] [blame] | 192 | |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 193 | return None |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 194 | |
| 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 | # |
| 200 | def create_line_connection(url_context, url_connectivity): |
| 201 | context = get_context(url_context) |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 202 | # select the first topo from all topologies |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 203 | 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 Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 209 | 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 221 | 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 Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 228 | def 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 Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 233 | # check the length equals 1 to verify the 1-to-1 mapping relationship |
| 234 | assert len(onep["mapped-service-interface-point"]) == 1 |
Boyuan Yan | f7f5054 | 2019-02-25 12:16:09 -0800 | [diff] [blame] | 235 | return onep |
Boyuan Yan | 2313d7d | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 236 | return None |
| 237 | |
| 238 | |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 239 | # |
| 240 | # Obtains existing connectivity services |
| 241 | # |
| 242 | def get_connection(url_connectivity, uuid): |
Boyuan Yan | 863db91 | 2019-02-15 12:24:43 -0800 | [diff] [blame] | 243 | # uuid is useless for this method |
| 244 | json = '{}' |
Andrea Campanella | 50c0c4c | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 245 | 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 |