Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [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 | |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 17 | sync_attributes = ("listening_endpoint", ) |
| 18 | |
| 19 | default_attributes = {} |
| 20 | def __init__(self, *args, **kwargs): |
| 21 | sflow_services = SFlowService.get_service_objects().all() |
| 22 | if sflow_services: |
| 23 | self._meta.get_field("provider_service").default = sflow_services[0].id |
| 24 | super(SFlowTenant, self).__init__(*args, **kwargs) |
| 25 | |
| 26 | @property |
| 27 | def creator(self): |
| 28 | from core.models import User |
| 29 | if getattr(self, "cached_creator", None): |
| 30 | return self.cached_creator |
| 31 | creator_id=self.get_attribute("creator_id") |
| 32 | if not creator_id: |
| 33 | return None |
| 34 | users=User.objects.filter(id=creator_id) |
| 35 | if not users: |
| 36 | return None |
| 37 | user=users[0] |
| 38 | self.cached_creator = users[0] |
| 39 | return user |
| 40 | |
| 41 | @creator.setter |
| 42 | def creator(self, value): |
| 43 | if value: |
| 44 | value = value.id |
| 45 | if (value != self.get_attribute("creator_id", None)): |
| 46 | self.cached_creator=None |
| 47 | self.set_attribute("creator_id", value) |
| 48 | |
| 49 | @property |
| 50 | def listening_endpoint(self): |
| 51 | return self.get_attribute("listening_endpoint", None) |
| 52 | |
| 53 | @listening_endpoint.setter |
| 54 | def listening_endpoint(self, value): |
| 55 | if urlparse(value).scheme != 'udp': |
| 56 | raise XOSProgrammingError("SFlowTenant: Only UDP listening endpoint URLs are accepted...valid syntax is: udp://ip:port") |
| 57 | self.set_attribute("listening_endpoint", value) |
| 58 | |
| 59 | def save(self, *args, **kwargs): |
| 60 | if not self.creator: |
| 61 | if not getattr(self, "caller", None): |
| 62 | # caller must be set when creating a SFlow tenant since it creates a slice |
| 63 | raise XOSProgrammingError("SFlowTenant's self.caller was not set") |
| 64 | self.creator = self.caller |
| 65 | if not self.creator: |
| 66 | raise XOSProgrammingError("SFlowTenant's self.creator was not set") |
| 67 | |
| 68 | if not self.listening_endpoint: |
| 69 | raise XOSProgrammingError("SFlowTenant's self.listening_endpoint was not set") |
| 70 | |
| 71 | if self.pk is None: |
| 72 | #Allow only one sflow channel per user and listening_endpoint |
| 73 | channel_count = sum ( [1 for channel in SFlowTenant.get_tenant_objects().all() if ((channel.creator == self.creator) and (channel.listening_endpoint == self.listening_endpoint))] ) |
| 74 | if channel_count > 0: |
| 75 | raise XOSValidationError("Already %s sflow channels exist for user Can only create max 1 tenant per user and listening endpoint" % str(channel_count)) |
| 76 | |
| 77 | super(SFlowTenant, self).save(*args, **kwargs) |
| 78 | |
| 79 | def delete(self, *args, **kwargs): |
| 80 | super(MonitoringChannel, self).delete(*args, **kwargs) |
| 81 | |
| 82 | @property |
| 83 | def authorized_resource_list(self): |
| 84 | return ['all'] |
| 85 | |
| 86 | @property |
| 87 | def authorized_resource_list_str(self): |
| 88 | return ", ".join(self.authorized_resource_list) |