1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
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
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()
29 "Unknown database driver {}".format(config
.get("section", "driver"))
31 self
.common_db
.db_connect(config
.get("database"))
33 def get_vnfr(self
, nsr_id
: str, member_index
: int):
34 vnfr
= self
.common_db
.get_one(
35 "vnfrs", {"nsr-id-ref": nsr_id
, "member-vnf-index-ref": str(member_index
)}
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")
44 self
.get_vnfr(nsr_id
, member
["member-vnf-index"])
45 for member
in self
.get_nsr(nsr_id
)["nsd"]["constituent-vnfd"]
48 vnfrs
= self
.common_db
.get_list("vnfrs", {"vim-account-id": vim_account_id
})
50 vnfrs
= self
.common_db
.get_list("vnfrs")
53 def get_nsr(self
, nsr_id
: str):
54 nsr
= self
.common_db
.get_one("nsrs", {"id": nsr_id
})
57 def decrypt_vim_password(self
, vim_password
: str, schema_version
: str, vim_id
: str):
58 return self
.common_db
.decrypt(vim_password
, schema_version
, vim_id
)
60 def get_vim_accounts(self
):
61 return self
.common_db
.get_list("vim_accounts")
63 def get_vim_account(self
, vim_account_id
: str) -> dict:
64 vim_account
= self
.common_db
.get_one("vim_accounts", {"_id": vim_account_id
})
65 vim_account
["vim_password"] = self
.decrypt_vim_password(
66 vim_account
["vim_password"], vim_account
["schema_version"], vim_account_id
68 vim_config_encrypted_dict
= {
69 "1.1": ("admin_password", "nsx_password", "vcenter_password"),
77 vim_config_encrypted
= vim_config_encrypted_dict
["default"]
78 if vim_account
["schema_version"] in vim_config_encrypted_dict
.keys():
79 vim_config_encrypted
= vim_config_encrypted_dict
[
80 vim_account
["schema_version"]
82 if "config" in vim_account
:
83 for key
in vim_account
["config"]:
84 if key
in vim_config_encrypted
:
85 vim_account
["config"][key
] = self
.decrypt_vim_password(
86 vim_account
["config"][key
],
87 vim_account
["schema_version"],