Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import requests |
| 4 | import json |
| 5 | import itertools |
| 6 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 7 | |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 8 | # |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 9 | # Creates client-side connectivity json |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 10 | # |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 11 | def tapi_client_input(sip_uuids): |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 12 | create_input = { |
| 13 | "tapi-connectivity:input": { |
| 14 | "end-point" : [ |
| 15 | { |
| 16 | "local-id": sip_uuids[0], |
| 17 | "service-interface-point": { |
| 18 | "service-interface-point-uuid" : sip_uuids[0] |
| 19 | } |
| 20 | } |
| 21 | , |
| 22 | { |
| 23 | "local-id": sip_uuids[1], |
| 24 | "service-interface-point": { |
| 25 | "service-interface-point-uuid" : sip_uuids[1] |
| 26 | } |
| 27 | } |
| 28 | ] |
| 29 | } |
| 30 | } |
| 31 | return create_input |
| 32 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 33 | |
| 34 | # |
| 35 | # Creates line-side connectivity json |
| 36 | # |
| 37 | def tapi_line_input(sip_uuids): |
| 38 | create_input = { |
| 39 | "tapi-connectivity:input" : { |
| 40 | "end-point" : [ |
| 41 | { |
| 42 | "layer-protocol-qualifier" : "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC", |
| 43 | "role" : "UNKNOWN", |
| 44 | "local-id" : "Src_end_point", |
| 45 | "direction" : "BIDIRECTIONAL", |
| 46 | "service-interface-point" : { |
| 47 | "service-interface-point-uuid" : sip_uuids[0] |
| 48 | }, |
| 49 | "protection-role" : "WORK", |
| 50 | "layer-protocol-name" : "PHOTONIC_MEDIA" |
| 51 | }, |
| 52 | { |
| 53 | "direction" : "BIDIRECTIONAL", |
| 54 | "service-interface-point" : { |
| 55 | "service-interface-point-uuid" : sip_uuids[1] |
| 56 | }, |
| 57 | "protection-role" : "WORK", |
| 58 | "layer-protocol-name" : "PHOTONIC_MEDIA", |
| 59 | "layer-protocol-qualifier" : "tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC", |
| 60 | "role" : "UNKNOWN", |
| 61 | "local-id" : "Dst_end_point" |
| 62 | } |
| 63 | ] |
| 64 | } |
| 65 | } |
| 66 | return create_input |
| 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: |
| 74 | raise Exception('GET {}'.format(resp.status_code)) |
| 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): |
| 107 | return sip["layer-protocol-name"]=="PHOTONIC_MEDIA" |
| 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): |
| 114 | return sip["layer-protocol-name"]=="DSR" |
| 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 | # |
| 130 | # Creates a connection first getting the context, parsing for SIPS and then issuing the request. |
| 131 | # |
| 132 | def create_connection(url_context, url_connectivity): |
| 133 | context = get_context(url_context) |
| 134 | return request_connection(url_connectivity, context) |
| 135 | |
Boyuan Yan | 4103678 | 2019-02-24 16:28:01 -0800 | [diff] [blame] | 136 | |
| 137 | # |
| 138 | # Create a client-side connection. Firstly, get the context, parsing for SIPs that connect |
| 139 | # with each other in line-side; Secondly, issue the request |
| 140 | # |
| 141 | def create_client_connection(url_context, url_connectivity): |
| 142 | context = get_context(url_context) |
| 143 | conn_context = context["tapi-connectivity:connectivity-context"] |
| 144 | return request_connection(url_connectivity, context) |
| 145 | |
| 146 | |
| 147 | # |
| 148 | # Create a line-side connection. Firstly, get the context, parsing for SIPs with photonic_media type, |
| 149 | # and select one pair of them; Secondly, issue the request |
| 150 | # |
| 151 | def create_line_connection(url_context, url_connectivity): |
| 152 | context = get_context(url_context) |
| 153 | print context |
| 154 | # select the first topo from all topologies |
| 155 | sips = context["tapi-common:context"]["service-interface-point"] |
| 156 | topo = context["tapi-common:context"]["tapi-topology:topology-context"]["topology"][0] |
| 157 | |
| 158 | # select the first link from all links of topo |
| 159 | nep_pair = topo["link"][0]["node-edge-point"] |
| 160 | assert topo["uuid"] == nep_pair[0]["topology-uuid"] |
| 161 | assert topo["uuid"] == nep_pair[1]["topology-uuid"] |
| 162 | sip_uuids = extract_photonic_sips(nep_pair, topo, sips) |
| 163 | create_input_json = json.dumps(tapi_line_input(sip_uuids)) |
| 164 | print create_input_json |
| 165 | headers = {'Content-type': 'application/json'} |
| 166 | resp = requests.post(url_connectivity, data=create_input_json, headers=headers, auth=('onos', 'rocks')) |
| 167 | if resp.status_code != 200: |
| 168 | raise Exception('POST {}'.format(resp.status_code)) |
| 169 | return resp |
| 170 | |
| 171 | |
| 172 | def extract_photonic_sips(neps, topo, sips): |
| 173 | # parse mapped node and edge point from nep |
| 174 | src_sip_uuid = extract_photonic_sip_uuid(neps[0], topo) |
| 175 | dst_sip_uuid = extract_photonic_sip_uuid(neps[1], topo) |
| 176 | src_sip = extract_photonic_sip(src_sip_uuid, sips) |
| 177 | dst_sip = extract_photonic_sip(dst_sip_uuid, sips) |
| 178 | print "Connection to be built between %s and %s, whose sip_uuid are %s and %s respectively." % \ |
| 179 | (src_sip["name"][0]["value"], dst_sip["name"][0]["value"], src_sip_uuid, dst_sip_uuid) |
| 180 | return src_sip_uuid, dst_sip_uuid |
| 181 | |
| 182 | |
| 183 | def extract_photonic_sip(sip_uuid, sips): |
| 184 | for sip in sips: |
| 185 | if sip["uuid"] == sip_uuid and sip["layer-protocol-name"] == "PHOTONIC_MEDIA": |
| 186 | return sip |
| 187 | return None |
| 188 | |
| 189 | |
| 190 | def extract_photonic_sip_uuid(nep, topo): |
| 191 | |
| 192 | for node in topo["node"]: |
| 193 | if node["uuid"] == nep["node-uuid"]: |
| 194 | oneps = node["owned-node-edge-point"] |
| 195 | for onep in oneps: |
| 196 | if onep["uuid"] == nep["node-edge-point-uuid"]: |
| 197 | # check the length equals 1 to verify the 1-to-1 mapping relationship |
| 198 | assert len(onep["mapped-service-interface-point"]) == 1 |
| 199 | sip_uuid = onep["mapped-service-interface-point"][0]["service-interface-point-uuid"] |
| 200 | return sip_uuid |
| 201 | return None |
| 202 | |
| 203 | |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 204 | # |
| 205 | # Obtains existing connectivity services |
| 206 | # |
| 207 | def get_connection(url_connectivity, uuid): |
Boyuan Yan | 528fdba | 2019-02-15 12:24:43 -0800 | [diff] [blame] | 208 | # uuid is useless for this method |
| 209 | json = '{}' |
Andrea Campanella | 6d77423 | 2018-12-21 12:18:21 +0100 | [diff] [blame] | 210 | headers = {'Content-type': 'application/json'} |
| 211 | resp = requests.post(url_connectivity, data=json, headers=headers, auth=('onos', 'rocks')) |
| 212 | if resp.status_code != 200: |
| 213 | raise Exception('POST {}'.format(resp.status_code)) |
| 214 | return resp |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |