blob: aa15f9032616021cf0fa6f6f839ec3c0e4714f35 [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
Woojoong Kim1a0c9b02018-01-22 17:42:24 -080056 service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1u
57 service_instance.enodeb_mac_addr = self.obj.enodeb_mac_addr_s1u
Woojoong Kim837b3342018-01-19 11:38:18 -080058 service_instance.appserver_ip_addr = self.obj.appserver_ip_addr
59 service_instance.appserver_mac_addr = self.obj.appserver_mac_addr
Andy Bavier52b5e0f2017-11-27 15:58:21 -070060 service_instance.invalidate_cache('vspgwu_vendor')
Sapan Bhatia2810e032017-11-03 00:31:00 -040061 elif service_instance.leaf_model_name == 'VSPGWCTenant':
62 vendor = VSPGWCVendor.objects.first()
63 if not vendor:
64 raise Exception('No VSPGWC vendors')
65 service_instance.vspgwc_vendor = vendor
Woojoong Kim1a0c9b02018-01-22 17:42:24 -080066 service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1u
67 service_instance.enodeb_mac_addr = self.obj.enodeb_mac_addr_s1u
Woojoong Kim837b3342018-01-19 11:38:18 -080068 service_instance.appserver_ip_addr = self.obj.appserver_ip_addr
69 service_instance.appserver_mac_addr = self.obj.appserver_mac_addr
Andy Bavier52b5e0f2017-11-27 15:58:21 -070070 service_instance.invalidate_cache('vspgwc_vendor')
Woojoong Kim5d8fb6d2017-12-18 13:58:49 -080071 elif service_instance.leaf_model_name == 'VMMETenant':
72 vendor = VMMEVendor.objects.first()
73 if not vendor:
74 raise Exception('No VMME vendors')
75 service_instance.vmme_vendor = vendor
Woojoong Kim1a0c9b02018-01-22 17:42:24 -080076 service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1mme
Woojoong Kim5d8fb6d2017-12-18 13:58:49 -080077 service_instance.invalidate_cache('vmme_vendor')
Andy Bavier8721f5e2018-01-02 14:36:23 -070078 elif service_instance.leaf_model_name == 'VHSSTenant':
79 vendor = VHSSVendor.objects.first()
80 if not vendor:
81 raise Exception('No VHSS vendors')
82 service_instance.vhss_vendor = vendor
83 service_instance.invalidate_cache('vhss_vendor')
Woojoong Kim4b5612b2018-01-11 10:08:02 -080084 elif service_instance.leaf_model_name == 'HSSDBServiceInstance':
85 vendor = HSSDBVendor.objects.first()
86 if not vendor:
87 raise Exception('No HSSDB vendors')
88 service_instance.hssdb_vendor = vendor
89 service_instance.invalidate_cache('hssdb_vendor')
Sapan Bhatia2810e032017-11-03 00:31:00 -040090
91 def child_service_instance_from_name(self, name):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050092 service_instances = self.obj.child_serviceinstances.all()
Sapan Bhatia503ad902017-11-13 13:06:17 -050093 service_instances.extend(self.in_memory_instances)
Sapan Bhatia2810e032017-11-03 00:31:00 -040094
95 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050096 service_instance = next(
97 s for s in service_instances if s.leaf_model_name == name)
Sapan Bhatia2810e032017-11-03 00:31:00 -040098 except StopIteration:
99 service_instance = None
100
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500101 return service_instance
Sapan Bhatia2810e032017-11-03 00:31:00 -0400102
103 def get_service_for_service_instance(self, si):
104 service = service_of_service_instance(si)
105 service_class = getattr(Slice().stub, service)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500106 service_obj = service_class.objects.first() # There's only one service object
Sapan Bhatia2810e032017-11-03 00:31:00 -0400107 return service_obj
108
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500109 def create_service_instance(self, si, node_label = None):
Sapan Bhatia2810e032017-11-03 00:31:00 -0400110 service = self.get_service_for_service_instance(si)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500111 if not service:
112 raise Exception('No service object for %s' % service)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400113
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500114 si_class = getattr(Slice().stub, si)
115 s = si_class(owner=service, name='epc-%s-%d' %
116 (si.lower(), self.obj.id))
117 s.master_serviceinstance = self.obj
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500118
119 if node_label:
120 s.node_label = '%s-%d'%(node_label, self.obj.id)
121
Sapan Bhatiadce1b8e2017-12-18 11:00:41 -0500122 s.no_sync = True
Sapan Bhatiaac9bd312017-12-19 11:17:48 -0500123 s.no_policy = True
Andy Bavier52b5e0f2017-11-27 15:58:21 -0700124 s.save()
Sapan Bhatia2810e032017-11-03 00:31:00 -0400125
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500126 self.configure_service_instance(s)
127 s.save()
Sapan Bhatia503ad902017-11-13 13:06:17 -0500128
129 self.in_memory_instances.append(s)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400130 return s
131
Andy Bavier6e37c832017-12-01 14:29:44 -0700132 def add_networks_to_service(self, service, networks):
133 for n in networks:
134 net = Network.objects.filter(name=n)[0]
135 one_and_only_slice_hopefully = service.slices.all()[0]
136 ns_object = NetworkSlice.objects.filter(
137 network=net.id, slice=one_and_only_slice_hopefully.id)
138 if not ns_object:
139 ns_object = NetworkSlice(
140 network=net, slice=one_and_only_slice_hopefully)
141 ns_object.save()
142
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500143 def add_networks_to_service_instance(self, instance, networks):
144 for n in networks:
145 net = Network.objects.filter(name=n)[0]
146 one_and_only_slice_hopefully = instance.owner.slices.all()[0]
147 ns_object = NetworkSlice.objects.filter(
148 network=net.id, slice=one_and_only_slice_hopefully.id)
149 if not ns_object:
150 ns_object = NetworkSlice(
151 network=net, slice=one_and_only_slice_hopefully)
152 ns_object.save()
153
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500154 def create_service_instance_with_networks(self, si_name, networks, node_label = None):
Andy Bavier6e37c832017-12-01 14:29:44 -0700155 service = self.get_service_for_service_instance(si_name)
156 self.add_networks_to_service(service, networks)
157
Sapan Bhatia491a0802017-11-22 19:37:28 -0500158 instance = self.child_service_instance_from_name(si_name)
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500159
Sapan Bhatia491a0802017-11-22 19:37:28 -0500160 if not instance:
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500161 instance = self.create_service_instance(si_name, node_label = node_label)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400162
Sapan Bhatia491a0802017-11-22 19:37:28 -0500163 return instance
164
165 def create_link(self, src_instance, dst_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500166 src_service = self.get_service_for_service_instance(
167 src_instance.leaf_model_name)
168 dst_service = self.get_service_for_service_instance(
169 dst_instance.leaf_model_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400170
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500171 service_dependency = ServiceDependency.objects.filter(
172 provider_service_id=dst_service.id, subscriber_service_id=src_service.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400173 if not service_dependency:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500174 service_dependency = ServiceDependency(
175 provider_service=dst_service, subscriber_service=src_service)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400176 service_dependency.save()
177
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500178 service_instance_link = ServiceInstanceLink.objects.filter(
179 provider_service_instance_id=dst_instance.id, subscriber_service_instance_id=src_instance.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400180 if not service_instance_link:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500181 service_instance_link = ServiceInstanceLink(
182 provider_service_instance=dst_instance, subscriber_service_instance=src_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400183 service_instance_link.save()
184
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500185 def recursive_create_instances_and_links(self, blueprint, src, service_instances):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500186 for node in blueprint:
187 k = node['name']
188 networks = node.get('networks', [])
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500189 links = node.get('links', [])
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500190
Sapan Bhatia11d0c9a2017-12-10 21:16:34 -0500191 try:
192 node_label = node['node_label']
193 except KeyError:
194 try:
195 node_label = next(l['node_label'] for l in links if l.get('node_label', None))
196 except StopIteration:
197 node_label = None
198
199 instance = self.create_service_instance_with_networks(k, networks, node_label = node_label)
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500200 service_instances.append(instance)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500201
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500202 if src:
203 self.add_networks_to_service_instance(src, networks)
204 self.create_link(src, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400205
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500206 service_instances = self.recursive_create_instances_and_links(links, instance, service_instances)
207
208 return service_instances
209
Sapan Bhatia491a0802017-11-22 19:37:28 -0500210 def create_epc_network(self, n):
211 network_name = n['name']
Andy Bavier52b5e0f2017-11-27 15:58:21 -0700212 site_name = self.obj.site.login_base
Sapan Bhatia491a0802017-11-22 19:37:28 -0500213
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500214 nets = Network.objects.filter(name=network_name)
215 if not nets:
216 template_name = n.get('template', 'public')
Andy Bavier8721f5e2018-01-02 14:36:23 -0700217 try:
218 template = NetworkTemplate.objects.filter(name=template_name)[0]
219 except:
220 raise Exception('Template %s for network %s not found' % (template_name, network_name))
221
222 slice_name = '%s_%s' % (site_name, n['owner'])
223 try:
224 slice = Slice.objects.filter(name=slice_name)[0]
225 except:
226 raise Exception('Owner slice %s for network %s not found' % (slice_name, network_name))
227
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500228
229 net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get(
Andy Bavier8721f5e2018-01-02 14:36:23 -0700230 'permit_all_slices', False), template=template, owner=slice)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500231 net.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500232 else:
233 net = nets[0]
234 if net.subnet != n['subnet']:
235 net.subnet = n['subnet']
236 net.save()
Sapan Bhatia491a0802017-11-22 19:37:28 -0500237
238 self.network_map[network_name] = net
239
240 def create_networks(self, networks):
241 for n in networks:
242 self.create_epc_network(n)
243
244 def create_networks_and_child_services(self, service_instance):
Sapan Bhatia2810e032017-11-03 00:31:00 -0400245 self.obj = service_instance
246 # Create service graph based on blueprint
247 chosen_blueprint = service_instance.blueprint
248 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500249 blueprint = next(
250 b for b in blueprints if b['name'] == chosen_blueprint)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400251 except StopIteration:
252 log.error('Chosen blueprint (%s) not found' % chosen_blueprint)
253
Sapan Bhatia491a0802017-11-22 19:37:28 -0500254 self.create_networks(blueprint['networks'])
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500255
256 service_instances = self.recursive_create_instances_and_links(blueprint['graph'], None, [])
257
258 for si in service_instances:
Sapan Bhatiaac9bd312017-12-19 11:17:48 -0500259 si.no_policy = False
Sapan Bhatia8af7b882017-12-08 15:46:07 -0500260 si.no_sync = False
261 si.save()
Sapan Bhatia2810e032017-11-03 00:31:00 -0400262
Sapan Bhatia503ad902017-11-13 13:06:17 -0500263 def handle_create(self, service_instance):
264 self.handle_update(service_instance)
265
Sapan Bhatia2810e032017-11-03 00:31:00 -0400266 def handle_update(self, service_instance):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500267 self.create_networks_and_child_services(service_instance)