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 | |
| 47 | return objs |
| 48 | |
| 49 | def map_sync_inputs(self, vroutertenant): |
| 50 | |
| 51 | fos = self.get_fabric_onos_service() |
| 52 | |
| 53 | name = None |
| 54 | instance = None |
| 55 | # VRouterTenant setup is kind of hacky right now, we'll |
| 56 | # need to revisit. The idea is: |
| 57 | # * Look up the instance corresponding to the address |
| 58 | # * Look up the node running the instance |
| 59 | # * Get the "location" tag, push to the fabric |
| 60 | # |
| 61 | # Do we have a vCPE subscriber_tenant? |
| 62 | if (vroutertenant.subscriber_tenant): |
| 63 | sub = vroutertenant.subscriber_tenant |
| 64 | if (sub.kind == 'vCPE'): |
| 65 | instance_id = sub.get_attribute("instance_id") |
| 66 | if instance_id: |
| 67 | instance = Instance.objects.filter(id=instance_id)[0] |
| 68 | name = str(sub) |
| 69 | else: |
| 70 | # Maybe the VRouterTenant is for an instance |
| 71 | instance_id = vroutertenant.get_attribute("tenant_for_instance_id") |
| 72 | if instance_id: |
| 73 | instance = Instance.objects.filter(id=instance_id)[0] |
| 74 | name = str(instance) |
| 75 | |
| 76 | node = instance.node |
| 77 | location = self.get_node_tag(node, "location") |
| 78 | |
| 79 | # Is it a POST or DELETE? |
| 80 | |
| 81 | # Create JSON |
| 82 | data = { |
| 83 | "%s/-1"%vroutertenant.public_mac : { |
| 84 | "basic" : { |
| 85 | "ips" : [ vroutertenant.public_ip ], |
| 86 | "location" : location |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | rest_json = json.dumps(data, indent=4) |
| 92 | |
| 93 | fields = { |
| 94 | 'rest_hostname': fos.rest_hostname, |
| 95 | 'rest_port': fos.rest_port, |
| 96 | 'rest_json': rest_json, |
| 97 | 'rest_endpoint': "onos/v1/network/configuration/hosts", |
| 98 | 'ansible_tag': '%s'%name, # name of ansible playbook |
| 99 | } |
| 100 | return fields |
| 101 | |
| 102 | def map_sync_outputs(self, controller_image, res): |
| 103 | pass |