Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import, division |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 16 | import arrow |
William Kurkian | ede82e9 | 2019-03-05 13:02:57 -0500 | [diff] [blame] | 17 | from voltha_protos.device_pb2 import PmConfig, PmGroupConfig |
| 18 | from voltha_protos.events_pb2 import MetricInformation, MetricMetaData |
Devmalya Paul | 0d3abf0 | 2019-07-31 18:34:27 -0400 | [diff] [blame] | 19 | from pyvoltha.adapters.extensions.events.kpi.adapter_pm_metrics import AdapterPmMetrics |
| 20 | from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_interval_metrics import OnuPmIntervalMetrics |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 21 | from pyvoltha.adapters.extensions.omci.omci_entities import UniG |
| 22 | from pyvoltha.adapters.extensions.omci.omci_entities import PptpEthernetUni |
| 23 | |
| 24 | |
| 25 | class OnuOmciPmMetrics(AdapterPmMetrics): |
| 26 | """ ONU OMCI related metrics """ |
| 27 | |
| 28 | # Metric default settings |
| 29 | # |
Chip Boling | de43a70 | 2021-01-26 11:46:38 -0600 | [diff] [blame^] | 30 | # Frequency values are in units of one second |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 31 | # |
| 32 | OMCI_DEV_KEY = 'omci-onu-dev' |
| 33 | OMCI_CC_GROUP_NAME = 'OMCI_CC' |
| 34 | DEFAULT_OMCI_CC_ENABLED = False |
Chip Boling | de43a70 | 2021-01-26 11:46:38 -0600 | [diff] [blame^] | 35 | DEFAULT_OMCI_CC_FREQUENCY = (2 * 60) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 36 | |
| 37 | OPTICAL_GROUP_NAME = 'PON_Optical' |
| 38 | DEFAULT_OPTICAL_ENABLED = True |
Chip Boling | de43a70 | 2021-01-26 11:46:38 -0600 | [diff] [blame^] | 39 | DEFAULT_OPTICAL_FREQUENCY = (15 * 60) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 40 | |
| 41 | UNI_STATUS_GROUP_NAME = 'UNI_Status' |
| 42 | DEFAULT_UNI_STATUS_ENABLED = True |
Chip Boling | de43a70 | 2021-01-26 11:46:38 -0600 | [diff] [blame^] | 43 | DEFAULT_UNI_STATUS_FREQUENCY = (15 * 60) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 44 | |
Devmalya Paul | 0d3abf0 | 2019-07-31 18:34:27 -0400 | [diff] [blame] | 45 | def __init__(self, event_mgr, core_proxy, device_id, logical_device_id, serial_number, |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 46 | grouped=False, freq_override=False, **kwargs): |
| 47 | """ |
| 48 | Initializer for shared ONU Device Adapter OMCI CC PM metrics |
| 49 | |
William Kurkian | 6eda818 | 2019-05-03 15:51:54 -0400 | [diff] [blame] | 50 | :param core_proxy: (CoreProxy) Core proxy for the device |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 51 | :param device_id: (str) Device ID |
| 52 | :param logical_device_id: (str) VOLTHA Logical Device ID |
| 53 | :param grouped: (bool) Flag indicating if statistics are managed as a group |
| 54 | :param freq_override: (bool) Flag indicating if frequency collection can be specified |
| 55 | on a per group basis |
| 56 | :param kwargs: (dict) Device Adapter specific values. For an ONU Device adapter, the |
| 57 | expected key-value pairs are listed below. If not provided, the |
| 58 | associated PM statistics are not gathered: |
| 59 | |
| 60 | 'omci-onu-dev': Reference to the OMCI OnuDeviceEtnry object for |
| 61 | retrieval of OpenOMCI Communications channel statistics |
| 62 | and retrieval of polled statistics. |
| 63 | """ |
Devmalya Paul | 0d3abf0 | 2019-07-31 18:34:27 -0400 | [diff] [blame] | 64 | super(OnuOmciPmMetrics, self).__init__(event_mgr, core_proxy, device_id, logical_device_id, serial_number, |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 65 | grouped=grouped, freq_override=freq_override, |
| 66 | **kwargs) |
| 67 | |
| 68 | self._omci_onu_device = kwargs.pop(OnuOmciPmMetrics.OMCI_DEV_KEY, None) |
| 69 | self._omci_cc = self._omci_onu_device.omci_cc if self._omci_onu_device is not None else None |
| 70 | |
| 71 | self.omci_cc_pm_names = { |
| 72 | ('tx_frames', PmConfig.COUNTER), |
| 73 | ('tx_errors', PmConfig.COUNTER), |
| 74 | ('rx_frames', PmConfig.COUNTER), |
| 75 | ('rx_unknown_tid', PmConfig.COUNTER), |
| 76 | ('rx_onu_frames', PmConfig.COUNTER), # Rx ONU autonomous messages |
| 77 | ('rx_unknown_me', PmConfig.COUNTER), # Managed Entities without a decode definition |
| 78 | ('rx_timeouts', PmConfig.COUNTER), |
| 79 | ('rx_late', PmConfig.COUNTER), |
| 80 | ('consecutive_errors', PmConfig.COUNTER), |
| 81 | ('reply_min', PmConfig.GAUGE), # Milliseconds |
| 82 | ('reply_max', PmConfig.GAUGE), # Milliseconds |
| 83 | ('reply_average', PmConfig.GAUGE), # Milliseconds |
| 84 | ('hp_tx_queue_len', PmConfig.GAUGE), |
| 85 | ('lp_tx_queue_len', PmConfig.GAUGE), |
| 86 | ('max_hp_tx_queue', PmConfig.GAUGE), |
| 87 | ('max_lp_tx_queue', PmConfig.GAUGE), |
| 88 | } |
| 89 | self.omci_cc_metrics_config = {m: PmConfig(name=m, type=t, enabled=True) |
| 90 | for (m, t) in self.omci_cc_pm_names} |
| 91 | |
| 92 | self.omci_optical_pm_names = { |
| 93 | ('intf_id', PmConfig.CONTEXT), |
| 94 | |
| 95 | ('transmit_power', PmConfig.GAUGE), |
| 96 | ('receive_power', PmConfig.GAUGE), |
| 97 | } |
| 98 | self.omci_optical_metrics_config = {m: PmConfig(name=m, type=t, enabled=True) |
| 99 | for (m, t) in self.omci_optical_pm_names} |
| 100 | |
| 101 | self.omci_uni_pm_names = { |
| 102 | ('intf_id', PmConfig.CONTEXT), |
| 103 | |
| 104 | ('ethernet_type', PmConfig.GAUGE), # PPTP Ethernet ME |
| 105 | ('oper_status', PmConfig.GAUGE), # PPTP Ethernet ME |
| 106 | ('pptp_admin_state', PmConfig.GAUGE), # PPTP Ethernet ME |
| 107 | ('uni_admin_state', PmConfig.GAUGE), # UNI-G ME |
| 108 | } |
| 109 | self.omci_uni_metrics_config = {m: PmConfig(name=m, type=t, enabled=True) |
| 110 | for (m, t) in self.omci_uni_pm_names} |
| 111 | |
Devmalya Paul | 0d3abf0 | 2019-07-31 18:34:27 -0400 | [diff] [blame] | 112 | self.openomci_interval_pm = OnuPmIntervalMetrics(event_mgr, core_proxy, device_id, logical_device_id, |
Yongjie Zhang | 415a296 | 2019-07-03 15:46:50 -0400 | [diff] [blame] | 113 | serial_number) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 114 | |
Scott Baker | de7c61a | 2020-01-15 14:22:11 -0800 | [diff] [blame] | 115 | self.last_collect_optical_metrics = None |
| 116 | |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 117 | def update(self, pm_config): |
| 118 | # TODO: Test frequency override capability for a particular group |
| 119 | if self.default_freq != pm_config.default_freq: |
| 120 | # Update the callback to the new frequency. |
| 121 | self.default_freq = pm_config.default_freq |
Rohan Agrawal | 9a02868 | 2020-06-01 09:08:14 +0000 | [diff] [blame] | 122 | self.stop_collector() |
| 123 | self.start_collector() |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 124 | |
| 125 | if pm_config.grouped: |
| 126 | for group in pm_config.groups: |
| 127 | group_config = self.pm_group_metrics.get(group.group_name) |
| 128 | if group_config is not None: |
| 129 | group_config.enabled = group.enabled |
| 130 | else: |
| 131 | msg = 'There are on independent OMCI metrics, only group metrics at this time' |
| 132 | raise NotImplemented(msg) |
| 133 | |
| 134 | self.openomci_interval_pm.update(pm_config) |
| 135 | |
| 136 | def make_proto(self, pm_config=None): |
| 137 | assert pm_config is not None |
| 138 | |
| 139 | # OMCI only supports grouped metrics |
| 140 | if self._omci_onu_device is None or not self.grouped: |
| 141 | return pm_config |
| 142 | |
| 143 | pm_omci_cc_stats = PmGroupConfig(group_name=OnuOmciPmMetrics.OMCI_CC_GROUP_NAME, |
| 144 | group_freq=OnuOmciPmMetrics.DEFAULT_OMCI_CC_FREQUENCY, |
| 145 | enabled=OnuOmciPmMetrics.DEFAULT_OMCI_CC_ENABLED) |
| 146 | self.pm_group_metrics[pm_omci_cc_stats.group_name] = pm_omci_cc_stats |
| 147 | |
| 148 | pm_omci_optical_stats = PmGroupConfig(group_name=OnuOmciPmMetrics.OPTICAL_GROUP_NAME, |
| 149 | group_freq=OnuOmciPmMetrics.DEFAULT_OPTICAL_FREQUENCY, |
| 150 | enabled=OnuOmciPmMetrics.DEFAULT_OPTICAL_ENABLED) |
| 151 | self.pm_group_metrics[pm_omci_optical_stats.group_name] = pm_omci_optical_stats |
| 152 | |
| 153 | pm_omci_uni_stats = PmGroupConfig(group_name=OnuOmciPmMetrics.UNI_STATUS_GROUP_NAME, |
| 154 | group_freq=OnuOmciPmMetrics.DEFAULT_UNI_STATUS_FREQUENCY, |
| 155 | enabled=OnuOmciPmMetrics.DEFAULT_UNI_STATUS_ENABLED) |
| 156 | self.pm_group_metrics[pm_omci_uni_stats.group_name] = pm_omci_uni_stats |
| 157 | |
| 158 | stats_and_config = [(pm_omci_cc_stats, self.omci_cc_metrics_config), |
| 159 | (pm_omci_optical_stats, self.omci_optical_metrics_config), |
| 160 | (pm_omci_uni_stats, self.omci_cc_metrics_config)] |
| 161 | |
| 162 | for stats, config in stats_and_config: |
| 163 | for m in sorted(config): |
| 164 | pm = config[m] |
| 165 | stats.metrics.extend([PmConfig(name=pm.name, |
| 166 | type=pm.type, |
| 167 | enabled=pm.enabled)]) |
| 168 | pm_config.groups.extend([stats]) |
| 169 | |
| 170 | # Also create OMCI Interval PM configs |
| 171 | return self.openomci_interval_pm.make_proto(pm_config) |
| 172 | |
| 173 | def collect_metrics(self, data=None): |
| 174 | """ |
| 175 | Collect metrics for this adapter. |
| 176 | |
| 177 | The data collected (or passed in) is a list of pairs/tuples. Each |
| 178 | pair is composed of a MetricMetaData metadata-portion and list of MetricValuePairs |
| 179 | that contains a single individual metric or list of metrics if this is a |
| 180 | group metric. |
| 181 | |
| 182 | This method is called for each adapter at a fixed frequency. |
| 183 | TODO: Currently all group metrics are collected on a single timer tick. |
| 184 | This needs to be fixed as independent group or instance collection is |
| 185 | desirable. |
| 186 | |
| 187 | :param data: (list) Existing list of collected metrics (MetricInformation). |
| 188 | This is provided to allow derived classes to call into |
| 189 | further encapsulated classes. |
| 190 | |
| 191 | :return: (list) metadata and metrics pairs - see description above |
| 192 | """ |
| 193 | if data is None: |
| 194 | data = list() |
| 195 | |
| 196 | # Note: Interval PM is collection done autonomously, not through this method |
| 197 | |
| 198 | if self._omci_cc is not None: |
| 199 | group_name = OnuOmciPmMetrics.OMCI_CC_GROUP_NAME |
| 200 | if self.pm_group_metrics[group_name].enabled: |
| 201 | group_data = self.collect_group_metrics(group_name, |
| 202 | self._omci_cc, |
| 203 | self.omci_cc_pm_names, |
| 204 | self.omci_cc_metrics_config) |
| 205 | if group_data is not None: |
| 206 | data.append(group_data) |
| 207 | |
| 208 | # Optical and UNI data is collected on a per-port basis |
| 209 | data.extend(self.collect_optical_metrics()) |
| 210 | data.extend(self.collect_uni_status_metrics()) |
| 211 | |
| 212 | return data |
| 213 | |
| 214 | def collect_optical_metrics(self): |
| 215 | """ |
| 216 | Collect the metrics for optical information from all ANI/PONs |
| 217 | |
| 218 | :return: (list) collected metrics (MetricInformation) |
| 219 | """ |
| 220 | now = self._omci_onu_device.timestamp |
| 221 | |
| 222 | group_name = OnuOmciPmMetrics.OPTICAL_GROUP_NAME |
| 223 | if now is None or not self.pm_group_metrics[group_name].enabled: |
| 224 | return [] |
| 225 | |
Scott Baker | de7c61a | 2020-01-15 14:22:11 -0800 | [diff] [blame] | 226 | # Because several different metrics are collected by this class, |
| 227 | # and the collection interval may be more often than the group interval, |
| 228 | # check and make sure these metrics are actually due at this time. |
| 229 | if self.last_collect_optical_metrics: |
Chip Boling | de43a70 | 2021-01-26 11:46:38 -0600 | [diff] [blame^] | 230 | elapsed_tenths = now - self.last_collect_optical_metrics |
Scott Baker | de7c61a | 2020-01-15 14:22:11 -0800 | [diff] [blame] | 231 | if elapsed_tenths < self.pm_group_metrics[group_name].group_freq: |
| 232 | self.log.info("collect-optical-metrics-not-time-yet", elapsed_tenths=elapsed_tenths) |
| 233 | return [] |
| 234 | |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 235 | # Scan all ANI-G ports |
| 236 | ani_g_entities = self._omci_onu_device.configuration.ani_g_entities |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 237 | ani_g_entities_ids = list(ani_g_entities.keys()) if ani_g_entities is not None else None |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 238 | metrics_info = [] |
| 239 | |
| 240 | if ani_g_entities_ids is not None and len(ani_g_entities_ids): |
| 241 | from pyvoltha.adapters.extensions.omci.omci_entities import AniG |
| 242 | ani_g_items = ['optical_signal_level', 'transmit_optical_level'] |
| 243 | |
| 244 | for entity_id in ani_g_entities_ids: |
| 245 | metrics = dict() |
| 246 | data = self._omci_onu_device.query_mib(class_id=AniG.class_id, |
| 247 | instance_id=entity_id, |
| 248 | attributes=ani_g_items) |
| 249 | if len(data): |
| 250 | if 'optical_signal_level' in data: |
| 251 | metrics['receive_power'] = data['optical_signal_level'] |
| 252 | |
| 253 | if 'transmit_optical_level' in data: |
| 254 | metrics['transmit_power'] = data['transmit_optical_level'] |
| 255 | |
| 256 | if len(metrics): |
| 257 | metric_data = MetricInformation(metadata=MetricMetaData(title=group_name, |
| 258 | ts=now, |
| 259 | logical_device_id=self.logical_device_id, |
| 260 | serial_no=self.serial_number, |
| 261 | device_id=self.device_id, |
| 262 | context={ |
| 263 | 'intf_id': str(entity_id) |
| 264 | }), |
| 265 | metrics=metrics) |
| 266 | metrics_info.append(metric_data) |
| 267 | |
Scott Baker | de7c61a | 2020-01-15 14:22:11 -0800 | [diff] [blame] | 268 | self.last_collect_optical_metrics = now |
| 269 | |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 270 | return metrics_info |
| 271 | |
| 272 | def collect_uni_status_metrics(self): |
| 273 | """ |
| 274 | Collect the metrics for optical information from all ANI/PONs |
| 275 | |
| 276 | :return: (list) collected metrics (MetricInformation) |
| 277 | """ |
| 278 | now = self._omci_onu_device.timestamp |
| 279 | |
| 280 | group_name = OnuOmciPmMetrics.UNI_STATUS_GROUP_NAME |
| 281 | if now is None or not self.pm_group_metrics[group_name].enabled: |
| 282 | return [] |
| 283 | |
| 284 | # Scan all UNI-G and PPTP ports |
| 285 | uni_g_entities = self._omci_onu_device.configuration.uni_g_entities |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 286 | uni_g_entities_ids = list(uni_g_entities.keys()) if uni_g_entities is not None else None |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 287 | pptp_entities = self._omci_onu_device.configuration.pptp_entities |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 288 | pptp_entities_ids = list(pptp_entities.keys()) if pptp_entities is not None else None |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 289 | |
| 290 | metrics_info = [] |
| 291 | |
| 292 | if uni_g_entities_ids and pptp_entities_ids and len(uni_g_entities_ids) and \ |
| 293 | len(uni_g_entities_ids) <= len(pptp_entities_ids): |
| 294 | |
| 295 | uni_g_items = ['administrative_state'] |
| 296 | pptp_items = ['administrative_state', 'operational_state', 'sensed_type'] |
| 297 | |
| 298 | for entity_id in pptp_entities_ids: |
| 299 | metrics = dict() |
| 300 | data = self._omci_onu_device.query_mib(class_id=UniG.class_id, |
| 301 | instance_id=entity_id, |
| 302 | attributes=uni_g_items) |
| 303 | if len(data): |
| 304 | if 'administrative_state' in data: |
| 305 | metrics['uni_admin_state'] = data['administrative_state'] |
| 306 | |
| 307 | data = self._omci_onu_device.query_mib(class_id=PptpEthernetUni.class_id, |
| 308 | instance_id=entity_id, |
| 309 | attributes=pptp_items) |
| 310 | if len(data): |
| 311 | if 'administrative_state' in data: |
| 312 | metrics['pptp_admin_state'] = data['administrative_state'] |
| 313 | |
| 314 | if 'operational_state' in data: |
| 315 | metrics['oper_status'] = data['operational_state'] |
| 316 | |
| 317 | if 'sensed_type' in data: |
| 318 | metrics['ethernet_type'] = data['sensed_type'] |
| 319 | |
| 320 | if len(metrics): |
| 321 | metric_data = MetricInformation(metadata=MetricMetaData(title=group_name, |
| 322 | ts=now, |
| 323 | logical_device_id=self.logical_device_id, |
| 324 | serial_no=self.serial_number, |
| 325 | device_id=self.device_id, |
| 326 | context={ |
| 327 | 'intf_id': str(entity_id & 0xFF) |
| 328 | }), |
| 329 | metrics=metrics) |
| 330 | metrics_info.append(metric_data) |
| 331 | |
| 332 | return metrics_info |