blob: d549289df2a24f882ef55fffae912d3c19edd376 [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):
Scott Baker8daf78d2017-03-15 11:39:44 -070027 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
28 object_id=node.id,
29 name=tagname)
Scott Baker71c20eb2017-03-14 21:35:18 -070030 if tags:
31 return tags[0].value
32 else:
Scott Baker46831592016-06-20 17:32:04 -070033 return None
34
Scott Baker71c20eb2017-03-14 21:35:18 -070035 def fetch_pending(self, deleted):
36 fs = FabricService.objects.first()
37 if (not fs) or (not fs.autoconfig):
38 return None
39
40 # TODO: Why is this a nonstandard synchronizer query?
41
Scott Baker46831592016-06-20 17:32:04 -070042 if (not deleted):
Scott Baker71c20eb2017-03-14 21:35:18 -070043 objs = VRouterTenant.objects.all()
Scott Baker46831592016-06-20 17:32:04 -070044 else:
Scott Baker8daf78d2017-03-15 11:39:44 -070045 objs = super(SyncVRouterTenant, self).fetch_pending(deleted)
Scott Baker46831592016-06-20 17:32:04 -070046
Scott Bakeref17d0c2017-02-20 08:45:45 -080047 objs = list(objs)
48
Andy Baviera3c73732016-06-27 15:24:59 -040049 # Check that each is a valid vCPE tenant or instance
50 for vroutertenant in objs:
51 # Do we have a vCPE subscriber_tenant?
52 if vroutertenant.subscriber_tenant:
53 sub = vroutertenant.subscriber_tenant
Scott Baker71c20eb2017-03-14 21:35:18 -070054 if sub.kind != 'vCPE':
Andy Baviera3c73732016-06-27 15:24:59 -040055 objs.remove(vroutertenant)
Scott Baker71c20eb2017-03-14 21:35:18 -070056 else:
57 # coerce the subscriber tenant over to the VSGTenant
58 vsg = VSGTenant.objects.filter(id=sub.id).first()
59 if not vsg.instance:
60 objs.remove(vroutertenant)
Andy Baviera3c73732016-06-27 15:24:59 -040061 else:
62 # Maybe the VRouterTenant is for an instance
Scott Baker71c20eb2017-03-14 21:35:18 -070063 # TODO: tenant_for_instance_id needs to be a real database field
Andy Baviera3c73732016-06-27 15:24:59 -040064 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
65 if not instance_id:
66 objs.remove(vroutertenant)
67 else:
68 instance = Instance.objects.filter(id=instance_id)[0]
69 if not instance.instance_name:
70 objs.remove(vroutertenant)
71
Scott Baker46831592016-06-20 17:32:04 -070072 return objs
73
74 def map_sync_inputs(self, vroutertenant):
75
76 fos = self.get_fabric_onos_service()
77
78 name = None
79 instance = None
80 # VRouterTenant setup is kind of hacky right now, we'll
81 # need to revisit. The idea is:
82 # * Look up the instance corresponding to the address
83 # * Look up the node running the instance
84 # * Get the "location" tag, push to the fabric
Andy Baviera3c73732016-06-27 15:24:59 -040085 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -070086 sub = vroutertenant.subscriber_tenant
Scott Baker71c20eb2017-03-14 21:35:18 -070087 assert(sub.kind == 'vCPE')
88 vsg = VSGTenant.objects.filter(id=sub.id).first()
89 instance = vsg.instance
Andy Baviera3c73732016-06-27 15:24:59 -040090 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -070091 else:
Scott Baker46831592016-06-20 17:32:04 -070092 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -040093 instance = Instance.objects.filter(id=instance_id)[0]
94 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -070095
96 node = instance.node
97 location = self.get_node_tag(node, "location")
98
Scott Baker71c20eb2017-03-14 21:35:18 -070099 if not location:
100 raise DeferredException("No location tag for node %s tenant %s -- skipping" % (str(node), str(vroutertenant)))
101
Andy Bavier82d89832016-06-28 15:31:06 -0400102 # Create JSON
103 data = {
104 "%s/-1" % vroutertenant.public_mac : {
105 "basic" : {
106 "ips" : [ vroutertenant.public_ip ],
107 "location" : location
108 }
109 }
110 }
111 # Stupid Ansible... leading space so it doesn't think it's a dict
112 rest_body = " " + json.dumps(data)
Andy Bavier92e3e002016-06-28 14:30:39 -0400113
Scott Baker46831592016-06-20 17:32:04 -0700114 # Is it a POST or DELETE?
115
Scott Baker46831592016-06-20 17:32:04 -0700116 fields = {
117 'rest_hostname': fos.rest_hostname,
118 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700119 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier82d89832016-06-28 15:31:06 -0400120 'rest_body': rest_body,
Scott Baker46831592016-06-20 17:32:04 -0700121 'ansible_tag': '%s'%name, # name of ansible playbook
122 }
123 return fields
124
125 def map_sync_outputs(self, controller_image, res):
126 pass