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