blob: 0a1f94733155d3a31ac88e508eaf57d195b1d784 [file] [log] [blame]
lavado456d0f32019-11-15 17:04:02 -05001# -*- coding: utf-8 -*-
2
bravof2fe71f22021-01-27 21:22:19 -03003# Copyright 2021 Whitestack, LLC
lavado456d0f32019-11-15 17:04:02 -05004# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20# For those usages not covered by the Apache License, Version 2.0 please
bravof960d0462020-12-17 17:27:29 -030021# contact: glavado@whitestack.com or fbravo@whitestack.com
lavado456d0f32019-11-15 17:04:02 -050022##
23import logging
24
25from osm_mon.core.common_db import CommonDbClient
26from osm_mon.core.config import Config
bravof2fe71f22021-01-27 21:22:19 -030027from osm_mon.core.keystone import KeystoneConnection
bravof396648b2020-03-31 18:42:45 -030028from osm_mon.dashboarder.backends.grafana import GrafanaBackend
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000029from osm_mon import __path__ as mon_path
bravof5ac54152021-03-08 16:50:37 -030030from osm_mon.core.utils import find_in_list, create_filter_from_nsr
lavado456d0f32019-11-15 17:04:02 -050031
32log = logging.getLogger(__name__)
33
34
35class DashboarderService:
36 def __init__(self, config: Config):
37 self.conf = config
38 self.common_db = CommonDbClient(self.conf)
bravof396648b2020-03-31 18:42:45 -030039 self.grafana = GrafanaBackend(self.conf)
lavado456d0f32019-11-15 17:04:02 -050040
garciadeblas8e4179f2021-05-14 16:47:03 +020041 if bool(self.conf.get("keystone", "enabled")):
bravof2fe71f22021-01-27 21:22:19 -030042 self.keystone = KeystoneConnection(self.conf)
43 else:
44 self.keystone = None
45
lavado456d0f32019-11-15 17:04:02 -050046 def create_dashboards(self):
47 # TODO lavado: migrate these methods to mongo change streams
48 # Lists all dashboards and OSM resources for later comparisons
bravof396648b2020-03-31 18:42:45 -030049 dashboard_uids = self.grafana.get_all_dashboard_uids()
lavado456d0f32019-11-15 17:04:02 -050050 osm_resource_uids = []
bravof2fe71f22021-01-27 21:22:19 -030051 projects = []
52
53 # Check if keystone is the auth/projects backend and get projects from there
54 if self.keystone:
55 try:
56 projects.extend(
garciadeblas8e4179f2021-05-14 16:47:03 +020057 map(
58 lambda project: {"_id": project.id, "name": project.name},
59 self.keystone.getProjects(),
60 )
bravof2fe71f22021-01-27 21:22:19 -030061 )
62 except Exception:
garciadeblas8e4179f2021-05-14 16:47:03 +020063 log.error("Cannot retrieve projects from keystone")
Atul Agarwal7af74e42021-03-16 11:02:14 +000064 else:
65 projects.extend(self.common_db.get_projects())
lavado456d0f32019-11-15 17:04:02 -050066
67 # Reads existing project list and creates a dashboard for each
lavado456d0f32019-11-15 17:04:02 -050068 for project in projects:
garciadeblas8e4179f2021-05-14 16:47:03 +020069 project_id = project["_id"]
lavado456d0f32019-11-15 17:04:02 -050070 # Collect Project IDs for periodical dashboard clean-up
71 osm_resource_uids.append(project_id)
garciadeblas8e4179f2021-05-14 16:47:03 +020072 dashboard_path = "{}/dashboarder/templates/project_scoped.json".format(
73 mon_path[0]
74 )
lavado456d0f32019-11-15 17:04:02 -050075 if project_id not in dashboard_uids:
garciadeblas8e4179f2021-05-14 16:47:03 +020076 project_name = project["name"]
palsus0ab64072021-02-09 18:23:03 +000077 if project_name != "admin":
78 # Create project folder in Grafana only if user is not admin.
79 # Admin user's dashboard will be created in default folder
80 self.grafana.create_grafana_folders(project_name)
garciadeblas8e4179f2021-05-14 16:47:03 +020081 self.grafana.create_dashboard(project_id, project_name, dashboard_path)
82 log.debug("Created dashboard for Project: %s", project_id)
lavado456d0f32019-11-15 17:04:02 -050083 else:
garciadeblas8e4179f2021-05-14 16:47:03 +020084 log.debug("Dashboard already exists")
lavado456d0f32019-11-15 17:04:02 -050085
86 # Reads existing NS list and creates a dashboard for each
87 # TODO lavado: only create for ACTIVE NSRs
88 nsrs = self.common_db.get_nsrs()
89 for nsr in nsrs:
garciadeblas8e4179f2021-05-14 16:47:03 +020090 nsr_id = nsr["_id"]
91 dashboard_path = "{}/dashboarder/templates/ns_scoped.json".format(
92 mon_path[0]
93 )
lavado456d0f32019-11-15 17:04:02 -050094 # Collect NS IDs for periodical dashboard clean-up
95 osm_resource_uids.append(nsr_id)
96 # Check if the NSR's VNFDs contain metrics
bravof12fda452020-12-17 10:21:22 -030097 # Only one DF at the moment, support for this feature is comming in the future
garciadeblas8e4179f2021-05-14 16:47:03 +020098 vnfds_profiles = nsr["nsd"]["df"][0]["vnf-profile"]
bravof12fda452020-12-17 10:21:22 -030099 for vnf_profile in vnfds_profiles:
lavado456d0f32019-11-15 17:04:02 -0500100 try:
garciadeblas8e4179f2021-05-14 16:47:03 +0200101 vnfd = self.common_db.get_vnfd_by_id(
102 vnf_profile["vnfd-id"], create_filter_from_nsr(nsr)
103 )
lavado456d0f32019-11-15 17:04:02 -0500104 # If there are metrics, create dashboard (if exists)
garciadeblas8e4179f2021-05-14 16:47:03 +0200105 vdu_found = find_in_list(
106 vnfd["vdu"], lambda a_vdu: "monitoring-parameter" in a_vdu
107 )
bravof960d0462020-12-17 17:27:29 -0300108 if vdu_found:
lavado456d0f32019-11-15 17:04:02 -0500109 if nsr_id not in dashboard_uids:
garciadeblas8e4179f2021-05-14 16:47:03 +0200110 nsr_name = nsr["name"]
agarwalat85a91852020-10-08 12:24:47 +0000111 project_id = nsr["_admin"]["projects_read"][0]
palsus0ab64072021-02-09 18:23:03 +0000112 try:
113 # Get project details from commondb
114 project_details = self.common_db.get_project(project_id)
115 project_name = project_details["name"]
116 except Exception as e:
117 # Project not found in commondb
118 if self.keystone:
119 # Serach project in keystone
120 for project in projects:
garciadeblas8e4179f2021-05-14 16:47:03 +0200121 if project_id == project["_id"]:
palsus0ab64072021-02-09 18:23:03 +0000122 project_name = project["name"]
123 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200124 log.info("Project %s not found", project_id)
125 log.debug("Exception %s" % e)
126 self.grafana.create_dashboard(
127 nsr_id, nsr_name, dashboard_path, project_name
128 )
129 log.debug("Created dashboard for NS: %s", nsr_id)
lavado456d0f32019-11-15 17:04:02 -0500130 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200131 log.debug("Dashboard already exists")
lavado456d0f32019-11-15 17:04:02 -0500132 break
133 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200134 log.debug("NS does not has metrics")
lavado456d0f32019-11-15 17:04:02 -0500135 except Exception:
136 log.exception("VNFD is not valid or has been renamed")
137 continue
138
139 # Delete obsolete dashboards
140 for dashboard_uid in dashboard_uids:
141 if dashboard_uid not in osm_resource_uids:
bravof396648b2020-03-31 18:42:45 -0300142 self.grafana.delete_dashboard(dashboard_uid)
garciadeblas8e4179f2021-05-14 16:47:03 +0200143 log.debug("Deleted obsolete dashboard: %s", dashboard_uid)
lavado456d0f32019-11-15 17:04:02 -0500144 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200145 log.debug("All dashboards in use")
agarwalat85a91852020-10-08 12:24:47 +0000146
147 def create_grafana_user(self, user):
148 self.grafana.create_grafana_users(user)
149
Atul Agarwal46dc3bd2021-08-27 12:33:57 +0000150 def delete_non_existing_users(self):
151 if self.keystone:
152 # Get users from keystone
153 users = self.keystone.getUsers()
154 usernames = []
155 for user in users:
156 usernames.append(user.name)
157 grafana_users = self.grafana.get_grafana_users()
158 users_to_be_deleted = list(set(grafana_users) - set(usernames))
159 for grafana_user in users_to_be_deleted:
160 self.grafana.delete_grafana_users(grafana_user)
161
garciadeblas8e4179f2021-05-14 16:47:03 +0200162 def create_grafana_team_member(
163 self, project_data, userid=None, project_list=None, user=None
164 ):
Atul Agarwal806fe452021-03-15 09:16:09 +0000165 if user:
166 user_name = user
167 else:
168 try:
169 # Get user details from commondb
170 user = self.common_db.get_user_by_id(userid)
171 user_name = user["username"]
172 except Exception as e:
173 # User not found in commondb
174 if self.keystone:
175 # Search user in keystone
176 user = self.keystone.getUserById(userid)
177 user_name = user.name
178 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200179 log.info("User %s not found", userid)
180 log.debug("Exception %s" % e)
Atul Agarwal806fe452021-03-15 09:16:09 +0000181 if project_list:
182 # user-project mapping is done by osm cli
183 for proj in project_data:
184 project = self.common_db.get_project(proj["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200185 proj_name = project["name"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000186 role_obj = self.common_db.get_role_by_id(proj["role"])
187 is_admin = role_obj["permissions"].get("admin")
garciadeblas8e4179f2021-05-14 16:47:03 +0200188 self.grafana.create_grafana_teams_members(
189 proj_name, user_name, is_admin, project_list
190 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000191 else:
192 # user-project mapping is done by osm ui
193 proj_list = []
palsus0ab64072021-02-09 18:23:03 +0000194 if self.keystone:
Atul Agarwal806fe452021-03-15 09:16:09 +0000195 users_proj_list = self.keystone.getProjectsById(userid)
196 for project in users_proj_list:
197 proj_list.append(project.name)
palsus0ab64072021-02-09 18:23:03 +0000198 else:
Atul Agarwal806fe452021-03-15 09:16:09 +0000199 users_proj_list = user.get("project_role_mappings")
200 for project in users_proj_list:
201 proj_data = self.common_db.get_project(project["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200202 proj_list.append(proj_data["name"])
Atul Agarwal806fe452021-03-15 09:16:09 +0000203 for proj in project_data:
204 if self.keystone:
205 # Backend authentication type is keystone
206 try:
207 # Getting project and role objects from keystone using ids
208 role_obj = self.keystone.getRoleById(proj["role"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200209 proj_data = self.keystone.getProjectsByProjectId(
210 proj["project"]
211 )
212 log.info(
213 "role object {} {}".format(
214 role_obj.permissions, proj_data.name
215 )
216 )
217 is_admin = role_obj.permissions["admin"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000218 except Exception:
219 # Getting project and role objects from keystone using names
220 role_obj = self.keystone.getRoleByName(proj["role"])[0]
garciadeblas8e4179f2021-05-14 16:47:03 +0200221 proj_data = self.keystone.getProjectsByProjectName(
222 proj["project"]
223 )[0]
224 is_admin = role_obj.to_dict().get("permissions").get("admin")
225 log.info(
226 "role object {} {}".format(
227 role_obj.to_dict(), proj_data.name
228 )
229 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000230 proj_name = proj_data.name
231 else:
232 # Backend authentication type is internal
233 try:
234 # Getting project and role object from commondb using names
235 role_obj = self.common_db.get_role_by_name(proj["role"])
236 proj_name = proj["project"]
237 except Exception:
238 # Getting project and role object from commondb using ids
239 role_obj = self.common_db.get_role_by_id(proj["role"])
240 proj_data = self.common_db.get_project(proj["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200241 proj_name = proj_data["name"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000242 is_admin = role_obj["permissions"].get("admin")
garciadeblas8e4179f2021-05-14 16:47:03 +0200243 self.grafana.create_grafana_teams_members(
244 proj_name, user_name, is_admin, proj_list
245 )
agarwalat85a91852020-10-08 12:24:47 +0000246
247 def create_grafana_team(self, team_name):
248 self.grafana.create_grafana_teams(team_name)
249
250 def delete_grafana_user(self, user_name):
251 self.grafana.delete_grafana_users(user_name)
252
253 def delete_grafana_team(self, project_name):
254 self.grafana.delete_grafana_team(project_name)
255
256 def update_grafana_team(self, project_new_name, project_old_name):
257 self.grafana.update_grafana_teams(project_new_name, project_old_name)
Atul Agarwal806fe452021-03-15 09:16:09 +0000258
259 def remove_grafana_team_members(self, user_id, proj_data):
260 try:
261 # Get user details from commondb
262 user = self.common_db.get_user_by_id(user_id)
263 user_name = user["username"]
264 except Exception as e:
265 # User not found in commondb
266 if self.keystone:
267 # Find user in keystone
268 user = self.keystone.getUserById(user_id)
269 user_name = user.name
270 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200271 log.info("User %s not found", user_id)
272 log.debug("Exception %s" % e)
Atul Agarwal806fe452021-03-15 09:16:09 +0000273 self.grafana.remove_grafana_team_member(user_name, proj_data)