| Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from xos.exceptions import * |
| 17 | from serviceinstance_decl import * |
| 18 | |
| 19 | class ServiceInstance(ServiceInstance_decl): |
| 20 | class Meta: |
| 21 | proxy = True |
| 22 | |
| 23 | def __init__(self, *args, **kwargs): |
| 24 | super(ServiceInstance, self).__init__(*args, **kwargs) |
| 25 | |
| 26 | @property |
| 27 | def tenantattribute_dict(self): |
| 28 | attrs = {} |
| 29 | for attr in self.tenantattributes.all(): |
| 30 | attrs[attr.name] = attr.value |
| 31 | return attrs |
| 32 | |
| 33 | # helper function to be used in subclasses that want to ensure |
| 34 | # service_specific_id is unique |
| 35 | |
| 36 | def validate_unique_service_specific_id(self, none_okay=False): |
| 37 | if not none_okay and (self.service_specific_id is None): |
| 38 | raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={ |
| 39 | "service_specific_id": "cannot be none"}) |
| 40 | |
| 41 | if self.service_specific_id: |
| 42 | conflicts = self.__class__.objects.filter( |
| 43 | service_specific_id=self.service_specific_id) |
| 44 | if self.pk: |
| 45 | conflicts = conflicts.exclude(pk=self.pk) |
| 46 | if conflicts: |
| 47 | raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={ |
| 48 | "service_specific_id": "duplicate key"}) |
| 49 | |
| 50 | def get_subscribed_tenants(self, tenant_class): |
| 51 | """ Return all ServiceInstances of class tenant_class that have a link to this ServiceInstance """ |
| 52 | results=[] |
| 53 | # TODO: Make query more efficient |
| 54 | for si in tenant_class.objects.all(): |
| 55 | for link in si.subscribed_links.all(): |
| 56 | if link.provider_service_instance == self: |
| 57 | results.append(si) |
| 58 | return results |
| 59 | |
| 60 | def get_newest_subscribed_tenant(self, kind): |
| 61 | st = list(self.get_subscribed_tenants(kind)) |
| 62 | if not st: |
| 63 | return None |
| 64 | return sorted(st, key=attrgetter('id'))[0] |
| 65 | |
| Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 66 | def save(self, *args, **kwargs): |
| 67 | if hasattr(self, "OWNER_CLASS_NAME"): |
| 68 | owner_class = self.get_model_class_by_name(self.OWNER_CLASS_NAME) |
| 69 | if not owner_class: |
| 70 | raise XOSValidationError("Cannot find owner class %s" % self.OWNER_CLASS_NAME) |
| 71 | |
| 72 | need_set_owner = True |
| 73 | if self.owner_id: |
| 74 | # Check to see if owner is set to a valid instance of owner_class. If it is, then we already have an |
| 75 | # owner. If it is not, then some other misbehaving class must have altered the ServiceInstance.meta |
| 76 | # to point to its own default (these services are being cleaned up). |
| 77 | if owner_class.objects.filter(id=self.owner_id).exists(): |
| 78 | need_set_owner = False |
| 79 | |
| 80 | if need_set_owner: |
| 81 | owners = owner_class.objects.all() |
| 82 | if not owners: |
| 83 | raise XOSValidationError("Cannot find eligible owner of class %s" % self.OWNER_CLASS_NAME) |
| 84 | |
| 85 | self.owner = owners[0] |
| Scott Baker | 95966a7 | 2018-02-26 12:50:58 -0800 | [diff] [blame] | 86 | else: |
| 87 | # Deal with legacy services that specify their owner as _meta field default. This is a workaround for |
| 88 | # what is probably a django bug (if a SerivceInstance without a default is created before a ServiceInstance |
| 89 | # that does have a default, then the later service's default is not honored by django). |
| 90 | |
| 91 | # TODO: Delete this after all services have been migrated away from using field defaults |
| 92 | |
| 93 | if (not self.owner_id) and (self._meta.get_field("owner").default): |
| 94 | self.owner = Service.objects.get(id = self._meta.get_field("owner").default) |
| Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 95 | |
| 96 | # If the model has a Creator and it's not specified, then attempt to default to the Caller. Caller is |
| 97 | # automatically filled in my the API layer. This code was typically used by ServiceInstances that lead to |
| 98 | # instance creation. |
| 99 | if (hasattr(self, "creator")) and (not self.creator) and (hasattr(self, "caller")) and (self.caller): |
| 100 | self.creator = self.caller |
| 101 | |
| 102 | super(ServiceInstance, self).save(*args, **kwargs) |
| 103 | |
| 104 | |