blob: 8c156762c29b037de82b44e3bb9f87b47ac05036 [file] [log] [blame]
Matteo Scandolo79fe3e12017-08-08 13:05:25 -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
Jonathan Harta6324982017-08-24 14:10:49 -070016import requests
Scott Baker71c20eb2017-03-14 21:35:18 -070017from synchronizers.new_base.syncstep import SyncStep, DeferredException
18from synchronizers.new_base.modelaccessor import *
Jonathan Harta6324982017-08-24 14:10:49 -070019
Scott Baker168fca92018-04-06 15:28:23 -070020from xosconfig import Config
21from multistructlog import create_logger
22
23log = create_logger(Config().get('logging'))
Jonathan Harta6324982017-08-24 14:10:49 -070024
25DATAPLANE_IP = "dataPlaneIp"
26PREFIX = "prefix"
27NEXT_HOP = "nextHop"
Scott Baker46831592016-06-20 17:32:04 -070028
Scott Baker8fddcac2017-08-24 08:56:04 -070029class SyncAddressManagerServiceInstance(SyncStep):
30 provides=[AddressManagerServiceInstance]
31 observes = AddressManagerServiceInstance
Scott Baker46831592016-06-20 17:32:04 -070032 requested_interval=30
Jonathan Harta6324982017-08-24 14:10:49 -070033
34 def get_fabric_onos_service_internal(self):
Scott Baker8fddcac2017-08-24 08:56:04 -070035 # There will be a ServiceInstanceLink from the FabricService to the Fabric ONOS App
Scott Baker71c20eb2017-03-14 21:35:18 -070036 fs = FabricService.objects.first()
Scott Baker7462a142017-07-19 16:34:29 -070037 for link in fs.subscribed_links.all():
38 if link.provider_service_instance:
39 # cast from ServiceInstance to ONOSApp
Scott Baker8fddcac2017-08-24 08:56:04 -070040 service_instance = link.provider_service_instance.leaf_model
41 # cast from Service to ONOSService
42 return service_instance.owner.leaf_model
43
Scott Baker7462a142017-07-19 16:34:29 -070044 return None
Scott Baker46831592016-06-20 17:32:04 -070045
Jonathan Harta6324982017-08-24 14:10:49 -070046 def get_fabric_onos_service(self):
47 fos = self.get_fabric_onos_service_internal()
48 if not fos:
49 raise Exception("Fabric ONOS service not found")
50 return fos
51
Scott Baker46831592016-06-20 17:32:04 -070052 def get_node_tag(self, node, tagname):
Scott Baker8daf78d2017-03-15 11:39:44 -070053 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
54 object_id=node.id,
55 name=tagname)
Scott Baker71c20eb2017-03-14 21:35:18 -070056 if tags:
57 return tags[0].value
58 else:
Scott Baker46831592016-06-20 17:32:04 -070059 return None
60
Scott Baker71c20eb2017-03-14 21:35:18 -070061 def fetch_pending(self, deleted):
Scott Baker8474a742017-06-15 10:34:07 -070062 # If fetch_pending is being called for delete, then just execute the standard delete logic.
63 if deleted:
Scott Baker8fddcac2017-08-24 08:56:04 -070064 return super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted)
Scott Baker8474a742017-06-15 10:34:07 -070065
Scott Baker71c20eb2017-03-14 21:35:18 -070066 fs = FabricService.objects.first()
67 if (not fs) or (not fs.autoconfig):
Scott Baker168fca92018-04-06 15:28:23 -070068 log.info("Not FabricServer or not autoconfig. Returning []");
Scott Baker57e9e4b2017-09-07 14:54:28 -070069 return []
Scott Baker71c20eb2017-03-14 21:35:18 -070070
Scott Baker57e9e4b2017-09-07 14:54:28 -070071 objs = super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted)
Scott Bakeref17d0c2017-02-20 08:45:45 -080072 objs = list(objs)
73
Scott Baker8fddcac2017-08-24 08:56:04 -070074 # Check that each is a valid VSG tenant or instance
Scott Baker168fca92018-04-06 15:28:23 -070075 for address_si in objs[:]:
Scott Baker8fddcac2017-08-24 08:56:04 -070076 sub = self.get_subscriber(address_si)
77 if sub:
Scott Baker168fca92018-04-06 15:28:23 -070078 # TODO: This check is making assumptions about the subscriber service. Consider breaking hardcoded
79 # dependency.
80 if (not hasattr(sub, "instance")) or (not sub.instance):
81 log.info("Skipping %s because it has no instance" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070082 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040083 else:
Scott Baker8fddcac2017-08-24 08:56:04 -070084 # Maybe the Address is for an instance
Scott Baker71c20eb2017-03-14 21:35:18 -070085 # TODO: tenant_for_instance_id needs to be a real database field
Scott Baker8fddcac2017-08-24 08:56:04 -070086 instance_id = address_si.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040087 if not instance_id:
Scott Baker168fca92018-04-06 15:28:23 -070088 log.info("Skipping %s because it has no tenant_for_instance_id" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070089 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040090 else:
Scott Baker168fca92018-04-06 15:28:23 -070091 instances = Instance.objects.filter(id=instance_id)
92 if not instances:
93 log.error("Skipping %s because it appears to be linked to a dead instance" % address_si)
94 objs.remove(address_si)
95 elif not instances[0].instance_name:
96 log.info("Skipping %s because it has no instance.instance_name" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070097 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040098
Scott Baker46831592016-06-20 17:32:04 -070099 return objs
100
Scott Baker8fddcac2017-08-24 08:56:04 -0700101 def get_subscriber(self, address_si):
102 links = address_si.provided_links.all()
Scott Baker7462a142017-07-19 16:34:29 -0700103 for link in links:
104 if not link.subscriber_service_instance:
105 continue
Scott Baker8fddcac2017-08-24 08:56:04 -0700106 # cast from ServiceInstance to VSGTEnant or similar
107 sub = link.subscriber_service_instance.leaf_model
108 # TODO: check here to make sure it's an appropriate type of ServiceInstance ?
109 return sub
Scott Baker7462a142017-07-19 16:34:29 -0700110 return None
111
Jonathan Harta6324982017-08-24 14:10:49 -0700112 def get_routes_url(self, fos):
Jonathan Hart017aec12018-02-02 14:34:33 -0800113 url = 'http://%s:%s/onos/routeservice/routes' % (fos.rest_hostname, fos.rest_port)
Scott Baker46831592016-06-20 17:32:04 -0700114
Scott Baker168fca92018-04-06 15:28:23 -0700115 log.info("url: %s" % url)
Jonathan Harta6324982017-08-24 14:10:49 -0700116 return url
117
Scott Baker57e9e4b2017-09-07 14:54:28 -0700118 def sync_record(self, address_si):
Scott Baker46831592016-06-20 17:32:04 -0700119 fos = self.get_fabric_onos_service()
120
Scott Baker57e9e4b2017-09-07 14:54:28 -0700121 data = self.map_tenant_to_route(address_si)
Scott Baker168fca92018-04-06 15:28:23 -0700122 if not data:
123 # Raise an exception so the synchronizer does not mark this record as synced
124 raise Exception("map_tenant_to_route returned no data for %s" % address_si)
Scott Baker7462a142017-07-19 16:34:29 -0700125
Jonathan Harta6324982017-08-24 14:10:49 -0700126 r = self.post_route(fos, data)
127
Scott Baker168fca92018-04-06 15:28:23 -0700128 log.info("Posted %s: status: %s result '%s'" % (address_si, r.status_code, r.text))
Jonathan Harta6324982017-08-24 14:10:49 -0700129
130 def delete_record(self, address_si):
131 pass
132 # Disabled for now due to lack of feedback state field
133 # self.delete_route(self.get_fabric_onos_service(), self.map_tenant_to_route(address_si))
134
135
136 def map_tenant_to_route(self, address_si):
Scott Baker46831592016-06-20 17:32:04 -0700137 instance = None
Scott Baker8fddcac2017-08-24 08:56:04 -0700138 # Address setup is kind of hacky right now, we'll
Scott Baker46831592016-06-20 17:32:04 -0700139 # need to revisit. The idea is:
140 # * Look up the instance corresponding to the address
141 # * Look up the node running the instance
Jonathan Harta6324982017-08-24 14:10:49 -0700142 # * Get the "dataPlaneIp" tag, push to the fabric
Scott Baker7462a142017-07-19 16:34:29 -0700143
Scott Baker8fddcac2017-08-24 08:56:04 -0700144 sub = self.get_subscriber(address_si)
145 if sub:
Scott Baker168fca92018-04-06 15:28:23 -0700146 # TODO: This check is making assumptions about the subscriber service. Consider breaking hardcoded
147 # dependency.
148 if hasattr(sub, "instance"):
149 instance = sub.instance
Scott Baker46831592016-06-20 17:32:04 -0700150 else:
Scott Baker8fddcac2017-08-24 08:56:04 -0700151 instance_id = address_si.get_attribute("tenant_for_instance_id")
Scott Baker168fca92018-04-06 15:28:23 -0700152 instances = Instance.objects.filter(id=instance_id)
153 if instances:
154 instance = instances[0]
155
156 if not instance:
157 return None
Scott Baker46831592016-06-20 17:32:04 -0700158
159 node = instance.node
Andy Bavieraf81e652018-01-22 13:41:03 -0800160 dataPlaneIp = node.dataPlaneIp
Scott Baker46831592016-06-20 17:32:04 -0700161
Jonathan Harta6324982017-08-24 14:10:49 -0700162 if not dataPlaneIp:
163 raise DeferredException("No IP found for node %s tenant %s -- skipping" % (str(node), str(address_si)))
Scott Baker71c20eb2017-03-14 21:35:18 -0700164
Andy Bavier82d89832016-06-28 15:31:06 -0400165 data = {
Jonathan Harta6324982017-08-24 14:10:49 -0700166 PREFIX : "%s/32" % address_si.public_ip,
167 NEXT_HOP : dataPlaneIp.split('/')[0]
Andy Bavier82d89832016-06-28 15:31:06 -0400168 }
Andy Bavier92e3e002016-06-28 14:30:39 -0400169
Jonathan Harta6324982017-08-24 14:10:49 -0700170 return data
Scott Baker46831592016-06-20 17:32:04 -0700171
Jonathan Harta6324982017-08-24 14:10:49 -0700172 def delete_route(self, fos, route):
173 url = self.get_routes_url(fos)
Scott Baker46831592016-06-20 17:32:04 -0700174
Jonathan Harta6324982017-08-24 14:10:49 -0700175 r = requests.delete(url, json=route, auth=(fos.rest_username, fos.rest_password))
176
Scott Baker168fca92018-04-06 15:28:23 -0700177 log.info("status: %s" % r.status_code)
178 log.info('result: %s' % r.text)
Jonathan Harta6324982017-08-24 14:10:49 -0700179
180 return r
181
182 def post_route(self, fos, route):
183 url = self.get_routes_url(fos)
184 return requests.post(url, json=route, auth=(fos.rest_username, fos.rest_password))
185