Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | from synchronizers.new_base.modelaccessor import * |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 16 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import Policy |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 17 | from synchronizers.new_base.exceptions import * |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | log = create_logger(Config().get('logging')) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 23 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 24 | blueprints = Config().get('blueprints') |
| 25 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 26 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 27 | def service_of_service_instance(si): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 28 | if si.endswith('Tenant'): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 29 | return si[:-len('Tenant')] + 'Service' |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 30 | elif si.endswith('ServiceInstance'): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 31 | return si[:-len('ServiceInstance')] + 'Service' |
| 32 | else: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 33 | raise Exception( |
| 34 | 'Could not translate service instance into service: %s' % si) |
| 35 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 36 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 37 | class VEPCServiceInstancePolicy(Policy): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 38 | model_name = "VEPCServiceInstance" |
| 39 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 40 | def __init__(self): |
| 41 | self.in_memory_instances = [] |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 42 | self.network_map = {} |
| 43 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 44 | super(VEPCServiceInstancePolicy, self).__init__() |
| 45 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 46 | """TODO: Update the following to not be service-specific |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 47 | This code assumes there is only one vendor installed |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 48 | """ |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 49 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 50 | 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 Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 56 | service_instance.invalidate_cache('vspgwu_vendor') |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 57 | 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 Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 62 | service_instance.invalidate_cache('vspgwc_vendor') |
| 63 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 64 | |
| 65 | def child_service_instance_from_name(self, name): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 66 | service_instances = self.obj.child_serviceinstances.all() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 67 | service_instances.extend(self.in_memory_instances) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 68 | |
| 69 | try: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 70 | service_instance = next( |
| 71 | s for s in service_instances if s.leaf_model_name == name) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 72 | except StopIteration: |
| 73 | service_instance = None |
| 74 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 75 | return service_instance |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 76 | |
| 77 | def get_service_for_service_instance(self, si): |
| 78 | service = service_of_service_instance(si) |
| 79 | service_class = getattr(Slice().stub, service) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 80 | service_obj = service_class.objects.first() # There's only one service object |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 81 | return service_obj |
| 82 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 83 | def create_service_instance(self, si): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 84 | service = self.get_service_for_service_instance(si) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 85 | if not service: |
| 86 | raise Exception('No service object for %s' % service) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 87 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 88 | 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 |
Sapan Bhatia | dce1b8e | 2017-12-18 11:00:41 -0500 | [diff] [blame^] | 92 | s.no_sync = True |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 93 | s.save() |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 94 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 95 | self.configure_service_instance(s) |
| 96 | s.save() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 97 | |
| 98 | self.in_memory_instances.append(s) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 99 | return s |
| 100 | |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame] | 101 | def add_networks_to_service(self, service, networks): |
| 102 | for n in networks: |
| 103 | net = Network.objects.filter(name=n)[0] |
| 104 | one_and_only_slice_hopefully = service.slices.all()[0] |
| 105 | ns_object = NetworkSlice.objects.filter( |
| 106 | network=net.id, slice=one_and_only_slice_hopefully.id) |
| 107 | if not ns_object: |
| 108 | ns_object = NetworkSlice( |
| 109 | network=net, slice=one_and_only_slice_hopefully) |
| 110 | ns_object.save() |
| 111 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 112 | def add_networks_to_service_instance(self, instance, networks): |
| 113 | for n in networks: |
| 114 | net = Network.objects.filter(name=n)[0] |
| 115 | one_and_only_slice_hopefully = instance.owner.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 Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 123 | def create_service_instance_with_networks(self, si_name, networks): |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame] | 124 | service = self.get_service_for_service_instance(si_name) |
| 125 | self.add_networks_to_service(service, networks) |
| 126 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 127 | instance = self.child_service_instance_from_name(si_name) |
| 128 | if not instance: |
| 129 | instance = self.create_service_instance(si_name) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 130 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 131 | return instance |
| 132 | |
| 133 | def create_link(self, src_instance, dst_instance): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 134 | src_service = self.get_service_for_service_instance( |
| 135 | src_instance.leaf_model_name) |
| 136 | dst_service = self.get_service_for_service_instance( |
| 137 | dst_instance.leaf_model_name) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 138 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 139 | service_dependency = ServiceDependency.objects.filter( |
| 140 | provider_service_id=dst_service.id, subscriber_service_id=src_service.id) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 141 | if not service_dependency: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 142 | service_dependency = ServiceDependency( |
| 143 | provider_service=dst_service, subscriber_service=src_service) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 144 | service_dependency.save() |
| 145 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 146 | service_instance_link = ServiceInstanceLink.objects.filter( |
| 147 | provider_service_instance_id=dst_instance.id, subscriber_service_instance_id=src_instance.id) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 148 | if not service_instance_link: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 149 | service_instance_link = ServiceInstanceLink( |
| 150 | provider_service_instance=dst_instance, subscriber_service_instance=src_instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 151 | service_instance_link.save() |
| 152 | |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 153 | def recursive_create_instances_and_links(self, blueprint, src, service_instances): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 154 | for node in blueprint: |
| 155 | k = node['name'] |
| 156 | networks = node.get('networks', []) |
| 157 | instance = self.create_service_instance_with_networks(k, networks) |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 158 | |
| 159 | service_instances.append(instance) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 160 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 161 | if src: |
| 162 | self.add_networks_to_service_instance(src, networks) |
| 163 | self.create_link(src, instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 164 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 165 | links = node.get('links', []) |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 166 | service_instances = self.recursive_create_instances_and_links(links, instance, service_instances) |
| 167 | |
| 168 | return service_instances |
| 169 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 170 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 171 | def create_epc_network(self, n): |
| 172 | network_name = n['name'] |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 173 | site_name = self.obj.site.login_base |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 174 | slice_name = '%s_%s' % ( |
| 175 | site_name, network_name.replace('_network', '')) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 176 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 177 | slices = Slice.objects.filter(name=slice_name) |
| 178 | if not slices: |
| 179 | flavor = Flavor.objects.all()[0] |
| 180 | image = Image.objects.all()[0] |
| 181 | slice = Slice(name=slice_name, default_isolation="vm", network="noauto", |
| 182 | site=self.obj.site, default_flavor=flavor, default_image=image) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 183 | slice.save() |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 184 | else: |
| 185 | slice = slices[0] |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 186 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 187 | nets = Network.objects.filter(name=network_name) |
| 188 | if not nets: |
| 189 | template_name = n.get('template', 'public') |
| 190 | templates = NetworkTemplate.objects.filter(name=template_name) |
| 191 | if not templates: |
| 192 | raise Exception('Template %s not found' % template_name) |
| 193 | |
| 194 | net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get( |
| 195 | 'permit_all_slices', False), template=templates[0], owner=slice) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 196 | net.save() |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 197 | else: |
| 198 | net = nets[0] |
| 199 | if net.subnet != n['subnet']: |
| 200 | net.subnet = n['subnet'] |
| 201 | net.save() |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 202 | |
| 203 | self.network_map[network_name] = net |
| 204 | |
| 205 | def create_networks(self, networks): |
| 206 | for n in networks: |
| 207 | self.create_epc_network(n) |
| 208 | |
| 209 | def create_networks_and_child_services(self, service_instance): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 210 | self.obj = service_instance |
| 211 | # Create service graph based on blueprint |
| 212 | chosen_blueprint = service_instance.blueprint |
| 213 | try: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 214 | blueprint = next( |
| 215 | b for b in blueprints if b['name'] == chosen_blueprint) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 216 | except StopIteration: |
| 217 | log.error('Chosen blueprint (%s) not found' % chosen_blueprint) |
| 218 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 219 | self.create_networks(blueprint['networks']) |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 220 | |
| 221 | service_instances = self.recursive_create_instances_and_links(blueprint['graph'], None, []) |
| 222 | |
| 223 | for si in service_instances: |
| 224 | si.no_sync = False |
| 225 | si.save() |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 226 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 227 | def handle_create(self, service_instance): |
| 228 | self.handle_update(service_instance) |
| 229 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 230 | def handle_update(self, service_instance): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 231 | self.create_networks_and_child_services(service_instance) |