Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -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 models_decl import * |
| 18 | from core.models import Service |
| 19 | |
| 20 | class CordSubscriberRoot(CordSubscriberRoot_decl): |
| 21 | class Meta: |
| 22 | proxy = True |
| 23 | |
| 24 | def invalidate_related_objects(self): |
| 25 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 26 | # will run. |
| 27 | |
| 28 | # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented. |
| 29 | |
| 30 | for link in self.subscribed_links.all(): |
| 31 | outer_service_instance = link.provider_service_instance |
| 32 | # TODO: We may need to invalide the vOLT too... |
| 33 | for link in outer_service_instance.subscribed_links.all(): |
| 34 | inner_service_instance = link.provider_service_instance |
| 35 | inner_service_instance.save(update_fields=["updated"]) |
| 36 | |
| 37 | def save(self, *args, **kwargs): |
| 38 | self.validate_unique_service_specific_id(none_okay=True) |
| 39 | |
| 40 | # NOTE setting owner_id |
| 41 | try: |
| 42 | rcord_service = Service.objects.filter(name="rcord")[0] |
| 43 | self.owner_id = rcord_service.id |
| 44 | except IndexError: |
| 45 | raise XOSValidationError("Service RCORD cannot be found, please make sure that the model exists.") |
| 46 | |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 47 | # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its |
| 48 | # Instance. |
| 49 | if not self.creator: |
| 50 | # If we weren't passed an explicit creator, then we will assume the caller is the creator. |
| 51 | if not getattr(self, "caller", None): |
| 52 | raise XOSProgrammingError("CordSubscriberRoot's self.caller was not set") |
| 53 | self.creator = self.caller |
| 54 | |
| 55 | # TODO: What is this for? |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 56 | if (not hasattr(self, 'caller') or not self.caller.is_admin): |
| 57 | if (self.has_field_changed("service_specific_id")): |
| 58 | raise XOSPermissionDenied("You do not have permission to change service_specific_id") |
| 59 | |
| 60 | super(CordSubscriberRoot, self).save(*args, **kwargs) |
| 61 | self.invalidate_related_objects() |
| 62 | return |