blob: c20795a2e352286f21be6e64877bd5ab50d088d4 [file] [log] [blame]
Scott Baker46831592016-06-20 17:32:04 -07001import os
2import base64
3from collections import defaultdict
Scott Baker46831592016-06-20 17:32:04 -07004from xos.config import Config
Scott Baker71c20eb2017-03-14 21:35:18 -07005from synchronizers.new_base.syncstep import SyncStep, DeferredException
6from synchronizers.new_base.modelaccessor import *
Scott Baker46831592016-06-20 17:32:04 -07007from xos.logger import observer_logger as logger
Scott Baker46831592016-06-20 17:32:04 -07008import json
9
10class SyncVRouterTenant(SyncStep):
11 provides=[VRouterTenant]
12 observes = VRouterTenant
13 requested_interval=30
14 playbook='sync_host.yaml'
15
16 def get_fabric_onos_service(self):
17 fos = None
Scott Baker71c20eb2017-03-14 21:35:18 -070018 fs = FabricService.objects.first()
Scott Baker46831592016-06-20 17:32:04 -070019 if fs.subscribed_tenants.exists():
Scott Baker71c20eb2017-03-14 21:35:18 -070020 app = fs.subscribed_tenants.first()
Scott Baker46831592016-06-20 17:32:04 -070021 if app.provider_service:
22 ps = app.provider_service
Scott Baker71c20eb2017-03-14 21:35:18 -070023 fos = ONOSService.objects.filter(id=ps.id)[0]
Scott Baker46831592016-06-20 17:32:04 -070024 return fos
25
26 def get_node_tag(self, node, tagname):
27 tags = Tag.select_by_content_object(node).filter(name=tagname)
Scott Baker71c20eb2017-03-14 21:35:18 -070028 if tags:
29 return tags[0].value
30 else:
Scott Baker46831592016-06-20 17:32:04 -070031 return None
32
Scott Baker71c20eb2017-03-14 21:35:18 -070033 def fetch_pending(self, deleted):
34 fs = FabricService.objects.first()
35 if (not fs) or (not fs.autoconfig):
36 return None
37
38 # TODO: Why is this a nonstandard synchronizer query?
39
Scott Baker46831592016-06-20 17:32:04 -070040 if (not deleted):
Scott Baker71c20eb2017-03-14 21:35:18 -070041 objs = VRouterTenant.objects.all()
Scott Baker46831592016-06-20 17:32:04 -070042 else:
Scott Baker71c20eb2017-03-14 21:35:18 -070043 objs = VRouterTenant.deleted_objects.all()
Scott Baker46831592016-06-20 17:32:04 -070044
Scott Bakeref17d0c2017-02-20 08:45:45 -080045 objs = list(objs)
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
Scott Baker71c20eb2017-03-14 21:35:18 -070052 if sub.kind != 'vCPE':
Andy Baviera3c73732016-06-27 15:24:59 -040053 objs.remove(vroutertenant)
Scott Baker71c20eb2017-03-14 21:35:18 -070054 else:
55 # coerce the subscriber tenant over to the VSGTenant
56 vsg = VSGTenant.objects.filter(id=sub.id).first()
57 if not vsg.instance:
58 objs.remove(vroutertenant)
Andy Baviera3c73732016-06-27 15:24:59 -040059 else:
60 # Maybe the VRouterTenant is for an instance
Scott Baker71c20eb2017-03-14 21:35:18 -070061 # TODO: tenant_for_instance_id needs to be a real database field
Andy Baviera3c73732016-06-27 15:24:59 -040062 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
63 if not instance_id:
64 objs.remove(vroutertenant)
65 else:
66 instance = Instance.objects.filter(id=instance_id)[0]
67 if not instance.instance_name:
68 objs.remove(vroutertenant)
69
Scott Baker46831592016-06-20 17:32:04 -070070 return objs
71
72 def map_sync_inputs(self, vroutertenant):
73
74 fos = self.get_fabric_onos_service()
75
76 name = None
77 instance = None
78 # VRouterTenant setup is kind of hacky right now, we'll
79 # need to revisit. The idea is:
80 # * Look up the instance corresponding to the address
81 # * Look up the node running the instance
82 # * Get the "location" tag, push to the fabric
Andy Baviera3c73732016-06-27 15:24:59 -040083 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070084 sub = vroutertenant.subscriber_tenant
Scott Baker71c20eb2017-03-14 21:35:18 -070085 assert(sub.kind == 'vCPE')
86 vsg = VSGTenant.objects.filter(id=sub.id).first()
87 instance = vsg.instance
Andy Baviera3c73732016-06-27 15:24:59 -040088 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070089 else:
Scott Baker46831592016-06-20 17:32:04 -070090 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040091 instance = Instance.objects.filter(id=instance_id)[0]
92 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070093
94 node = instance.node
95 location = self.get_node_tag(node, "location")
96
Scott Baker71c20eb2017-03-14 21:35:18 -070097 if not location:
98 raise DeferredException("No location tag for node %s tenant %s -- skipping" % (str(node), str(vroutertenant)))
99
Andy Bavier82d89832016-06-28 15:31:06 -0400100 # Create JSON
101 data = {
102 "%s/-1" % vroutertenant.public_mac : {
103 "basic" : {
104 "ips" : [ vroutertenant.public_ip ],
105 "location" : location
106 }
107 }
108 }
109 # Stupid Ansible... leading space so it doesn't think it's a dict
110 rest_body = " " + json.dumps(data)
Andy Bavier92e3e002016-06-28 14:30:39 -0400111
Scott Baker46831592016-06-20 17:32:04 -0700112 # Is it a POST or DELETE?
113
Scott Baker46831592016-06-20 17:32:04 -0700114 fields = {
115 'rest_hostname': fos.rest_hostname,
116 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700117 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400118 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700119 'ansible_tag': '%s'%name, # name of ansible playbook
120 }
121 return fields
122
123 def map_sync_outputs(self, controller_image, res):
124 pass