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
19 from airflow
.decorators
import dag
, task
20 from osm_mon
.core
.common_db
import CommonDbClient
21 from osm_mon
.core
.config
import Config
22 from prometheus_client
import CollectorRegistry
, Gauge
, push_to_gateway
25 PROMETHEUS_PUSHGW
= "pushgateway-prometheus-pushgateway:9091"
26 PROMETHEUS_JOB
= "airflow_osm_ns_topology"
27 PROMETHEUS_METRIC
= "ns_topology"
28 PROMETHEUS_METRIC_DESCRIPTION
= "Network services topology"
35 "depends_on_past": False,
37 "retry_delay": timedelta(seconds
=10),
39 description
="NS topology",
40 is_paused_upon_creation
=False,
41 # schedule_interval=timedelta(minutes=SCHEDULE_INTERVAL),
42 schedule_interval
=f
"*/{SCHEDULE_INTERVAL} * * * *",
43 start_date
=datetime(2022, 1, 1),
44 tags
=["osm", "topology"],
47 @task(task_id
="get_topology")
50 Get NS topology from MongoDB and exports it as a metric
54 # Define Prometheus Metric for NS topology
55 registry
= CollectorRegistry()
58 PROMETHEUS_METRIC_DESCRIPTION
,
72 # Getting VNFR list from MongoDB
73 print("Getting VNFR list from MongoDB")
76 common_db
= CommonDbClient(cfg
)
77 vnfr_list
= common_db
.get_vnfrs()
79 # Only send topology if ns state is one of the nsAllowedStatesSet
80 nsAllowedStatesSet
= {"INSTANTIATED"}
82 # For loop to get NS topology.
83 # For each VDU, a metric sample is created with the appropriate labels
84 for vnfr
in vnfr_list
:
88 ns_id
= vnfr
["nsr-id-ref"]
90 vnfd_id
= vnfr
["vnfd-ref"]
92 project_list
= vnfr
.get("_admin", {}).get("projects_read", [])
95 project_id
= project_list
[0]
96 # TODO: use logger with loglevels instead of print
98 ns_state
= vnfr
["_admin"]["nsState"]
99 vnf_membex_index
= vnfr
["member-vnf-index-ref"]
101 f
"Read VNFR: id: {vnf_id}, ns_id: {ns_id}, ",
102 f
"state: {ns_state}, vnfd_id: {vnfd_id}, ",
103 f
"vnf_membex_index: {vnf_membex_index}, ",
104 f
"project_id: {project_id}",
106 # Only send topology if ns State is one of the nsAllowedStatesSet
107 if ns_state
not in nsAllowedStatesSet
:
111 for vdu
in vnfr
.get("vdur", []):
115 vim_info
= vdu
.get("vim_info")
117 print("Error: vim_info not available in vdur")
119 if len(vim_info
) != 1:
120 print("Error: more than one vim_info in vdur")
122 vim_id
= next(iter(vim_info
))[4:]
124 vm_id
= vdu
["vim-id"]
126 vdu_name
= vdu
.get("name", "UNKNOWN")
128 f
" id: {vdu_id}, name: {vdu_name}, "
129 f
"vim_id: {vim_id}, vm_id: {vm_id}"
132 f
"METRIC SAMPLE: ns_id: {ns_id}, ",
133 f
"project_id: {project_id}, vnf_id: {vnf_id}, ",
134 f
"vdu_id: {vdu_id}, vm_id: {vm_id}, vim_id: {vim_id}",
147 # print("Push to gateway")
149 gateway
=PROMETHEUS_PUSHGW
, job
=PROMETHEUS_JOB
, registry
=registry