Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 4 | from xos.config import Config |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 5 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 6 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 7 | from xos.logger import observer_logger as logger |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 8 | import json |
| 9 | |
| 10 | class SyncVRouterTenant(SyncStep): |
| 11 | provides=[VRouterTenant] |
| 12 | observes = VRouterTenant |
| 13 | requested_interval=30 |
| 14 | playbook='sync_host.yaml' |
| 15 | |
| 16 | def get_fabric_onos_service(self): |
| 17 | fos = None |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 18 | fs = FabricService.objects.first() |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 19 | if fs.subscribed_tenants.exists(): |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 20 | app = fs.subscribed_tenants.first() |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 21 | if app.provider_service: |
| 22 | ps = app.provider_service |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 23 | fos = ONOSService.objects.filter(id=ps.id)[0] |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 24 | return fos |
| 25 | |
| 26 | def get_node_tag(self, node, tagname): |
Scott Baker | 8daf78d | 2017-03-15 11:39:44 -0700 | [diff] [blame] | 27 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 28 | object_id=node.id, |
| 29 | name=tagname) |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 30 | if tags: |
| 31 | return tags[0].value |
| 32 | else: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 33 | return None |
| 34 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 35 | def fetch_pending(self, deleted): |
| 36 | fs = FabricService.objects.first() |
| 37 | if (not fs) or (not fs.autoconfig): |
| 38 | return None |
| 39 | |
| 40 | # TODO: Why is this a nonstandard synchronizer query? |
| 41 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 42 | if (not deleted): |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 43 | objs = VRouterTenant.objects.all() |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 44 | else: |
Scott Baker | 8daf78d | 2017-03-15 11:39:44 -0700 | [diff] [blame] | 45 | objs = super(SyncVRouterTenant, self).fetch_pending(deleted) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 46 | |
Scott Baker | ef17d0c | 2017-02-20 08:45:45 -0800 | [diff] [blame] | 47 | objs = list(objs) |
| 48 | |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 49 | # Check that each is a valid vCPE tenant or instance |
| 50 | for vroutertenant in objs: |
| 51 | # Do we have a vCPE subscriber_tenant? |
| 52 | if vroutertenant.subscriber_tenant: |
| 53 | sub = vroutertenant.subscriber_tenant |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 54 | if sub.kind != 'vCPE': |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 55 | objs.remove(vroutertenant) |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 56 | else: |
| 57 | # coerce the subscriber tenant over to the VSGTenant |
| 58 | vsg = VSGTenant.objects.filter(id=sub.id).first() |
| 59 | if not vsg.instance: |
| 60 | objs.remove(vroutertenant) |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 61 | else: |
| 62 | # Maybe the VRouterTenant is for an instance |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 63 | # TODO: tenant_for_instance_id needs to be a real database field |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 64 | instance_id = vroutertenant.get_attribute("tenant_for_instance_id") |
| 65 | if not instance_id: |
| 66 | objs.remove(vroutertenant) |
| 67 | else: |
| 68 | instance = Instance.objects.filter(id=instance_id)[0] |
| 69 | if not instance.instance_name: |
| 70 | objs.remove(vroutertenant) |
| 71 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 72 | return objs |
| 73 | |
| 74 | def map_sync_inputs(self, vroutertenant): |
| 75 | |
| 76 | fos = self.get_fabric_onos_service() |
| 77 | |
| 78 | name = None |
| 79 | instance = None |
| 80 | # VRouterTenant setup is kind of hacky right now, we'll |
| 81 | # need to revisit. The idea is: |
| 82 | # * Look up the instance corresponding to the address |
| 83 | # * Look up the node running the instance |
| 84 | # * Get the "location" tag, push to the fabric |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 85 | if vroutertenant.subscriber_tenant: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 86 | sub = vroutertenant.subscriber_tenant |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 87 | assert(sub.kind == 'vCPE') |
| 88 | vsg = VSGTenant.objects.filter(id=sub.id).first() |
| 89 | instance = vsg.instance |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 90 | name = str(sub) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 91 | else: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 92 | instance_id = vroutertenant.get_attribute("tenant_for_instance_id") |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 93 | instance = Instance.objects.filter(id=instance_id)[0] |
| 94 | name = str(instance) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 95 | |
| 96 | node = instance.node |
| 97 | location = self.get_node_tag(node, "location") |
| 98 | |
Scott Baker | 71c20eb | 2017-03-14 21:35:18 -0700 | [diff] [blame] | 99 | if not location: |
| 100 | raise DeferredException("No location tag for node %s tenant %s -- skipping" % (str(node), str(vroutertenant))) |
| 101 | |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 102 | # Create JSON |
| 103 | data = { |
| 104 | "%s/-1" % vroutertenant.public_mac : { |
| 105 | "basic" : { |
| 106 | "ips" : [ vroutertenant.public_ip ], |
| 107 | "location" : location |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | # Stupid Ansible... leading space so it doesn't think it's a dict |
| 112 | rest_body = " " + json.dumps(data) |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 113 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 114 | # Is it a POST or DELETE? |
| 115 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 116 | fields = { |
| 117 | 'rest_hostname': fos.rest_hostname, |
| 118 | 'rest_port': fos.rest_port, |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 119 | 'rest_endpoint': "onos/v1/network/configuration/hosts", |
Andy Bavier | 82d8983 | 2016-06-28 15:31:06 -0400 | [diff] [blame] | 120 | 'rest_body': rest_body, |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 121 | 'ansible_tag': '%s'%name, # name of ansible playbook |
| 122 | } |
| 123 | return fields |
| 124 | |
| 125 | def map_sync_outputs(self, controller_image, res): |
| 126 | pass |