blob: 10abd3fcb0583ca67e126a1939a29d546a71f33f [file] [log] [blame]
Scott Bakera33ccb02018-01-26 13:03:28 -08001# 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 Bakere4c28002018-02-28 14:42:48 -080015from django.db.models.fields import NOT_PROVIDED
Matteo Scandolo03e8f602018-05-16 14:16:13 -070016from xos.exceptions import XOSValidationError, XOSMissingField, XOSDuplicateKey
Scott Bakera33ccb02018-01-26 13:03:28 -080017from serviceinstance_decl import *
18
Zack Williams045b63d2019-01-22 16:30:57 -070019
Scott Bakera33ccb02018-01-26 13:03:28 -080020class 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):
Zack Williams045b63d2019-01-22 16:30:57 -070030 raise XOSMissingField(
31 "subscriber_specific_id is None, and it's a required field",
32 fields={"service_specific_id": "cannot be none"},
33 )
Scott Bakera33ccb02018-01-26 13:03:28 -080034
35 if self.service_specific_id:
36 conflicts = self.__class__.objects.filter(
Zack Williams045b63d2019-01-22 16:30:57 -070037 service_specific_id=self.service_specific_id
38 )
Scott Bakera33ccb02018-01-26 13:03:28 -080039 if self.pk:
40 conflicts = conflicts.exclude(pk=self.pk)
41 if conflicts:
Zack Williams045b63d2019-01-22 16:30:57 -070042 raise XOSDuplicateKey(
43 "service_specific_id %s already exists" % self.service_specific_id,
44 fields={"service_specific_id": "duplicate key"},
45 )
Scott Bakera33ccb02018-01-26 13:03:28 -080046
Matteo Scandolo03e8f602018-05-16 14:16:13 -070047 def set_owner(self):
Scott Baker0d2dd982018-02-20 09:27:52 -080048 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 Williams045b63d2019-01-22 16:30:57 -070051 raise XOSValidationError(
52 "Cannot find owner class %s" % self.OWNER_CLASS_NAME
53 )
Scott Baker0d2dd982018-02-20 09:27:52 -080054
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 Williams045b63d2019-01-22 16:30:57 -070066 raise XOSValidationError(
67 "Cannot find eligible owner of class %s" % self.OWNER_CLASS_NAME
68 )
Scott Baker0d2dd982018-02-20 09:27:52 -080069
70 self.owner = owners[0]
Scott Baker95966a72018-02-26 12:50:58 -080071 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 Williams045b63d2019-01-22 16:30:57 -070078 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 Baker0d2dd982018-02-20 09:27:52 -080086
Matteo Scandolo03e8f602018-05-16 14:16:13 -070087 def save(self, *args, **kwargs):
Scott Baker69551b62018-07-25 18:05:00 -070088 # NOTE(CORD-3128): Only set the owner if not in deleted state.
89 if not self.deleted:
90 self.set_owner()
Matteo Scandolo03e8f602018-05-16 14:16:13 -070091
Scott Baker0d2dd982018-02-20 09:27:52 -080092 # 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 Williams045b63d2019-01-22 16:30:57 -070095 if (
96 (hasattr(self, "creator"))
97 and (not self.creator)
98 and (hasattr(self, "caller"))
99 and (self.caller)
100 ):
Scott Baker0d2dd982018-02-20 09:27:52 -0800101 self.creator = self.caller
102
103 super(ServiceInstance, self).save(*args, **kwargs)