blob: 150a59627ba7690ef7f9e5fe6f59267fb305b30e [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
6from synchronizers.base.openstacksyncstep import OpenStackSyncStep
7from synchronizers.base.syncstep import *
8from core.models import Controller
9from core.models import Image, ControllerImages
10from xos.logger import observer_logger as logger
11from synchronizers.base.ansible import *
12from services.vrouter.models import VRouterTenant
13from services.onos.models import ONOSService
14from services.fabric.models import FabricService
15import json
16
17class SyncVRouterTenant(SyncStep):
18 provides=[VRouterTenant]
19 observes = VRouterTenant
20 requested_interval=30
21 playbook='sync_host.yaml'
22
Andy Bavier92e3e002016-06-28 14:30:39 -040023 def get_files_dir(self):
24 if not hasattr(Config(), "observer_steps_dir"):
25 # make steps_dir mandatory; there's no valid reason for it to not
26 # be defined.
27 raise Exception("observer_steps_dir is not defined in config file")
28
29 step_dir = Config().observer_steps_dir
30
31 return os.path.join(step_dir, "..", "files")
32
Scott Baker46831592016-06-20 17:32:04 -070033 def get_fabric_onos_service(self):
34 fos = None
35 fs = FabricService.get_service_objects().all()[0]
36 if fs.subscribed_tenants.exists():
37 app = fs.subscribed_tenants.all()[0]
38 if app.provider_service:
39 ps = app.provider_service
40 fos = ONOSService.get_service_objects().filter(id=ps.id)[0]
41 return fos
42
43 def get_node_tag(self, node, tagname):
44 tags = Tag.select_by_content_object(node).filter(name=tagname)
45 return tags[0].value
46
47 def fetch_pending(self, deleted):
48 fs = FabricService.get_service_objects().all()[0]
49 if not fs.autoconfig:
50 return None
51
52 if (not deleted):
53 objs = VRouterTenant.get_tenant_objects().filter(Q(lazy_blocked=False))
54 else:
55 objs = VRouterTenant.get_deleted_tenant_objects()
56
Andy Baviera3c73732016-06-27 15:24:59 -040057 # Check that each is a valid vCPE tenant or instance
58 for vroutertenant in objs:
59 # Do we have a vCPE subscriber_tenant?
60 if vroutertenant.subscriber_tenant:
61 sub = vroutertenant.subscriber_tenant
62 if sub.kind != 'vCPE' or not sub.get_attribute("instance_id"):
63 objs.remove(vroutertenant)
64 else:
65 # Maybe the VRouterTenant is for an instance
66 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
67 if not instance_id:
68 objs.remove(vroutertenant)
69 else:
70 instance = Instance.objects.filter(id=instance_id)[0]
71 if not instance.instance_name:
72 objs.remove(vroutertenant)
73
Scott Baker46831592016-06-20 17:32:04 -070074 return objs
75
Andy Bavier92e3e002016-06-28 14:30:39 -040076 def write_config(self, files_dir, name, public_mac, public_ip, location):
77 if not os.path.exists(files_dir):
78 os.makedirs(files_dir)
79
80 # Create JSON
81 data = {
82 "%s/-1"%public_mac : {
83 "basic" : {
84 "ips" : [ public_ip ],
85 "location" : location
86 }
87 }
88 }
89
90 file(os.path.join(files_dir, name),"w").write(json_dumps(data,indent=4))
91
Scott Baker46831592016-06-20 17:32:04 -070092 def map_sync_inputs(self, vroutertenant):
93
94 fos = self.get_fabric_onos_service()
95
96 name = None
97 instance = None
98 # VRouterTenant setup is kind of hacky right now, we'll
99 # need to revisit. The idea is:
100 # * Look up the instance corresponding to the address
101 # * Look up the node running the instance
102 # * Get the "location" tag, push to the fabric
Andy Baviera3c73732016-06-27 15:24:59 -0400103 if vroutertenant.subscriber_tenant:
Scott Baker46831592016-06-20 17:32:04 -0700104 sub = vroutertenant.subscriber_tenant
Andy Baviera3c73732016-06-27 15:24:59 -0400105 instance_id = sub.get_attribute("instance_id")
106 instance = Instance.objects.filter(id=instance_id)[0]
107 name = str(sub)
Scott Baker46831592016-06-20 17:32:04 -0700108 else:
Scott Baker46831592016-06-20 17:32:04 -0700109 instance_id = vroutertenant.get_attribute("tenant_for_instance_id")
Andy Baviera3c73732016-06-27 15:24:59 -0400110 instance = Instance.objects.filter(id=instance_id)[0]
111 name = str(instance)
Scott Baker46831592016-06-20 17:32:04 -0700112
113 node = instance.node
114 location = self.get_node_tag(node, "location")
115
Andy Bavier92e3e002016-06-28 14:30:39 -0400116 files_dir = self.get_files_dir()
117 self.write_config(files_dir, name, vroutertenant.public_mac, vroutertenant.public_ip, location)
118
Scott Baker46831592016-06-20 17:32:04 -0700119 # Is it a POST or DELETE?
120
Scott Baker46831592016-06-20 17:32:04 -0700121 fields = {
122 'rest_hostname': fos.rest_hostname,
123 'rest_port': fos.rest_port,
Scott Baker46831592016-06-20 17:32:04 -0700124 'rest_endpoint': "onos/v1/network/configuration/hosts",
Andy Bavier92e3e002016-06-28 14:30:39 -0400125 'files_dir': files_dir,
126 'rest_config.fn': name,
Scott Baker46831592016-06-20 17:32:04 -0700127 'ansible_tag': '%s'%name, # name of ansible playbook
128 }
129 return fields
130
131 def map_sync_outputs(self, controller_image, res):
132 pass