Bug 1651 fix
[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 self.loop = asyncio.get_event_loop()
43 # host = config.get("vca", "host")
44 # port = config.get("vca", "port") if "port" in config.conf["vca"] else 17070
45
46 # Backwards compatibility
47 if "cacert" in config.conf["vca"]:
48 ca_cert = config.conf["vca"].pop("cacert")
49 config.set("vca", "ca_cert", ca_cert)
50
51 if "pubkey" in config.conf["vca"]:
52 public_key = config.conf["vca"].pop("pubkey")
53 config.set("vca", "public_key", public_key)
54
55 if "apiproxy" in config.conf["vca"]:
56 api_proxy = config.conf["vca"].pop("apiproxy")
57 config.set("vca", "api_proxy", api_proxy)
58
59 self.n2vc = N2VCJujuConnector(
60 db=self.common_db.common_db,
61 fs=object(),
62 log=log,
63 loop=self.loop,
64 on_update_db=None,
65 )
66
67 def collect(self, vnfr: dict) -> List[Metric]:
68 nsr_id = vnfr["nsr-id-ref"]
69 vnf_member_index = vnfr["member-vnf-index-ref"]
70 vnfd = self.common_db.get_vnfd(vnfr["vnfd-id"])
71
72 # Populate extra tags for metrics
73 tags = {}
74 tags["ns_name"] = self.common_db.get_nsr(nsr_id)["name"]
75 if vnfr["_admin"]["projects_read"]:
76 tags["project_id"] = vnfr["_admin"]["projects_read"][0]
77 else:
78 tags["project_id"] = ""
79
80 metrics = []
81 vdur = None
82 lcm_ops = vnfd["df"][0].get("lcm-operations-configuration")
83 if not lcm_ops:
84 return metrics
85 ops_config = lcm_ops.get("operate-vnf-op-config")
86 if not ops_config:
87 return metrics
88 day12ops = ops_config.get("day1-2", [])
89 for day12op in day12ops:
90 if day12op and "metrics" in day12op:
91 vdur = next(filter(lambda vdur: vdur["vdu-id-ref"] == day12op["id"], vnfr["vdur"]))
92
93 # This avoids errors when vdur records have not been completely filled
94 if vdur and "name" in vdur:
95 try:
96 vca_deployment_info = self.get_vca_deployment_info(
97 nsr_id,
98 vnf_member_index,
99 vdur["vdu-id-ref"],
100 vdur["count-index"],
101 )
102 except VcaDeploymentInfoNotFound as e:
103 log.warning(repr(e))
104 continue
105 # This avoids errors before application and model is not ready till they are occured
106 if vca_deployment_info.get("model") and vca_deployment_info.get("application"):
107 measures = self.loop.run_until_complete(
108 self.n2vc.get_metrics(
109 vca_deployment_info["model"],
110 vca_deployment_info["application"],
111 vca_id=vnfr.get("vca-id"),
112 )
113 )
114 log.debug("Measures: %s", measures)
115 for measure_list in measures.values():
116 for measure in measure_list:
117 log.debug("Measure: %s", measure)
118 metric = VnfMetric(
119 nsr_id,
120 vnf_member_index,
121 vdur["name"],
122 measure["key"],
123 float(measure["value"]),
124 tags,
125 )
126 metrics.append(metric)
127
128 return metrics
129
130 def get_vca_deployment_info(
131 self, nsr_id, vnf_member_index, vdu_id=None, vdu_count=0
132 ):
133 nsr = self.common_db.get_nsr(nsr_id)
134 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
135 if vca_deployment:
136 if vdu_id is None:
137 if (
138 vca_deployment["member-vnf-index"] == vnf_member_index
139 and vca_deployment["vdu_id"] is None
140 ):
141 return vca_deployment
142 else:
143 if (
144 vca_deployment["member-vnf-index"] == vnf_member_index
145 and vca_deployment["vdu_id"] == vdu_id
146 and vca_deployment["vdu_count_index"] == vdu_count
147 ):
148 return vca_deployment
149 raise VcaDeploymentInfoNotFound(
150 "VCA deployment info for nsr_id {}, index {}, vdu_id {} and vdu_count_index {} not found.".format(
151 nsr_id, vnf_member_index, vdu_id, vdu_count
152 )
153 )