blob: 1e81c8ad64fd96b2e2541a49996873828e62511b [file] [log] [blame]
lavado456d0f32019-11-15 17:04:02 -05001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
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
21# contact: glavado@whitestack.com
22##
23import logging
24
25from osm_mon.core.common_db import CommonDbClient
26from osm_mon.core.config import Config
bravof396648b2020-03-31 18:42:45 -030027from osm_mon.dashboarder.backends.grafana import GrafanaBackend
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000028from osm_mon import __path__ as mon_path
lavado456d0f32019-11-15 17:04:02 -050029
30log = logging.getLogger(__name__)
31
32
33class DashboarderService:
34 def __init__(self, config: Config):
35 self.conf = config
36 self.common_db = CommonDbClient(self.conf)
bravof396648b2020-03-31 18:42:45 -030037 self.grafana = GrafanaBackend(self.conf)
lavado456d0f32019-11-15 17:04:02 -050038
39 def create_dashboards(self):
40 # TODO lavado: migrate these methods to mongo change streams
41 # Lists all dashboards and OSM resources for later comparisons
bravof396648b2020-03-31 18:42:45 -030042 dashboard_uids = self.grafana.get_all_dashboard_uids()
lavado456d0f32019-11-15 17:04:02 -050043 osm_resource_uids = []
44
45 # Reads existing project list and creates a dashboard for each
46 projects = self.common_db.get_projects()
47 for project in projects:
48 project_id = project['_id']
49 # Collect Project IDs for periodical dashboard clean-up
50 osm_resource_uids.append(project_id)
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000051 dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050052 if project_id not in dashboard_uids:
53 project_name = project['name']
bravof396648b2020-03-31 18:42:45 -030054 self.grafana.create_dashboard(project_id, project_name,
55 dashboard_path)
lavado456d0f32019-11-15 17:04:02 -050056 log.debug('Created dashboard for Project: %s', project_id)
57 else:
58 log.debug('Dashboard already exists')
59
60 # Reads existing NS list and creates a dashboard for each
61 # TODO lavado: only create for ACTIVE NSRs
62 nsrs = self.common_db.get_nsrs()
63 for nsr in nsrs:
64 nsr_id = nsr['_id']
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000065 dashboard_path = '{}/dashboarder/templates/ns_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050066 # Collect NS IDs for periodical dashboard clean-up
67 osm_resource_uids.append(nsr_id)
68 # Check if the NSR's VNFDs contain metrics
bravof12fda452020-12-17 10:21:22 -030069 # Only one DF at the moment, support for this feature is comming in the future
70 vnfds_profiles = nsr['nsd']["df"][0]['vnf-profile']
71 for vnf_profile in vnfds_profiles:
lavado456d0f32019-11-15 17:04:02 -050072 try:
bravof12fda452020-12-17 10:21:22 -030073 vnfd = self.common_db.get_vnfd_by_id(vnf_profile['vnfd-id'])
lavado456d0f32019-11-15 17:04:02 -050074 # If there are metrics, create dashboard (if exists)
Ubuntu883def32020-12-17 16:17:23 +000075 if vnfd and 'monitoring-parameter' in vnfd["vdu"][0]:
lavado456d0f32019-11-15 17:04:02 -050076 if nsr_id not in dashboard_uids:
77 nsr_name = nsr['name']
agarwalat85a91852020-10-08 12:24:47 +000078 project_id = nsr["_admin"]["projects_read"][0]
79 project_details = self.common_db.get_project(project_id)
80 project_name = project_details["name"]
bravof396648b2020-03-31 18:42:45 -030081 self.grafana.create_dashboard(nsr_id, nsr_name,
agarwalat85a91852020-10-08 12:24:47 +000082 dashboard_path, project_name)
lavado456d0f32019-11-15 17:04:02 -050083 log.debug('Created dashboard for NS: %s', nsr_id)
84 else:
85 log.debug('Dashboard already exists')
86 break
87 else:
88 log.debug('NS does not has metrics')
89 except Exception:
90 log.exception("VNFD is not valid or has been renamed")
91 continue
92
93 # Delete obsolete dashboards
94 for dashboard_uid in dashboard_uids:
95 if dashboard_uid not in osm_resource_uids:
bravof396648b2020-03-31 18:42:45 -030096 self.grafana.delete_dashboard(dashboard_uid)
lavado456d0f32019-11-15 17:04:02 -050097 log.debug('Deleted obsolete dashboard: %s', dashboard_uid)
98 else:
99 log.debug('All dashboards in use')
agarwalat85a91852020-10-08 12:24:47 +0000100
101 def create_grafana_user(self, user):
102 self.grafana.create_grafana_users(user)
103
104 def create_grafana_team_member(self, project_data, userid):
105 user = self.common_db.get_user_by_id(userid)
106 user_name = user["username"]
107 proj_list = []
108 for project in project_data:
109 proj_list.append(project["project"])
110 for proj in project_data:
111 role_obj = self.common_db.get_role_by_name(proj["role"])
112 is_admin = role_obj["permissions"].get("admin")
113 self.grafana.create_grafana_teams_members(proj["project"], user_name, is_admin, proj_list)
114
115 def create_grafana_team(self, team_name):
116 self.grafana.create_grafana_teams(team_name)
117
118 def delete_grafana_user(self, user_name):
119 self.grafana.delete_grafana_users(user_name)
120
121 def delete_grafana_team(self, project_name):
122 self.grafana.delete_grafana_team(project_name)
123
124 def update_grafana_team(self, project_new_name, project_old_name):
125 self.grafana.update_grafana_teams(project_new_name, project_old_name)