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