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 | |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 20 | from xosconfig import Config |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | log = create_logger(Config().get('logging')) |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 24 | |
| 25 | DATAPLANE_IP = "dataPlaneIp" |
| 26 | PREFIX = "prefix" |
| 27 | NEXT_HOP = "nextHop" |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 28 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 29 | class SyncAddressManagerServiceInstance(SyncStep): |
| 30 | provides=[AddressManagerServiceInstance] |
| 31 | observes = AddressManagerServiceInstance |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 32 | requested_interval=30 |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 33 | |
| 34 | def get_fabric_onos_service_internal(self): |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 35 | # 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] | 36 | fs = FabricService.objects.first() |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 37 | for link in fs.subscribed_links.all(): |
| 38 | if link.provider_service_instance: |
| 39 | # cast from ServiceInstance to ONOSApp |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 40 | service_instance = link.provider_service_instance.leaf_model |
| 41 | # cast from Service to ONOSService |
| 42 | return service_instance.owner.leaf_model |
| 43 | |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 44 | return None |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 45 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 46 | 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 Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 52 | def get_node_tag(self, node, tagname): |
Scott Baker | 8daf78d | 2017-03-15 11:39:44 -0700 | [diff] [blame] | 53 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 54 | object_id=node.id, |
| 55 | name=tagname) |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 56 | if tags: |
| 57 | return tags[0].value |
| 58 | else: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 59 | return None |
| 60 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 61 | def fetch_pending(self, deleted): |
Scott Baker | 8474a74 | 2017-06-15 10:34:07 -0700 | [diff] [blame] | 62 | # If fetch_pending is being called for delete, then just execute the standard delete logic. |
| 63 | if deleted: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 64 | return super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
Scott Baker | 8474a74 | 2017-06-15 10:34:07 -0700 | [diff] [blame] | 65 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 66 | fs = FabricService.objects.first() |
| 67 | if (not fs) or (not fs.autoconfig): |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 68 | log.info("Not FabricServer or not autoconfig. Returning []"); |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 69 | return [] |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 70 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 71 | objs = super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
Scott Baker | ef17d0c | 2017-02-20 08:45:45 -0800 | [diff] [blame] | 72 | objs = list(objs) |
| 73 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 74 | # Check that each is a valid VSG tenant or instance |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 75 | for address_si in objs[:]: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 76 | sub = self.get_subscriber(address_si) |
| 77 | if sub: |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 78 | # 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 Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 82 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 83 | else: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 84 | # Maybe the Address is for an instance |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 85 | # TODO: tenant_for_instance_id needs to be a real database field |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 86 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 87 | if not instance_id: |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 88 | log.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] | 89 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 90 | else: |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 91 | 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 Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 97 | objs.remove(address_si) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 98 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 99 | return objs |
| 100 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 101 | def get_subscriber(self, address_si): |
| 102 | links = address_si.provided_links.all() |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 103 | for link in links: |
| 104 | if not link.subscriber_service_instance: |
| 105 | continue |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 106 | # 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 Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 110 | return None |
| 111 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 112 | def get_routes_url(self, fos): |
Jonathan Hart | 017aec1 | 2018-02-02 14:34:33 -0800 | [diff] [blame] | 113 | 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] | 114 | |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 115 | log.info("url: %s" % url) |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 116 | return url |
| 117 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 118 | def sync_record(self, address_si): |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 119 | fos = self.get_fabric_onos_service() |
| 120 | |
Scott Baker | 57e9e4b | 2017-09-07 14:54:28 -0700 | [diff] [blame] | 121 | data = self.map_tenant_to_route(address_si) |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 122 | 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 Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 125 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 126 | r = self.post_route(fos, data) |
| 127 | |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 128 | log.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] | 129 | |
| 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 Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 137 | instance = None |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 138 | # Address setup is kind of hacky right now, we'll |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 139 | # need to revisit. The idea is: |
| 140 | # * Look up the instance corresponding to the address |
| 141 | # * Look up the node running the instance |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 142 | # * Get the "dataPlaneIp" tag, push to the fabric |
Scott Baker | 7462a14 | 2017-07-19 16:34:29 -0700 | [diff] [blame] | 143 | |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 144 | sub = self.get_subscriber(address_si) |
| 145 | if sub: |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 146 | # 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 Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 150 | else: |
Scott Baker | 8fddcac | 2017-08-24 08:56:04 -0700 | [diff] [blame] | 151 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 152 | instances = Instance.objects.filter(id=instance_id) |
| 153 | if instances: |
| 154 | instance = instances[0] |
| 155 | |
| 156 | if not instance: |
| 157 | return None |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 158 | |
| 159 | node = instance.node |
Andy Bavier | af81e65 | 2018-01-22 13:41:03 -0800 | [diff] [blame] | 160 | dataPlaneIp = node.dataPlaneIp |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 161 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 162 | if not dataPlaneIp: |
| 163 | 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] | 164 | |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 165 | data = { |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 166 | PREFIX : "%s/32" % address_si.public_ip, |
| 167 | NEXT_HOP : dataPlaneIp.split('/')[0] |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 168 | } |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 169 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 170 | return data |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 171 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 172 | def delete_route(self, fos, route): |
| 173 | url = self.get_routes_url(fos) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 174 | |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 175 | r = requests.delete(url, json=route, auth=(fos.rest_username, fos.rest_password)) |
| 176 | |
Scott Baker | 168fca9 | 2018-04-06 15:28:23 -0700 | [diff] [blame] | 177 | log.info("status: %s" % r.status_code) |
| 178 | log.info('result: %s' % r.text) |
Jonathan Hart | a632498 | 2017-08-24 14:10:49 -0700 | [diff] [blame] | 179 | |
| 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 | |