Deb package creation for NG-SA
[osm/NG-SA.git] / src / osm_ngsa / dags / ns_topology.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 from datetime import datetime, timedelta
18
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
23
24
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"
29 SCHEDULE_INTERVAL = 2
30
31
32 @dag(
33 catchup=False,
34 default_args={
35 "depends_on_past": False,
36 "retries": 1,
37 "retry_delay": timedelta(seconds=10),
38 },
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"],
45 )
46 def ns_topology():
47 @task(task_id="get_topology")
48 def get_topology():
49 """
50 Get NS topology from MongoDB and exports it as a metric
51 to Prometheus
52 """
53
54 # Define Prometheus Metric for NS topology
55 registry = CollectorRegistry()
56 metric = Gauge(
57 PROMETHEUS_METRIC,
58 PROMETHEUS_METRIC_DESCRIPTION,
59 labelnames=[
60 "ns_id",
61 "project_id",
62 "vnf_id",
63 "vdu_id",
64 "vm_id",
65 "vim_id",
66 "vdu_name",
67 "vnf_member_index",
68 ],
69 registry=registry,
70 )
71
72 # Getting VNFR list from MongoDB
73 print("Getting VNFR list from MongoDB")
74 cfg = Config()
75 print(cfg.conf)
76 common_db = CommonDbClient(cfg)
77 vnfr_list = common_db.get_vnfrs()
78
79 # Only send topology if ns state is one of the nsAllowedStatesSet
80 nsAllowedStatesSet = {"INSTANTIATED"}
81
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:
85 # Label ns_id
86 vnf_id = vnfr["_id"]
87 # Label ns_id
88 ns_id = vnfr["nsr-id-ref"]
89 # Label vnfd_id
90 vnfd_id = vnfr["vnfd-ref"]
91 # Label project_id
92 project_list = vnfr.get("_admin", {}).get("projects_read", [])
93 project_id = "None"
94 if project_list:
95 project_id = project_list[0]
96 # TODO: use logger with loglevels instead of print
97 # Other info
98 ns_state = vnfr["_admin"]["nsState"]
99 vnf_membex_index = vnfr["member-vnf-index-ref"]
100 print(
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}",
105 )
106 # Only send topology if ns State is one of the nsAllowedStatesSet
107 if ns_state not in nsAllowedStatesSet:
108 continue
109
110 print("VDU list:")
111 for vdu in vnfr.get("vdur", []):
112 # Label vdu_id
113 vdu_id = vdu["_id"]
114 # Label vim_id
115 vim_info = vdu.get("vim_info")
116 if not vim_info:
117 print("Error: vim_info not available in vdur")
118 continue
119 if len(vim_info) != 1:
120 print("Error: more than one vim_info in vdur")
121 continue
122 vim_id = next(iter(vim_info))[4:]
123 # Label vm_id
124 vm_id = vdu["vim-id"]
125 # Other VDU info
126 vdu_name = vdu.get("name", "UNKNOWN")
127 print(
128 f" id: {vdu_id}, name: {vdu_name}, "
129 f"vim_id: {vim_id}, vm_id: {vm_id}"
130 )
131 print(
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}",
135 )
136 metric.labels(
137 ns_id,
138 project_id,
139 vnf_id,
140 vdu_id,
141 vm_id,
142 vim_id,
143 vdu_name,
144 vnf_membex_index,
145 ).set(1)
146
147 # print("Push to gateway")
148 push_to_gateway(
149 gateway=PROMETHEUS_PUSHGW, job=PROMETHEUS_JOB, registry=registry
150 )
151 return
152
153 get_topology()
154
155
156 dag = ns_topology()