Boyuan Yan | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 2 | |
| 3 | import requests |
| 4 | import json |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 5 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 6 | |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 7 | # |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 8 | # Creates client-side connectivity json |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 9 | # |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 10 | def tapi_client_input(sip_uuids): |
Andrea Campanella | 6d77423 | 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 | 4103678 | 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 | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 67 | |
Andrea Campanella | 6d77423 | 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 | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 74 | raise Exception('GET {}'.format(resp.status_code)) |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 75 | return resp.json() |
| 76 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 77 | |
Andrea Campanella | 6d77423 | 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 | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 94 | create_input_json = json.dumps(tapi_client_input(sip_uuids)) |
Andrea Campanella | 6d77423 | 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 | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 102 | |
Andrea Campanella | 6d77423 | 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 | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 107 | return sip["layer-protocol-name"] == "PHOTONIC_MEDIA" |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 108 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 109 | |
Andrea Campanella | 6d77423 | 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 | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 114 | return sip["layer-protocol-name"] == "DSR" |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 115 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 116 | |
Andrea Campanella | 6d77423 | 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 | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 128 | |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 129 | # |
Boyuan Yan | 4103678 | 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 | 6b5d4fd | 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"])) |
| 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 | # |
| 165 | def 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 Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 182 | |
| 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 | # |
| 188 | def create_line_connection(url_context, url_connectivity): |
| 189 | context = get_context(url_context) |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 190 | # select the first topo from all topologies |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 191 | 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 Yan | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 197 | 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 Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 209 | 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 Yan | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 216 | def 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 Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 221 | # check the length equals 1 to verify the 1-to-1 mapping relationship |
| 222 | assert len(onep["mapped-service-interface-point"]) == 1 |
Boyuan Yan | 6b5d4fd | 2019-02-25 12:16:09 -0800 | [diff] [blame^] | 223 | return onep |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 224 | return None |
| 225 | |
| 226 | |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 227 | # |
| 228 | # Obtains existing connectivity services |
| 229 | # |
| 230 | def get_connection(url_connectivity, uuid): |
Boyuan Yan | 528fdba | 2019-02-15 12:24:43 -0800 | [diff] [blame] | 231 | # uuid is useless for this method |
| 232 | json = '{}' |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 233 | 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 |