Matteo Scandolo | 517962f | 2018-05-01 09:42:04 -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 | |
| 16 | import requests |
| 17 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 18 | from synchronizers.new_base.modelaccessor import * |
| 19 | |
| 20 | from xosconfig import Config |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | log = create_logger(Config().get('logging')) |
| 24 | |
| 25 | DATAPLANE_IP = "dataPlaneIp" |
| 26 | PREFIX = "prefix" |
| 27 | NEXT_HOP = "nextHop" |
| 28 | |
| 29 | class SyncAddressManagerServiceInstance(SyncStep): |
| 30 | provides=[AddressManagerServiceInstance] |
| 31 | observes = AddressManagerServiceInstance |
| 32 | requested_interval=30 |
| 33 | |
| 34 | def get_fabric_onos_service_internal(self): |
| 35 | # There will be a ServiceInstanceLink from the FabricService to the Fabric ONOS App |
| 36 | fs = FabricService.objects.first() |
| 37 | for link in fs.subscribed_links.all(): |
| 38 | if link.provider_service_instance: |
| 39 | # cast from ServiceInstance to ONOSApp |
| 40 | service_instance = link.provider_service_instance.leaf_model |
| 41 | # cast from Service to ONOSService |
| 42 | return service_instance.owner.leaf_model |
| 43 | |
| 44 | return None |
| 45 | |
| 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 | |
| 52 | def get_node_tag(self, node, tagname): |
| 53 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 54 | object_id=node.id, |
| 55 | name=tagname) |
| 56 | if tags: |
| 57 | return tags[0].value |
| 58 | else: |
| 59 | return None |
| 60 | |
| 61 | def fetch_pending(self, deleted): |
| 62 | # If fetch_pending is being called for delete, then just execute the standard delete logic. |
| 63 | if deleted: |
| 64 | return super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
| 65 | |
| 66 | fs = FabricService.objects.first() |
| 67 | if (not fs) or (not fs.autoconfig): |
| 68 | log.info("Not FabricServer or not autoconfig. Returning []"); |
| 69 | return [] |
| 70 | |
| 71 | objs = super(SyncAddressManagerServiceInstance, self).fetch_pending(deleted) |
| 72 | objs = list(objs) |
| 73 | |
| 74 | # Check that each is a valid VSG tenant or instance |
| 75 | for address_si in objs[:]: |
| 76 | sub = self.get_subscriber(address_si) |
| 77 | if sub: |
| 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) |
| 82 | objs.remove(address_si) |
| 83 | else: |
| 84 | # Maybe the Address is for an instance |
| 85 | # TODO: tenant_for_instance_id needs to be a real database field |
| 86 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
| 87 | if not instance_id: |
| 88 | log.info("Skipping %s because it has no tenant_for_instance_id" % address_si) |
| 89 | objs.remove(address_si) |
| 90 | else: |
| 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) |
| 97 | objs.remove(address_si) |
| 98 | |
| 99 | return objs |
| 100 | |
| 101 | def get_subscriber(self, address_si): |
| 102 | links = address_si.provided_links.all() |
| 103 | for link in links: |
| 104 | if not link.subscriber_service_instance: |
| 105 | continue |
| 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 |
| 110 | return None |
| 111 | |
| 112 | def get_routes_url(self, fos): |
| 113 | url = 'http://%s:%s/onos/routeservice/routes' % (fos.rest_hostname, fos.rest_port) |
| 114 | |
| 115 | log.info("url: %s" % url) |
| 116 | return url |
| 117 | |
| 118 | def sync_record(self, address_si): |
| 119 | fos = self.get_fabric_onos_service() |
| 120 | |
| 121 | data = self.map_tenant_to_route(address_si) |
| 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) |
| 125 | |
| 126 | r = self.post_route(fos, data) |
| 127 | |
| 128 | log.info("Posted %s: status: %s result '%s'" % (address_si, r.status_code, r.text)) |
| 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): |
| 137 | instance = None |
| 138 | # Address setup is kind of hacky right now, we'll |
| 139 | # need to revisit. The idea is: |
| 140 | # * Look up the instance corresponding to the address |
| 141 | # * Look up the node running the instance |
| 142 | # * Get the "dataPlaneIp" tag, push to the fabric |
| 143 | |
| 144 | sub = self.get_subscriber(address_si) |
| 145 | if sub: |
| 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 |
| 150 | else: |
| 151 | instance_id = address_si.get_attribute("tenant_for_instance_id") |
| 152 | instances = Instance.objects.filter(id=instance_id) |
| 153 | if instances: |
| 154 | instance = instances[0] |
| 155 | |
| 156 | if not instance: |
| 157 | return None |
| 158 | |
| 159 | node = instance.node |
| 160 | dataPlaneIp = node.dataPlaneIp |
| 161 | |
| 162 | if not dataPlaneIp: |
| 163 | raise DeferredException("No IP found for node %s tenant %s -- skipping" % (str(node), str(address_si))) |
| 164 | |
| 165 | data = { |
| 166 | PREFIX : "%s/32" % address_si.public_ip, |
| 167 | NEXT_HOP : dataPlaneIp.split('/')[0] |
| 168 | } |
| 169 | |
| 170 | return data |
| 171 | |
| 172 | def delete_route(self, fos, route): |
| 173 | url = self.get_routes_url(fos) |
| 174 | |
| 175 | r = requests.delete(url, json=route, auth=(fos.rest_username, fos.rest_password)) |
| 176 | |
| 177 | log.info("status: %s" % r.status_code) |
| 178 | log.info('result: %s' % r.text) |
| 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 | |