| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| bravof | 2fe71f2 | 2021-01-27 21:22:19 -0300 | [diff] [blame] | 3 | # Copyright 2021 Whitestack, LLC |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 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 |
| bravof | 960d046 | 2020-12-17 17:27:29 -0300 | [diff] [blame] | 21 | # contact: glavado@whitestack.com or fbravo@whitestack.com |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 22 | ## |
| 23 | import logging |
| 24 | |
| 25 | from osm_mon.core.common_db import CommonDbClient |
| 26 | from osm_mon.core.config import Config |
| bravof | 2fe71f2 | 2021-01-27 21:22:19 -0300 | [diff] [blame] | 27 | from osm_mon.core.keystone import KeystoneConnection |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 28 | from osm_mon.dashboarder.backends.grafana import GrafanaBackend |
| Gianpietro Lavado | 1d71df5 | 2019-12-02 17:41:20 +0000 | [diff] [blame] | 29 | from osm_mon import __path__ as mon_path |
| bravof | 960d046 | 2020-12-17 17:27:29 -0300 | [diff] [blame] | 30 | from osm_mon.dashboarder.utils import find_in_list |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 31 | |
| 32 | log = logging.getLogger(__name__) |
| 33 | |
| 34 | |
| 35 | class DashboarderService: |
| 36 | def __init__(self, config: Config): |
| 37 | self.conf = config |
| 38 | self.common_db = CommonDbClient(self.conf) |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 39 | self.grafana = GrafanaBackend(self.conf) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 40 | |
| bravof | 2fe71f2 | 2021-01-27 21:22:19 -0300 | [diff] [blame] | 41 | if bool(self.conf.get('keystone', 'enabled')): |
| 42 | self.keystone = KeystoneConnection(self.conf) |
| 43 | else: |
| 44 | self.keystone = None |
| 45 | |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 46 | def create_dashboards(self): |
| 47 | # TODO lavado: migrate these methods to mongo change streams |
| 48 | # Lists all dashboards and OSM resources for later comparisons |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 49 | dashboard_uids = self.grafana.get_all_dashboard_uids() |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 50 | osm_resource_uids = [] |
| bravof | 2fe71f2 | 2021-01-27 21:22:19 -0300 | [diff] [blame] | 51 | 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') |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 61 | |
| 62 | # Reads existing project list and creates a dashboard for each |
| bravof | 2fe71f2 | 2021-01-27 21:22:19 -0300 | [diff] [blame] | 63 | projects.extend(self.common_db.get_projects()) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 64 | 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 Lavado | 6fb6948 | 2019-12-03 15:16:14 +0000 | [diff] [blame] | 68 | dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0]) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 69 | if project_id not in dashboard_uids: |
| 70 | project_name = project['name'] |
| palsus | 0ab6407 | 2021-02-09 18:23:03 +0000 | [diff] [blame^] | 71 | if project_name != "admin": |
| 72 | # Create project folder in Grafana only if user is not admin. |
| 73 | # Admin user's dashboard will be created in default folder |
| 74 | self.grafana.create_grafana_folders(project_name) |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 75 | self.grafana.create_dashboard(project_id, project_name, |
| 76 | dashboard_path) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 77 | log.debug('Created dashboard for Project: %s', project_id) |
| 78 | else: |
| 79 | log.debug('Dashboard already exists') |
| 80 | |
| 81 | # Reads existing NS list and creates a dashboard for each |
| 82 | # TODO lavado: only create for ACTIVE NSRs |
| 83 | nsrs = self.common_db.get_nsrs() |
| 84 | for nsr in nsrs: |
| 85 | nsr_id = nsr['_id'] |
| Gianpietro Lavado | 6fb6948 | 2019-12-03 15:16:14 +0000 | [diff] [blame] | 86 | dashboard_path = '{}/dashboarder/templates/ns_scoped.json'.format(mon_path[0]) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 87 | # Collect NS IDs for periodical dashboard clean-up |
| 88 | osm_resource_uids.append(nsr_id) |
| 89 | # Check if the NSR's VNFDs contain metrics |
| bravof | 12fda45 | 2020-12-17 10:21:22 -0300 | [diff] [blame] | 90 | # Only one DF at the moment, support for this feature is comming in the future |
| 91 | vnfds_profiles = nsr['nsd']["df"][0]['vnf-profile'] |
| 92 | for vnf_profile in vnfds_profiles: |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 93 | try: |
| bravof | 12fda45 | 2020-12-17 10:21:22 -0300 | [diff] [blame] | 94 | vnfd = self.common_db.get_vnfd_by_id(vnf_profile['vnfd-id']) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 95 | # If there are metrics, create dashboard (if exists) |
| bravof | 960d046 | 2020-12-17 17:27:29 -0300 | [diff] [blame] | 96 | vdu_found = find_in_list(vnfd["vdu"], lambda a_vdu: "monitoring-parameter" in a_vdu) |
| 97 | if vdu_found: |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 98 | if nsr_id not in dashboard_uids: |
| 99 | nsr_name = nsr['name'] |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 100 | project_id = nsr["_admin"]["projects_read"][0] |
| palsus | 0ab6407 | 2021-02-09 18:23:03 +0000 | [diff] [blame^] | 101 | try: |
| 102 | # Get project details from commondb |
| 103 | project_details = self.common_db.get_project(project_id) |
| 104 | project_name = project_details["name"] |
| 105 | except Exception as e: |
| 106 | # Project not found in commondb |
| 107 | if self.keystone: |
| 108 | # Serach project in keystone |
| 109 | for project in projects: |
| 110 | if project_id == project['_id']: |
| 111 | project_name = project["name"] |
| 112 | else: |
| 113 | log.info('Project %s not found', project_id) |
| 114 | log.debug('Exception %s' % e) |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 115 | self.grafana.create_dashboard(nsr_id, nsr_name, |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 116 | dashboard_path, project_name) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 117 | log.debug('Created dashboard for NS: %s', nsr_id) |
| 118 | else: |
| 119 | log.debug('Dashboard already exists') |
| 120 | break |
| 121 | else: |
| 122 | log.debug('NS does not has metrics') |
| 123 | except Exception: |
| 124 | log.exception("VNFD is not valid or has been renamed") |
| 125 | continue |
| 126 | |
| 127 | # Delete obsolete dashboards |
| 128 | for dashboard_uid in dashboard_uids: |
| 129 | if dashboard_uid not in osm_resource_uids: |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 130 | self.grafana.delete_dashboard(dashboard_uid) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 131 | log.debug('Deleted obsolete dashboard: %s', dashboard_uid) |
| 132 | else: |
| 133 | log.debug('All dashboards in use') |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 134 | |
| 135 | def create_grafana_user(self, user): |
| 136 | self.grafana.create_grafana_users(user) |
| 137 | |
| 138 | def create_grafana_team_member(self, project_data, userid): |
| palsus | 0ab6407 | 2021-02-09 18:23:03 +0000 | [diff] [blame^] | 139 | try: |
| 140 | # Get user details from commondb |
| 141 | user = self.common_db.get_user_by_id(userid) |
| 142 | user_name = user["username"] |
| 143 | except Exception as e: |
| 144 | # User not found in commondb |
| 145 | if self.keystone: |
| 146 | # Serach user in keystone |
| 147 | user = self.keystone.getUserById(userid) |
| 148 | user_name = user.name |
| 149 | else: |
| 150 | log.info('User %s not found', userid) |
| 151 | log.debug('Exception %s' % e) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 152 | proj_list = [] |
| 153 | for project in project_data: |
| 154 | proj_list.append(project["project"]) |
| 155 | for proj in project_data: |
| 156 | role_obj = self.common_db.get_role_by_name(proj["role"]) |
| 157 | is_admin = role_obj["permissions"].get("admin") |
| 158 | self.grafana.create_grafana_teams_members(proj["project"], user_name, is_admin, proj_list) |
| 159 | |
| 160 | def create_grafana_team(self, team_name): |
| 161 | self.grafana.create_grafana_teams(team_name) |
| 162 | |
| 163 | def delete_grafana_user(self, user_name): |
| 164 | self.grafana.delete_grafana_users(user_name) |
| 165 | |
| 166 | def delete_grafana_team(self, project_name): |
| 167 | self.grafana.delete_grafana_team(project_name) |
| 168 | |
| 169 | def update_grafana_team(self, project_new_name, project_old_name): |
| 170 | self.grafana.update_grafana_teams(project_new_name, project_old_name) |