blob: 6ec561072a182d6aad600d0e9450633252e32664 [file] [log] [blame]
Scott Bakerb3a80de2016-09-06 16:51:27 -07001import os
2import requests
3import socket
4import sys
5import base64
6from django.db.models import F, Q
7from xos.config import Config
8from synchronizers.base.syncstep import SyncStep
9from core.models import Service, Network, Port
10from core.models.service import COARSE_KIND
11from services.vtn.vtnnetport import VTNNetwork, VTNPort
12from services.vtn.models import VTNService
13from xos.logger import Logger, logging
14from requests.auth import HTTPBasicAuth
15
16# hpclibrary will be in steps/..
17parentdir = os.path.join(os.path.dirname(__file__),"..")
18sys.path.insert(0,parentdir)
19
20logger = Logger(level=logging.INFO)
21
22# XXX should save and load this
23glo_saved_vtn_maps = []
24glo_saved_networks = {}
25glo_saved_ports = {}
26
27class 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 Bakerff016682016-11-28 09:53:13 -080053 # code below this point is for ONOS running in a slice, and is
54 # probably outdated
55
Scott Bakerb3a80de2016-09-06 16:51:27 -070056 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 Bakerff016682016-11-28 09:53:13 -080068 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 Bakerb3a80de2016-09-06 16:51:27 -070079 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 Bakerff016682016-11-28 09:53:13 -080095 url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0], vtn_map[1])
Scott Bakerb3a80de2016-09-06 16:51:27 -070096
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 Bakerff016682016-11-28 09:53:13 -0800105 url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0],vtn_map[1])
Scott Bakerb3a80de2016-09-06 16:51:27 -0700106
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 Baker90f780d2016-09-07 22:57:28 -0700115 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 Bakerb3a80de2016-09-06 16:51:27 -0700129 def sync_service_networks(self):
130 valid_ids = []
131 for network in Network.objects.all():
132 network = VTNNetwork(network)
Scott Bakerf2291012016-09-08 13:19:45 -0700133
134 if not network.id:
135 continue
136
Scott Bakerb3a80de2016-09-06 16:51:27 -0700137 valid_ids.append(network.id)
Scott Bakerf2291012016-09-08 13:19:45 -0700138
Scott Bakerb3a80de2016-09-06 16:51:27 -0700139 if (glo_saved_networks.get(network.id, None) != network.to_dict()):
Scott Bakerff016682016-11-28 09:53:13 -0800140 (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/serviceNetworks", network.id)
Scott Bakerb3a80de2016-09-06 16:51:27 -0700141
Scott Baker29067002016-09-28 17:14:45 -0700142 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 Baker90f780d2016-09-07 22:57:28 -0700146 logger.info("%sing VTN API for network %s" % (method, network.id))
147
Scott Bakerb3a80de2016-09-06 16:51:27 -0700148 logger.info("URL: %s" % url)
149
Scott Baker90f780d2016-09-07 22:57:28 -0700150 # 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 Bakerb3a80de2016-09-06 16:51:27 -0700154 "type": network.type,
Scott Baker90f780d2016-09-07 22:57:28 -0700155 "providerNetworks": providerNetworks} }
Scott Bakerb3a80de2016-09-06 16:51:27 -0700156 logger.info("DATA: %s" % str(data))
157
Scott Baker90f780d2016-09-07 22:57:28 -0700158 r=req_func(url, json=data, auth=self.get_vtn_auth() )
159 if (r.status_code in [200,201]):
Scott Bakerb3a80de2016-09-06 16:51:27 -0700160 glo_saved_networks[network.id] = network.to_dict()
161 else:
162 logger.error("Received error from vtn service (%d)" % r.status_code)
163
Scott Baker90f780d2016-09-07 22:57:28 -0700164
Scott Bakerb3a80de2016-09-06 16:51:27 -0700165 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 Bakerff016682016-11-28 09:53:13 -0800169 url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/serviceNetworks/%s" % network_id
Scott Bakerb3a80de2016-09-06 16:51:27 -0700170 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 Bakerf2291012016-09-08 13:19:45 -0700182
183 if not port.id:
184 continue
185
Scott Baker29067002016-09-28 17:14:45 -0700186 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 Bakerb3a80de2016-09-06 16:51:27 -0700190 valid_ids.append(port.id)
Scott Bakerf2291012016-09-08 13:19:45 -0700191
Scott Bakerb3a80de2016-09-06 16:51:27 -0700192 if (glo_saved_ports.get(port.id, None) != port.to_dict()):
Scott Bakerff016682016-11-28 09:53:13 -0800193 (exists, url, method, req_func) = self.get_method("http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/servicePorts", port.id)
Scott Bakerb3a80de2016-09-06 16:51:27 -0700194
Scott Baker90f780d2016-09-07 22:57:28 -0700195 logger.info("%sing VTN API for port %s" % (method, port.id))
196
Scott Bakerb3a80de2016-09-06 16:51:27 -0700197 logger.info("URL: %s" % url)
198
Scott Baker90f780d2016-09-07 22:57:28 -0700199 data = {"ServicePort": {"id": port.id,
Scott Bakerb3a80de2016-09-06 16:51:27 -0700200 "vlan_id": port.vlan_id,
Scott Baker90f780d2016-09-07 22:57:28 -0700201 "floating_address_pairs": port.floating_address_pairs} }
Scott Bakerb3a80de2016-09-06 16:51:27 -0700202 logger.info("DATA: %s" % str(data))
203
Scott Baker90f780d2016-09-07 22:57:28 -0700204 r=req_func(url, json=data, auth=self.get_vtn_auth() )
Scott Bakerf2291012016-09-08 13:19:45 -0700205 if (r.status_code in [200,201]):
Scott Bakerb3a80de2016-09-06 16:51:27 -0700206 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 Bakerff016682016-11-28 09:53:13 -0800214 url = "http://" + self.get_vtn_addr() + ":" + self.get_vtn_port() + "/onos/cordvtn/servicePorts/%s" % port_id
Scott Bakerb3a80de2016-09-06 16:51:27 -0700215 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 Baker855a9df2016-09-09 09:23:50 -0700230 if vtn_service.vtnAPIVersion>=2:
Scott Bakerb3a80de2016-09-06 16:51:27 -0700231 # 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 Bakercdbb71e2016-10-26 15:50:03 -0700238 raise Exception("VTN API Version 1 is no longer supported by VTN Synchronizer")
239 #self.sync_legacy_vtn_api()
Scott Bakerb3a80de2016-09-06 16:51:27 -0700240
241