blob: 32128c2a4733020a6cb89153c21a61f2a2b16075 [file] [log] [blame]
lavado456d0f32019-11-15 17:04:02 -05001# -*- coding: utf-8 -*-
2
bravofcd89bb42021-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
bravofcf0c2e12020-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
bravofcd89bb42021-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
bravofcf0c2e12020-12-17 17:27:29 -030030from osm_mon.dashboarder.utils import find_in_list
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
bravofcd89bb42021-01-27 21:22:19 -030041 if bool(self.conf.get('keystone', 'enabled')):
42 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 = []
bravofcd89bb42021-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(
57 map(lambda project: {'_id': project.id, 'name': project.name}, self.keystone.getProjects())
58 )
59 except Exception:
60 log.error('Cannot retrieve projects from keystone')
lavado456d0f32019-11-15 17:04:02 -050061
62 # Reads existing project list and creates a dashboard for each
bravofcd89bb42021-01-27 21:22:19 -030063 projects.extend(self.common_db.get_projects())
lavado456d0f32019-11-15 17:04:02 -050064 for project in projects:
65 project_id = project['_id']
66 # Collect Project IDs for periodical dashboard clean-up
67 osm_resource_uids.append(project_id)
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000068 dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050069 if project_id not in dashboard_uids:
70 project_name = project['name']
Atul Agarwal9006f6d2021-01-06 13:29:19 +000071 self.grafana.create_grafana_folders(project_name)
bravof396648b2020-03-31 18:42:45 -030072 self.grafana.create_dashboard(project_id, project_name,
73 dashboard_path)
lavado456d0f32019-11-15 17:04:02 -050074 log.debug('Created dashboard for Project: %s', project_id)
75 else:
76 log.debug('Dashboard already exists')
77
78 # Reads existing NS list and creates a dashboard for each
79 # TODO lavado: only create for ACTIVE NSRs
80 nsrs = self.common_db.get_nsrs()
81 for nsr in nsrs:
82 nsr_id = nsr['_id']
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000083 dashboard_path = '{}/dashboarder/templates/ns_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050084 # Collect NS IDs for periodical dashboard clean-up
85 osm_resource_uids.append(nsr_id)
86 # Check if the NSR's VNFDs contain metrics
bravof12fda452020-12-17 10:21:22 -030087 # Only one DF at the moment, support for this feature is comming in the future
88 vnfds_profiles = nsr['nsd']["df"][0]['vnf-profile']
89 for vnf_profile in vnfds_profiles:
lavado456d0f32019-11-15 17:04:02 -050090 try:
bravof12fda452020-12-17 10:21:22 -030091 vnfd = self.common_db.get_vnfd_by_id(vnf_profile['vnfd-id'])
lavado456d0f32019-11-15 17:04:02 -050092 # If there are metrics, create dashboard (if exists)
bravofcf0c2e12020-12-17 17:27:29 -030093 vdu_found = find_in_list(vnfd["vdu"], lambda a_vdu: "monitoring-parameter" in a_vdu)
94 if vdu_found:
lavado456d0f32019-11-15 17:04:02 -050095 if nsr_id not in dashboard_uids:
96 nsr_name = nsr['name']
agarwalat85a91852020-10-08 12:24:47 +000097 project_id = nsr["_admin"]["projects_read"][0]
98 project_details = self.common_db.get_project(project_id)
99 project_name = project_details["name"]
bravof396648b2020-03-31 18:42:45 -0300100 self.grafana.create_dashboard(nsr_id, nsr_name,
agarwalat85a91852020-10-08 12:24:47 +0000101 dashboard_path, project_name)
lavado456d0f32019-11-15 17:04:02 -0500102 log.debug('Created dashboard for NS: %s', nsr_id)
103 else:
104 log.debug('Dashboard already exists')
105 break
106 else:
107 log.debug('NS does not has metrics')
108 except Exception:
109 log.exception("VNFD is not valid or has been renamed")
110 continue
111
112 # Delete obsolete dashboards
113 for dashboard_uid in dashboard_uids:
114 if dashboard_uid not in osm_resource_uids:
bravof396648b2020-03-31 18:42:45 -0300115 self.grafana.delete_dashboard(dashboard_uid)
lavado456d0f32019-11-15 17:04:02 -0500116 log.debug('Deleted obsolete dashboard: %s', dashboard_uid)
117 else:
118 log.debug('All dashboards in use')
agarwalat85a91852020-10-08 12:24:47 +0000119
120 def create_grafana_user(self, user):
121 self.grafana.create_grafana_users(user)
122
123 def create_grafana_team_member(self, project_data, userid):
124 user = self.common_db.get_user_by_id(userid)
125 user_name = user["username"]
126 proj_list = []
127 for project in project_data:
128 proj_list.append(project["project"])
129 for proj in project_data:
130 role_obj = self.common_db.get_role_by_name(proj["role"])
131 is_admin = role_obj["permissions"].get("admin")
132 self.grafana.create_grafana_teams_members(proj["project"], user_name, is_admin, proj_list)
133
134 def create_grafana_team(self, team_name):
135 self.grafana.create_grafana_teams(team_name)
136
137 def delete_grafana_user(self, user_name):
138 self.grafana.delete_grafana_users(user_name)
139
140 def delete_grafana_team(self, project_name):
141 self.grafana.delete_grafana_team(project_name)
142
143 def update_grafana_team(self, project_new_name, project_old_name):
144 self.grafana.update_grafana_teams(project_new_name, project_old_name)