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 | |
Scott Baker | e4c2800 | 2018-02-28 14:42:48 -0800 | [diff] [blame] | 16 | from django.db.models.fields import NOT_PROVIDED |
Matteo Scandolo | 03e8f60 | 2018-05-16 14:16:13 -0700 | [diff] [blame] | 17 | from xos.exceptions import XOSValidationError, XOSMissingField, XOSDuplicateKey |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 18 | from serviceinstance_decl import * |
| 19 | |
| 20 | class ServiceInstance(ServiceInstance_decl): |
| 21 | class Meta: |
| 22 | proxy = True |
| 23 | |
| 24 | def __init__(self, *args, **kwargs): |
| 25 | super(ServiceInstance, self).__init__(*args, **kwargs) |
| 26 | |
Scott Baker | 234c271 | 2018-03-26 10:25:15 -0700 | [diff] [blame] | 27 | # TODO: Used by CordSubscriberRoot. Verify whether the usage is necessary. |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 28 | def validate_unique_service_specific_id(self, none_okay=False): |
| 29 | if not none_okay and (self.service_specific_id is None): |
| 30 | raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={ |
| 31 | "service_specific_id": "cannot be none"}) |
| 32 | |
| 33 | if self.service_specific_id: |
| 34 | conflicts = self.__class__.objects.filter( |
| 35 | service_specific_id=self.service_specific_id) |
| 36 | if self.pk: |
| 37 | conflicts = conflicts.exclude(pk=self.pk) |
| 38 | if conflicts: |
| 39 | raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={ |
| 40 | "service_specific_id": "duplicate key"}) |
| 41 | |
Matteo Scandolo | 03e8f60 | 2018-05-16 14:16:13 -0700 | [diff] [blame] | 42 | def set_owner(self): |
Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 43 | if hasattr(self, "OWNER_CLASS_NAME"): |
| 44 | owner_class = self.get_model_class_by_name(self.OWNER_CLASS_NAME) |
| 45 | if not owner_class: |
| 46 | raise XOSValidationError("Cannot find owner class %s" % self.OWNER_CLASS_NAME) |
| 47 | |
| 48 | need_set_owner = True |
| 49 | if self.owner_id: |
| 50 | # Check to see if owner is set to a valid instance of owner_class. If it is, then we already have an |
| 51 | # owner. If it is not, then some other misbehaving class must have altered the ServiceInstance.meta |
| 52 | # to point to its own default (these services are being cleaned up). |
| 53 | if owner_class.objects.filter(id=self.owner_id).exists(): |
| 54 | need_set_owner = False |
| 55 | |
| 56 | if need_set_owner: |
| 57 | owners = owner_class.objects.all() |
| 58 | if not owners: |
| 59 | raise XOSValidationError("Cannot find eligible owner of class %s" % self.OWNER_CLASS_NAME) |
| 60 | |
| 61 | self.owner = owners[0] |
Scott Baker | 95966a7 | 2018-02-26 12:50:58 -0800 | [diff] [blame] | 62 | else: |
| 63 | # Deal with legacy services that specify their owner as _meta field default. This is a workaround for |
| 64 | # what is probably a django bug (if a SerivceInstance without a default is created before a ServiceInstance |
| 65 | # that does have a default, then the later service's default is not honored by django). |
| 66 | |
| 67 | # TODO: Delete this after all services have been migrated away from using field defaults |
| 68 | |
Scott Baker | e4c2800 | 2018-02-28 14:42:48 -0800 | [diff] [blame] | 69 | if (not self.owner_id) and (self._meta.get_field("owner").default) and \ |
| 70 | (self._meta.get_field("owner").default!=NOT_PROVIDED): |
Scott Baker | 95966a7 | 2018-02-26 12:50:58 -0800 | [diff] [blame] | 71 | self.owner = Service.objects.get(id = self._meta.get_field("owner").default) |
Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 72 | |
Matteo Scandolo | 03e8f60 | 2018-05-16 14:16:13 -0700 | [diff] [blame] | 73 | def save(self, *args, **kwargs): |
Scott Baker | 69551b6 | 2018-07-25 18:05:00 -0700 | [diff] [blame^] | 74 | # NOTE(CORD-3128): Only set the owner if not in deleted state. |
| 75 | if not self.deleted: |
| 76 | self.set_owner() |
Matteo Scandolo | 03e8f60 | 2018-05-16 14:16:13 -0700 | [diff] [blame] | 77 | |
Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 78 | # If the model has a Creator and it's not specified, then attempt to default to the Caller. Caller is |
| 79 | # automatically filled in my the API layer. This code was typically used by ServiceInstances that lead to |
| 80 | # instance creation. |
| 81 | if (hasattr(self, "creator")) and (not self.creator) and (hasattr(self, "caller")) and (self.caller): |
| 82 | self.creator = self.caller |
| 83 | |
| 84 | super(ServiceInstance, self).save(*args, **kwargs) |
| 85 | |
| 86 | |