Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 1 | import os |
| 2 | import requests |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | from django.db.models import F, Q |
| 7 | from xos.config import Config |
| 8 | from synchronizers.base.syncstep import SyncStep |
| 9 | from core.models import Service, Network, Port |
| 10 | from core.models.service import COARSE_KIND |
| 11 | from services.vtn.vtnnetport import VTNNetwork, VTNPort |
| 12 | from services.vtn.models import VTNService |
| 13 | from xos.logger import Logger, logging |
| 14 | from requests.auth import HTTPBasicAuth |
| 15 | |
| 16 | # hpclibrary will be in steps/.. |
| 17 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 18 | sys.path.insert(0,parentdir) |
| 19 | |
| 20 | logger = Logger(level=logging.INFO) |
| 21 | |
| 22 | # XXX should save and load this |
| 23 | glo_saved_vtn_maps = [] |
| 24 | glo_saved_networks = {} |
| 25 | glo_saved_ports = {} |
| 26 | |
| 27 | class SyncVTNService(SyncStep): |
| 28 | provides=[Service] |
| 29 | observes=Service |
| 30 | requested_interval=0 |
| 31 | |
| 32 | def __init__(self, **args): |
| 33 | SyncStep.__init__(self, **args) |
| 34 | |
| 35 | def get_vtn_onos_service(self): |
| 36 | from services.onos.models import ONOSService |
| 37 | |
| 38 | vtn_service = ONOSService.get_service_objects().filter(name="ONOS_CORD") # XXX fixme - harcoded |
| 39 | if not vtn_service: |
| 40 | raise "No VTN Onos Service" |
| 41 | |
| 42 | return vtn_service[0] |
| 43 | |
| 44 | def get_vtn_auth(self): |
| 45 | return HTTPBasicAuth('karaf', 'karaf') # XXX fixme - hardcoded auth |
| 46 | |
| 47 | def get_vtn_addr(self): |
| 48 | vtn_service = self.get_vtn_onos_service() |
| 49 | |
| 50 | if vtn_service.rest_hostname: |
| 51 | return vtn_service.rest_hostname |
| 52 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 53 | # code below this point is for ONOS running in a slice, and is |
| 54 | # probably outdated |
| 55 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 56 | if not vtn_service.slices.exists(): |
| 57 | raise "VTN Service has no slices" |
| 58 | |
| 59 | vtn_slice = vtn_service.slices.all()[0] |
| 60 | |
| 61 | if not vtn_slice.instances.exists(): |
| 62 | raise "VTN Slice has no instances" |
| 63 | |
| 64 | vtn_instance = vtn_slice.instances.all()[0] |
| 65 | |
| 66 | return vtn_instance.node.name |
| 67 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 68 | def get_vtn_port(self): |
| 69 | vtn_service = self.get_vtn_onos_service() |
| 70 | |
| 71 | if vtn_service.rest_port: |
| 72 | return vtn_service.rest_port |
| 73 | |
| 74 | # code below this point is for ONOS running in a slice, and is |
| 75 | # probably outdated |
| 76 | |
| 77 | raise Exception("Must set rest_port") |
| 78 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 79 | def sync_legacy_vtn_api(self): |
| 80 | global glo_saved_vtn_maps |
| 81 | |
| 82 | logger.info("sync'ing vtn services") |
| 83 | |
| 84 | vtn_maps = [] |
| 85 | for service in Service.objects.all(): |
| 86 | for id in service.get_vtn_src_ids(): |
| 87 | dependencies = service.get_vtn_dependencies_ids() |
| 88 | if dependencies: |
| 89 | for dependency in dependencies: |
| 90 | vtn_maps.append( (id, dependency) ) |
| 91 | |
| 92 | for vtn_map in vtn_maps: |
| 93 | if not (vtn_map in glo_saved_vtn_maps): |
| 94 | # call vtn rest api to add map |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 95 | url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0], vtn_map[1]) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 96 | |
| 97 | print "POST %s" % url |
| 98 | r = requests.post(url, auth=self.get_vtn_auth() ) |
| 99 | if (r.status_code != 200): |
| 100 | raise Exception("Received error from vtn service (%d)" % r.status_code) |
| 101 | |
| 102 | for vtn_map in glo_saved_vtn_maps: |
| 103 | if not vtn_map in vtn_maps: |
| 104 | # call vtn rest api to delete map |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 105 | url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0],vtn_map[1]) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 106 | |
| 107 | print "DELETE %s" % url |
| 108 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 109 | if (r.status_code != 200): |
| 110 | raise Exception("Received error from vtn service (%d)" % r.status_code) |
| 111 | |
| 112 | glo_saved_vtn_maps = vtn_maps |
| 113 | # TODO: save this |
| 114 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 115 | def get_method(self, url, id): |
| 116 | url_with_id = "%s/%s" % (url, id) |
| 117 | r = requests.get(url_with_id, auth=self.get_vtn_auth()) |
| 118 | if (r.status_code==200): |
| 119 | method="PUT" |
| 120 | url = url_with_id |
| 121 | req_func = requests.put |
| 122 | exists=True |
| 123 | else: |
| 124 | method="POST" |
| 125 | req_func = requests.post |
| 126 | exists=False |
| 127 | return (exists, url, method, req_func) |
| 128 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 129 | def sync_service_networks(self): |
| 130 | valid_ids = [] |
| 131 | for network in Network.objects.all(): |
| 132 | network = VTNNetwork(network) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 133 | |
| 134 | if not network.id: |
| 135 | continue |
| 136 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 137 | valid_ids.append(network.id) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 138 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 139 | if (glo_saved_networks.get(network.id, None) != network.to_dict()): |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 140 | (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/serviceNetworks", network.id) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 141 | |
Scott Baker | 2906700 | 2016-09-28 17:14:45 -0700 | [diff] [blame] | 142 | if (network.type=="PRIVATE") and (not network.providerNetworks): |
| 143 | logger.info("Skipping network %s because it has no relevant state" % network.id) |
| 144 | continue |
| 145 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 146 | logger.info("%sing VTN API for network %s" % (method, network.id)) |
| 147 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 148 | logger.info("URL: %s" % url) |
| 149 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 150 | # clean the providerNetworks list |
| 151 | providerNetworks = [{"id": x["id"], "bidirectional": x["bidirectional"]} for x in network.providerNetworks] |
| 152 | |
| 153 | data = {"ServiceNetwork": {"id": network.id, |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 154 | "type": network.type, |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 155 | "providerNetworks": providerNetworks} } |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 156 | logger.info("DATA: %s" % str(data)) |
| 157 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 158 | r=req_func(url, json=data, auth=self.get_vtn_auth() ) |
| 159 | if (r.status_code in [200,201]): |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 160 | glo_saved_networks[network.id] = network.to_dict() |
| 161 | else: |
| 162 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 163 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 164 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 165 | for network_id in glo_saved_networks.keys(): |
| 166 | if network_id not in valid_ids: |
| 167 | logger.info("DELETEing VTN API for network %s" % network_id) |
| 168 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 169 | url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/serviceNetworks/%s" % network_id |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 170 | logger.info("URL: %s" % url) |
| 171 | |
| 172 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 173 | if (r.status_code in [200,204]): |
| 174 | del glo_saved_networks[network_id] |
| 175 | else: |
| 176 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 177 | |
| 178 | def sync_service_ports(self): |
| 179 | valid_ids = [] |
| 180 | for port in Port.objects.all(): |
| 181 | port = VTNPort(port) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 182 | |
| 183 | if not port.id: |
| 184 | continue |
| 185 | |
Scott Baker | 2906700 | 2016-09-28 17:14:45 -0700 | [diff] [blame] | 186 | if (not port.vlan_id) and (not port.floating_address_pairs): |
| 187 | logger.info("Skipping port %s because it has no relevant state" % port.id) |
| 188 | continue |
| 189 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 190 | valid_ids.append(port.id) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 191 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 192 | if (glo_saved_ports.get(port.id, None) != port.to_dict()): |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 193 | (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/servicePorts", port.id) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 194 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 195 | logger.info("%sing VTN API for port %s" % (method, port.id)) |
| 196 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 197 | logger.info("URL: %s" % url) |
| 198 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 199 | data = {"ServicePort": {"id": port.id, |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 200 | "vlan_id": port.vlan_id, |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 201 | "floating_address_pairs": port.floating_address_pairs} } |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 202 | logger.info("DATA: %s" % str(data)) |
| 203 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 204 | r=req_func(url, json=data, auth=self.get_vtn_auth() ) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 205 | if (r.status_code in [200,201]): |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 206 | glo_saved_ports[port.id] = port.to_dict() |
| 207 | else: |
| 208 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 209 | |
| 210 | for port_id in glo_saved_ports.keys(): |
| 211 | if port_id not in valid_ids: |
| 212 | logger.info("DELETEing VTN API for port %s" % port_id) |
| 213 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 214 | url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/servicePorts/%s" % port_id |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 215 | logger.info("URL: %s" % url) |
| 216 | |
| 217 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 218 | if (r.status_code in [200,204]): |
| 219 | del glo_saved_ports[port_id] |
| 220 | else: |
| 221 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 222 | |
| 223 | def call(self, **args): |
| 224 | vtn_service = VTNService.get_service_objects().all() |
| 225 | if not vtn_service: |
| 226 | raise Exception("No VTN Service") |
| 227 | |
| 228 | vtn_service = vtn_service[0] |
| 229 | |
Scott Baker | 855a9df | 2016-09-09 09:23:50 -0700 | [diff] [blame] | 230 | if vtn_service.vtnAPIVersion>=2: |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 231 | # version 2 means use new API |
| 232 | logger.info("Using New API") |
| 233 | self.sync_service_networks() |
| 234 | self.sync_service_ports() |
| 235 | else: |
| 236 | # default to legacy |
| 237 | logger.info("Using Old API") |
Scott Baker | cdbb71e | 2016-10-26 15:50:03 -0700 | [diff] [blame] | 238 | raise Exception("VTN API Version 1 is no longer supported by VTN Synchronizer") |
| 239 | #self.sync_legacy_vtn_api() |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 240 | |
| 241 | |