blob: 06f5e4a1b6919268ddd3158f8825fce9f00bb9ed [file] [log] [blame]
Scott Baker46831592016-06-20 17:32:04 -07001import os
2import base64
3from collections import defaultdict
4from django.db.models import F, Q
5from xos.config import Config
6from synchronizers.base.openstacksyncstep import OpenStackSyncStep
7from synchronizers.base.syncstep import *
8from core.models import Controller
9from core.models import Image, ControllerImages
10from xos.logger import observer_logger as logger
11from synchronizers.base.ansible import *
12from services.vrouter.models import VRouterTenant
13from services.onos.models import ONOSService
14from services.fabric.models import FabricService
15import json
16
17class 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 Baviera3c73732016-06-27 15:24:59 -040047 # 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 Baker46831592016-06-20 17:32:04 -070064 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 Baviera3c73732016-06-27 15:24:59 -040077 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070078 sub = vroutertenant.subscriber_tenant
Andy Baviera3c73732016-06-27 15:24:59 -040079 instance_id = sub.get_attribute("instance_id")
80 instance = Instance.objects.filter(id=instance_id)[0]
81 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070082 else:
Scott Baker46831592016-06-20 17:32:04 -070083 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040084 instance = Instance.objects.filter(id=instance_id)[0]
85 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070086
87 node = instance.node
88 location = self.get_node_tag(node, "location")
89
Andy Bavier82d89832016-06-28 15:31:06 -040090 # Create JSON
91 data = {
92 "%s/-1" % vroutertenant.public_mac : {
93 "basic" : {
94 "ips" : [ vroutertenant.public_ip ],
95 "location" : location
96 }
97 }
98 }
99 # Stupid Ansible... leading space so it doesn't think it's a dict
100 rest_body = " " + json.dumps(data)
Andy Bavier92e3e002016-06-28 14:30:39 -0400101
Scott Baker46831592016-06-20 17:32:04 -0700102 # Is it a POST or DELETE?
103
Scott Baker46831592016-06-20 17:32:04 -0700104 fields = {
105 'rest_hostname': fos.rest_hostname,
106 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700107 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400108 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700109 'ansible_tag': '%s'%name, # name of ansible playbook
110 }
111 return fields
112
113 def map_sync_outputs(self, controller_image, res):
114 pass