Airflow DAG and connectors to get SDNC status
[osm/NG-SA.git] / src / osm_ngsa / osm_mon / core / common_db.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 from osm_common import dbmemory, dbmongo
18 from osm_mon.core.config import Config
19
20
21 class CommonDbClient:
22 def __init__(self, config: Config):
23 if config.get("database", "driver") == "mongo":
24 self.common_db = dbmongo.DbMongo()
25 elif config.get("database", "driver") == "memory":
26 self.common_db = dbmemory.DbMemory()
27 else:
28 raise Exception(
29 "Unknown database driver {}".format(config.get("section", "driver"))
30 )
31 self.common_db.db_connect(config.get("database"))
32
33 def get_vnfr(self, nsr_id: str, member_index: str):
34 vnfr = self.common_db.get_one(
35 "vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": member_index}
36 )
37 return vnfr
38
39 def get_vnfrs(self, nsr_id: str = None, vim_account_id: str = None):
40 if nsr_id and vim_account_id:
41 raise NotImplementedError("Only one filter is currently supported")
42 if nsr_id:
43 vnfrs = [
44 self.get_vnfr(nsr_id, member["member-vnf-index"])
45 for member in self.get_nsr(nsr_id)["nsd"]["constituent-vnfd"]
46 ]
47 elif vim_account_id:
48 vnfrs = self.common_db.get_list("vnfrs", {"vim-account-id": vim_account_id})
49 else:
50 vnfrs = self.common_db.get_list("vnfrs")
51 return vnfrs
52
53 def get_nsr(self, nsr_id: str):
54 nsr = self.common_db.get_one("nsrs", {"id": nsr_id})
55 return nsr
56
57 def get_vnfds(self):
58 return self.common_db.get_list("vnfds")
59
60 def get_monitoring_vnfds(self):
61 return self.common_db.get_list(
62 "vnfds", {"vdu.monitoring-parameter": {"$exists": "true"}}
63 )
64
65 def decrypt_vim_password(self, vim_password: str, schema_version: str, vim_id: str):
66 return self.common_db.decrypt(vim_password, schema_version, vim_id)
67
68 def decrypt_sdnc_password(
69 self, sdnc_password: str, schema_version: str, sdnc_id: str
70 ):
71 return self.common_db.decrypt(sdnc_password, schema_version, sdnc_id)
72
73 def get_vim_accounts(self):
74 return self.common_db.get_list("vim_accounts")
75
76 def get_vim_account(self, vim_account_id: str) -> dict:
77 vim_account = self.common_db.get_one("vim_accounts", {"_id": vim_account_id})
78 vim_account["vim_password"] = self.decrypt_vim_password(
79 vim_account["vim_password"], vim_account["schema_version"], vim_account_id
80 )
81 vim_config_encrypted_dict = {
82 "1.1": ("admin_password", "nsx_password", "vcenter_password"),
83 "default": (
84 "admin_password",
85 "nsx_password",
86 "vcenter_password",
87 "vrops_password",
88 ),
89 }
90 vim_config_encrypted = vim_config_encrypted_dict["default"]
91 if vim_account["schema_version"] in vim_config_encrypted_dict.keys():
92 vim_config_encrypted = vim_config_encrypted_dict[
93 vim_account["schema_version"]
94 ]
95 if "config" in vim_account:
96 for key in vim_account["config"]:
97 if key in vim_config_encrypted:
98 vim_account["config"][key] = self.decrypt_vim_password(
99 vim_account["config"][key],
100 vim_account["schema_version"],
101 vim_account_id,
102 )
103 return vim_account
104
105 def get_sdnc_accounts(self):
106 return self.common_db.get_list("sdns")
107
108 def get_sdnc_account(self, sdnc_account_id: str) -> dict:
109 sdnc_account = self.common_db.get_one("sdns", {"_id": sdnc_account_id})
110 sdnc_account["password"] = self.decrypt_vim_password(
111 sdnc_account["password"], sdnc_account["schema_version"], sdnc_account_id
112 )
113 return sdnc_account
114
115 def get_alert(
116 self,
117 nsr_id: str,
118 vnf_member_index: str,
119 vdu_id: str,
120 vdu_name: str,
121 action_type: str,
122 ):
123 q_filter = {"action_type": action_type}
124 if nsr_id:
125 q_filter["tags.ns_id"] = nsr_id
126 if vnf_member_index:
127 q_filter["tags.vnf_member_index"] = vnf_member_index
128 if vdu_id:
129 q_filter["tags.vdu_id"] = vdu_id
130 if vdu_name:
131 q_filter["tags.vdu_name"] = vdu_name
132 alert = self.common_db.get_one(
133 table="alerts", q_filter=q_filter, fail_on_empty=False
134 )
135 return alert
136
137 def update_alert_status(self, uuid: str, alarm_status: str):
138 modified_count = self.common_db.set_one(
139 "alerts", {"uuid": uuid}, {"alarm_status": alarm_status}
140 )
141 return modified_count
142
143 def create_nslcmop(self, nslcmop: dict):
144 self.common_db.create("nslcmops", nslcmop)
145
146 def get_nslcmop(self, nsr_id: str, operation_type: str, since: str):
147 q_filter = {}
148 if nsr_id:
149 q_filter["nsInstanceId"] = nsr_id
150 if operation_type:
151 q_filter["lcmOperationType"] = operation_type
152 if since:
153 q_filter["startTime"] = {"$gt": since}
154 ops = self.common_db.get_list(table="nslcmops", q_filter=q_filter)
155 return ops