Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import os |
| 18 | import requests |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | from synchronizers.vtn.vtnnetport import VTNNetwork, VTNPort |
| 23 | from synchronizers.new_base.syncstep import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import * |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 25 | from requests.auth import HTTPBasicAuth |
| 26 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 27 | from xosconfig import Config |
| 28 | from multistructlog import create_logger |
| 29 | |
| 30 | log = create_logger(Config().get('logging')) |
| 31 | |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 32 | |
| 33 | # XXX should save and load this |
| 34 | glo_saved_networks = {} |
| 35 | glo_saved_ports = {} |
| 36 | |
| 37 | class SyncVTNService(SyncStep): |
| 38 | provides=[Service] |
| 39 | observes=Service |
| 40 | requested_interval=0 |
| 41 | |
| 42 | def __init__(self, **args): |
| 43 | SyncStep.__init__(self, **args) |
| 44 | |
| 45 | def get_vtn_onos_app(self, vtn_service): |
| 46 | links = vtn_service.subscribed_links.all() |
| 47 | for link in links: |
| 48 | # We're looking for an ONOS App. It's the only ServiceInstance that VTN can be implemented on. |
| 49 | if link.provider_service_instance.leaf_model_name != "ONOSApp": |
| 50 | continue |
| 51 | |
| 52 | # TODO: Rather than checking model name, check for the right interface |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 53 | # NOTE: Deferred until service interfaces are revisited |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 54 | |
| 55 | #if not link.provider_service_interface: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 56 | # log.warning("Link %s does not have a provider_service_interface. Skipping" % link) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 57 | # continue |
| 58 | # |
| 59 | #if link.provider_service_interface.interface_type.name != "onos_app_interface": |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 60 | # log.warning("Link %s provider_service_interface type is not equal to onos_app_interface" % link) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 61 | # continue |
| 62 | |
| 63 | # cast from ServiceInstance to ONOSApp |
| 64 | app = link.provider_service_instance.leaf_model |
| 65 | return app |
| 66 | |
| 67 | raise Exception("No ServiceInstanceLink from VTN Service to VTN ONOS App") |
| 68 | |
| 69 | def get_vtn_endpoint(self, vtn_service): |
| 70 | """ Get connection info for the ONOS that is hosting the VTN ONOS App. |
| 71 | |
| 72 | returns (hostname, port, auth) |
| 73 | """ |
| 74 | app = self.get_vtn_onos_app(vtn_service) |
| 75 | # cast from Service to ONOSService |
| 76 | onos = app.owner.leaf_model |
| 77 | if not (onos.rest_hostname): |
| 78 | raise Exception("onos.rest_hostname is not set") |
| 79 | if not (onos.rest_port): |
| 80 | raise Exception("onos.rest_port is not set") |
| 81 | if not (onos.rest_password): |
| 82 | raise Exception("onos.rest_password is not set") |
| 83 | if not (onos.rest_username): |
| 84 | raise Exception("onos.rest_username is not set") |
| 85 | auth = HTTPBasicAuth(onos.rest_username, onos.rest_password) |
| 86 | return (onos.rest_hostname, onos.rest_port, auth) |
| 87 | |
| 88 | def get_method(self, auth, url, id): |
| 89 | url_with_id = "%s/%s" % (url, id) |
| 90 | r = requests.get(url_with_id, auth=auth) |
| 91 | if (r.status_code==200): |
| 92 | method="PUT" |
| 93 | url = url_with_id |
| 94 | req_func = requests.put |
| 95 | exists=True |
| 96 | else: |
| 97 | method="POST" |
| 98 | req_func = requests.post |
| 99 | exists=False |
| 100 | return (exists, url, method, req_func) |
| 101 | |
| 102 | def sync_service_networks(self, vtn_service): |
| 103 | (onos_hostname, onos_port, onos_auth) = self.get_vtn_endpoint(vtn_service) |
| 104 | |
| 105 | valid_ids = [] |
| 106 | for network in Network.objects.all(): |
| 107 | network = VTNNetwork(network) |
| 108 | |
| 109 | if not network.id: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 110 | log.info("Skipping network because it has no id") |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 111 | continue |
| 112 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 113 | if (not network.subnet): |
| 114 | log.info("Skipping network %s because it has no subnet" % network.id) |
| 115 | continue |
| 116 | |
| 117 | if (not network.segmentation_id): |
| 118 | log.info("Skipping network %s because it has no segmentation id" % network.id) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 119 | continue |
| 120 | |
| 121 | valid_ids.append(network.id) |
| 122 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 123 | # clean the providerNetworks list to what the VTN App expects |
| 124 | providerNetworks = [{"id": x["id"], "bidirectional": x["bidirectional"]} for x in network.providerNetworks] |
| 125 | |
| 126 | # This is the data we will be sending ONOS |
| 127 | data = {"ServiceNetwork": {"id": network.id, |
| 128 | "name": network.name, |
| 129 | "type": network.type, |
| 130 | "segment_id": network.segmentation_id, |
| 131 | "subnet": network.subnet, |
| 132 | "service_ip": network.gateway, |
| 133 | "providerNetworks": providerNetworks}} |
| 134 | |
| 135 | if (glo_saved_networks.get(network.id, None) != data): |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 136 | (exists, url, method, req_func) = self.get_method(onos_auth, "http://%s:%d/onos/cordvtn/serviceNetworks" % (onos_hostname, onos_port), network.id) |
| 137 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 138 | log.info("%sing VTN API for network %s" % (method, network.id), url=url, data=data) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 139 | |
| 140 | r=req_func(url, json=data, auth=onos_auth ) |
| 141 | if (r.status_code in [200,201]): |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 142 | glo_saved_networks[network.id] = data |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 143 | else: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 144 | log.error("Received error from vtn service (%d)" % r.status_code) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 145 | |
| 146 | |
| 147 | for network_id in glo_saved_networks.keys(): |
| 148 | if network_id not in valid_ids: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 149 | log.info("DELETEing VTN API for network %s" % network_id) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 150 | |
| 151 | url = "http://%s:%d/onos/cordvtn/serviceNetworks/%s" % (onos_hostname, onos_port, network_id) |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 152 | log.info("URL: %s" % url) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 153 | |
| 154 | r = requests.delete(url, auth=onos_auth ) |
| 155 | if (r.status_code in [200,204]): |
| 156 | del glo_saved_networks[network_id] |
| 157 | else: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 158 | log.error("Received error from vtn service (%d)" % r.status_code) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 159 | |
| 160 | def sync_service_ports(self, vtn_service): |
| 161 | (onos_hostname, onos_port, onos_auth) = self.get_vtn_endpoint(vtn_service) |
| 162 | |
| 163 | valid_ids = [] |
| 164 | for port in Port.objects.all(): |
| 165 | port = VTNPort(port) |
| 166 | |
| 167 | if not port.id: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 168 | log.info("Skipping port because it has no id") |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 169 | continue |
| 170 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 171 | if (not port.network_id): |
| 172 | log.info("Skipping port %s because it is missing network_id" % port.id) |
| 173 | continue |
| 174 | |
| 175 | if (not port.mac_address): |
| 176 | log.info("Skipping port %s because it is missing mac_address" % port.id) |
| 177 | continue |
| 178 | |
| 179 | if (not port.ip_address): |
| 180 | log.info("Skipping port %s because it is missing ip_address" % port.id) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 181 | continue |
| 182 | |
| 183 | valid_ids.append(port.id) |
| 184 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 185 | # This is the data we will be sending ONOS |
| 186 | data = {"ServicePort": {"id": port.id, |
| 187 | "name": port.name, |
| 188 | "network_id": port.network_id, |
| 189 | "mac_address": port.mac_address, |
| 190 | "ip_address": port.ip_address, |
| 191 | "floating_address_pairs": port.floating_address_pairs}} |
| 192 | if port.vlan_id: |
| 193 | data["ServicePort"]["vlan_id"] = port.vlan_id |
| 194 | |
| 195 | if (glo_saved_ports.get(port.id, None) != data): |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 196 | (exists, url, method, req_func) = self.get_method(onos_auth, "http://%s:%d/onos/cordvtn/servicePorts" % (onos_hostname, onos_port), port.id) |
| 197 | |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 198 | log.info("%sing VTN API for port %s" % (method, port.id), url=url, data=data) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 199 | |
| 200 | r=req_func(url, json=data, auth=onos_auth ) |
| 201 | if (r.status_code in [200,201]): |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 202 | glo_saved_ports[port.id] = data |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 203 | else: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 204 | log.error("Received error from vtn service (%d)" % r.status_code) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 205 | |
| 206 | for port_id in glo_saved_ports.keys(): |
| 207 | if port_id not in valid_ids: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 208 | log.info("DELETEing VTN API for port %s" % port_id) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 209 | |
| 210 | url = "http://%s:%d/onos/cordvtn/servicePorts/%s" % (onos_hostname, onos_port, port_id) |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 211 | log.info("URL: %s" % url) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 212 | |
| 213 | r = requests.delete(url, auth=onos_auth ) |
| 214 | if (r.status_code in [200,204]): |
| 215 | del glo_saved_ports[port_id] |
| 216 | else: |
Scott Baker | f6cfb38 | 2018-04-03 09:48:01 -0700 | [diff] [blame^] | 217 | log.error("Received error from vtn service (%d)" % r.status_code) |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 218 | |
| 219 | def call(self, **args): |
| 220 | global glo_saved_networks |
| 221 | global glo_saved_ports |
| 222 | |
| 223 | vtn_service = VTNService.objects.all() |
| 224 | if not vtn_service: |
| 225 | raise Exception("No VTN Service") |
| 226 | |
| 227 | vtn_service = vtn_service[0] |
| 228 | |
| 229 | # TODO: We should check get_vtn_onos_app() and make sure that it has been synced, and that any necessary |
| 230 | # attributes (netcfg, etc) is filled out. |
| 231 | |
| 232 | if (vtn_service.resync): |
| 233 | # If the VTN app requested a full resync, clear our saved network |
| 234 | # so we will resync everything, then reset the 'resync' flag |
| 235 | glo_saved_networks = {} |
| 236 | glo_saved_ports = {} |
| 237 | |
| 238 | vtn_service.resync = False |
| 239 | vtn_service.save() |
| 240 | |
| 241 | if vtn_service.vtnAPIVersion>=2: |
| 242 | # version 2 means use new API |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 243 | self.sync_service_networks(vtn_service) |
| 244 | self.sync_service_ports(vtn_service) |
| 245 | else: |
| 246 | raise Exception("VTN API Version 1 is no longer supported by VTN Synchronizer") |
| 247 | |
| 248 | |