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