| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 1 | # -*- 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: bdiaz@whitestack.com or glavado@whitestack.com |
| 22 | ## |
| 23 | import json |
| 24 | import logging |
| 25 | import uuid |
| 26 | |
| 27 | from osm_mon.core import database |
| 28 | from osm_mon.core.common_db import CommonDbClient |
| 29 | from osm_mon.core.config import Config |
| 30 | from osm_mon.core.database import VimCredentialsRepository, VimCredentials, AlarmRepository, Alarm |
| 31 | |
| 32 | log = logging.getLogger(__name__) |
| 33 | |
| 34 | |
| 35 | class ServerService: |
| 36 | |
| 37 | def __init__(self, config: Config): |
| 38 | self.common_db = CommonDbClient(config) |
| 39 | |
| 40 | def upsert_vim_account(self, |
| 41 | vim_uuid: str, |
| 42 | name: str, |
| 43 | vim_type: str, |
| 44 | url: str, |
| 45 | user: str, |
| 46 | password: str, |
| 47 | tenant_name: str, |
| 48 | schema_version: str, |
| 49 | config: dict) -> VimCredentials: |
| 50 | decrypted_vim_password = self.common_db.decrypt_vim_password(password, |
| 51 | schema_version, |
| 52 | vim_uuid) |
| 53 | |
| 54 | vim_config_encrypted = ("admin_password", "nsx_password", "vcenter_password") |
| 55 | for key in config: |
| 56 | if key in vim_config_encrypted: |
| 57 | config[key] = self.common_db.decrypt_vim_password(config[key], |
| 58 | schema_version, |
| 59 | vim_uuid) |
| 60 | database.db.connect() |
| 61 | try: |
| 62 | with database.db.atomic(): |
| 63 | return VimCredentialsRepository.upsert( |
| 64 | uuid=vim_uuid, |
| 65 | name=name, |
| 66 | type=vim_type, |
| 67 | url=url, |
| 68 | user=user, |
| 69 | password=decrypted_vim_password, |
| 70 | tenant_name=tenant_name, |
| 71 | config=json.dumps(config) |
| 72 | ) |
| 73 | finally: |
| 74 | database.db.close() |
| 75 | |
| 76 | def delete_vim_account(self, vim_uuid: str) -> None: |
| 77 | database.db.connect() |
| 78 | try: |
| 79 | with database.db.atomic(): |
| 80 | vim_credentials = VimCredentialsRepository.get(VimCredentials.uuid == vim_uuid) |
| 81 | vim_credentials.delete_instance() |
| 82 | finally: |
| 83 | database.db.close() |
| 84 | |
| 85 | def create_alarm(self, |
| 86 | name: str, |
| 87 | threshold: str, |
| 88 | operation: str, |
| 89 | severity: str, |
| 90 | statistic: str, |
| 91 | metric_name: str, |
| 92 | vdur_name: str, |
| 93 | vnf_member_index: str, |
| 94 | nsr_id: str) -> Alarm: |
| 95 | database.db.connect() |
| 96 | try: |
| 97 | with database.db.atomic(): |
| 98 | return AlarmRepository.create( |
| 99 | uuid=str(uuid.uuid4()), |
| 100 | name=name, |
| 101 | threshold=threshold, |
| 102 | operation=operation.lower(), |
| 103 | severity=severity.lower(), |
| 104 | statistic=statistic.lower(), |
| 105 | monitoring_param=metric_name, |
| 106 | vdur_name=vdur_name, |
| 107 | vnf_member_index=vnf_member_index, |
| 108 | nsr_id=nsr_id |
| 109 | ) |
| 110 | |
| 111 | finally: |
| 112 | database.db.close() |
| 113 | |
| 114 | def delete_alarm(self, |
| 115 | alarm_uuid: str) -> None: |
| 116 | database.db.connect() |
| 117 | try: |
| 118 | with database.db.atomic(): |
| 119 | alarm = AlarmRepository.get(Alarm.uuid == alarm_uuid) |
| 120 | alarm.delete_instance() |
| 121 | finally: |
| 122 | database.db.close() |