blob: 82f0d67b03ac790816747c63ed54be119ed8da3d [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 -070019from xos.logger import Logger, logging
20
21logger = Logger(level=logging.INFO)
22
23DATAPLANE_IP = "dataPlaneIp"
24PREFIX = "prefix"
25NEXT_HOP = "nextHop"
Scott Baker46831592016-06-20 17:32:04 -070026
Scott Baker8fddcac2017-08-24 08:56:04 -070027class SyncAddressManagerServiceInstance(SyncStep):
28 provides=[AddressManagerServiceInstance]
29 observes = AddressManagerServiceInstance
Scott Baker46831592016-06-20 17:32:04 -070030 requested_interval=30
Jonathan Harta6324982017-08-24 14:10:49 -070031
32 def get_fabric_onos_service_internal(self):
Scott Baker8fddcac2017-08-24 08:56:04 -070033 # There will be a ServiceInstanceLink from the FabricService to the Fabric ONOS App
Scott Baker71c20eb2017-03-14 21:35:18 -070034 fs = FabricService.objects.first()
Scott Baker7462a142017-07-19 16:34:29 -070035 for link in fs.subscribed_links.all():
36 if link.provider_service_instance:
37 # cast from ServiceInstance to ONOSApp
Scott Baker8fddcac2017-08-24 08:56:04 -070038 service_instance = link.provider_service_instance.leaf_model
39 # cast from Service to ONOSService
40 return service_instance.owner.leaf_model
41
Scott Baker7462a142017-07-19 16:34:29 -070042 return None
Scott Baker46831592016-06-20 17:32:04 -070043
Jonathan Harta6324982017-08-24 14:10:49 -070044 def get_fabric_onos_service(self):
45 fos = self.get_fabric_onos_service_internal()
46 if not fos:
47 raise Exception("Fabric ONOS service not found")
48 return fos
49
Scott Baker46831592016-06-20 17:32:04 -070050 def get_node_tag(self, node, tagname):
Scott Baker8daf78d2017-03-15 11:39:44 -070051 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
52 object_id=node.id,
53 name=tagname)
Scott Baker71c20eb2017-03-14 21:35:18 -070054 if tags:
55 return tags[0].value
56 else:
Scott Baker46831592016-06-20 17:32:04 -070057 return None
58
Scott Baker71c20eb2017-03-14 21:35:18 -070059 def fetch_pending(self, deleted):
Scott Baker8474a742017-06-15 10:34:07 -070060 # If fetch_pending is being called for delete, then just execute the standard delete logic.
61 if deleted:
Scott Baker8fddcac2017-08-24 08:56:04 -070062 return super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted)
Scott Baker8474a742017-06-15 10:34:07 -070063
Scott Baker71c20eb2017-03-14 21:35:18 -070064 fs = FabricService.objects.first()
65 if (not fs) or (not fs.autoconfig):
Scott Baker57e9e4b2017-09-07 14:54:28 -070066 logger.info("Not FabricServer or not autoconfig. Returning []");
67 return []
Scott Baker71c20eb2017-03-14 21:35:18 -070068
Scott Baker57e9e4b2017-09-07 14:54:28 -070069 objs = super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted)
Scott Bakeref17d0c2017-02-20 08:45:45 -080070 objs = list(objs)
71
Scott Baker8fddcac2017-08-24 08:56:04 -070072 # Check that each is a valid VSG tenant or instance
73 for address_si in objs:
74 sub = self.get_subscriber(address_si)
75 if sub:
76 if not sub.instance:
Scott Baker57e9e4b2017-09-07 14:54:28 -070077 logger.info("Skipping %s because it has no instance" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070078 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040079 else:
Scott Baker8fddcac2017-08-24 08:56:04 -070080 # Maybe the Address is for an instance
Scott Baker71c20eb2017-03-14 21:35:18 -070081 # TODO: tenant_for_instance_id needs to be a real database field
Scott Baker8fddcac2017-08-24 08:56:04 -070082 instance_id = address_si.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040083 if not instance_id:
Scott Baker57e9e4b2017-09-07 14:54:28 -070084 logger.info("Skipping %s because it has no tenant_for_instance_id" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070085 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040086 else:
87 instance = Instance.objects.filter(id=instance_id)[0]
88 if not instance.instance_name:
Scott Baker57e9e4b2017-09-07 14:54:28 -070089 logger.info("Skipping %s because it has no instance.instance_name" % address_si)
Scott Baker8fddcac2017-08-24 08:56:04 -070090 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040091
Scott Baker46831592016-06-20 17:32:04 -070092 return objs
93
Scott Baker8fddcac2017-08-24 08:56:04 -070094 def get_subscriber(self, address_si):
95 links = address_si.provided_links.all()
Scott Baker7462a142017-07-19 16:34:29 -070096 for link in links:
97 if not link.subscriber_service_instance:
98 continue
Scott Baker8fddcac2017-08-24 08:56:04 -070099 # cast from ServiceInstance to VSGTEnant or similar
100 sub = link.subscriber_service_instance.leaf_model
101 # TODO: check here to make sure it's an appropriate type of ServiceInstance ?
102 return sub
Scott Baker7462a142017-07-19 16:34:29 -0700103 return None
104
Jonathan Harta6324982017-08-24 14:10:49 -0700105 def get_routes_url(self, fos):
106 url = 'http://%s:%s/onos/v1/routes' % (fos.rest_hostname, fos.rest_port)
Scott Baker46831592016-06-20 17:32:04 -0700107
Jonathan Harta6324982017-08-24 14:10:49 -0700108 logger.info("url: %s" % url)
109 return url
110
Scott Baker57e9e4b2017-09-07 14:54:28 -0700111 def sync_record(self, address_si):
Scott Baker46831592016-06-20 17:32:04 -0700112 fos = self.get_fabric_onos_service()
113
Scott Baker57e9e4b2017-09-07 14:54:28 -0700114 data = self.map_tenant_to_route(address_si)
Scott Baker7462a142017-07-19 16:34:29 -0700115
Jonathan Harta6324982017-08-24 14:10:49 -0700116 r = self.post_route(fos, data)
117
Scott Baker57e9e4b2017-09-07 14:54:28 -0700118 logger.info("Posted %s: status: %s result '%s'" % (address_si, r.status_code, r.text))
Jonathan Harta6324982017-08-24 14:10:49 -0700119
120 def delete_record(self, address_si):
121 pass
122 # Disabled for now due to lack of feedback state field
123 # self.delete_route(self.get_fabric_onos_service(), self.map_tenant_to_route(address_si))
124
125
126 def map_tenant_to_route(self, address_si):
Scott Baker46831592016-06-20 17:32:04 -0700127 instance = None
Scott Baker8fddcac2017-08-24 08:56:04 -0700128 # Address setup is kind of hacky right now, we'll
Scott Baker46831592016-06-20 17:32:04 -0700129 # need to revisit. The idea is:
130 # * Look up the instance corresponding to the address
131 # * Look up the node running the instance
Jonathan Harta6324982017-08-24 14:10:49 -0700132 # * Get the "dataPlaneIp" tag, push to the fabric
Scott Baker7462a142017-07-19 16:34:29 -0700133
Scott Baker8fddcac2017-08-24 08:56:04 -0700134 sub = self.get_subscriber(address_si)
135 if sub:
136 instance = sub.instance
Scott Baker46831592016-06-20 17:32:04 -0700137 else:
Scott Baker8fddcac2017-08-24 08:56:04 -0700138 instance_id = address_si.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -0400139 instance = Instance.objects.filter(id=instance_id)[0]
Scott Baker46831592016-06-20 17:32:04 -0700140
141 node = instance.node
Jonathan Harta6324982017-08-24 14:10:49 -0700142 dataPlaneIp = self.get_node_tag(node, DATAPLANE_IP)
Scott Baker46831592016-06-20 17:32:04 -0700143
Jonathan Harta6324982017-08-24 14:10:49 -0700144 if not dataPlaneIp:
145 raise DeferredException("No IP found for node %s tenant %s -- skipping" % (str(node), str(address_si)))
Scott Baker71c20eb2017-03-14 21:35:18 -0700146
Andy Bavier82d89832016-06-28 15:31:06 -0400147 data = {
Jonathan Harta6324982017-08-24 14:10:49 -0700148 PREFIX : "%s/32" % address_si.public_ip,
149 NEXT_HOP : dataPlaneIp.split('/')[0]
Andy Bavier82d89832016-06-28 15:31:06 -0400150 }
Andy Bavier92e3e002016-06-28 14:30:39 -0400151
Jonathan Harta6324982017-08-24 14:10:49 -0700152 return data
Scott Baker46831592016-06-20 17:32:04 -0700153
Jonathan Harta6324982017-08-24 14:10:49 -0700154 def delete_route(self, fos, route):
155 url = self.get_routes_url(fos)
Scott Baker46831592016-06-20 17:32:04 -0700156
Jonathan Harta6324982017-08-24 14:10:49 -0700157 r = requests.delete(url, json=route, auth=(fos.rest_username, fos.rest_password))
158
159 logger.info("status: %s" % r.status_code)
160 logger.info('result: %s' % r.text)
161
162 return r
163
164 def post_route(self, fos, route):
165 url = self.get_routes_url(fos)
166 return requests.post(url, json=route, auth=(fos.rest_username, fos.rest_password))
167