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 | def __init__(self, *args, **kwargs): |
| 18 | self.cached_target_service = None |
| 19 | self.cached_tenancy_from_target_service = None |
| 20 | self.cached_service_monitoring_agent = None |
| 21 | super(UserServiceMonitoringPublisher, self).__init__(*args, **kwargs) |
| 22 | |
| 23 | def can_update(self, user): |
| 24 | #Don't allow creation of this model instances for non-admin users also |
| 25 | return True |
| 26 | |
| 27 | @property |
| 28 | def target_service(self): |
| 29 | if getattr(self, "cached_target_service", None): |
| 30 | return self.cached_target_service |
| 31 | target_service_id = self.get_attribute("target_service_id") |
| 32 | if not target_service_id: |
| 33 | return None |
| 34 | services = Service.objects.filter(id=target_service_id) |
| 35 | if not services: |
| 36 | return None |
| 37 | target_service = services[0] |
| 38 | self.cached_target_service = target_service |
| 39 | return target_service |
| 40 | |
| 41 | @target_service.setter |
| 42 | def target_service(self, value): |
| 43 | if value: |
| 44 | value = value.id |
| 45 | if (value != self.get_attribute("target_service_id", None)): |
| 46 | self.cached_target_service = None |
| 47 | self.set_attribute("target_service_id", value) |
| 48 | |
| 49 | @property |
| 50 | def tenancy_from_target_service(self): |
| 51 | if getattr(self, "cached_tenancy_from_target_service", None): |
| 52 | return self.cached_tenancy_from_target_service |
| 53 | tenancy_from_target_service_id = self.get_attribute("tenancy_from_target_service_id") |
| 54 | if not tenancy_from_target_service_id: |
| 55 | return None |
| 56 | tenancy_from_target_services = ServiceDependency.objects.filter(id=tenancy_from_target_service_id) |
| 57 | if not tenancy_from_target_services: |
| 58 | return None |
| 59 | tenancy_from_target_service = tenancy_from_target_services[0] |
| 60 | self.cached_tenancy_from_target_service = tenancy_from_target_service |
| 61 | return tenancy_from_target_service |
| 62 | |
| 63 | @tenancy_from_target_service.setter |
| 64 | def tenancy_from_target_service(self, value): |
| 65 | if value: |
| 66 | value = value.id |
| 67 | if (value != self.get_attribute("tenancy_from_target_service_id", None)): |
| 68 | self.cached_tenancy_from_target_service = None |
| 69 | self.set_attribute("tenancy_from_target_service_id", value) |
| 70 | |
| 71 | @property |
| 72 | def service_monitoring_agent(self): |
| 73 | if getattr(self, "cached_service_monitoring_agent", None): |
| 74 | return self.cached_service_monitoring_agent |
| 75 | service_monitoring_agent_id = self.get_attribute("service_monitoring_agent") |
| 76 | if not service_monitoring_agent_id: |
| 77 | return None |
| 78 | service_monitoring_agents = ServiceDependency.objects.filter(id=service_monitoring_agent_id) |
| 79 | if not service_monitoring_agents: |
| 80 | return None |
| 81 | service_monitoring_agent = service_monitoring_agents[0] |
| 82 | self.cached_service_monitoring_agent = service_monitoring_agent |
| 83 | return service_monitoring_agent |
| 84 | |
| 85 | @service_monitoring_agent.setter |
| 86 | def service_monitoring_agent(self, value): |
| 87 | if value: |
| 88 | value = value.id |
| 89 | if (value != self.get_attribute("service_monitoring_agent", None)): |
| 90 | self.cached_service_monitoring_agent = None |
| 91 | self.set_attribute("service_monitoring_agent", value) |
| 92 | |
Sapan Bhatia | 2914953 | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 93 | def __xos_save_base(self, *args, **kwargs): |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 94 | if not self.creator: |
| 95 | if not getattr(self, "caller", None): |
| 96 | # caller must be set when creating a monitoring channel since it creates a slice |
| 97 | raise XOSProgrammingError("UserServiceMonitoringPublisher's self.caller was not set") |
| 98 | self.creator = self.caller |
| 99 | if not self.creator: |
| 100 | raise XOSProgrammingError("UserServiceMonitoringPublisher's self.creator was not set") |
| 101 | |
| 102 | tenancy_from_target_service = None |
| 103 | if self.pk is None: |
| 104 | if self.target_service is None: |
| 105 | raise XOSValidationError("Target service is not specified in UserServiceMonitoringPublisher") |
| 106 | #Allow only one monitoring publisher for a given service |
| 107 | publisher_count = sum ( [1 for publisher in UserServiceMonitoringPublisher.get_tenant_objects().all() if (publisher.target_service.id == self.target_service.id)] ) |
| 108 | if publisher_count > 0: |
| 109 | raise XOSValidationError("Already %s publishers exist for service. Can only create max 1 UserServiceMonitoringPublisher instances" % str(publisher_count)) |
| 110 | #Create Service composition object here |
| 111 | tenancy_from_target_service = ServiceDependency(provider_service = self.provider_service, |
| 112 | subscriber_service = self.target_service) |
| 113 | tenancy_from_target_service.save() |
| 114 | self.tenancy_from_target_service = tenancy_from_target_service |
| 115 | |
| 116 | target_uri = CeilometerService.objects.get(id=self.provider_service.id).ceilometer_rabbit_uri |
| 117 | if target_uri is None: |
| 118 | raise XOSProgrammingError("Unable to get the Target_URI for Monitoring Agent") |
| 119 | service_monitoring_agent = ServiceMonitoringAgentInfo(service = self.target_service, |
| 120 | target_uri = target_uri) |
| 121 | service_monitoring_agent.save() |
| 122 | self.service_monitoring_agent = service_monitoring_agent |
| 123 | |
| 124 | try: |
| 125 | super(UserServiceMonitoringPublisher, self).save(*args, **kwargs) |
| 126 | except: |
| 127 | if tenancy_from_target_service: |
| 128 | tenancy_from_target_service.delete() |
| 129 | if service_monitoring_agent: |
| 130 | service_monitoring_agent.delete() |
Sapan Bhatia | 2914953 | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 131 | raise |
| 132 | |
| 133 | return True # Indicate that we called super.save() |