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 |
Woojoong Kim | 1a0c9b0 | 2018-01-22 17:42:24 -0800 | [diff] [blame] | 56 | service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1u |
| 57 | service_instance.enodeb_mac_addr = self.obj.enodeb_mac_addr_s1u |
Woojoong Kim | 837b334 | 2018-01-19 11:38:18 -0800 | [diff] [blame] | 58 | service_instance.appserver_ip_addr = self.obj.appserver_ip_addr |
| 59 | service_instance.appserver_mac_addr = self.obj.appserver_mac_addr |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 60 | service_instance.invalidate_cache('vspgwu_vendor') |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 61 | 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 Kim | 1a0c9b0 | 2018-01-22 17:42:24 -0800 | [diff] [blame] | 66 | service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1u |
| 67 | service_instance.enodeb_mac_addr = self.obj.enodeb_mac_addr_s1u |
Woojoong Kim | 837b334 | 2018-01-19 11:38:18 -0800 | [diff] [blame] | 68 | service_instance.appserver_ip_addr = self.obj.appserver_ip_addr |
| 69 | service_instance.appserver_mac_addr = self.obj.appserver_mac_addr |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 70 | service_instance.invalidate_cache('vspgwc_vendor') |
Woojoong Kim | 5d8fb6d | 2017-12-18 13:58:49 -0800 | [diff] [blame] | 71 | 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 Kim | 1a0c9b0 | 2018-01-22 17:42:24 -0800 | [diff] [blame] | 76 | service_instance.enodeb_ip_addr = self.obj.enodeb_ip_addr_s1mme |
Woojoong Kim | 5d8fb6d | 2017-12-18 13:58:49 -0800 | [diff] [blame] | 77 | service_instance.invalidate_cache('vmme_vendor') |
Andy Bavier | 8721f5e | 2018-01-02 14:36:23 -0700 | [diff] [blame] | 78 | 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 Kim | 4b5612b | 2018-01-11 10:08:02 -0800 | [diff] [blame] | 84 | 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 Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 90 | |
| 91 | def child_service_instance_from_name(self, name): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 92 | service_instances = self.obj.child_serviceinstances.all() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 93 | service_instances.extend(self.in_memory_instances) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 94 | |
| 95 | try: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 96 | service_instance = next( |
| 97 | s for s in service_instances if s.leaf_model_name == name) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 98 | except StopIteration: |
| 99 | service_instance = None |
| 100 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 101 | return service_instance |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 102 | |
| 103 | def get_service_for_service_instance(self, si): |
| 104 | service = service_of_service_instance(si) |
| 105 | service_class = getattr(Slice().stub, service) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 106 | service_obj = service_class.objects.first() # There's only one service object |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 107 | return service_obj |
| 108 | |
Sapan Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 109 | def create_service_instance(self, si, node_label = None): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 110 | service = self.get_service_for_service_instance(si) |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 111 | if not service: |
| 112 | raise Exception('No service object for %s' % service) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 113 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 114 | 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 Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 118 | |
| 119 | if node_label: |
| 120 | s.node_label = '%s-%d'%(node_label, self.obj.id) |
| 121 | |
Sapan Bhatia | dce1b8e | 2017-12-18 11:00:41 -0500 | [diff] [blame] | 122 | s.no_sync = True |
Sapan Bhatia | ac9bd31 | 2017-12-19 11:17:48 -0500 | [diff] [blame] | 123 | s.no_policy = True |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 124 | s.save() |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 125 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 126 | self.configure_service_instance(s) |
| 127 | s.save() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 128 | |
| 129 | self.in_memory_instances.append(s) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 130 | return s |
| 131 | |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame] | 132 | 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 Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 143 | 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 Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 154 | def create_service_instance_with_networks(self, si_name, networks, node_label = None): |
Andy Bavier | 6e37c83 | 2017-12-01 14:29:44 -0700 | [diff] [blame] | 155 | service = self.get_service_for_service_instance(si_name) |
| 156 | self.add_networks_to_service(service, networks) |
| 157 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 158 | instance = self.child_service_instance_from_name(si_name) |
Sapan Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 159 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 160 | if not instance: |
Sapan Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 161 | instance = self.create_service_instance(si_name, node_label = node_label) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 162 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 163 | return instance |
| 164 | |
| 165 | def create_link(self, src_instance, dst_instance): |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 166 | 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 Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 170 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 171 | service_dependency = ServiceDependency.objects.filter( |
| 172 | provider_service_id=dst_service.id, subscriber_service_id=src_service.id) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 173 | if not service_dependency: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 174 | service_dependency = ServiceDependency( |
| 175 | provider_service=dst_service, subscriber_service=src_service) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 176 | service_dependency.save() |
| 177 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 178 | service_instance_link = ServiceInstanceLink.objects.filter( |
| 179 | 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] | 180 | if not service_instance_link: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 181 | service_instance_link = ServiceInstanceLink( |
| 182 | provider_service_instance=dst_instance, subscriber_service_instance=src_instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 183 | service_instance_link.save() |
| 184 | |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 185 | def recursive_create_instances_and_links(self, blueprint, src, service_instances): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 186 | for node in blueprint: |
| 187 | k = node['name'] |
| 188 | networks = node.get('networks', []) |
Sapan Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 189 | links = node.get('links', []) |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 190 | |
Sapan Bhatia | 11d0c9a | 2017-12-10 21:16:34 -0500 | [diff] [blame] | 191 | 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 Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 200 | service_instances.append(instance) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 201 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 202 | if src: |
| 203 | self.add_networks_to_service_instance(src, networks) |
| 204 | self.create_link(src, instance) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 205 | |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 206 | service_instances = self.recursive_create_instances_and_links(links, instance, service_instances) |
| 207 | |
| 208 | return service_instances |
| 209 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 210 | def create_epc_network(self, n): |
| 211 | network_name = n['name'] |
Andy Bavier | 52b5e0f | 2017-11-27 15:58:21 -0700 | [diff] [blame] | 212 | site_name = self.obj.site.login_base |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 213 | |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 214 | nets = Network.objects.filter(name=network_name) |
| 215 | if not nets: |
| 216 | template_name = n.get('template', 'public') |
Andy Bavier | 8721f5e | 2018-01-02 14:36:23 -0700 | [diff] [blame] | 217 | 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 Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 228 | |
| 229 | net = Network(name=network_name, subnet=n['subnet'], permit_all_slices=n.get( |
Andy Bavier | 8721f5e | 2018-01-02 14:36:23 -0700 | [diff] [blame] | 230 | 'permit_all_slices', False), template=template, owner=slice) |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 231 | net.save() |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 232 | else: |
| 233 | net = nets[0] |
| 234 | if net.subnet != n['subnet']: |
| 235 | net.subnet = n['subnet'] |
| 236 | net.save() |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 237 | |
| 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 Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 245 | self.obj = service_instance |
| 246 | # Create service graph based on blueprint |
| 247 | chosen_blueprint = service_instance.blueprint |
| 248 | try: |
Sapan Bhatia | 6eeb694 | 2017-11-27 00:27:56 -0500 | [diff] [blame] | 249 | blueprint = next( |
| 250 | b for b in blueprints if b['name'] == chosen_blueprint) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 251 | except StopIteration: |
| 252 | log.error('Chosen blueprint (%s) not found' % chosen_blueprint) |
| 253 | |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 254 | self.create_networks(blueprint['networks']) |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 255 | |
| 256 | service_instances = self.recursive_create_instances_and_links(blueprint['graph'], None, []) |
| 257 | |
| 258 | for si in service_instances: |
Sapan Bhatia | ac9bd31 | 2017-12-19 11:17:48 -0500 | [diff] [blame] | 259 | si.no_policy = False |
Sapan Bhatia | 8af7b88 | 2017-12-08 15:46:07 -0500 | [diff] [blame] | 260 | si.no_sync = False |
| 261 | si.save() |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 262 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 263 | def handle_create(self, service_instance): |
| 264 | self.handle_update(service_instance) |
| 265 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 266 | def handle_update(self, service_instance): |
Sapan Bhatia | 491a080 | 2017-11-22 19:37:28 -0500 | [diff] [blame] | 267 | self.create_networks_and_child_services(service_instance) |