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