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')) |
| 23 | blueprints = Config().get('blueprints') |
| 24 | |
| 25 | def service_of_service_instance(si): |
| 26 | if si.endswith('Tenant'): |
| 27 | return si[:-len('Tenant')] + 'Service' |
| 28 | elif si.endswith('ServiceInstance'): |
| 29 | return si[:-len('ServiceInstance')] + 'Service' |
| 30 | else: |
| 31 | raise Exception('Could not translate service instance into service: %s'%si) |
| 32 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 33 | class VEPCServiceInstancePolicy(Policy): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 34 | model_name = "VEPCServiceInstance" |
| 35 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 36 | def __init__(self): |
| 37 | self.in_memory_instances = [] |
| 38 | super(VEPCServiceInstancePolicy, self).__init__() |
| 39 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 40 | """TODO: Update the following to not be service-specific |
| 41 | This code assumes there is only one vendor installed |
| 42 | """ |
| 43 | def configure_service_instance(self, service_instance): |
| 44 | if service_instance.leaf_model_name == 'VSPGWUTenant': |
| 45 | vendor = VSPGWUVendor.objects.first() |
| 46 | if not vendor: |
| 47 | raise Exception('No VSPGWU vendors') |
| 48 | service_instance.vspgwu_vendor = vendor |
| 49 | elif service_instance.leaf_model_name == 'VSPGWCTenant': |
| 50 | vendor = VSPGWCVendor.objects.first() |
| 51 | if not vendor: |
| 52 | raise Exception('No VSPGWC vendors') |
| 53 | service_instance.vspgwc_vendor = vendor |
| 54 | |
| 55 | def child_service_instance_from_name(self, name): |
| 56 | service_instances = self.obj.child_serviceinstances.all() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 57 | service_instances.extend(self.in_memory_instances) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 58 | |
| 59 | try: |
| 60 | service_instance = next(s for s in service_instances if s.leaf_model_name == name) |
| 61 | except StopIteration: |
| 62 | service_instance = None |
| 63 | |
| 64 | return service_instance |
| 65 | |
| 66 | def get_service_for_service_instance(self, si): |
| 67 | service = service_of_service_instance(si) |
| 68 | service_class = getattr(Slice().stub, service) |
| 69 | service_obj = service_class.objects.first() # There's only one service object |
| 70 | return service_obj |
| 71 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 72 | def create_service_instance(self, si): |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 73 | service = self.get_service_for_service_instance(si) |
| 74 | if not service: |
| 75 | raise Exception('No service object for %s'%service) |
| 76 | |
| 77 | si_class = getattr(Slice().stub, si) |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 78 | s = si_class(owner = service, name = 'epc-%s-%d'%(si.lower(), self.obj.id)) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 79 | s.master_serviceinstance = self.obj |
| 80 | |
| 81 | self.configure_service_instance(s) |
| 82 | s.save() |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 83 | |
| 84 | self.in_memory_instances.append(s) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 85 | return s |
| 86 | |
| 87 | def create_link(self, src, dst): |
| 88 | src_instance = self.child_service_instance_from_name(src) |
| 89 | if not src_instance: |
| 90 | src_instance = self.create_service_instance(src) |
| 91 | |
| 92 | dst_instance = self.child_service_instance_from_name(dst) |
| 93 | if not dst_instance: |
| 94 | dst_instance = self.create_service_instance(dst) |
| 95 | |
| 96 | src_service = self.get_service_for_service_instance(src) |
| 97 | dst_service = self.get_service_for_service_instance(dst) |
| 98 | |
| 99 | service_dependency = ServiceDependency.objects.filter(provider_service_id = dst_service.id, subscriber_service_id = src_service.id) |
| 100 | if not service_dependency: |
| 101 | service_dependency = ServiceDependency(provider_service = dst_service, subscriber_service = src_service) |
| 102 | service_dependency.save() |
| 103 | |
| 104 | service_instance_link = ServiceInstanceLink.objects.filter(provider_service_instance_id = dst_instance.id, subscriber_service_instance_id = src_instance.id) |
| 105 | if not service_instance_link: |
| 106 | service_instance_link = ServiceInstanceLink(provider_service_instance = dst_instance, subscriber_service_instance = src_instance) |
| 107 | service_instance_link.save() |
| 108 | |
| 109 | def recursive_create_links(self, blueprint, src): |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 110 | for k, v in blueprint.iteritems(): |
| 111 | if src: |
| 112 | self.create_link(src, k) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 113 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 114 | if isinstance(v, dict): |
| 115 | self.recursive_create_links(v, k) |
| 116 | else: |
| 117 | self.create_link(k, v) |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 118 | |
| 119 | def create_child_services(self, service_instance): |
| 120 | self.obj = service_instance |
| 121 | # Create service graph based on blueprint |
| 122 | chosen_blueprint = service_instance.blueprint |
| 123 | try: |
| 124 | blueprint = next(b for b in blueprints if b['name'] == chosen_blueprint) |
| 125 | except StopIteration: |
| 126 | log.error('Chosen blueprint (%s) not found' % chosen_blueprint) |
| 127 | |
| 128 | self.recursive_create_links(blueprint['graph'], None) |
| 129 | |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 130 | def handle_create(self, service_instance): |
| 131 | self.handle_update(service_instance) |
| 132 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 133 | def handle_update(self, service_instance): |
| 134 | self.create_child_services(service_instance) |
| 135 | |
Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame] | 136 | def handle_delete(self, service_instance): |
Sapan Bhatia | 503ad90 | 2017-11-13 13:06:17 -0500 | [diff] [blame] | 137 | raise Exception("Not implemented") |