blob: a6e747598a84a0e3409c140d31a4194f0203c84a [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
47 This code assumes there is only one vendor installed
48 """
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
56 elif service_instance.leaf_model_name == 'VSPGWCTenant':
57 vendor = VSPGWCVendor.objects.first()
58 if not vendor:
59 raise Exception('No VSPGWC vendors')
60 service_instance.vspgwc_vendor = vendor
61
62 def child_service_instance_from_name(self, name):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050063 service_instances = self.obj.child_serviceinstances.all()
Sapan Bhatia503ad902017-11-13 13:06:17 -050064 service_instances.extend(self.in_memory_instances)
Sapan Bhatia2810e032017-11-03 00:31:00 -040065
66 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050067 service_instance = next(
68 s for s in service_instances if s.leaf_model_name == name)
Sapan Bhatia2810e032017-11-03 00:31:00 -040069 except StopIteration:
70 service_instance = None
71
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050072 return service_instance
Sapan Bhatia2810e032017-11-03 00:31:00 -040073
74 def get_service_for_service_instance(self, si):
75 service = service_of_service_instance(si)
76 service_class = getattr(Slice().stub, service)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050077 service_obj = service_class.objects.first() # There's only one service object
Sapan Bhatia2810e032017-11-03 00:31:00 -040078 return service_obj
79
Sapan Bhatia503ad902017-11-13 13:06:17 -050080 def create_service_instance(self, si):
Sapan Bhatia2810e032017-11-03 00:31:00 -040081 service = self.get_service_for_service_instance(si)
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050082 if not service:
83 raise Exception('No service object for %s' % service)
Sapan Bhatia2810e032017-11-03 00:31:00 -040084
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050085 si_class = getattr(Slice().stub, si)
86 s = si_class(owner=service, name='epc-%s-%d' %
87 (si.lower(), self.obj.id))
88 s.master_serviceinstance = self.obj
Sapan Bhatia2810e032017-11-03 00:31:00 -040089
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050090 self.configure_service_instance(s)
91 s.save()
Sapan Bhatia503ad902017-11-13 13:06:17 -050092
93 self.in_memory_instances.append(s)
Sapan Bhatia2810e032017-11-03 00:31:00 -040094 return s
95
Sapan Bhatia6eeb6942017-11-27 00:27:56 -050096 def add_networks_to_service_instance(self, instance, networks):
97 for n in networks:
98 net = Network.objects.filter(name=n)[0]
99 one_and_only_slice_hopefully = instance.owner.slices.all()[0]
100 ns_object = NetworkSlice.objects.filter(
101 network=net.id, slice=one_and_only_slice_hopefully.id)
102 if not ns_object:
103 ns_object = NetworkSlice(
104 network=net, slice=one_and_only_slice_hopefully)
105 ns_object.save()
106
Sapan Bhatia491a0802017-11-22 19:37:28 -0500107 def create_service_instance_with_networks(self, si_name, networks):
108 instance = self.child_service_instance_from_name(si_name)
109 if not instance:
110 instance = self.create_service_instance(si_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400111
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500112 self.add_networks_to_service_instance(instance, networks)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500113
114 return instance
115
116 def create_link(self, src_instance, dst_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500117 src_service = self.get_service_for_service_instance(
118 src_instance.leaf_model_name)
119 dst_service = self.get_service_for_service_instance(
120 dst_instance.leaf_model_name)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400121
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500122 service_dependency = ServiceDependency.objects.filter(
123 provider_service_id=dst_service.id, subscriber_service_id=src_service.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400124 if not service_dependency:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500125 service_dependency = ServiceDependency(
126 provider_service=dst_service, subscriber_service=src_service)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400127 service_dependency.save()
128
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500129 service_instance_link = ServiceInstanceLink.objects.filter(
130 provider_service_instance_id=dst_instance.id, subscriber_service_instance_id=src_instance.id)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400131 if not service_instance_link:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500132 service_instance_link = ServiceInstanceLink(
133 provider_service_instance=dst_instance, subscriber_service_instance=src_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400134 service_instance_link.save()
135
136 def recursive_create_links(self, blueprint, src):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500137 for node in blueprint:
138 k = node['name']
139 networks = node.get('networks', [])
140 instance = self.create_service_instance_with_networks(k, networks)
141
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500142 if src:
143 self.add_networks_to_service_instance(src, networks)
144 self.create_link(src, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400145
Sapan Bhatia491a0802017-11-22 19:37:28 -0500146 links = node.get('links', [])
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500147 self.recursive_create_links(links, instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400148
Sapan Bhatia491a0802017-11-22 19:37:28 -0500149 def create_epc_network(self, n):
150 network_name = n['name']
151 site_name = self.obj.site.name
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500152 slice_name = '%s_%s' % (
153 site_name, network_name.replace('_network', ''))
Sapan Bhatia491a0802017-11-22 19:37:28 -0500154
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500155 slices = Slice.objects.filter(name=slice_name)
156 if not slices:
157 flavor = Flavor.objects.all()[0]
158 image = Image.objects.all()[0]
159 slice = Slice(name=slice_name, default_isolation="vm", network="noauto",
160 site=self.obj.site, default_flavor=flavor, default_image=image)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500161 slice.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500162 else:
163 slice = slices[0]
Sapan Bhatia491a0802017-11-22 19:37:28 -0500164
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500165 nets = Network.objects.filter(name=network_name)
166 if not nets:
167 template_name = n.get('template', 'public')
168 templates = NetworkTemplate.objects.filter(name=template_name)
169 if not templates:
170 raise Exception('Template %s not found' % template_name)
171
172 net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get(
173 'permit_all_slices', False), template=templates[0], owner=slice)
Sapan Bhatia491a0802017-11-22 19:37:28 -0500174 net.save()
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500175 else:
176 net = nets[0]
177 if net.subnet != n['subnet']:
178 net.subnet = n['subnet']
179 net.save()
Sapan Bhatia491a0802017-11-22 19:37:28 -0500180
181 self.network_map[network_name] = net
182
183 def create_networks(self, networks):
184 for n in networks:
185 self.create_epc_network(n)
186
187 def create_networks_and_child_services(self, service_instance):
Sapan Bhatia2810e032017-11-03 00:31:00 -0400188 self.obj = service_instance
189 # Create service graph based on blueprint
190 chosen_blueprint = service_instance.blueprint
191 try:
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500192 blueprint = next(
193 b for b in blueprints if b['name'] == chosen_blueprint)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400194 except StopIteration:
195 log.error('Chosen blueprint (%s) not found' % chosen_blueprint)
196
Sapan Bhatia491a0802017-11-22 19:37:28 -0500197 self.create_networks(blueprint['networks'])
Sapan Bhatia2810e032017-11-03 00:31:00 -0400198 self.recursive_create_links(blueprint['graph'], None)
199
Sapan Bhatia503ad902017-11-13 13:06:17 -0500200 def handle_create(self, service_instance):
201 self.handle_update(service_instance)
202
Sapan Bhatia2810e032017-11-03 00:31:00 -0400203 def handle_update(self, service_instance):
Sapan Bhatia491a0802017-11-22 19:37:28 -0500204 self.create_networks_and_child_services(service_instance)
Sapan Bhatia2810e032017-11-03 00:31:00 -0400205
Sapan Bhatia2810e032017-11-03 00:31:00 -0400206 def handle_delete(self, service_instance):
Sapan Bhatia6eeb6942017-11-27 00:27:56 -0500207 raise Exception("Not implemented")