Matteo Scandolo | 79fe3e1 | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 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 Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 16 | import requests |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 18 | from synchronizers.new_base.modelaccessor import * |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 19 | from xos.logger import Logger, logging |
| 20 | |
| 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | DATAPLANE_IP = "dataPlaneIp" |
| 24 | PREFIX = "prefix" |
| 25 | NEXT_HOP = "nextHop" |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 26 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 27 | class SyncAddressManagerServiceInstance(SyncStep): |
| 28 | provides=[AddressManagerServiceInstance] |
| 29 | observes = AddressManagerServiceInstance |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 30 | requested_interval=30 |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 31 | |
| 32 | def get_fabric_onos_service_internal(self): |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 33 | # There will be a ServiceInstanceLink from the FabricService to the Fabric ONOS App |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 34 | fs = FabricService.objects.first() |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 35 | for link in fs.subscribed_links.all(): |
| 36 | if link.provider_service_instance: |
| 37 | # cast from ServiceInstance to ONOSApp |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 38 | service_instance = link.provider_service_instance.leaf_model |
| 39 | # cast from Service to ONOSService |
| 40 | return service_instance.owner.leaf_model |
| 41 | |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 42 | return None |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 44 | 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 Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 50 | def get_node_tag(self, node, tagname): |
Scott Baker | 8daf78d | 2017-03-15 11:39:44 -0700 | [diff] [blame] | 51 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 52 | object_id=node.id, |
| 53 | name=tagname) |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 54 | if tags: |
| 55 | return tags[0].value |
| 56 | else: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 57 | return None |
| 58 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 59 | def fetch_pending(self, deleted): |
Scott Baker | 8474a74 | 2017-06-15 10:34:07 -0700 | [diff] [blame] | 60 | # If fetch_pending is being called for delete, then just execute the standard delete logic. |
| 61 | if deleted: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 62 | return super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
Scott Baker | 8474a74 | 2017-06-15 10:34:07 -0700 | [diff] [blame] | 63 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 64 | fs = FabricService.objects.first() |
| 65 | if (not fs) or (not fs.autoconfig): |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 66 | logger.info("Not FabricServer or not autoconfig. Returning []"); |
| 67 | return [] |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 68 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 69 | objs = super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
Scott Baker | ef17d0c | 2017-02-20 08:45:45 -0800 | [diff] [blame] | 70 | objs = list(objs) |
| 71 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 72 | # 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 Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 77 | logger.info("Skipping %s because it has no instance" % address_si) |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 78 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 79 | else: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 80 | # Maybe the Address is for an instance |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 81 | # TODO: tenant_for_instance_id needs to be a real database field |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 82 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 83 | if not instance_id: |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 84 | logger.info("Skipping %s because it has no tenant_for_instance_id" % address_si) |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 85 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 86 | else: |
| 87 | instance = Instance.objects.filter(id=instance_id)[0] |
| 88 | if not instance.instance_name: |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 89 | logger.info("Skipping %s because it has no instance.instance_name" % address_si) |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 90 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 91 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 92 | return objs |
| 93 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 94 | def get_subscriber(self, address_si): |
| 95 | links = address_si.provided_links.all() |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 96 | for link in links: |
| 97 | if not link.subscriber_service_instance: |
| 98 | continue |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 99 | # 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 Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 103 | return None |
| 104 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 105 | def get_routes_url(self, fos): |
Jonathan Hart | 017aec1 | 2018-02-02 14:34:33 -0800 | [diff] [blame] | 106 | url = 'http://%s:%s/onos/routeservice/routes' % (fos.rest_hostname, fos.rest_port) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 107 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 108 | logger.info("url: %s" % url) |
| 109 | return url |
| 110 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 111 | def sync_record(self, address_si): |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 112 | fos = self.get_fabric_onos_service() |
| 113 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 114 | data = self.map_tenant_to_route(address_si) |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 115 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 116 | r = self.post_route(fos, data) |
| 117 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 118 | logger.info("Posted %s: status: %s result '%s'" % (address_si, r.status_code, r.text)) |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 119 | |
| 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 Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 127 | instance = None |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 128 | # Address setup is kind of hacky right now, we'll |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 129 | # need to revisit. The idea is: |
| 130 | # * Look up the instance corresponding to the address |
| 131 | # * Look up the node running the instance |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 132 | # * Get the "dataPlaneIp" tag, push to the fabric |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 133 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 134 | sub = self.get_subscriber(address_si) |
| 135 | if sub: |
| 136 | instance = sub.instance |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 137 | else: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 138 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 139 | instance = Instance.objects.filter(id=instance_id)[0] |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 140 | |
| 141 | node = instance.node |
Andy Bavier | af81e65 | 2018-01-22 13:41:03 -0800 | [diff] [blame] | 142 | dataPlaneIp = node.dataPlaneIp |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 143 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 144 | if not dataPlaneIp: |
| 145 | raise DeferredException("No IP found for node %s tenant %s -- skipping" % (str(node), str(address_si))) |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 146 | |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 147 | data = { |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 148 | PREFIX : "%s/32" % address_si.public_ip, |
| 149 | NEXT_HOP : dataPlaneIp.split('/')[0] |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 150 | } |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 151 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 152 | return data |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 153 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 154 | def delete_route(self, fos, route): |
| 155 | url = self.get_routes_url(fos) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 156 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 157 | 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 | |