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 | |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 23 | def get_files_dir(self): |
| 24 | if not hasattr(Config(), "observer_steps_dir"): |
| 25 | # make steps_dir mandatory; there's no valid reason for it to not |
| 26 | # be defined. |
| 27 | raise Exception("observer_steps_dir is not defined in config file") |
| 28 | |
| 29 | step_dir = Config().observer_steps_dir |
| 30 | |
| 31 | return os.path.join(step_dir, "..", "files") |
| 32 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 33 | def get_fabric_onos_service(self): |
| 34 | fos = None |
| 35 | fs = FabricService.get_service_objects().all()[0] |
| 36 | if fs.subscribed_tenants.exists(): |
| 37 | app = fs.subscribed_tenants.all()[0] |
| 38 | if app.provider_service: |
| 39 | ps = app.provider_service |
| 40 | fos = ONOSService.get_service_objects().filter(id=ps.id)[0] |
| 41 | return fos |
| 42 | |
| 43 | def get_node_tag(self, node, tagname): |
| 44 | tags = Tag.select_by_content_object(node).filter(name=tagname) |
| 45 | return tags[0].value |
| 46 | |
| 47 | def fetch_pending(self, deleted): |
| 48 | fs = FabricService.get_service_objects().all()[0] |
| 49 | if not fs.autoconfig: |
| 50 | return None |
| 51 | |
| 52 | if (not deleted): |
| 53 | objs = VRouterTenant.get_tenant_objects().filter(Q(lazy_blocked=False)) |
| 54 | else: |
| 55 | objs = VRouterTenant.get_deleted_tenant_objects() |
| 56 | |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 57 | # Check that each is a valid vCPE tenant or instance |
| 58 | for vroutertenant in objs: |
| 59 | # Do we have a vCPE subscriber_tenant? |
| 60 | if vroutertenant.subscriber_tenant: |
| 61 | sub = vroutertenant.subscriber_tenant |
| 62 | if sub.kind != 'vCPE' or not sub.get_attribute("instance_id"): |
| 63 | objs.remove(vroutertenant) |
| 64 | else: |
| 65 | # Maybe the VRouterTenant is for an instance |
| 66 | instance_id = vroutertenant.get_attribute("tenant_for_instance_id") |
| 67 | if not instance_id: |
| 68 | objs.remove(vroutertenant) |
| 69 | else: |
| 70 | instance = Instance.objects.filter(id=instance_id)[0] |
| 71 | if not instance.instance_name: |
| 72 | objs.remove(vroutertenant) |
| 73 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 74 | return objs |
| 75 | |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 76 | def write_config(self, files_dir, name, public_mac, public_ip, location): |
| 77 | if not os.path.exists(files_dir): |
| 78 | os.makedirs(files_dir) |
| 79 | |
| 80 | # Create JSON |
| 81 | data = { |
| 82 | "%s/-1"%public_mac : { |
| 83 | "basic" : { |
| 84 | "ips" : [ public_ip ], |
| 85 | "location" : location |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | file(os.path.join(files_dir, name),"w").write(json_dumps(data,indent=4)) |
| 91 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 92 | def map_sync_inputs(self, vroutertenant): |
| 93 | |
| 94 | fos = self.get_fabric_onos_service() |
| 95 | |
| 96 | name = None |
| 97 | instance = None |
| 98 | # VRouterTenant setup is kind of hacky right now, we'll |
| 99 | # need to revisit. The idea is: |
| 100 | # * Look up the instance corresponding to the address |
| 101 | # * Look up the node running the instance |
| 102 | # * Get the "location" tag, push to the fabric |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 103 | if vroutertenant.subscriber_tenant: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 104 | sub = vroutertenant.subscriber_tenant |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 105 | instance_id = sub.get_attribute("instance_id") |
| 106 | instance = Instance.objects.filter(id=instance_id)[0] |
| 107 | name = str(sub) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 108 | else: |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 109 | instance_id = vroutertenant.get_attribute("tenant_for_instance_id") |
Andy Bavier | a3c7373 | 2016-06-27 15:24:59 -0400 | [diff] [blame] | 110 | instance = Instance.objects.filter(id=instance_id)[0] |
| 111 | name = str(instance) |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 112 | |
| 113 | node = instance.node |
| 114 | location = self.get_node_tag(node, "location") |
| 115 | |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 116 | files_dir = self.get_files_dir() |
| 117 | self.write_config(files_dir, name, vroutertenant.public_mac, vroutertenant.public_ip, location) |
| 118 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 119 | # Is it a POST or DELETE? |
| 120 | |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 121 | fields = { |
| 122 | 'rest_hostname': fos.rest_hostname, |
| 123 | 'rest_port': fos.rest_port, |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 124 | 'rest_endpoint': "onos/v1/network/configuration/hosts", |
Andy Bavier | 92e3e00 | 2016-06-28 14:30:39 -0400 | [diff] [blame] | 125 | 'files_dir': files_dir, |
| 126 | 'rest_config.fn': name, |
Scott Baker | 4683159 | 2016-06-20 17:32:04 -0700 | [diff] [blame] | 127 | 'ansible_tag': '%s'%name, # name of ansible playbook |
| 128 | } |
| 129 | return fields |
| 130 | |
| 131 | def map_sync_outputs(self, controller_image, res): |
| 132 | pass |