blob: 48e8150d4a6105d71885b63dba49a42ed07ec125 [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
garciadeblas8e4179f2021-05-14 16:47:03 +0200150 def create_grafana_team_member(
151 self, project_data, userid=None, project_list=None, user=None
152 ):
Atul Agarwal806fe452021-03-15 09:16:09 +0000153 if user:
154 user_name = user
155 else:
156 try:
157 # Get user details from commondb
158 user = self.common_db.get_user_by_id(userid)
159 user_name = user["username"]
160 except Exception as e:
161 # User not found in commondb
162 if self.keystone:
163 # Search user in keystone
164 user = self.keystone.getUserById(userid)
165 user_name = user.name
166 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200167 log.info("User %s not found", userid)
168 log.debug("Exception %s" % e)
Atul Agarwal806fe452021-03-15 09:16:09 +0000169 if project_list:
170 # user-project mapping is done by osm cli
171 for proj in project_data:
172 project = self.common_db.get_project(proj["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200173 proj_name = project["name"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000174 role_obj = self.common_db.get_role_by_id(proj["role"])
175 is_admin = role_obj["permissions"].get("admin")
garciadeblas8e4179f2021-05-14 16:47:03 +0200176 self.grafana.create_grafana_teams_members(
177 proj_name, user_name, is_admin, project_list
178 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000179 else:
180 # user-project mapping is done by osm ui
181 proj_list = []
palsus0ab64072021-02-09 18:23:03 +0000182 if self.keystone:
Atul Agarwal806fe452021-03-15 09:16:09 +0000183 users_proj_list = self.keystone.getProjectsById(userid)
184 for project in users_proj_list:
185 proj_list.append(project.name)
palsus0ab64072021-02-09 18:23:03 +0000186 else:
Atul Agarwal806fe452021-03-15 09:16:09 +0000187 users_proj_list = user.get("project_role_mappings")
188 for project in users_proj_list:
189 proj_data = self.common_db.get_project(project["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200190 proj_list.append(proj_data["name"])
Atul Agarwal806fe452021-03-15 09:16:09 +0000191 for proj in project_data:
192 if self.keystone:
193 # Backend authentication type is keystone
194 try:
195 # Getting project and role objects from keystone using ids
196 role_obj = self.keystone.getRoleById(proj["role"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200197 proj_data = self.keystone.getProjectsByProjectId(
198 proj["project"]
199 )
200 log.info(
201 "role object {} {}".format(
202 role_obj.permissions, proj_data.name
203 )
204 )
205 is_admin = role_obj.permissions["admin"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000206 except Exception:
207 # Getting project and role objects from keystone using names
208 role_obj = self.keystone.getRoleByName(proj["role"])[0]
garciadeblas8e4179f2021-05-14 16:47:03 +0200209 proj_data = self.keystone.getProjectsByProjectName(
210 proj["project"]
211 )[0]
212 is_admin = role_obj.to_dict().get("permissions").get("admin")
213 log.info(
214 "role object {} {}".format(
215 role_obj.to_dict(), proj_data.name
216 )
217 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000218 proj_name = proj_data.name
219 else:
220 # Backend authentication type is internal
221 try:
222 # Getting project and role object from commondb using names
223 role_obj = self.common_db.get_role_by_name(proj["role"])
224 proj_name = proj["project"]
225 except Exception:
226 # Getting project and role object from commondb using ids
227 role_obj = self.common_db.get_role_by_id(proj["role"])
228 proj_data = self.common_db.get_project(proj["project"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200229 proj_name = proj_data["name"]
Atul Agarwal806fe452021-03-15 09:16:09 +0000230 is_admin = role_obj["permissions"].get("admin")
garciadeblas8e4179f2021-05-14 16:47:03 +0200231 self.grafana.create_grafana_teams_members(
232 proj_name, user_name, is_admin, proj_list
233 )
agarwalat85a91852020-10-08 12:24:47 +0000234
235 def create_grafana_team(self, team_name):
236 self.grafana.create_grafana_teams(team_name)
237
238 def delete_grafana_user(self, user_name):
239 self.grafana.delete_grafana_users(user_name)
240
241 def delete_grafana_team(self, project_name):
242 self.grafana.delete_grafana_team(project_name)
243
244 def update_grafana_team(self, project_new_name, project_old_name):
245 self.grafana.update_grafana_teams(project_new_name, project_old_name)
Atul Agarwal806fe452021-03-15 09:16:09 +0000246
247 def remove_grafana_team_members(self, user_id, proj_data):
248 try:
249 # Get user details from commondb
250 user = self.common_db.get_user_by_id(user_id)
251 user_name = user["username"]
252 except Exception as e:
253 # User not found in commondb
254 if self.keystone:
255 # Find user in keystone
256 user = self.keystone.getUserById(user_id)
257 user_name = user.name
258 else:
garciadeblas8e4179f2021-05-14 16:47:03 +0200259 log.info("User %s not found", user_id)
260 log.debug("Exception %s" % e)
Atul Agarwal806fe452021-03-15 09:16:09 +0000261 self.grafana.remove_grafana_team_member(user_name, proj_data)