blob: 0874978d1fb151388903f0870634ce64e5fe26d3 [file] [log] [blame]
Andy Bavier7b321c52017-08-30 15:33:59 -07001
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
17import os
18import requests
19import socket
20import sys
21import base64
22from synchronizers.vtn.vtnnetport import VTNNetwork, VTNPort
23from synchronizers.new_base.syncstep import SyncStep
24from synchronizers.new_base.modelaccessor import *
Andy Bavier7b321c52017-08-30 15:33:59 -070025from requests.auth import HTTPBasicAuth
26
Scott Bakerf6cfb382018-04-03 09:48:01 -070027from xosconfig import Config
28from multistructlog import create_logger
29
30log = create_logger(Config().get('logging'))
31
Andy Bavier7b321c52017-08-30 15:33:59 -070032
33# XXX should save and load this
34glo_saved_networks = {}
35glo_saved_ports = {}
36
37class 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 Bakerf6cfb382018-04-03 09:48:01 -070053 # NOTE: Deferred until service interfaces are revisited
Andy Bavier7b321c52017-08-30 15:33:59 -070054
55 #if not link.provider_service_interface:
Scott Bakerf6cfb382018-04-03 09:48:01 -070056 # log.warning("Link %s does not have a provider_service_interface. Skipping" % link)
Andy Bavier7b321c52017-08-30 15:33:59 -070057 # continue
58 #
59 #if link.provider_service_interface.interface_type.name != "onos_app_interface":
Scott Bakerf6cfb382018-04-03 09:48:01 -070060 # log.warning("Link %s provider_service_interface type is not equal to onos_app_interface" % link)
Andy Bavier7b321c52017-08-30 15:33:59 -070061 # 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 Bakerf6cfb382018-04-03 09:48:01 -0700110 log.info("Skipping network because it has no id")
Andy Bavier7b321c52017-08-30 15:33:59 -0700111 continue
112
Scott Bakerf6cfb382018-04-03 09:48:01 -0700113 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 Bavier7b321c52017-08-30 15:33:59 -0700119 continue
120
121 valid_ids.append(network.id)
122
Scott Bakerf6cfb382018-04-03 09:48:01 -0700123 # 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 Bavier7b321c52017-08-30 15:33:59 -0700136 (exists, url, method, req_func) = self.get_method(onos_auth, "http://%s:%d/onos/cordvtn/serviceNetworks" % (onos_hostname, onos_port), network.id)
137
Scott Bakerf6cfb382018-04-03 09:48:01 -0700138 log.info("%sing VTN API for network %s" % (method, network.id), url=url, data=data)
Andy Bavier7b321c52017-08-30 15:33:59 -0700139
140 r=req_func(url, json=data, auth=onos_auth )
141 if (r.status_code in [200,201]):
Scott Bakerf6cfb382018-04-03 09:48:01 -0700142 glo_saved_networks[network.id] = data
Andy Bavier7b321c52017-08-30 15:33:59 -0700143 else:
Scott Bakerf6cfb382018-04-03 09:48:01 -0700144 log.error("Received error from vtn service (%d)" % r.status_code)
Andy Bavier7b321c52017-08-30 15:33:59 -0700145
146
147 for network_id in glo_saved_networks.keys():
148 if network_id not in valid_ids:
Scott Bakerf6cfb382018-04-03 09:48:01 -0700149 log.info("DELETEing VTN API for network %s" % network_id)
Andy Bavier7b321c52017-08-30 15:33:59 -0700150
151 url = "http://%s:%d/onos/cordvtn/serviceNetworks/%s" % (onos_hostname, onos_port, network_id)
Scott Bakerf6cfb382018-04-03 09:48:01 -0700152 log.info("URL: %s" % url)
Andy Bavier7b321c52017-08-30 15:33:59 -0700153
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 Bakerf6cfb382018-04-03 09:48:01 -0700158 log.error("Received error from vtn service (%d)" % r.status_code)
Andy Bavier7b321c52017-08-30 15:33:59 -0700159
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 Bakerf6cfb382018-04-03 09:48:01 -0700168 log.info("Skipping port because it has no id")
Andy Bavier7b321c52017-08-30 15:33:59 -0700169 continue
170
Scott Bakerf6cfb382018-04-03 09:48:01 -0700171 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 Bavier7b321c52017-08-30 15:33:59 -0700181 continue
182
183 valid_ids.append(port.id)
184
Scott Bakerf6cfb382018-04-03 09:48:01 -0700185 # 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 Bavier7b321c52017-08-30 15:33:59 -0700196 (exists, url, method, req_func) = self.get_method(onos_auth, "http://%s:%d/onos/cordvtn/servicePorts" % (onos_hostname, onos_port), port.id)
197
Scott Bakerf6cfb382018-04-03 09:48:01 -0700198 log.info("%sing VTN API for port %s" % (method, port.id), url=url, data=data)
Andy Bavier7b321c52017-08-30 15:33:59 -0700199
200 r=req_func(url, json=data, auth=onos_auth )
201 if (r.status_code in [200,201]):
Scott Bakerf6cfb382018-04-03 09:48:01 -0700202 glo_saved_ports[port.id] = data
Andy Bavier7b321c52017-08-30 15:33:59 -0700203 else:
Scott Bakerf6cfb382018-04-03 09:48:01 -0700204 log.error("Received error from vtn service (%d)" % r.status_code)
Andy Bavier7b321c52017-08-30 15:33:59 -0700205
206 for port_id in glo_saved_ports.keys():
207 if port_id not in valid_ids:
Scott Bakerf6cfb382018-04-03 09:48:01 -0700208 log.info("DELETEing VTN API for port %s" % port_id)
Andy Bavier7b321c52017-08-30 15:33:59 -0700209
210 url = "http://%s:%d/onos/cordvtn/servicePorts/%s" % (onos_hostname, onos_port, port_id)
Scott Bakerf6cfb382018-04-03 09:48:01 -0700211 log.info("URL: %s" % url)
Andy Bavier7b321c52017-08-30 15:33:59 -0700212
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 Bakerf6cfb382018-04-03 09:48:01 -0700217 log.error("Received error from vtn service (%d)" % r.status_code)
Andy Bavier7b321c52017-08-30 15:33:59 -0700218
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 Bavier7b321c52017-08-30 15:33:59 -0700243 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