blob: 867d5d2609add946503a58e84a87308dd4cb1f84 [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')
63
Sapan Bhatia2810e032017-11-03 00:31:00 -040064
65 def child_service_instance_from_name(self, name):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050066 service_instances = self.obj.child_serviceinstances.all()
Sapan Bhatia503ad902017-11-13 13:06:17 -050067 service_instances.extend(self.in_memory_instances)
Sapan Bhatia2810e032017-11-03 00:31:00 -040068
69 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050070 service_instance = next(
71 s for s in service_instances if s.leaf_model_name == name)
Sapan Bhatia2810e032017-11-03 00:31:00 -040072 except StopIteration:
73 service_instance = None
74
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050075 return service_instance
Sapan Bhatia2810e032017-11-03 00:31:00 -040076
77 def get_service_for_service_instance(self, si):
78 service = service_of_service_instance(si)
79 service_class = getattr(Slice().stub, service)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050080 service_obj = service_class.objects.first() # There's only one service object
Sapan Bhatia2810e032017-11-03 00:31:00 -040081 return service_obj
82
Sapan Bhatia503ad902017-11-13 13:06:17 -050083 def create_service_instance(self, si):
Sapan Bhatia2810e032017-11-03 00:31:00 -040084 service = self.get_service_for_service_instance(si)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050085 if not service:
86 raise Exception('No service object for %s' % service)
Sapan Bhatia2810e032017-11-03 00:31:00 -040087
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050088 si_class = getattr(Slice().stub, si)
89 s = si_class(owner=service, name='epc-%s-%d' %
90 (si.lower(), self.obj.id))
91 s.master_serviceinstance = self.obj
Andy Bavier52b5e0f2017-11-27 15:58:21 -070092 s.save()
Sapan Bhatia2810e032017-11-03 00:31:00 -040093
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050094 self.configure_service_instance(s)
95 s.save()
Sapan Bhatia503ad902017-11-13 13:06:17 -050096
97 self.in_memory_instances.append(s)
Sapan Bhatia2810e032017-11-03 00:31:00 -040098 return s
99
Andy Bavier6e37c832017-12-01 14:29:44 -0700100 def add_networks_to_service(self, service, networks):
101 for n in networks:
102 net = Network.objects.filter(name=n)[0]
103 one_and_only_slice_hopefully = service.slices.all()[0]
104 ns_object = NetworkSlice.objects.filter(
105 network=net.id, slice=one_and_only_slice_hopefully.id)
106 if not ns_object:
107 ns_object = NetworkSlice(
108 network=net, slice=one_and_only_slice_hopefully)
109 ns_object.save()
110
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500111 def add_networks_to_service_instance(self, instance, networks):
112 for n in networks:
113 net = Network.objects.filter(name=n)[0]
114 one_and_only_slice_hopefully = instance.owner.slices.all()[0]
115 ns_object = NetworkSlice.objects.filter(
116 network=net.id, slice=one_and_only_slice_hopefully.id)
117 if not ns_object:
118 ns_object = NetworkSlice(
119 network=net, slice=one_and_only_slice_hopefully)
120 ns_object.save()
121
Sapan Bhatia491a0802017-11-22 19:37:28 -0500122 def create_service_instance_with_networks(self, si_name, networks):
Andy Bavier6e37c832017-12-01 14:29:44 -0700123 service = self.get_service_for_service_instance(si_name)
124 self.add_networks_to_service(service, networks)
125
Sapan Bhatia491a0802017-11-22 19:37:28 -0500126 instance = self.child_service_instance_from_name(si_name)
127 if not instance:
128 instance = self.create_service_instance(si_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400129
Sapan Bhatia491a0802017-11-22 19:37:28 -0500130 return instance
131
132 def create_link(self, src_instance, dst_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500133 src_service = self.get_service_for_service_instance(
134 src_instance.leaf_model_name)
135 dst_service = self.get_service_for_service_instance(
136 dst_instance.leaf_model_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400137
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500138 service_dependency = ServiceDependency.objects.filter(
139 provider_service_id=dst_service.id, subscriber_service_id=src_service.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400140 if not service_dependency:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500141 service_dependency = ServiceDependency(
142 provider_service=dst_service, subscriber_service=src_service)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400143 service_dependency.save()
144
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500145 service_instance_link = ServiceInstanceLink.objects.filter(
146 provider_service_instance_id=dst_instance.id, subscriber_service_instance_id=src_instance.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400147 if not service_instance_link:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500148 service_instance_link = ServiceInstanceLink(
149 provider_service_instance=dst_instance, subscriber_service_instance=src_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400150 service_instance_link.save()
151
152 def recursive_create_links(self, blueprint, src):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500153 for node in blueprint:
154 k = node['name']
155 networks = node.get('networks', [])
156 instance = self.create_service_instance_with_networks(k, networks)
157
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500158 if src:
159 self.add_networks_to_service_instance(src, networks)
160 self.create_link(src, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400161
Sapan Bhatia491a0802017-11-22 19:37:28 -0500162 links = node.get('links', [])
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500163 self.recursive_create_links(links, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400164
Sapan Bhatia491a0802017-11-22 19:37:28 -0500165 def create_epc_network(self, n):
166 network_name = n['name']
Andy Bavier52b5e0f2017-11-27 15:58:21 -0700167 site_name = self.obj.site.login_base
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500168 slice_name = '%s_%s' % (
169 site_name, network_name.replace('_network', ''))
Sapan Bhatia491a0802017-11-22 19:37:28 -0500170
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500171 slices = Slice.objects.filter(name=slice_name)
172 if not slices:
173 flavor = Flavor.objects.all()[0]
174 image = Image.objects.all()[0]
175 slice = Slice(name=slice_name, default_isolation="vm", network="noauto",
176 site=self.obj.site, default_flavor=flavor, default_image=image)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500177 slice.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500178 else:
179 slice = slices[0]
Sapan Bhatia491a0802017-11-22 19:37:28 -0500180
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500181 nets = Network.objects.filter(name=network_name)
182 if not nets:
183 template_name = n.get('template', 'public')
184 templates = NetworkTemplate.objects.filter(name=template_name)
185 if not templates:
186 raise Exception('Template %s not found' % template_name)
187
188 net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get(
189 'permit_all_slices', False), template=templates[0], owner=slice)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500190 net.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500191 else:
192 net = nets[0]
193 if net.subnet != n['subnet']:
194 net.subnet = n['subnet']
195 net.save()
Sapan Bhatia491a0802017-11-22 19:37:28 -0500196
197 self.network_map[network_name] = net
198
199 def create_networks(self, networks):
200 for n in networks:
201 self.create_epc_network(n)
202
203 def create_networks_and_child_services(self, service_instance):
Sapan Bhatia2810e032017-11-03 00:31:00 -0400204 self.obj = service_instance
205 # Create service graph based on blueprint
206 chosen_blueprint = service_instance.blueprint
207 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500208 blueprint = next(
209 b for b in blueprints if b['name'] == chosen_blueprint)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400210 except StopIteration:
211 log.error('Chosen blueprint (%s) not found' % chosen_blueprint)
212
Sapan Bhatia491a0802017-11-22 19:37:28 -0500213 self.create_networks(blueprint['networks'])
Sapan Bhatia2810e032017-11-03 00:31:00 -0400214 self.recursive_create_links(blueprint['graph'], None)
215
Sapan Bhatia503ad902017-11-13 13:06:17 -0500216 def handle_create(self, service_instance):
217 self.handle_update(service_instance)
218
Sapan Bhatia2810e032017-11-03 00:31:00 -0400219 def handle_update(self, service_instance):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500220 self.create_networks_and_child_services(service_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400221
Sapan Bhatia2810e032017-11-03 00:31:00 -0400222 def handle_delete(self, service_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500223 raise Exception("Not implemented")