blob: 250d6c1abf573ed774a0bfd93f5f9de8fbd52871 [file] [log] [blame]
Matteo Scandoloeb0d11c2017-08-08 13:05:26 -07001
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 Parlakisik638c65f2017-05-31 11:10:24 +030017sync_attributes = ("private_ip", "private_mac",
18 "ceilometer_ip", "ceilometer_mac",
19 "nat_ip", "nat_mac", "ceilometer_port",)
20
21default_attributes = {}
22def __init__(self, *args, **kwargs):
23 ceilometer_services = CeilometerService.get_service_objects().all()
24 if ceilometer_services:
25 self._meta.get_field("provider_service").default = ceilometer_services[0].id
26 super(MonitoringChannel, self).__init__(*args, **kwargs)
27 self.set_attribute("use_same_instance_for_multiple_tenants", True)
28
29def can_update(self, user):
30 #Allow creation of this model instances for non-admin users also
31 return True
32
Sapan Bhatiadf970812017-09-20 06:42:38 -070033def __xos_save_base(self, *args, **kwargs):
Murat Parlakisik638c65f2017-05-31 11:10:24 +030034 if not self.creator:
35 if not getattr(self, "caller", None):
36 # caller must be set when creating a monitoring channel since it creates a slice
37 raise XOSProgrammingError("MonitoringChannel's self.caller was not set")
38 self.creator = self.caller
39 if not self.creator:
40 raise XOSProgrammingError("MonitoringChannel's self.creator was not set")
41
42 if self.pk is None:
43 #Allow only one monitoring channel per user
44 channel_count = sum ( [1 for channel in MonitoringChannel.get_tenant_objects().all() if (channel.creator == self.creator)] )
45 if channel_count > 0:
46 raise XOSValidationError("Already %s channels exist for user Can only create max 1 MonitoringChannel instance per user" % str(channel_count))
47
Murat Parlakisik638c65f2017-05-31 11:10:24 +030048 model_policy_monitoring_channel(self.pk)
Sapan Bhatiadf970812017-09-20 06:42:38 -070049 return False
Murat Parlakisik638c65f2017-05-31 11:10:24 +030050
51def delete(self, *args, **kwargs):
52 self.cleanup_container()
53 super(MonitoringChannel, self).delete(*args, **kwargs)
54
55@property
56def addresses(self):
57 if (not self.id) or (not self.instance):
58 return {}
59
60 addresses = {}
61 for ns in self.instance.ports.all():
62 if "private" in ns.network.name.lower():
63 addresses["private"] = (ns.ip, ns.mac)
64 elif ("nat" in ns.network.name.lower()) or ("management" in ns.network.name.lower()):
65 addresses["nat"] = (ns.ip, ns.mac)
66 #TODO: Do we need this client_access_network. Revisit in VTN context
67 #elif "ceilometer_client_access" in ns.network.labels.lower():
68 # addresses["ceilometer"] = (ns.ip, ns.mac)
69 return addresses
70
71@property
72def nat_ip(self):
73 return self.addresses.get("nat", (None, None))[0]
74
75@property
76def nat_mac(self):
77 return self.addresses.get("nat", (None, None))[1]
78
79@property
80def private_ip(self):
81 return self.addresses.get("private", (None, None))[0]
82
83@property
84def private_mac(self):
85 return self.addresses.get("private", (None, None))[1]
86
87@property
88def ceilometer_ip(self):
89 return self.addresses.get("ceilometer", (None, None))[0]
90
91@property
92def ceilometer_mac(self):
93 return self.addresses.get("ceilometer", (None, None))[1]
94
95@property
96def site_tenant_list(self):
97 tenant_ids = Set()
98 for sp in SitePrivilege.objects.filter(user=self.creator):
99 site = sp.site
100 for cs in site.controllersite.all():
101 if cs.tenant_id:
102 tenant_ids.add(cs.tenant_id)
103 return tenant_ids
104
105@property
106def slice_tenant_list(self):
107 tenant_ids = Set()
108 for sp in SlicePrivilege.objects.filter(user=self.creator):
109 slice = sp.slice
110 for cs in slice.controllerslices.all():
111 if cs.tenant_id:
112 tenant_ids.add(cs.tenant_id)
113 for slice in Slice.objects.filter(creator=self.creator):
114 for cs in slice.controllerslices.all():
115 if cs.tenant_id:
116 tenant_ids.add(cs.tenant_id)
117 if self.creator.is_admin:
118 #TODO: Ceilometer publishes the SDN meters without associating to any tenant IDs.
119 #For now, ceilometer code is changed to pusblish all such meters with tenant
120 #id as "default_admin_tenant". Here add that default tenant as authroized tenant_id
121 #for all admin users.
122 tenant_ids.add("default_admin_tenant")
123 return tenant_ids
124
125@property
126def tenant_list(self):
127 return self.slice_tenant_list | self.site_tenant_list
128
129@property
130def tenant_list_str(self):
131 return ", ".join(self.tenant_list)
132
133@property
134def ceilometer_port(self):
135 # TODO: Find a better logic to choose unique ceilometer port number for each instance
136 if not self.id:
137 return None
138 return 8888+self.id
139
140@property
141def ssh_proxy_tunnel(self):
142 return self.get_attribute("ssh_proxy_tunnel", False)
143
144@ssh_proxy_tunnel.setter
145def ssh_proxy_tunnel(self, value):
146 self.set_attribute("ssh_proxy_tunnel", value)
147
148@property
149def ssh_tunnel_port(self):
150 return self.get_attribute("ssh_tunnel_port")
151
152@ssh_tunnel_port.setter
153def ssh_tunnel_port(self, value):
154 self.set_attribute("ssh_tunnel_port", value)
155
156@property
157def ssh_tunnel_ip(self):
158 return self.get_attribute("ssh_tunnel_ip")
159
160@ssh_tunnel_ip.setter
161def ssh_tunnel_ip(self, value):
162 self.set_attribute("ssh_tunnel_ip", value)
163
164@property
165def ceilometer_url(self):
166 if self.private_ip and self.ceilometer_port:
167 return "http://" + self.private_ip + ":" + str(self.ceilometer_port) + "/"
168 else:
169 return None
170
171@property
172def ceilometer_ssh_proxy_url(self):
173 if self.ssh_proxy_tunnel:
174 if self.ssh_tunnel_ip and self.ssh_tunnel_port:
175 return "http://" + self.ssh_tunnel_ip + ":" + str(self.ssh_tunnel_port) + "/"
176 else:
177 return None
178 else:
179 return None
180
181@property
182def kafka_url(self):
183 ceilometer_services = CeilometerService.get_service_objects().all()
184 if not ceilometer_services:
185 return None
Sapan Bhatiadf970812017-09-20 06:42:38 -0700186 return ceilometer_services[0].kafka_url