blob: 8674446299f5fec865b189c3eca836be772016d8 [file] [log] [blame]
Scott Baker96746882017-06-09 14:12:15 -07001from synchronizers.new_base.modelaccessor import *
2from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
3from synchronizers.new_base.exceptions import *
4
5class VSGTenantPolicy(TenantWithContainerPolicy):
6 model_name = "VSGTenant"
7
8 def handle_create(self, tenant):
9 return self.handle_update(tenant)
10
11 def handle_update(self, tenant):
12 self.manage_container(tenant)
13 self.manage_vrouter(tenant)
14 self.cleanup_orphans(tenant)
15
16 def handle_delete(self, tenant):
17 if tenant.vrouter:
18 tenant.vrouter.delete()
19
20 def manage_vrouter(self, tenant):
21 if tenant.deleted:
22 return
23
24 if tenant.vrouter is None:
25 vrouter = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=tenant)
26 vrouter.save()
27
28 def cleanup_orphans(self, tenant):
29 # ensure vSG only has one vRouter
30 cur_vrouter = tenant.vrouter
Scott Baker80238f82017-07-18 16:01:10 -070031 for link in tenant.subscribed_links.all():
32 # TODO: hardcoded dependency
33 # cast from ServiceInstance to VRouterTenant
34 vrouters = VRouterTenant.objects.filter(id = link.provider_service_instance.id)
35 for vrouter in vrouters:
36 if (not cur_vrouter) or (vrouter.id != cur_vrouter.id):
37 # print "XXX clean up orphaned vrouter", vrouter
38 vrouter.delete()
Scott Baker96746882017-06-09 14:12:15 -070039
40 def get_vsg_service(self, tenant):
Scott Baker80238f82017-07-18 16:01:10 -070041 return VSGService.objects.get(id=tenant.owner.id)
Scott Baker96746882017-06-09 14:12:15 -070042
43 def find_instance_for_s_tag(self, s_tag):
44 tags = Tag.objects.filter(name="s_tag", value=s_tag)
45 if tags:
46 return tags[0].content_object
47
48 return None
49
50 def find_or_make_instance_for_s_tag(self, tenant, s_tag):
51 instance = self.find_instance_for_s_tag(tenant.volt.s_tag)
52 if instance:
53 if instance.no_sync:
54 # if no_sync is still set, then perhaps we failed while saving it and need to retry.
55 self.save_instance(tenant, instance)
56 return instance
57
58 desired_image = self.get_image(tenant)
59
60 flavors = Flavor.objects.filter(name="m1.small")
61 if not flavors:
62 raise SynchronizerConfigurationError("No m1.small flavor")
63
Scott Baker80238f82017-07-18 16:01:10 -070064 slice = tenant.owner.slices.first()
Scott Baker96746882017-06-09 14:12:15 -070065
66 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(tenant).node_label).pick()
67
68 assert (slice is not None)
69 assert (node is not None)
70 assert (desired_image is not None)
71 assert (tenant.creator is not None)
72 assert (node.site_deployment.deployment is not None)
73 assert (desired_image is not None)
74
75 instance = Instance(slice=slice,
76 node=node,
77 image=desired_image,
78 creator=tenant.creator,
79 deployment=node.site_deployment.deployment,
80 flavor=flavors[0],
81 isolation=slice.default_isolation,
82 parent=parent)
83
84 self.save_instance(tenant, instance)
85
86 return instance
87
88 def manage_container(self, tenant):
89 if tenant.deleted:
90 return
91
92 if not tenant.volt:
93 raise SynchronizerConfigurationError("This VSG container has no volt")
94
95 if tenant.instance:
96 # We're good.
97 return
98
99 instance = self.find_or_make_instance_for_s_tag(tenant, tenant.volt.s_tag)
100 tenant.instance = instance
101 # TODO: possible for partial failure here?
102 tenant.save()
103
104 def find_or_make_port(self, instance, network, **kwargs):
105 port = Port.objects.filter(instance_id=instance.id, network_id=network.id)
106 if port:
107 port = port[0]
108 else:
109 port = Port(instance=instance, network=network, **kwargs)
110 port.save()
111 return port
112
113 def get_lan_network(self, tenant, instance):
Scott Baker80238f82017-07-18 16:01:10 -0700114 slice = tenant.owner.slices.all()[0]
Scott Baker96746882017-06-09 14:12:15 -0700115 # there should only be one network private network, and its template should not be the management template
116 lan_networks = [x for x in slice.networks.all() if
117 x.template.visibility == "private" and (not "management" in x.template.name)]
118 if len(lan_networks) > 1:
119 raise SynchronizerProgrammingError("The vSG slice should only have one non-management private network")
120 if not lan_networks:
121 raise SynchronizerProgrammingError("No lan_network")
122 return lan_networks[0]
123
124 def port_set_parameter(self, port, name, value):
125 pt = NetworkParameterType.objects.get(name=name)
126 existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id)
127
128 if existing_params:
129 p = existing_params[0]
130 p.value = str(value)
131 p.save()
132 else:
133 p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value))
134 p.save()
135
136 def save_instance(self, tenant, instance):
137 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
138 instance.no_sync = True # prevent instance from being synced until we're done with it
139 super(VSGTenantPolicy, self).save_instance(instance)
140 try:
141 if instance.isolation in ["container", "container_vm"]:
142 raise Exception("Not supported")
143
144 if instance.isolation in ["vm"]:
145 lan_network = self.get_lan_network(tenant, instance)
146 port = self.find_or_make_port(instance, lan_network)
147 self.port_set_parameter(port, "c_tag", tenant.volt.c_tag)
148 self.port_set_parameter(port, "s_tag", tenant.volt.s_tag)
149 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % tenant.volt.s_tag)
150 port.save()
151
152 # tag the instance with the s-tag, so we can easily find the
153 # instance later
154 if tenant.volt and tenant.volt.s_tag:
155 tags = Tag.objects.filter(name="s_tag", value=tenant.volt.s_tag)
156 if not tags:
Scott Baker80238f82017-07-18 16:01:10 -0700157 tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="s_tag", value=str(tenant.volt.s_tag))
Scott Baker96746882017-06-09 14:12:15 -0700158 tag.save()
159
160 # VTN-CORD needs a WAN address for the VM, so that the VM can
161 # be configured.
162 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant")
163 if not tags:
164 vrouter = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
Scott Baker80238f82017-07-18 16:01:10 -0700165 subscriber_service=tenant.owner)
Scott Baker96746882017-06-09 14:12:15 -0700166 vrouter.set_attribute("tenant_for_instance_id", instance.id)
167 vrouter.save()
168 # TODO: potential partial failure
Scott Baker80238f82017-07-18 16:01:10 -0700169 tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant", value="%d" % vrouter.id)
Scott Baker96746882017-06-09 14:12:15 -0700170 tag.save()
171
172 instance.no_sync = False # allow the synchronizer to run now
173 super(VSGTenantPolicy, self).save_instance(instance)
174 except:
175 # need to clean up any failures here
176 raise
177
178