blob: a67d210fff62262073a85ead61c2145fa95b013e [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
Scott Baker46831592016-06-20 17:32:04 -07006from synchronizers.base.syncstep import *
7from core.models import Controller
8from core.models import Image, ControllerImages
9from xos.logger import observer_logger as logger
10from synchronizers.base.ansible import *
11from services.vrouter.models import VRouterTenant
12from services.onos.models import ONOSService
13from services.fabric.models import FabricService
14import json
15
16class 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 Baviera3c73732016-06-27 15:24:59 -040046 # 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 Baker46831592016-06-20 17:32:04 -070063 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 Baviera3c73732016-06-27 15:24:59 -040076 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070077 sub = vroutertenant.subscriber_tenant
Andy Baviera3c73732016-06-27 15:24:59 -040078 instance_id = sub.get_attribute("instance_id")
79 instance = Instance.objects.filter(id=instance_id)[0]
80 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070081 else:
Scott Baker46831592016-06-20 17:32:04 -070082 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040083 instance = Instance.objects.filter(id=instance_id)[0]
84 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070085
86 node = instance.node
87 location = self.get_node_tag(node, "location")
88
Andy Bavier82d89832016-06-28 15:31:06 -040089 # 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 Bavier92e3e002016-06-28 14:30:39 -0400100
Scott Baker46831592016-06-20 17:32:04 -0700101 # Is it a POST or DELETE?
102
Scott Baker46831592016-06-20 17:32:04 -0700103 fields = {
104 'rest_hostname': fos.rest_hostname,
105 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700106 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400107 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700108 'ansible_tag': '%s'%name, # name of ansible playbook
109 }
110 return fields
111
112 def map_sync_outputs(self, controller_image, res):
113 pass