blob: ad100637d38c8dd857f22f797386eb7b51bfa4d3 [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):
66 return None
67
68 # TODO: Why is this a nonstandard synchronizer query?
Scott Baker8fddcac2017-08-24 08:56:04 -070069 objs = AddressManagerServiceInstance.objects.all()
Scott Baker46831592016-06-20 17:32:04 -070070
Scott Bakeref17d0c2017-02-20 08:45:45 -080071 objs = list(objs)
72
Scott Baker8fddcac2017-08-24 08:56:04 -070073 # Check that each is a valid VSG tenant or instance
74 for address_si in objs:
75 sub = self.get_subscriber(address_si)
76 if sub:
77 if not sub.instance:
78 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 Baker8fddcac2017-08-24 08:56:04 -070084 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040085 else:
86 instance = Instance.objects.filter(id=instance_id)[0]
87 if not instance.instance_name:
Scott Baker8fddcac2017-08-24 08:56:04 -070088 objs.remove(address_si)
Andy Baviera3c73732016-06-27 15:24:59 -040089
Scott Baker46831592016-06-20 17:32:04 -070090 return objs
91
Scott Baker8fddcac2017-08-24 08:56:04 -070092 def get_subscriber(self, address_si):
93 links = address_si.provided_links.all()
Scott Baker7462a142017-07-19 16:34:29 -070094 for link in links:
95 if not link.subscriber_service_instance:
96 continue
Scott Baker8fddcac2017-08-24 08:56:04 -070097 # cast from ServiceInstance to VSGTEnant or similar
98 sub = link.subscriber_service_instance.leaf_model
99 # TODO: check here to make sure it's an appropriate type of ServiceInstance ?
100 return sub
Scott Baker7462a142017-07-19 16:34:29 -0700101 return None
102
Jonathan Harta6324982017-08-24 14:10:49 -0700103 def get_routes_url(self, fos):
104 url = 'http://%s:%s/onos/v1/routes' % (fos.rest_hostname, fos.rest_port)
Scott Baker46831592016-06-20 17:32:04 -0700105
Jonathan Harta6324982017-08-24 14:10:49 -0700106 logger.info("url: %s" % url)
107 return url
108
109 def sync_record(self, vroutertenant):
Scott Baker46831592016-06-20 17:32:04 -0700110 fos = self.get_fabric_onos_service()
111
Jonathan Harta6324982017-08-24 14:10:49 -0700112 data = self.map_tenant_to_route(vroutertenant)
Scott Baker7462a142017-07-19 16:34:29 -0700113
Jonathan Harta6324982017-08-24 14:10:49 -0700114 r = self.post_route(fos, data)
115
116 logger.info("status: %s" % r.status_code)
117 logger.info('result: %s' % r.text)
118
119 def delete_record(self, address_si):
120 pass
121 # Disabled for now due to lack of feedback state field
122 # self.delete_route(self.get_fabric_onos_service(), self.map_tenant_to_route(address_si))
123
124
125 def map_tenant_to_route(self, address_si):
Scott Baker46831592016-06-20 17:32:04 -0700126 instance = None
Scott Baker8fddcac2017-08-24 08:56:04 -0700127 # Address setup is kind of hacky right now, we'll
Scott Baker46831592016-06-20 17:32:04 -0700128 # need to revisit. The idea is:
129 # * Look up the instance corresponding to the address
130 # * Look up the node running the instance
Jonathan Harta6324982017-08-24 14:10:49 -0700131 # * Get the "dataPlaneIp" tag, push to the fabric
Scott Baker7462a142017-07-19 16:34:29 -0700132
Scott Baker8fddcac2017-08-24 08:56:04 -0700133 sub = self.get_subscriber(address_si)
134 if sub:
135 instance = sub.instance
Scott Baker46831592016-06-20 17:32:04 -0700136 else:
Scott Baker8fddcac2017-08-24 08:56:04 -0700137 instance_id = address_si.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -0400138 instance = Instance.objects.filter(id=instance_id)[0]
Scott Baker46831592016-06-20 17:32:04 -0700139
140 node = instance.node
Jonathan Harta6324982017-08-24 14:10:49 -0700141 dataPlaneIp = self.get_node_tag(node, DATAPLANE_IP)
Scott Baker46831592016-06-20 17:32:04 -0700142
Jonathan Harta6324982017-08-24 14:10:49 -0700143 if not dataPlaneIp:
144 raise DeferredException("No IP found for node %s tenant %s -- skipping" % (str(node), str(address_si)))
Scott Baker71c20eb2017-03-14 21:35:18 -0700145
Andy Bavier82d89832016-06-28 15:31:06 -0400146 data = {
Jonathan Harta6324982017-08-24 14:10:49 -0700147 PREFIX : "%s/32" % address_si.public_ip,
148 NEXT_HOP : dataPlaneIp.split('/')[0]
Andy Bavier82d89832016-06-28 15:31:06 -0400149 }
Andy Bavier92e3e002016-06-28 14:30:39 -0400150
Jonathan Harta6324982017-08-24 14:10:49 -0700151 return data
Scott Baker46831592016-06-20 17:32:04 -0700152
Jonathan Harta6324982017-08-24 14:10:49 -0700153 def delete_route(self, fos, route):
154 url = self.get_routes_url(fos)
Scott Baker46831592016-06-20 17:32:04 -0700155
Jonathan Harta6324982017-08-24 14:10:49 -0700156 r = requests.delete(url, json=route, auth=(fos.rest_username, fos.rest_password))
157
158 logger.info("status: %s" % r.status_code)
159 logger.info('result: %s' % r.text)
160
161 return r
162
163 def post_route(self, fos, route):
164 url = self.get_routes_url(fos)
165 return requests.post(url, json=route, auth=(fos.rest_username, fos.rest_password))
166