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 | |
| 53 | if not vtn_service.slices.exists(): |
| 54 | raise "VTN Service has no slices" |
| 55 | |
| 56 | vtn_slice = vtn_service.slices.all()[0] |
| 57 | |
| 58 | if not vtn_slice.instances.exists(): |
| 59 | raise "VTN Slice has no instances" |
| 60 | |
| 61 | vtn_instance = vtn_slice.instances.all()[0] |
| 62 | |
| 63 | return vtn_instance.node.name |
| 64 | |
| 65 | def sync_legacy_vtn_api(self): |
| 66 | global glo_saved_vtn_maps |
| 67 | |
| 68 | logger.info("sync'ing vtn services") |
| 69 | |
| 70 | vtn_maps = [] |
| 71 | for service in Service.objects.all(): |
| 72 | for id in service.get_vtn_src_ids(): |
| 73 | dependencies = service.get_vtn_dependencies_ids() |
| 74 | if dependencies: |
| 75 | for dependency in dependencies: |
| 76 | vtn_maps.append( (id, dependency) ) |
| 77 | |
| 78 | for vtn_map in vtn_maps: |
| 79 | if not (vtn_map in glo_saved_vtn_maps): |
| 80 | # call vtn rest api to add map |
| 81 | url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0], vtn_map[1]) |
| 82 | |
| 83 | print "POST %s" % url |
| 84 | r = requests.post(url, auth=self.get_vtn_auth() ) |
| 85 | if (r.status_code != 200): |
| 86 | raise Exception("Received error from vtn service (%d)" % r.status_code) |
| 87 | |
| 88 | for vtn_map in glo_saved_vtn_maps: |
| 89 | if not vtn_map in vtn_maps: |
| 90 | # call vtn rest api to delete map |
| 91 | url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0],vtn_map[1]) |
| 92 | |
| 93 | print "DELETE %s" % url |
| 94 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 95 | if (r.status_code != 200): |
| 96 | raise Exception("Received error from vtn service (%d)" % r.status_code) |
| 97 | |
| 98 | glo_saved_vtn_maps = vtn_maps |
| 99 | # TODO: save this |
| 100 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 101 | def get_method(self, url, id): |
| 102 | url_with_id = "%s/%s" % (url, id) |
| 103 | r = requests.get(url_with_id, auth=self.get_vtn_auth()) |
| 104 | if (r.status_code==200): |
| 105 | method="PUT" |
| 106 | url = url_with_id |
| 107 | req_func = requests.put |
| 108 | exists=True |
| 109 | else: |
| 110 | method="POST" |
| 111 | req_func = requests.post |
| 112 | exists=False |
| 113 | return (exists, url, method, req_func) |
| 114 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 115 | def sync_service_networks(self): |
| 116 | valid_ids = [] |
| 117 | for network in Network.objects.all(): |
| 118 | network = VTNNetwork(network) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 119 | |
| 120 | if not network.id: |
| 121 | continue |
| 122 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 123 | valid_ids.append(network.id) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 124 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 125 | if (glo_saved_networks.get(network.id, None) != network.to_dict()): |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 126 | (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/serviceNetworks", network.id) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 127 | |
Scott Baker | 2906700 | 2016-09-28 17:14:45 -0700 | [diff] [blame] | 128 | if (network.type=="PRIVATE") and (not network.providerNetworks): |
| 129 | logger.info("Skipping network %s because it has no relevant state" % network.id) |
| 130 | continue |
| 131 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 132 | logger.info("%sing VTN API for network %s" % (method, network.id)) |
| 133 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 134 | logger.info("URL: %s" % url) |
| 135 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 136 | # clean the providerNetworks list |
| 137 | providerNetworks = [{"id": x["id"], "bidirectional": x["bidirectional"]} for x in network.providerNetworks] |
| 138 | |
| 139 | data = {"ServiceNetwork": {"id": network.id, |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 140 | "type": network.type, |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 141 | "providerNetworks": providerNetworks} } |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 142 | logger.info("DATA: %s" % str(data)) |
| 143 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 144 | r=req_func(url, json=data, auth=self.get_vtn_auth() ) |
| 145 | if (r.status_code in [200,201]): |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 146 | glo_saved_networks[network.id] = network.to_dict() |
| 147 | else: |
| 148 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 149 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 150 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 151 | for network_id in glo_saved_networks.keys(): |
| 152 | if network_id not in valid_ids: |
| 153 | logger.info("DELETEing VTN API for network %s" % network_id) |
| 154 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 155 | url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/serviceNetworks/%s" % network_id |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 156 | logger.info("URL: %s" % url) |
| 157 | |
| 158 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 159 | if (r.status_code in [200,204]): |
| 160 | del glo_saved_networks[network_id] |
| 161 | else: |
| 162 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 163 | |
| 164 | def sync_service_ports(self): |
| 165 | valid_ids = [] |
| 166 | for port in Port.objects.all(): |
| 167 | port = VTNPort(port) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 168 | |
| 169 | if not port.id: |
| 170 | continue |
| 171 | |
Scott Baker | 2906700 | 2016-09-28 17:14:45 -0700 | [diff] [blame] | 172 | if (not port.vlan_id) and (not port.floating_address_pairs): |
| 173 | logger.info("Skipping port %s because it has no relevant state" % port.id) |
| 174 | continue |
| 175 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 176 | valid_ids.append(port.id) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 177 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 178 | if (glo_saved_ports.get(port.id, None) != port.to_dict()): |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 179 | (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/servicePorts", port.id) |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 180 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 181 | logger.info("%sing VTN API for port %s" % (method, port.id)) |
| 182 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 183 | logger.info("URL: %s" % url) |
| 184 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 185 | data = {"ServicePort": {"id": port.id, |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 186 | "vlan_id": port.vlan_id, |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 187 | "floating_address_pairs": port.floating_address_pairs} } |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 188 | logger.info("DATA: %s" % str(data)) |
| 189 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 190 | r=req_func(url, json=data, auth=self.get_vtn_auth() ) |
Scott Baker | f229101 | 2016-09-08 13:19:45 -0700 | [diff] [blame] | 191 | if (r.status_code in [200,201]): |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 192 | glo_saved_ports[port.id] = port.to_dict() |
| 193 | else: |
| 194 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 195 | |
| 196 | for port_id in glo_saved_ports.keys(): |
| 197 | if port_id not in valid_ids: |
| 198 | logger.info("DELETEing VTN API for port %s" % port_id) |
| 199 | |
Scott Baker | 90f780d | 2016-09-07 22:57:28 -0700 | [diff] [blame] | 200 | url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/servicePorts/%s" % port_id |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 201 | logger.info("URL: %s" % url) |
| 202 | |
| 203 | r = requests.delete(url, auth=self.get_vtn_auth() ) |
| 204 | if (r.status_code in [200,204]): |
| 205 | del glo_saved_ports[port_id] |
| 206 | else: |
| 207 | logger.error("Received error from vtn service (%d)" % r.status_code) |
| 208 | |
| 209 | def call(self, **args): |
| 210 | vtn_service = VTNService.get_service_objects().all() |
| 211 | if not vtn_service: |
| 212 | raise Exception("No VTN Service") |
| 213 | |
| 214 | vtn_service = vtn_service[0] |
| 215 | |
Scott Baker | 855a9df | 2016-09-09 09:23:50 -0700 | [diff] [blame] | 216 | if vtn_service.vtnAPIVersion>=2: |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 217 | # version 2 means use new API |
| 218 | logger.info("Using New API") |
| 219 | self.sync_service_networks() |
| 220 | self.sync_service_ports() |
| 221 | else: |
| 222 | # default to legacy |
| 223 | logger.info("Using Old API") |
| 224 | self.sync_legacy_vtn_api() |
| 225 | |
| 226 | |