blob: 3fa95a4cf19619fd7f126aeb93fa10c70302cd16 [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
Sapan Bhatia46093982017-01-24 19:32:59 +010010from synchronizers.base.ansible_helper import *
Scott Baker46831592016-06-20 17:32:04 -070011from 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
Scott Bakeref17d0c2017-02-20 08:45:45 -080046 objs = list(objs)
47
Andy Baviera3c73732016-06-27 15:24:59 -040048 # Check that each is a valid vCPE tenant or instance
49 for vroutertenant in objs:
50 # Do we have a vCPE subscriber_tenant?
51 if vroutertenant.subscriber_tenant:
52 sub = vroutertenant.subscriber_tenant
53 if sub.kind != 'vCPE' or not sub.get_attribute("instance_id"):
54 objs.remove(vroutertenant)
55 else:
56 # Maybe the VRouterTenant is for an instance
57 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
58 if not instance_id:
59 objs.remove(vroutertenant)
60 else:
61 instance = Instance.objects.filter(id=instance_id)[0]
62 if not instance.instance_name:
63 objs.remove(vroutertenant)
64
Scott Baker46831592016-06-20 17:32:04 -070065 return objs
66
67 def map_sync_inputs(self, vroutertenant):
68
69 fos = self.get_fabric_onos_service()
70
71 name = None
72 instance = None
73 # VRouterTenant setup is kind of hacky right now, we'll
74 # need to revisit. The idea is:
75 # * Look up the instance corresponding to the address
76 # * Look up the node running the instance
77 # * Get the "location" tag, push to the fabric
Andy Baviera3c73732016-06-27 15:24:59 -040078 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070079 sub = vroutertenant.subscriber_tenant
Andy Baviera3c73732016-06-27 15:24:59 -040080 instance_id = sub.get_attribute("instance_id")
81 instance = Instance.objects.filter(id=instance_id)[0]
82 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070083 else:
Scott Baker46831592016-06-20 17:32:04 -070084 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040085 instance = Instance.objects.filter(id=instance_id)[0]
86 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070087
88 node = instance.node
89 location = self.get_node_tag(node, "location")
90
Andy Bavier82d89832016-06-28 15:31:06 -040091 # Create JSON
92 data = {
93 "%s/-1" % vroutertenant.public_mac : {
94 "basic" : {
95 "ips" : [ vroutertenant.public_ip ],
96 "location" : location
97 }
98 }
99 }
100 # Stupid Ansible... leading space so it doesn't think it's a dict
101 rest_body = " " + json.dumps(data)
Andy Bavier92e3e002016-06-28 14:30:39 -0400102
Scott Baker46831592016-06-20 17:32:04 -0700103 # Is it a POST or DELETE?
104
Scott Baker46831592016-06-20 17:32:04 -0700105 fields = {
106 'rest_hostname': fos.rest_hostname,
107 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700108 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400109 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700110 'ansible_tag': '%s'%name, # name of ansible playbook
111 }
112 return fields
113
114 def map_sync_outputs(self, controller_image, res):
115 pass