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 datetime
import datetime
, timedelta
20 from airflow
.decorators
import dag
, task
21 from osm_mon
.core
.common_db
import CommonDbClient
22 from osm_mon
.core
.config
import Config
23 from prometheus_client
import CollectorRegistry
, Gauge
, push_to_gateway
26 PROMETHEUS_PUSHGW
= "pushgateway-prometheus-pushgateway:9091"
27 PROMETHEUS_JOB
= "airflow_osm_ns_topology"
28 PROMETHEUS_METRIC
= "ns_topology"
29 PROMETHEUS_METRIC_DESCRIPTION
= "Network services topology"
33 logger
= logging
.getLogger("airflow.task")
39 "depends_on_past": False,
41 "retry_delay": timedelta(seconds
=10),
43 description
="NS topology",
44 is_paused_upon_creation
=False,
46 # schedule_interval=timedelta(minutes=SCHEDULE_INTERVAL),
47 schedule_interval
=f
"*/{SCHEDULE_INTERVAL} * * * *",
48 start_date
=datetime(2022, 1, 1),
49 tags
=["osm", "topology"],
52 @task(task_id
="get_topology")
55 Get NS topology from MongoDB and exports it as a metric
59 # Define Prometheus Metric for NS topology
60 registry
= CollectorRegistry()
63 PROMETHEUS_METRIC_DESCRIPTION
,
77 # Getting VNFR list from MongoDB
78 logger
.info("Getting VNFR list from MongoDB")
80 # logger.debug(cfg.conf)
81 common_db
= CommonDbClient(cfg
)
82 vnfr_list
= common_db
.get_vnfrs()
84 # Only send topology if ns state is one of the nsAllowedStatesSet
85 nsAllowedStatesSet
= {"INSTANTIATED"}
87 # For loop to get NS topology.
88 # For each VDU, a metric sample is created with the appropriate labels
89 for vnfr
in vnfr_list
:
93 ns_id
= vnfr
["nsr-id-ref"]
95 vnfd_id
= vnfr
["vnfd-ref"]
97 project_list
= vnfr
.get("_admin", {}).get("projects_read", [])
100 project_id
= project_list
[0]
102 ns_state
= vnfr
["_admin"]["nsState"]
103 vnf_membex_index
= vnfr
["member-vnf-index-ref"]
105 f
"Read VNFR: id: {vnf_id}, ns_id: {ns_id}, "
106 f
"state: {ns_state}, vnfd_id: {vnfd_id}, "
107 f
"vnf_membex_index: {vnf_membex_index}, "
108 f
"project_id: {project_id}"
110 # Only send topology if ns State is one of the nsAllowedStatesSet
111 if ns_state
not in nsAllowedStatesSet
:
114 logger
.debug("VDU list:")
115 for vdu
in vnfr
.get("vdur", []):
119 vim_info
= vdu
.get("vim_info")
121 logger
.info("Error: vim_info not available in vdur")
123 if len(vim_info
) != 1:
124 logger
.info("Error: more than one vim_info in vdur")
126 vim_id
= next(iter(vim_info
))[4:]
127 # TODO: check if it makes sense to use vnfr.vim-account-id as vim_id instead of the vim_info key
129 vm_id
= vdu
.get("vim-id")
131 logger
.info("Error: vim-id not available in vdur")
134 vdu_name
= vdu
.get("name", "UNKNOWN")
136 f
" VDU id: {vdu_id}, name: {vdu_name}, "
137 f
"vim_id: {vim_id}, vm_id: {vm_id}"
140 f
"METRIC SAMPLE: ns_id: {ns_id}, "
141 f
"project_id: {project_id}, vnf_id: {vnf_id}, "
142 f
"vdu_id: {vdu_id}, vm_id: {vm_id}, vim_id: {vim_id}"
156 gateway
=PROMETHEUS_PUSHGW
, job
=PROMETHEUS_JOB
, registry
=registry