blob: 577b3d971895ceb2d40ff7f801ad9348ac626c39 [file] [log] [blame]
Scott Bakera33ccb02018-01-26 13:03:28 -08001
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 Bakere4c28002018-02-28 14:42:48 -080016from django.db.models.fields import NOT_PROVIDED
Matteo Scandolo03e8f602018-05-16 14:16:13 -070017from xos.exceptions import XOSValidationError, XOSMissingField, XOSDuplicateKey
Scott Bakera33ccb02018-01-26 13:03:28 -080018from serviceinstance_decl import *
19
20class 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 Baker234c2712018-03-26 10:25:15 -070027 # TODO: Used by CordSubscriberRoot. Verify whether the usage is necessary.
Scott Bakera33ccb02018-01-26 13:03:28 -080028 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 Scandolo03e8f602018-05-16 14:16:13 -070042 def set_owner(self):
Scott Baker0d2dd982018-02-20 09:27:52 -080043 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 Baker95966a72018-02-26 12:50:58 -080062 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 Bakere4c28002018-02-28 14:42:48 -080069 if (not self.owner_id) and (self._meta.get_field("owner").default) and \
70 (self._meta.get_field("owner").default!=NOT_PROVIDED):
Scott Baker95966a72018-02-26 12:50:58 -080071 self.owner = Service.objects.get(id = self._meta.get_field("owner").default)
Scott Baker0d2dd982018-02-20 09:27:52 -080072
Matteo Scandolo03e8f602018-05-16 14:16:13 -070073 def save(self, *args, **kwargs):
Scott Baker69551b62018-07-25 18:05:00 -070074 # NOTE(CORD-3128): Only set the owner if not in deleted state.
75 if not self.deleted:
76 self.set_owner()
Matteo Scandolo03e8f602018-05-16 14:16:13 -070077
Scott Baker0d2dd982018-02-20 09:27:52 -080078 # 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