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 |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 92 | s.save() |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 93 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 94 | self.configure_service_instance(s) |
| 95 | s.save() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 96 | |
| 97 | self.in_memory_instances.append(s) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 98 | return s |
| 99 | |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame^] | 100 | 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 Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 111 | 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 Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 122 | def create_service_instance_with_networks(self, si_name, networks): |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame^] | 123 | service = self.get_service_for_service_instance(si_name) |
| 124 | self.add_networks_to_service(service, networks) |
| 125 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 126 | instance = self.child_service_instance_from_name(si_name) |
| 127 | if not instance: |
| 128 | instance = self.create_service_instance(si_name) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 129 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 130 | return instance |
| 131 | |
| 132 | def create_link(self, src_instance, dst_instance): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 133 | 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 Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 137 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 138 | service_dependency = ServiceDependency.objects.filter( |
| 139 | provider_service_id=dst_service.id, subscriber_service_id=src_service.id) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 140 | if not service_dependency: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 141 | service_dependency = ServiceDependency( |
| 142 | provider_service=dst_service, subscriber_service=src_service) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 143 | service_dependency.save() |
| 144 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 145 | service_instance_link = ServiceInstanceLink.objects.filter( |
| 146 | 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] | 147 | if not service_instance_link: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 148 | service_instance_link = ServiceInstanceLink( |
| 149 | provider_service_instance=dst_instance, subscriber_service_instance=src_instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 150 | service_instance_link.save() |
| 151 | |
| 152 | def recursive_create_links(self, blueprint, src): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 153 | 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 Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 158 | if src: |
| 159 | self.add_networks_to_service_instance(src, networks) |
| 160 | self.create_link(src, instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 161 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 162 | links = node.get('links', []) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 163 | self.recursive_create_links(links, 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 | def create_epc_network(self, n): |
| 166 | network_name = n['name'] |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 167 | site_name = self.obj.site.login_base |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 168 | slice_name = '%s_%s' % ( |
| 169 | site_name, network_name.replace('_network', '')) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 170 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 171 | 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 Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 177 | slice.save() |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 178 | else: |
| 179 | slice = slices[0] |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 180 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 181 | 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 Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 190 | net.save() |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 191 | else: |
| 192 | net = nets[0] |
| 193 | if net.subnet != n['subnet']: |
| 194 | net.subnet = n['subnet'] |
| 195 | net.save() |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 196 | |
| 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 Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 204 | self.obj = service_instance |
| 205 | # Create service graph based on blueprint |
| 206 | chosen_blueprint = service_instance.blueprint |
| 207 | try: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 208 | blueprint = next( |
| 209 | b for b in blueprints if b['name'] == chosen_blueprint) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 210 | except StopIteration: |
| 211 | log.error('Chosen blueprint (%s) not found' % chosen_blueprint) |
| 212 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 213 | self.create_networks(blueprint['networks']) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 214 | self.recursive_create_links(blueprint['graph'], None) |
| 215 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 216 | def handle_create(self, service_instance): |
| 217 | self.handle_update(service_instance) |
| 218 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 219 | def handle_update(self, service_instance): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 220 | self.create_networks_and_child_services(service_instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 221 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 222 | def handle_delete(self, service_instance): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 223 | raise Exception("Not implemented") |