Fix improper restriction of XMl External Entity Reference, by using lxml
[osm/MON.git] / osm_mon / collector / vnf_collectors / juju.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 import asyncio
23 import logging
24 from typing import List
25
26 from n2vc.n2vc_juju_conn import N2VCJujuConnector
27
28 from osm_mon.collector.metric import Metric
29 from osm_mon.collector.vnf_collectors.base import BaseCollector
30 from osm_mon.collector.vnf_metric import VnfMetric
31 from osm_mon.core.common_db import CommonDbClient
32 from osm_mon.core.config import Config
33 from osm_mon.core.exceptions import VcaDeploymentInfoNotFound
34
35 log = logging.getLogger(__name__)
36
37
38 class VCACollector(BaseCollector):
39 def __init__(self, config: Config):
40 super().__init__(config)
41 self.common_db = CommonDbClient(config)
42 # host = config.get("vca", "host")
43 # port = config.get("vca", "port") if "port" in config.conf["vca"] else 17070
44
45 # Backwards compatibility
46 if "cacert" in config.conf["vca"]:
47 ca_cert = config.conf["vca"].pop("cacert")
48 config.set("vca", "ca_cert", ca_cert)
49
50 if "pubkey" in config.conf["vca"]:
51 public_key = config.conf["vca"].pop("pubkey")
52 config.set("vca", "public_key", public_key)
53
54 if "apiproxy" in config.conf["vca"]:
55 api_proxy = config.conf["vca"].pop("apiproxy")
56 config.set("vca", "api_proxy", api_proxy)
57
58 self.n2vc = N2VCJujuConnector(
59 db=self.common_db.common_db,
60 fs=object(),
61 log=log,
62 on_update_db=None,
63 )
64
65 def collect(self, vnfr: dict) -> List[Metric]:
66 nsr_id = vnfr["nsr-id-ref"]
67 vnf_member_index = vnfr["member-vnf-index-ref"]
68 vnfd = self.common_db.get_vnfd(vnfr["vnfd-id"])
69
70 # Populate extra tags for metrics
71 tags = {}
72 tags["ns_name"] = self.common_db.get_nsr(nsr_id)["name"]
73 if vnfr["_admin"]["projects_read"]:
74 tags["project_id"] = vnfr["_admin"]["projects_read"][0]
75 else:
76 tags["project_id"] = ""
77
78 metrics = []
79 vdur = None
80 lcm_ops = vnfd["df"][0].get("lcm-operations-configuration")
81 if not lcm_ops:
82 return metrics
83 ops_config = lcm_ops.get("operate-vnf-op-config")
84 if not ops_config:
85 return metrics
86 day12ops = ops_config.get("day1-2", [])
87 for day12op in day12ops:
88 if day12op and "metrics" in day12op:
89 vdur = next(
90 filter(
91 lambda vdur: vdur["vdu-id-ref"] == day12op["id"], vnfr["vdur"]
92 )
93 )
94
95 # This avoids errors when vdur records have not been completely filled
96 if vdur and "name" in vdur:
97 try:
98 vca_deployment_info = self.get_vca_deployment_info(
99 nsr_id,
100 vnf_member_index,
101 vdur["vdu-id-ref"],
102 vdur["count-index"],
103 )
104 except VcaDeploymentInfoNotFound as e:
105 log.warning(repr(e))
106 continue
107 # This avoids errors before application and model is not ready till they are occured
108 if vca_deployment_info.get("model") and vca_deployment_info.get(
109 "application"
110 ):
111 measures = asyncio.run(
112 self.n2vc.get_metrics(
113 vca_deployment_info["model"],
114 vca_deployment_info["application"],
115 vca_id=vnfr.get("vca-id"),
116 )
117 )
118 log.debug("Measures: %s", measures)
119 for measure_list in measures.values():
120 for measure in measure_list:
121 log.debug("Measure: %s", measure)
122 metric = VnfMetric(
123 nsr_id,
124 vnf_member_index,
125 vdur["name"],
126 measure["key"],
127 float(measure["value"]),
128 tags,
129 )
130 metrics.append(metric)
131
132 return metrics
133
134 def get_vca_deployment_info(
135 self, nsr_id, vnf_member_index, vdu_id=None, vdu_count=0
136 ):
137 nsr = self.common_db.get_nsr(nsr_id)
138 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
139 if vca_deployment:
140 if vdu_id is None:
141 if (
142 vca_deployment["member-vnf-index"] == vnf_member_index
143 and vca_deployment["vdu_id"] is None
144 ):
145 return vca_deployment
146 else:
147 if (
148 vca_deployment["member-vnf-index"] == vnf_member_index
149 and vca_deployment["vdu_id"] == vdu_id
150 and vca_deployment["vdu_count_index"] == vdu_count
151 ):
152 return vca_deployment
153 raise VcaDeploymentInfoNotFound(
154 "VCA deployment info for nsr_id {}, index {}, vdu_id {} and vdu_count_index {} not found.".format(
155 nsr_id, vnf_member_index, vdu_id, vdu_count
156 )
157 )