blob: fe3fae015dc7243ffaa3a4c9572b971742e3e506 [file] [log] [blame]
Scott Baker46831592016-06-20 17:32:04 -07001import os
2import base64
3from collections import defaultdict
Scott Baker71c20eb2017-03-14 21:35:18 -07004from synchronizers.new_base.syncstep import SyncStep, DeferredException
5from synchronizers.new_base.modelaccessor import *
Scott Baker46831592016-06-20 17:32:04 -07006from xos.logger import observer_logger as logger
Scott Baker46831592016-06-20 17:32:04 -07007import json
8
9class SyncVRouterTenant(SyncStep):
10 provides=[VRouterTenant]
11 observes = VRouterTenant
12 requested_interval=30
13 playbook='sync_host.yaml'
14
15 def get_fabric_onos_service(self):
16 fos = None
Scott Baker71c20eb2017-03-14 21:35:18 -070017 fs = FabricService.objects.first()
Scott Baker46831592016-06-20 17:32:04 -070018 if fs.subscribed_tenants.exists():
Scott Baker71c20eb2017-03-14 21:35:18 -070019 app = fs.subscribed_tenants.first()
Scott Baker46831592016-06-20 17:32:04 -070020 if app.provider_service:
21 ps = app.provider_service
Scott Baker71c20eb2017-03-14 21:35:18 -070022 fos = ONOSService.objects.filter(id=ps.id)[0]
Scott Baker46831592016-06-20 17:32:04 -070023 return fos
24
25 def get_node_tag(self, node, tagname):
Scott Baker8daf78d2017-03-15 11:39:44 -070026 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
27 object_id=node.id,
28 name=tagname)
Scott Baker71c20eb2017-03-14 21:35:18 -070029 if tags:
30 return tags[0].value
31 else:
Scott Baker46831592016-06-20 17:32:04 -070032 return None
33
Scott Baker71c20eb2017-03-14 21:35:18 -070034 def fetch_pending(self, deleted):
Scott Baker8474a742017-06-15 10:34:07 -070035 # If fetch_pending is being called for delete, then just execute the standard delete logic.
36 if deleted:
37 return super(SyncVRouterTenant, self).fetch_pending(deleted)
38
Scott Baker71c20eb2017-03-14 21:35:18 -070039 fs = FabricService.objects.first()
40 if (not fs) or (not fs.autoconfig):
41 return None
42
43 # TODO: Why is this a nonstandard synchronizer query?
Scott Baker8474a742017-06-15 10:34:07 -070044 objs = VRouterTenant.objects.all()
Scott Baker46831592016-06-20 17:32:04 -070045
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
Scott Baker71c20eb2017-03-14 21:35:18 -070053 if sub.kind != 'vCPE':
Andy Baviera3c73732016-06-27 15:24:59 -040054 objs.remove(vroutertenant)
Scott Baker71c20eb2017-03-14 21:35:18 -070055 else:
56 # coerce the subscriber tenant over to the VSGTenant
57 vsg = VSGTenant.objects.filter(id=sub.id).first()
58 if not vsg.instance:
59 objs.remove(vroutertenant)
Andy Baviera3c73732016-06-27 15:24:59 -040060 else:
61 # Maybe the VRouterTenant is for an instance
Scott Baker71c20eb2017-03-14 21:35:18 -070062 # TODO: tenant_for_instance_id needs to be a real database field
Andy Baviera3c73732016-06-27 15:24:59 -040063 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
64 if not instance_id:
65 objs.remove(vroutertenant)
66 else:
67 instance = Instance.objects.filter(id=instance_id)[0]
68 if not instance.instance_name:
69 objs.remove(vroutertenant)
70
Scott Baker46831592016-06-20 17:32:04 -070071 return objs
72
73 def map_sync_inputs(self, vroutertenant):
74
75 fos = self.get_fabric_onos_service()
76
77 name = None
78 instance = None
79 # VRouterTenant setup is kind of hacky right now, we'll
80 # need to revisit. The idea is:
81 # * Look up the instance corresponding to the address
82 # * Look up the node running the instance
83 # * Get the "location" tag, push to the fabric
Andy Baviera3c73732016-06-27 15:24:59 -040084 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070085 sub = vroutertenant.subscriber_tenant
Scott Baker71c20eb2017-03-14 21:35:18 -070086 assert(sub.kind == 'vCPE')
87 vsg = VSGTenant.objects.filter(id=sub.id).first()
88 instance = vsg.instance
Andy Baviera3c73732016-06-27 15:24:59 -040089 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070090 else:
Scott Baker46831592016-06-20 17:32:04 -070091 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040092 instance = Instance.objects.filter(id=instance_id)[0]
93 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070094
95 node = instance.node
96 location = self.get_node_tag(node, "location")
97
Scott Baker71c20eb2017-03-14 21:35:18 -070098 if not location:
99 raise DeferredException("No location tag for node %s tenant %s -- skipping" % (str(node), str(vroutertenant)))
100
Andy Bavier82d89832016-06-28 15:31:06 -0400101 # Create JSON
102 data = {
103 "%s/-1" % vroutertenant.public_mac : {
104 "basic" : {
105 "ips" : [ vroutertenant.public_ip ],
106 "location" : location
107 }
108 }
109 }
110 # Stupid Ansible... leading space so it doesn't think it's a dict
111 rest_body = " " + json.dumps(data)
Andy Bavier92e3e002016-06-28 14:30:39 -0400112
Scott Baker46831592016-06-20 17:32:04 -0700113 # Is it a POST or DELETE?
114
Scott Baker46831592016-06-20 17:32:04 -0700115 fields = {
116 'rest_hostname': fos.rest_hostname,
117 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700118 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400119 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700120 'ansible_tag': '%s'%name, # name of ansible playbook
121 }
122 return fields
123
124 def map_sync_outputs(self, controller_image, res):
125 pass