blob: aac6f7cb9c9cb9766b1ed048958b57af8a4b4692 [file] [log] [blame]
Sapan Bhatia2810e032017-11-03 00:31:00 -04001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from synchronizers.new_base.modelaccessor import *
Sapan Bhatia503ad902017-11-13 13:06:17 -050016from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import Policy
Sapan Bhatia2810e032017-11-03 00:31:00 -040017from synchronizers.new_base.exceptions import *
18
19from xosconfig import Config
20from multistructlog import create_logger
21
22log = create_logger(Config().get('logging'))
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050023
Sapan Bhatia2810e032017-11-03 00:31:00 -040024blueprints = Config().get('blueprints')
25
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050026
Sapan Bhatia2810e032017-11-03 00:31:00 -040027def service_of_service_instance(si):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050028 if si.endswith('Tenant'):
Sapan Bhatia2810e032017-11-03 00:31:00 -040029 return si[:-len('Tenant')] + 'Service'
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050030 elif si.endswith('ServiceInstance'):
Sapan Bhatia2810e032017-11-03 00:31:00 -040031 return si[:-len('ServiceInstance')] + 'Service'
32 else:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050033 raise Exception(
34 'Could not translate service instance into service: %s' % si)
35
Sapan Bhatia2810e032017-11-03 00:31:00 -040036
Sapan Bhatia503ad902017-11-13 13:06:17 -050037class VEPCServiceInstancePolicy(Policy):
Sapan Bhatia2810e032017-11-03 00:31:00 -040038 model_name = "VEPCServiceInstance"
39
Sapan Bhatia503ad902017-11-13 13:06:17 -050040 def __init__(self):
41 self.in_memory_instances = []
Sapan Bhatia491a0802017-11-22 19:37:28 -050042 self.network_map = {}
43
Sapan Bhatia503ad902017-11-13 13:06:17 -050044 super(VEPCServiceInstancePolicy, self).__init__()
45
Sapan Bhatia2810e032017-11-03 00:31:00 -040046 """TODO: Update the following to not be service-specific
Andy Bavier52b5e0f2017-11-27 15:58:21 -070047 This code assumes there is only one vendor installed
Sapan Bhatia2810e032017-11-03 00:31:00 -040048 """
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050049
Sapan Bhatia2810e032017-11-03 00:31:00 -040050 def configure_service_instance(self, service_instance):
51 if service_instance.leaf_model_name == 'VSPGWUTenant':
52 vendor = VSPGWUVendor.objects.first()
53 if not vendor:
54 raise Exception('No VSPGWU vendors')
55 service_instance.vspgwu_vendor = vendor
Andy Bavier52b5e0f2017-11-27 15:58:21 -070056 service_instance.invalidate_cache('vspgwu_vendor')
Sapan Bhatia2810e032017-11-03 00:31:00 -040057 elif service_instance.leaf_model_name == 'VSPGWCTenant':
58 vendor = VSPGWCVendor.objects.first()
59 if not vendor:
60 raise Exception('No VSPGWC vendors')
61 service_instance.vspgwc_vendor = vendor
Andy Bavier52b5e0f2017-11-27 15:58:21 -070062 service_instance.invalidate_cache('vspgwc_vendor')
Woojoong Kim5d8fb6d2017-12-18 13:58:49 -080063 elif service_instance.leaf_model_name == 'VMMETenant':
64 vendor = VMMEVendor.objects.first()
65 if not vendor:
66 raise Exception('No VMME vendors')
67 service_instance.vmme_vendor = vendor
68 service_instance.invalidate_cache('vmme_vendor')
Andy Bavier52b5e0f2017-11-27 15:58:21 -070069
Sapan Bhatia2810e032017-11-03 00:31:00 -040070
71 def child_service_instance_from_name(self, name):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050072 service_instances = self.obj.child_serviceinstances.all()
Sapan Bhatia503ad902017-11-13 13:06:17 -050073 service_instances.extend(self.in_memory_instances)
Sapan Bhatia2810e032017-11-03 00:31:00 -040074
75 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050076 service_instance = next(
77 s for s in service_instances if s.leaf_model_name == name)
Sapan Bhatia2810e032017-11-03 00:31:00 -040078 except StopIteration:
79 service_instance = None
80
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050081 return service_instance
Sapan Bhatia2810e032017-11-03 00:31:00 -040082
83 def get_service_for_service_instance(self, si):
84 service = service_of_service_instance(si)
85 service_class = getattr(Slice().stub, service)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050086 service_obj = service_class.objects.first() # There's only one service object
Sapan Bhatia2810e032017-11-03 00:31:00 -040087 return service_obj
88
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -050089 def create_service_instance(self, si, node_label = None):
Sapan Bhatia2810e032017-11-03 00:31:00 -040090 service = self.get_service_for_service_instance(si)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050091 if not service:
92 raise Exception('No service object for %s' % service)
Sapan Bhatia2810e032017-11-03 00:31:00 -040093
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050094 si_class = getattr(Slice().stub, si)
95 s = si_class(owner=service, name='epc-%s-%d' %
96 (si.lower(), self.obj.id))
97 s.master_serviceinstance = self.obj
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -050098
99 if node_label:
100 s.node_label = '%s-%d'%(node_label, self.obj.id)
101
Sapan Bhatiadce1b8e2017-12-18 11:00:41 -0500102 s.no_sync = True
Sapan Bhatiaac9bd312017-12-19 11:17:48 -0500103 s.no_policy = True
Andy Bavier52b5e0f2017-11-27 15:58:21 -0700104 s.save()
Sapan Bhatia2810e032017-11-03 00:31:00 -0400105
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500106 self.configure_service_instance(s)
107 s.save()
Sapan Bhatia503ad902017-11-13 13:06:17 -0500108
109 self.in_memory_instances.append(s)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400110 return s
111
Andy Bavier6e37c832017-12-01 14:29:44 -0700112 def add_networks_to_service(self, service, networks):
113 for n in networks:
114 net = Network.objects.filter(name=n)[0]
115 one_and_only_slice_hopefully = service.slices.all()[0]
116 ns_object = NetworkSlice.objects.filter(
117 network=net.id, slice=one_and_only_slice_hopefully.id)
118 if not ns_object:
119 ns_object = NetworkSlice(
120 network=net, slice=one_and_only_slice_hopefully)
121 ns_object.save()
122
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500123 def add_networks_to_service_instance(self, instance, networks):
124 for n in networks:
125 net = Network.objects.filter(name=n)[0]
126 one_and_only_slice_hopefully = instance.owner.slices.all()[0]
127 ns_object = NetworkSlice.objects.filter(
128 network=net.id, slice=one_and_only_slice_hopefully.id)
129 if not ns_object:
130 ns_object = NetworkSlice(
131 network=net, slice=one_and_only_slice_hopefully)
132 ns_object.save()
133
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500134 def create_service_instance_with_networks(self, si_name, networks, node_label = None):
Andy Bavier6e37c832017-12-01 14:29:44 -0700135 service = self.get_service_for_service_instance(si_name)
136 self.add_networks_to_service(service, networks)
137
Sapan Bhatia491a0802017-11-22 19:37:28 -0500138 instance = self.child_service_instance_from_name(si_name)
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500139
Sapan Bhatia491a0802017-11-22 19:37:28 -0500140 if not instance:
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500141 instance = self.create_service_instance(si_name, node_label = node_label)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400142
Sapan Bhatia491a0802017-11-22 19:37:28 -0500143 return instance
144
145 def create_link(self, src_instance, dst_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500146 src_service = self.get_service_for_service_instance(
147 src_instance.leaf_model_name)
148 dst_service = self.get_service_for_service_instance(
149 dst_instance.leaf_model_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400150
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500151 service_dependency = ServiceDependency.objects.filter(
152 provider_service_id=dst_service.id, subscriber_service_id=src_service.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400153 if not service_dependency:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500154 service_dependency = ServiceDependency(
155 provider_service=dst_service, subscriber_service=src_service)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400156 service_dependency.save()
157
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500158 service_instance_link = ServiceInstanceLink.objects.filter(
159 provider_service_instance_id=dst_instance.id, subscriber_service_instance_id=src_instance.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400160 if not service_instance_link:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500161 service_instance_link = ServiceInstanceLink(
162 provider_service_instance=dst_instance, subscriber_service_instance=src_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400163 service_instance_link.save()
164
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500165 def recursive_create_instances_and_links(self, blueprint, src, service_instances):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500166 for node in blueprint:
167 k = node['name']
168 networks = node.get('networks', [])
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500169 links = node.get('links', [])
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500170
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500171 try:
172 node_label = node['node_label']
173 except KeyError:
174 try:
175 node_label = next(l['node_label'] for l in links if l.get('node_label', None))
176 except StopIteration:
177 node_label = None
178
179 instance = self.create_service_instance_with_networks(k, networks, node_label = node_label)
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500180 service_instances.append(instance)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500181
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500182 if src:
183 self.add_networks_to_service_instance(src, networks)
184 self.create_link(src, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400185
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500186 service_instances = self.recursive_create_instances_and_links(links, instance, service_instances)
187
188 return service_instances
189
Sapan Bhatia491a0802017-11-22 19:37:28 -0500190 def create_epc_network(self, n):
191 network_name = n['name']
Andy Bavier52b5e0f2017-11-27 15:58:21 -0700192 site_name = self.obj.site.login_base
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500193 slice_name = '%s_%s' % (
194 site_name, network_name.replace('_network', ''))
Sapan Bhatia491a0802017-11-22 19:37:28 -0500195
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500196 slices = Slice.objects.filter(name=slice_name)
197 if not slices:
198 flavor = Flavor.objects.all()[0]
199 image = Image.objects.all()[0]
200 slice = Slice(name=slice_name, default_isolation="vm", network="noauto",
201 site=self.obj.site, default_flavor=flavor, default_image=image)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500202 slice.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500203 else:
204 slice = slices[0]
Sapan Bhatia491a0802017-11-22 19:37:28 -0500205
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500206 nets = Network.objects.filter(name=network_name)
207 if not nets:
208 template_name = n.get('template', 'public')
209 templates = NetworkTemplate.objects.filter(name=template_name)
210 if not templates:
211 raise Exception('Template %s not found' % template_name)
212
213 net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get(
214 'permit_all_slices', False), template=templates[0], owner=slice)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500215 net.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500216 else:
217 net = nets[0]
218 if net.subnet != n['subnet']:
219 net.subnet = n['subnet']
220 net.save()
Sapan Bhatia491a0802017-11-22 19:37:28 -0500221
222 self.network_map[network_name] = net
223
224 def create_networks(self, networks):
225 for n in networks:
226 self.create_epc_network(n)
227
228 def create_networks_and_child_services(self, service_instance):
Sapan Bhatia2810e032017-11-03 00:31:00 -0400229 self.obj = service_instance
230 # Create service graph based on blueprint
231 chosen_blueprint = service_instance.blueprint
232 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500233 blueprint = next(
234 b for b in blueprints if b['name'] == chosen_blueprint)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400235 except StopIteration:
236 log.error('Chosen blueprint (%s) not found' % chosen_blueprint)
237
Sapan Bhatia491a0802017-11-22 19:37:28 -0500238 self.create_networks(blueprint['networks'])
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500239
240 service_instances = self.recursive_create_instances_and_links(blueprint['graph'], None, [])
241
242 for si in service_instances:
Sapan Bhatiaac9bd312017-12-19 11:17:48 -0500243 si.no_policy = False
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500244 si.no_sync = False
245 si.save()
Sapan Bhatia2810e032017-11-03 00:31:00 -0400246
Sapan Bhatia503ad902017-11-13 13:06:17 -0500247 def handle_create(self, service_instance):
248 self.handle_update(service_instance)
249
Sapan Bhatia2810e032017-11-03 00:31:00 -0400250 def handle_update(self, service_instance):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500251 self.create_networks_and_child_services(service_instance)