Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //Package adaptercoreonu provides the utility for onu devices, flows and statistics |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 22 | "fmt" |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 23 | "github.com/looplab/fsm" |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 24 | "github.com/opencord/omci-lib-go" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
| 26 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 27 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 28 | "sync" |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 29 | "time" |
| 30 | ) |
| 31 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 32 | const ( |
| 33 | // events of L2 PM FSM |
| 34 | l2PmEventInit = "l2PmEventInit" |
| 35 | l2PmEventTick = "l2PmEventTick" |
| 36 | l2PmEventSuccess = "l2PmEventSuccess" |
| 37 | l2PmEventFailure = "l2PmEventFailure" |
| 38 | l2PmEventAddMe = "l2PmEventAddMe" |
| 39 | l2PmEventDeleteMe = "l2PmEventDeleteMe" |
| 40 | l2PmEventStop = "l2PmEventStop" |
| 41 | ) |
| 42 | const ( |
| 43 | // states of L2 PM FSM |
| 44 | l2PmStNull = "l2PmStNull" |
| 45 | l2PmStStarting = "l2PmStStarting" |
| 46 | l2PmStSyncTime = "l2PmStSyncTime" |
| 47 | l2PmStIdle = "l2PmStIdle" |
| 48 | l2PmStCreatePmMe = "l2PmStCreatePm" |
| 49 | l2PmStDeletePmMe = "l2PmStDeletePmMe" |
| 50 | l2PmStCollectData = "l2PmStCollectData" |
| 51 | ) |
| 52 | |
| 53 | const cL2PmFsmIdleState = l2PmStIdle |
| 54 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 55 | // general constants used for overall Metric Collection management |
| 56 | const ( |
| 57 | DefaultMetricCollectionFrequency = 15 * 60 // unit in seconds. This setting can be changed from voltha NBI PmConfig configuration |
| 58 | GroupMetricEnabled = true // This is READONLY and cannot be changed from VOLTHA NBI |
| 59 | DefaultFrequencyOverrideEnabled = true // This is READONLY and cannot be changed from VOLTHA NBI |
| 60 | FrequencyGranularity = 5 // The frequency (in seconds) has to be multiple of 5. This setting cannot changed later. |
| 61 | ) |
| 62 | |
| 63 | // OpticalPowerGroupMetrics are supported optical pm names |
| 64 | var OpticalPowerGroupMetrics = map[string]voltha.PmConfig_PmType{ |
| 65 | "ani_g_instance_id": voltha.PmConfig_CONTEXT, |
| 66 | "transmit_power": voltha.PmConfig_GAUGE, |
| 67 | "receive_power": voltha.PmConfig_GAUGE, |
| 68 | } |
| 69 | |
| 70 | // OpticalPowerGroupMetrics specific constants |
| 71 | const ( |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 72 | OpticalPowerGroupMetricName = "PON_Optical" |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 73 | OpticalPowerGroupMetricEnabled = true // This setting can be changed from voltha NBI PmConfig configuration |
| 74 | OpticalPowerMetricGroupCollectionFrequency = 5 * 60 // unit in seconds. This setting can be changed from voltha NBI PmConfig configuration |
| 75 | ) |
| 76 | |
| 77 | // UniStatusGroupMetrics are supported UNI status names |
| 78 | var UniStatusGroupMetrics = map[string]voltha.PmConfig_PmType{ |
| 79 | "uni_port_no": voltha.PmConfig_CONTEXT, |
| 80 | "ethernet_type": voltha.PmConfig_GAUGE, |
| 81 | "oper_status": voltha.PmConfig_GAUGE, |
| 82 | "uni_admin_state": voltha.PmConfig_GAUGE, |
| 83 | } |
| 84 | |
| 85 | // UniStatusGroupMetrics specific constants |
| 86 | const ( |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 87 | UniStatusGroupMetricName = "UNI_Status" |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 88 | UniStatusGroupMetricEnabled = true // This setting can be changed from voltha NBI PmConfig configuration |
| 89 | UniStatusMetricGroupCollectionFrequency = 5 * 60 // unit in seconds. This setting can be changed from voltha NBI PmConfig configuration |
| 90 | ) |
| 91 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 92 | // *** Classical L2 PM Counters begin *** |
| 93 | |
| 94 | // EthernetBridgeHistory are supported ethernet bridge history counters fetched from |
| 95 | // Ethernet Frame Performance Monitoring History Data Downstream and Ethernet Frame Performance Monitoring History Data Upstream MEs. |
| 96 | var EthernetBridgeHistory = map[string]voltha.PmConfig_PmType{ |
| 97 | "class_id": voltha.PmConfig_CONTEXT, |
| 98 | "entity_id": voltha.PmConfig_CONTEXT, |
| 99 | "interval_end_time": voltha.PmConfig_CONTEXT, |
| 100 | "parent_class_id": voltha.PmConfig_CONTEXT, |
| 101 | "parent_entity_id": voltha.PmConfig_CONTEXT, |
| 102 | "upstream": voltha.PmConfig_CONTEXT, |
| 103 | |
| 104 | "drop_events": voltha.PmConfig_COUNTER, |
| 105 | "octets": voltha.PmConfig_COUNTER, |
| 106 | "packets": voltha.PmConfig_COUNTER, |
| 107 | "broadcast_packets": voltha.PmConfig_COUNTER, |
| 108 | "multicast_packets": voltha.PmConfig_COUNTER, |
| 109 | "crc_errored_packets": voltha.PmConfig_COUNTER, |
| 110 | "undersize_packets": voltha.PmConfig_COUNTER, |
| 111 | "oversize_packets": voltha.PmConfig_COUNTER, |
| 112 | "64_octets": voltha.PmConfig_COUNTER, |
| 113 | "65_to_127_octets": voltha.PmConfig_COUNTER, |
| 114 | "128_to_255_octets": voltha.PmConfig_COUNTER, |
| 115 | "256_to_511_octets": voltha.PmConfig_COUNTER, |
| 116 | "512_to_1023_octets": voltha.PmConfig_COUNTER, |
| 117 | "1024_to_1518_octets": voltha.PmConfig_COUNTER, |
| 118 | } |
| 119 | |
| 120 | // EthernetUniHistory are supported ethernet uni history counters fetched from |
| 121 | // Ethernet Performance Monitoring History Data ME. |
| 122 | var EthernetUniHistory = map[string]voltha.PmConfig_PmType{ |
| 123 | "class_id": voltha.PmConfig_CONTEXT, |
| 124 | "entity_id": voltha.PmConfig_CONTEXT, |
| 125 | "interval_end_time": voltha.PmConfig_CONTEXT, |
| 126 | |
| 127 | "fcs_errors": voltha.PmConfig_COUNTER, |
| 128 | "excessive_collision_counter": voltha.PmConfig_COUNTER, |
| 129 | "late_collision_counter": voltha.PmConfig_COUNTER, |
| 130 | "frames_too_long": voltha.PmConfig_COUNTER, |
| 131 | "buffer_overflows_on_rx": voltha.PmConfig_COUNTER, |
| 132 | "buffer_overflows_on_tx": voltha.PmConfig_COUNTER, |
| 133 | "single_collision_frame_counter": voltha.PmConfig_COUNTER, |
| 134 | "multiple_collisions_frame_counter": voltha.PmConfig_COUNTER, |
| 135 | "sqe_counter": voltha.PmConfig_COUNTER, |
| 136 | "deferred_tx_counter": voltha.PmConfig_COUNTER, |
| 137 | "internal_mac_tx_error_counter": voltha.PmConfig_COUNTER, |
| 138 | "carrier_sense_error_counter": voltha.PmConfig_COUNTER, |
| 139 | "alignment_error_counter": voltha.PmConfig_COUNTER, |
| 140 | "internal_mac_rx_error_counter": voltha.PmConfig_COUNTER, |
| 141 | } |
| 142 | |
| 143 | // Constants specific for L2 PM collection |
| 144 | const ( |
| 145 | L2PmCollectionInterval = 15 * 60 // Unit in seconds. Do not change this as this fixed by OMCI specification for L2 PM counters |
| 146 | SyncTimeRetryInterval = 15 // Unit seconds |
| 147 | L2PmCreateAttempts = 3 |
| 148 | L2PmCollectAttempts = 3 |
Girish Gowdra | 453750f | 2021-02-16 16:36:46 -0800 | [diff] [blame^] | 149 | // Per Table 11.2.9-1 – OMCI baseline message limitations in G.988 spec, the max GET Response |
| 150 | // payload size is 25. We define 24 (one less) to allow for dynamic insertion of IntervalEndTime |
| 151 | // attribute (1 byte) in L2 PM GET Requests. |
| 152 | MaxL2PMGetPayLoadSize = 24 |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 153 | ) |
| 154 | |
| 155 | // EthernetUniHistoryName specific constants |
| 156 | const ( |
| 157 | EthernetBridgeHistoryName = "Ethernet_Bridge_Port_History" |
| 158 | EthernetBridgeHistoryEnabled = true // This setting can be changed from voltha NBI PmConfig configuration |
| 159 | EthernetBridgeHistoryFrequency = L2PmCollectionInterval |
| 160 | ) |
| 161 | |
| 162 | // EthernetBridgeHistory specific constants |
| 163 | const ( |
| 164 | EthernetUniHistoryName = "Ethernet_UNI_History" |
| 165 | EthernetUniHistoryEnabled = true // This setting can be changed from voltha NBI PmConfig configuration |
| 166 | EthernetUniHistoryFrequency = L2PmCollectionInterval |
| 167 | ) |
| 168 | |
| 169 | // *** Classical L2 PM Counters end *** |
| 170 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 171 | type groupMetric struct { |
| 172 | groupName string |
| 173 | enabled bool |
| 174 | frequency uint32 // valid only if FrequencyOverride is enabled. |
| 175 | metricMap map[string]voltha.PmConfig_PmType |
| 176 | nextCollectionInterval time.Time // valid only if FrequencyOverride is enabled. |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 177 | isL2PMCounter bool // true for only L2 PM counters |
| 178 | collectAttempts uint32 // number of attempts to collect L2 PM data |
| 179 | createRetryAttempts uint32 // number of attempts to try creating the L2 PM ME |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | type standaloneMetric struct { |
| 183 | metricName string |
| 184 | enabled bool |
| 185 | frequency uint32 // valid only if FrequencyOverride is enabled. |
| 186 | nextCollectionInterval time.Time // valid only if FrequencyOverride is enabled. |
| 187 | } |
| 188 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 189 | type onuMetricsManager struct { |
| 190 | pDeviceHandler *deviceHandler |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 191 | pAdaptFsm *AdapterFsm |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 192 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 193 | opticalMetricsChan chan me.AttributeValueMap |
| 194 | uniStatusMetricsChan chan me.AttributeValueMap |
| 195 | l2PmChan chan me.AttributeValueMap |
| 196 | syncTimeResponseChan chan bool // true is success, false is fail |
| 197 | l2PmCreateOrDeleteResponseChan chan bool // true is success, false is fail |
| 198 | |
| 199 | activeL2Pms []string // list of active l2 pm MEs created on the ONU. |
| 200 | l2PmToDelete []string // list of L2 PMs to delete |
| 201 | l2PmToAdd []string // list of L2 PM to add |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 202 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 203 | groupMetricMap map[string]*groupMetric |
| 204 | standaloneMetricMap map[string]*standaloneMetric |
| 205 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 206 | stopProcessingOmciResponses chan bool |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 207 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 208 | stopTicks chan bool |
| 209 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 210 | nextGlobalMetricCollectionTime time.Time // valid only if pmConfig.FreqOverride is set to false. |
| 211 | |
| 212 | onuMetricsManagerLock sync.RWMutex |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // newonuMetricsManager returns a new instance of the newonuMetricsManager |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 216 | // Note that none of the context stored internally in onuMetricsManager is backed up on KV store for resiliency. |
| 217 | // Metric collection is not a critical operation that needs support for resiliency. On adapter restart, some context |
| 218 | // could be lost (except for Device.PmConfigs which is backed up the rw-core on KV store). An example of information |
| 219 | // that is lost on adapter restart is nextCollectionInterval time. |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 220 | func newonuMetricsManager(ctx context.Context, dh *deviceHandler) *onuMetricsManager { |
| 221 | |
| 222 | var metricsManager onuMetricsManager |
| 223 | logger.Debugw(ctx, "init-onuMetricsManager", log.Fields{"device-id": dh.deviceID}) |
| 224 | metricsManager.pDeviceHandler = dh |
| 225 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 226 | commMetricsChan := make(chan Message) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 227 | metricsManager.opticalMetricsChan = make(chan me.AttributeValueMap) |
| 228 | metricsManager.uniStatusMetricsChan = make(chan me.AttributeValueMap) |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 229 | metricsManager.l2PmChan = make(chan me.AttributeValueMap) |
| 230 | |
| 231 | metricsManager.syncTimeResponseChan = make(chan bool) |
| 232 | metricsManager.l2PmCreateOrDeleteResponseChan = make(chan bool) |
| 233 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 234 | metricsManager.stopProcessingOmciResponses = make(chan bool) |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 235 | metricsManager.stopTicks = make(chan bool) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 236 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 237 | metricsManager.groupMetricMap = make(map[string]*groupMetric) |
| 238 | metricsManager.standaloneMetricMap = make(map[string]*standaloneMetric) |
| 239 | |
| 240 | if dh.pmConfigs == nil { // dh.pmConfigs is NOT nil if adapter comes back from a restart. We should NOT go back to defaults in this case |
| 241 | dh.pmConfigs = &voltha.PmConfigs{} |
| 242 | dh.pmConfigs.Id = dh.deviceID |
| 243 | dh.pmConfigs.DefaultFreq = DefaultMetricCollectionFrequency |
| 244 | dh.pmConfigs.Grouped = GroupMetricEnabled |
| 245 | dh.pmConfigs.FreqOverride = DefaultFrequencyOverrideEnabled |
| 246 | |
| 247 | // Populate group metrics. |
| 248 | // Lets populate irrespective of GroupMetricEnabled is true or not. |
| 249 | // The group metrics collection will decided on this flag later |
| 250 | |
| 251 | // Populate optical power group metrics |
| 252 | var opPmConfigSlice []*voltha.PmConfig |
| 253 | for k, v := range OpticalPowerGroupMetrics { |
| 254 | opPmConfigSlice = append(opPmConfigSlice, &voltha.PmConfig{Name: k, Type: v}) |
| 255 | } |
| 256 | opticalPowerGroupMetric := voltha.PmGroupConfig{ |
| 257 | GroupName: OpticalPowerGroupMetricName, |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 258 | Enabled: OpticalPowerGroupMetricEnabled && dh.pOpenOnuAc.metricsEnabled, |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 259 | GroupFreq: OpticalPowerMetricGroupCollectionFrequency, |
| 260 | Metrics: opPmConfigSlice, |
| 261 | } |
| 262 | dh.pmConfigs.Groups = append(dh.pmConfigs.Groups, &opticalPowerGroupMetric) |
| 263 | |
| 264 | // Populate uni status group metrics |
| 265 | var uniStPmConfigSlice []*voltha.PmConfig |
| 266 | for k, v := range UniStatusGroupMetrics { |
| 267 | uniStPmConfigSlice = append(uniStPmConfigSlice, &voltha.PmConfig{Name: k, Type: v}) |
| 268 | } |
| 269 | uniStatusGroupMetric := voltha.PmGroupConfig{ |
| 270 | GroupName: UniStatusGroupMetricName, |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 271 | Enabled: UniStatusGroupMetricEnabled && dh.pOpenOnuAc.metricsEnabled, |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 272 | GroupFreq: UniStatusMetricGroupCollectionFrequency, |
| 273 | Metrics: uniStPmConfigSlice, |
| 274 | } |
| 275 | dh.pmConfigs.Groups = append(dh.pmConfigs.Groups, &uniStatusGroupMetric) |
| 276 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 277 | // classical l2 pm counter start |
| 278 | |
| 279 | // Populate ethernet bridge history group metrics |
| 280 | var ethBridgeHistoryConfigSlice []*voltha.PmConfig |
| 281 | for k, v := range EthernetBridgeHistory { |
| 282 | ethBridgeHistoryConfigSlice = append(ethBridgeHistoryConfigSlice, &voltha.PmConfig{Name: k, Type: v}) |
| 283 | } |
| 284 | ethBridgeHistoryGroupMetric := voltha.PmGroupConfig{ |
| 285 | GroupName: EthernetBridgeHistoryName, |
| 286 | Enabled: EthernetBridgeHistoryEnabled && dh.pOpenOnuAc.metricsEnabled, |
| 287 | GroupFreq: EthernetBridgeHistoryFrequency, |
| 288 | Metrics: ethBridgeHistoryConfigSlice, |
| 289 | } |
| 290 | dh.pmConfigs.Groups = append(dh.pmConfigs.Groups, ðBridgeHistoryGroupMetric) |
| 291 | |
| 292 | // Populate ethernet bridge history group metrics |
| 293 | var ethUniHistoryConfigSlice []*voltha.PmConfig |
| 294 | for k, v := range EthernetUniHistory { |
| 295 | ethUniHistoryConfigSlice = append(ethUniHistoryConfigSlice, &voltha.PmConfig{Name: k, Type: v}) |
| 296 | } |
| 297 | ethUniHistoryGroupMetric := voltha.PmGroupConfig{ |
| 298 | GroupName: EthernetUniHistoryName, |
| 299 | Enabled: EthernetUniHistoryEnabled && dh.pOpenOnuAc.metricsEnabled, |
| 300 | GroupFreq: EthernetUniHistoryFrequency, |
| 301 | Metrics: ethUniHistoryConfigSlice, |
| 302 | } |
| 303 | dh.pmConfigs.Groups = append(dh.pmConfigs.Groups, ðUniHistoryGroupMetric) |
| 304 | |
| 305 | // classical l2 pm counter end |
| 306 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 307 | // Add standalone metric (if present) after this (will be added to dh.pmConfigs.Metrics) |
| 308 | } |
| 309 | |
| 310 | // Populate local group metric structures |
| 311 | for _, g := range dh.pmConfigs.Groups { |
| 312 | metricsManager.groupMetricMap[g.GroupName] = &groupMetric{ |
| 313 | groupName: g.GroupName, |
| 314 | enabled: g.Enabled, |
| 315 | frequency: g.GroupFreq, |
| 316 | } |
| 317 | switch g.GroupName { |
| 318 | case OpticalPowerGroupMetricName: |
| 319 | metricsManager.groupMetricMap[g.GroupName].metricMap = OpticalPowerGroupMetrics |
| 320 | case UniStatusGroupMetricName: |
| 321 | metricsManager.groupMetricMap[g.GroupName].metricMap = UniStatusGroupMetrics |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 322 | case EthernetBridgeHistoryName: |
| 323 | metricsManager.groupMetricMap[g.GroupName].metricMap = EthernetBridgeHistory |
| 324 | metricsManager.groupMetricMap[g.GroupName].isL2PMCounter = true |
| 325 | case EthernetUniHistoryName: |
| 326 | metricsManager.groupMetricMap[g.GroupName].metricMap = EthernetUniHistory |
| 327 | metricsManager.groupMetricMap[g.GroupName].isL2PMCounter = true |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 328 | default: |
| 329 | logger.Errorw(ctx, "unhandled-group-name", log.Fields{"groupName": g.GroupName}) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Populate local standalone metric structures |
| 334 | for _, m := range dh.pmConfigs.Metrics { |
| 335 | metricsManager.standaloneMetricMap[m.Name] = &standaloneMetric{ |
| 336 | metricName: m.Name, |
| 337 | enabled: m.Enabled, |
| 338 | frequency: m.SampleFreq, |
| 339 | } |
| 340 | switch m.Name { |
| 341 | // None exist as of now. Add when available. |
| 342 | default: |
| 343 | logger.Errorw(ctx, "unhandled-metric-name", log.Fields{"metricName": m.Name}) |
| 344 | } |
| 345 | } |
| 346 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 347 | metricsManager.pAdaptFsm = NewAdapterFsm("L2PmFSM", dh.deviceID, commMetricsChan) |
| 348 | if metricsManager.pAdaptFsm == nil { |
| 349 | logger.Errorw(ctx, "L2PMFsm AdapterFsm could not be instantiated!!", log.Fields{ |
| 350 | "device-id": dh.deviceID}) |
| 351 | return nil |
| 352 | } |
| 353 | // L2 PM FSM related state machine |
| 354 | metricsManager.pAdaptFsm.pFsm = fsm.NewFSM( |
| 355 | l2PmStNull, |
| 356 | fsm.Events{ |
| 357 | {Name: l2PmEventInit, Src: []string{l2PmStNull}, Dst: l2PmStStarting}, |
| 358 | {Name: l2PmEventTick, Src: []string{l2PmStStarting}, Dst: l2PmStSyncTime}, |
| 359 | {Name: l2PmEventTick, Src: []string{l2PmStIdle, l2PmEventDeleteMe, l2PmEventAddMe}, Dst: l2PmStCollectData}, |
| 360 | {Name: l2PmEventSuccess, Src: []string{l2PmStSyncTime, l2PmStCreatePmMe, l2PmStDeletePmMe, l2PmStCollectData}, Dst: l2PmStIdle}, |
| 361 | {Name: l2PmEventFailure, Src: []string{l2PmStCreatePmMe, l2PmStDeletePmMe, l2PmStCollectData}, Dst: l2PmStIdle}, |
| 362 | {Name: l2PmEventFailure, Src: []string{l2PmStSyncTime}, Dst: l2PmStSyncTime}, |
| 363 | {Name: l2PmEventAddMe, Src: []string{l2PmStIdle}, Dst: l2PmStCreatePmMe}, |
| 364 | {Name: l2PmEventDeleteMe, Src: []string{l2PmStIdle}, Dst: l2PmStDeletePmMe}, |
| 365 | {Name: l2PmEventStop, Src: []string{l2PmStNull, l2PmStStarting, l2PmStSyncTime, l2PmStIdle, l2PmStCreatePmMe, l2PmStDeletePmMe, l2PmStCollectData}, Dst: l2PmStNull}, |
| 366 | }, |
| 367 | fsm.Callbacks{ |
| 368 | "enter_state": func(e *fsm.Event) { metricsManager.pAdaptFsm.logFsmStateChange(ctx, e) }, |
| 369 | "enter_" + l2PmStNull: func(e *fsm.Event) { metricsManager.l2PMFsmNull(ctx, e) }, |
| 370 | "enter_" + l2PmStIdle: func(e *fsm.Event) { metricsManager.l2PMFsmIdle(ctx, e) }, |
| 371 | "enter_" + l2PmStStarting: func(e *fsm.Event) { metricsManager.l2PMFsmStarting(ctx, e) }, |
| 372 | "enter_" + l2PmStSyncTime: func(e *fsm.Event) { metricsManager.l2PMFsmSyncTime(ctx, e) }, |
| 373 | "enter_" + l2PmStCollectData: func(e *fsm.Event) { metricsManager.l2PmFsmCollectData(ctx, e) }, |
| 374 | "enter_" + l2PmStCreatePmMe: func(e *fsm.Event) { metricsManager.l2PmFsmCreatePM(ctx, e) }, |
| 375 | "enter_" + l2PmStDeletePmMe: func(e *fsm.Event) { metricsManager.l2PmFsmDeletePM(ctx, e) }, |
| 376 | }, |
| 377 | ) |
| 378 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 379 | // initialize the next metric collection intervals. |
| 380 | metricsManager.initializeMetricCollectionTime(ctx) |
| 381 | logger.Info(ctx, "init-onuMetricsManager completed", log.Fields{"device-id": dh.deviceID}) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 382 | return &metricsManager |
| 383 | } |
| 384 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 385 | func (mm *onuMetricsManager) initializeMetricCollectionTime(ctx context.Context) { |
| 386 | if mm.pDeviceHandler.pmConfigs.FreqOverride { |
| 387 | // If mm.pDeviceHandler.pmConfigs.FreqOverride is set to true, then group/standalone metric specific interval applies |
| 388 | mm.onuMetricsManagerLock.Lock() |
| 389 | defer mm.onuMetricsManagerLock.Unlock() |
| 390 | for _, v := range mm.groupMetricMap { |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 391 | if v.enabled && !v.isL2PMCounter { // L2 PM counter collection is managed in a L2PmFsm |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 392 | v.nextCollectionInterval = time.Now().Add(time.Duration(v.frequency) * time.Second) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | for _, v := range mm.standaloneMetricMap { |
| 397 | if v.enabled { |
| 398 | v.nextCollectionInterval = time.Now().Add(time.Duration(v.frequency) * time.Second) |
| 399 | } |
| 400 | } |
| 401 | } else { |
| 402 | // If mm.pDeviceHandler.pmConfigs.FreqOverride is set to false, then overall metric specific interval applies |
| 403 | mm.nextGlobalMetricCollectionTime = time.Now().Add(time.Duration(mm.pDeviceHandler.pmConfigs.DefaultFreq) * time.Second) |
| 404 | } |
| 405 | logger.Infow(ctx, "initialized standalone group/metric collection time", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 406 | } |
| 407 | |
| 408 | func (mm *onuMetricsManager) updateDefaultFrequency(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 409 | // Verify that the configured DefaultFrequency is > 0 and is a multiple of FrequencyGranularity |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 410 | if pmConfigs.DefaultFreq == 0 || (pmConfigs.DefaultFreq > 0 && pmConfigs.DefaultFreq%FrequencyGranularity != 0) { |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 411 | logger.Errorf(ctx, "frequency-%u-should-be-a-multiple-of-%u", pmConfigs.DefaultFreq, FrequencyGranularity) |
| 412 | return fmt.Errorf("frequency-%d-should-be-a-multiple-of-%d", pmConfigs.DefaultFreq, FrequencyGranularity) |
| 413 | } |
| 414 | mm.pDeviceHandler.pmConfigs.DefaultFreq = pmConfigs.DefaultFreq |
| 415 | // re-set the nextGlobalMetricCollectionTime based on the new DefaultFreq |
| 416 | mm.nextGlobalMetricCollectionTime = time.Now().Add(time.Duration(mm.pDeviceHandler.pmConfigs.DefaultFreq) * time.Second) |
| 417 | logger.Debugw(ctx, "frequency-updated--new-frequency", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "frequency": mm.pDeviceHandler.pmConfigs.DefaultFreq}) |
| 418 | return nil |
| 419 | } |
| 420 | |
| 421 | func (mm *onuMetricsManager) updateGroupFreq(ctx context.Context, aGroupName string, pmConfigs *voltha.PmConfigs) error { |
| 422 | var newGroupFreq uint32 |
| 423 | found := false |
| 424 | groupSliceIdx := 0 |
| 425 | var group *voltha.PmGroupConfig |
| 426 | for groupSliceIdx, group = range pmConfigs.Groups { |
| 427 | if group.GroupName == aGroupName { |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 428 | // freq 0 is not allowed and it should be multiple of FrequencyGranularity |
| 429 | if group.GroupFreq == 0 || (group.GroupFreq > 0 && group.GroupFreq%FrequencyGranularity != 0) { |
| 430 | logger.Errorf(ctx, "frequency-%u-should-be-a-multiple-of-%u", group.GroupFreq, FrequencyGranularity) |
| 431 | return fmt.Errorf("frequency-%d-should-be-a-multiple-of-%d", group.GroupFreq, FrequencyGranularity) |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 432 | } |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 433 | newGroupFreq = group.GroupFreq |
| 434 | found = true |
| 435 | break |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | // if not found update group freq and next collection interval for the group |
| 439 | if !found { |
| 440 | logger.Errorw(ctx, "group name not found", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": aGroupName}) |
| 441 | return fmt.Errorf("group-name-not-found-%v", aGroupName) |
| 442 | } |
| 443 | |
| 444 | updated := false |
| 445 | mm.onuMetricsManagerLock.Lock() |
| 446 | defer mm.onuMetricsManagerLock.Unlock() |
| 447 | for k, v := range mm.groupMetricMap { |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 448 | if k == aGroupName && !v.isL2PMCounter { // We cannot allow the L2 PM counter frequency to be updated. It is 15min fixed by OMCI spec |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 449 | v.frequency = newGroupFreq |
| 450 | // update internal pm config |
| 451 | mm.pDeviceHandler.pmConfigs.Groups[groupSliceIdx].GroupFreq = newGroupFreq |
| 452 | // Also updated the next group metric collection time from now |
| 453 | v.nextCollectionInterval = time.Now().Add(time.Duration(newGroupFreq) * time.Second) |
| 454 | updated = true |
| 455 | logger.Infow(ctx, "group frequency updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "newGroupFreq": newGroupFreq, "groupName": aGroupName}) |
| 456 | } |
| 457 | } |
| 458 | if !updated { |
| 459 | logger.Errorw(ctx, "group frequency not updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "newGroupFreq": newGroupFreq, "groupName": aGroupName}) |
| 460 | return fmt.Errorf("internal-error-during-group-freq-update--groupname-%s-freq-%d", aGroupName, newGroupFreq) |
| 461 | } |
| 462 | return nil |
| 463 | } |
| 464 | |
| 465 | func (mm *onuMetricsManager) updateMetricFreq(ctx context.Context, aMetricName string, pmConfigs *voltha.PmConfigs) error { |
| 466 | var newMetricFreq uint32 |
| 467 | found := false |
| 468 | metricSliceIdx := 0 |
| 469 | var metric *voltha.PmConfig |
| 470 | for metricSliceIdx, metric = range pmConfigs.Metrics { |
| 471 | if metric.Name == aMetricName { |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 472 | // freq 0 is not allowed and it should be multiple of FrequencyGranularity |
| 473 | if metric.SampleFreq == 0 || (metric.SampleFreq > 0 && metric.SampleFreq%FrequencyGranularity != 0) { |
| 474 | logger.Errorf(ctx, "frequency-%u-should-be-a-multiple-of-%u", metric.SampleFreq, FrequencyGranularity) |
| 475 | return fmt.Errorf("frequency-%d-should-be-a-multiple-of-%d", metric.SampleFreq, FrequencyGranularity) |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 476 | } |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 477 | newMetricFreq = metric.SampleFreq |
| 478 | found = true |
| 479 | break |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | if !found { |
| 483 | logger.Errorw(ctx, "metric name not found", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": aMetricName}) |
| 484 | return fmt.Errorf("metric-name-not-found-%v", aMetricName) |
| 485 | } |
| 486 | |
| 487 | updated := false |
| 488 | mm.onuMetricsManagerLock.Lock() |
| 489 | defer mm.onuMetricsManagerLock.Unlock() |
| 490 | for k, v := range mm.groupMetricMap { |
Girish Gowdra | af0ad63 | 2021-01-27 13:00:01 -0800 | [diff] [blame] | 491 | if k == aMetricName { |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 492 | v.frequency = newMetricFreq |
| 493 | // update internal pm config |
| 494 | mm.pDeviceHandler.pmConfigs.Metrics[metricSliceIdx].SampleFreq = newMetricFreq |
| 495 | // Also updated the next standalone metric collection time from now |
| 496 | v.nextCollectionInterval = time.Now().Add(time.Duration(newMetricFreq) * time.Second) |
| 497 | updated = true |
| 498 | logger.Infow(ctx, "metric frequency updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "newMetricFreq": newMetricFreq, "aMetricName": aMetricName}) |
| 499 | } |
| 500 | } |
| 501 | if !updated { |
| 502 | logger.Errorw(ctx, "metric frequency not updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "newMetricFreq": newMetricFreq, "aMetricName": aMetricName}) |
| 503 | return fmt.Errorf("internal-error-during-standalone-metric-update--matricnane-%s-freq-%d", aMetricName, newMetricFreq) |
| 504 | } |
| 505 | return nil |
| 506 | } |
| 507 | |
| 508 | func (mm *onuMetricsManager) updateGroupSupport(ctx context.Context, aGroupName string, pmConfigs *voltha.PmConfigs) error { |
| 509 | groupSliceIdx := 0 |
| 510 | var group *voltha.PmGroupConfig |
| 511 | |
| 512 | for groupSliceIdx, group = range pmConfigs.Groups { |
| 513 | if group.GroupName == aGroupName { |
| 514 | break |
| 515 | } |
| 516 | } |
| 517 | if group == nil { |
| 518 | logger.Errorw(ctx, "group metric not found", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": aGroupName}) |
| 519 | return fmt.Errorf("group-not-found--groupName-%s", aGroupName) |
| 520 | } |
| 521 | |
| 522 | updated := false |
| 523 | mm.onuMetricsManagerLock.Lock() |
| 524 | defer mm.onuMetricsManagerLock.Unlock() |
| 525 | for k, v := range mm.groupMetricMap { |
| 526 | if k == aGroupName && v.enabled != group.Enabled { |
| 527 | mm.pDeviceHandler.pmConfigs.Groups[groupSliceIdx].Enabled = group.Enabled |
| 528 | v.enabled = group.Enabled |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 529 | if group.Enabled { |
| 530 | if v.isL2PMCounter { |
| 531 | // If it is a L2 PM counter we need to mark the PM to be added |
| 532 | mm.l2PmToAdd = mm.appendIfMissing(mm.l2PmToAdd, v.groupName) |
| 533 | // If the group support flag toggles too soon, we need to delete the group name from l2PmToDelete slice |
| 534 | mm.l2PmToDelete = mm.removeIfFound(mm.l2PmToDelete, v.groupName) |
| 535 | } else if mm.pDeviceHandler.pmConfigs.FreqOverride { // otherwise just update the next collection interval |
| 536 | v.nextCollectionInterval = time.Now().Add(time.Duration(v.frequency) * time.Second) |
| 537 | } |
| 538 | } else { // group counter is disabled |
| 539 | if v.isL2PMCounter { |
| 540 | // If it is a L2 PM counter we need to mark the PM to be deleted |
| 541 | mm.l2PmToDelete = mm.appendIfMissing(mm.l2PmToDelete, v.groupName) |
| 542 | // If the group support flag toggles too soon, we need to delete the group name from l2PmToAdd slice |
| 543 | mm.l2PmToAdd = mm.removeIfFound(mm.l2PmToAdd, v.groupName) |
| 544 | } |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 545 | } |
| 546 | updated = true |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 547 | if v.isL2PMCounter { |
| 548 | logger.Infow(ctx, "l2 pm group metric support updated", |
| 549 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": aGroupName, "enabled": group.Enabled, "l2PmToAdd": mm.l2PmToAdd, "l2PmToDelete": mm.l2PmToDelete}) |
| 550 | } else { |
| 551 | logger.Infow(ctx, "group metric support updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": aGroupName, "enabled": group.Enabled}) |
| 552 | } |
| 553 | break |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | if !updated { |
| 558 | logger.Errorw(ctx, "group metric support not updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": aGroupName}) |
| 559 | return fmt.Errorf("internal-error-during-group-support-update--groupName-%s", aGroupName) |
| 560 | } |
| 561 | return nil |
| 562 | } |
| 563 | |
| 564 | func (mm *onuMetricsManager) updateMetricSupport(ctx context.Context, aMetricName string, pmConfigs *voltha.PmConfigs) error { |
| 565 | metricSliceIdx := 0 |
| 566 | var metric *voltha.PmConfig |
| 567 | |
| 568 | for metricSliceIdx, metric = range pmConfigs.Metrics { |
| 569 | if metric.Name == aMetricName { |
| 570 | break |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if metric == nil { |
| 575 | logger.Errorw(ctx, "standalone metric not found", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": aMetricName}) |
| 576 | return fmt.Errorf("metric-not-found--metricname-%s", aMetricName) |
| 577 | } |
| 578 | |
| 579 | updated := false |
| 580 | mm.onuMetricsManagerLock.Lock() |
| 581 | defer mm.onuMetricsManagerLock.Unlock() |
| 582 | for k, v := range mm.standaloneMetricMap { |
| 583 | if k == aMetricName && v.enabled != metric.Enabled { |
| 584 | mm.pDeviceHandler.pmConfigs.Metrics[metricSliceIdx].Enabled = metric.Enabled |
| 585 | v.enabled = metric.Enabled |
| 586 | // If the standalone metric is now enabled and frequency override is enabled, set the next metric collection time |
| 587 | if metric.Enabled && mm.pDeviceHandler.pmConfigs.FreqOverride { |
| 588 | v.nextCollectionInterval = time.Now().Add(time.Duration(v.frequency) * time.Second) |
| 589 | } |
| 590 | updated = true |
| 591 | logger.Infow(ctx, "standalone metric support updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": aMetricName, "enabled": metric.Enabled}) |
| 592 | } |
| 593 | } |
| 594 | if !updated { |
| 595 | logger.Errorw(ctx, "standalone metric support not updated", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": aMetricName}) |
| 596 | return fmt.Errorf("internal-error-during-standalone-support-update--metricname-%s", aMetricName) |
| 597 | } |
| 598 | return nil |
| 599 | } |
| 600 | |
| 601 | func (mm *onuMetricsManager) collectAllGroupAndStandaloneMetrics(ctx context.Context) { |
| 602 | if mm.pDeviceHandler.pmConfigs.Grouped { // metrics are managed as a group. |
| 603 | go mm.collectAllGroupMetrics(ctx) |
| 604 | } else { |
| 605 | go mm.collectAllStandaloneMetrics(ctx) |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | func (mm *onuMetricsManager) collectAllGroupMetrics(ctx context.Context) { |
| 610 | go func() { |
| 611 | logger.Debug(ctx, "startCollector before collecting optical metrics") |
| 612 | metricInfo := mm.collectOpticalMetrics(ctx) |
| 613 | if metricInfo != nil { |
| 614 | mm.publishMetrics(ctx, metricInfo) |
| 615 | } |
| 616 | }() |
| 617 | |
| 618 | go func() { |
| 619 | logger.Debug(ctx, "startCollector before collecting uni metrics") |
| 620 | metricInfo := mm.collectUniStatusMetrics(ctx) |
| 621 | if metricInfo != nil { |
| 622 | mm.publishMetrics(ctx, metricInfo) |
| 623 | } |
| 624 | }() |
| 625 | |
| 626 | // Add more here |
| 627 | } |
| 628 | |
| 629 | func (mm *onuMetricsManager) collectAllStandaloneMetrics(ctx context.Context) { |
| 630 | // None exists as of now, add when available here |
| 631 | } |
| 632 | |
| 633 | func (mm *onuMetricsManager) collectGroupMetric(ctx context.Context, groupName string) { |
| 634 | switch groupName { |
| 635 | case OpticalPowerGroupMetricName: |
| 636 | go func() { |
| 637 | if mi := mm.collectOpticalMetrics(ctx); mm != nil { |
| 638 | mm.publishMetrics(ctx, mi) |
| 639 | } |
| 640 | }() |
| 641 | case UniStatusGroupMetricName: |
| 642 | go func() { |
| 643 | if mi := mm.collectUniStatusMetrics(ctx); mm != nil { |
| 644 | mm.publishMetrics(ctx, mi) |
| 645 | } |
| 646 | }() |
| 647 | default: |
| 648 | logger.Errorw(ctx, "unhandled group metric name", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "groupName": groupName}) |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | func (mm *onuMetricsManager) collectStandaloneMetric(ctx context.Context, metricName string) { |
| 653 | switch metricName { |
| 654 | // None exist as of now, add when available |
| 655 | default: |
| 656 | logger.Errorw(ctx, "unhandled standalone metric name", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": metricName}) |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // collectOpticalMetrics collects groups metrics related to optical power from ani-g ME. |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 661 | func (mm *onuMetricsManager) collectOpticalMetrics(ctx context.Context) []*voltha.MetricInformation { |
| 662 | logger.Debugw(ctx, "collectOpticalMetrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 663 | |
| 664 | mm.onuMetricsManagerLock.RLock() |
| 665 | if !mm.groupMetricMap[OpticalPowerGroupMetricName].enabled { |
| 666 | mm.onuMetricsManagerLock.RUnlock() |
| 667 | logger.Debugw(ctx, "optical power group metric is not enabled", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 668 | return nil |
| 669 | } |
| 670 | mm.onuMetricsManagerLock.RUnlock() |
| 671 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 672 | var metricInfoSlice []*voltha.MetricInformation |
| 673 | metricsContext := make(map[string]string) |
| 674 | metricsContext["onuID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.OnuId) |
| 675 | metricsContext["intfID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.ChannelId) |
| 676 | metricsContext["devicetype"] = mm.pDeviceHandler.DeviceType |
| 677 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 678 | raisedTs := time.Now().Unix() |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 679 | mmd := voltha.MetricMetaData{ |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 680 | Title: OpticalPowerGroupMetricName, |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 681 | Ts: float64(raisedTs), |
| 682 | Context: metricsContext, |
| 683 | DeviceId: mm.pDeviceHandler.deviceID, |
| 684 | LogicalDeviceId: mm.pDeviceHandler.logicalDeviceID, |
| 685 | SerialNo: mm.pDeviceHandler.device.SerialNumber, |
| 686 | } |
| 687 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 688 | // get the ANI-G instance IDs |
| 689 | anigInstKeys := mm.pDeviceHandler.pOnuOmciDevice.pOnuDB.getSortedInstKeys(ctx, me.AniGClassID) |
| 690 | loop: |
| 691 | for _, anigInstID := range anigInstKeys { |
| 692 | var meAttributes me.AttributeValueMap |
| 693 | opticalMetrics := make(map[string]float32) |
| 694 | // Get the ANI-G instance optical power attributes |
| 695 | requestedAttributes := me.AttributeValueMap{"OpticalSignalLevel": 0, "TransmitOpticalLevel": 0} |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 696 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, me.AniGClassID, anigInstID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 697 | select { |
| 698 | case meAttributes = <-mm.opticalMetricsChan: |
| 699 | logger.Debugw(ctx, "received optical metrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 700 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 701 | logger.Errorw(ctx, "timeout waiting for omci-get response for uni status", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 702 | // The metrics will be empty in this case |
| 703 | break loop |
| 704 | } |
| 705 | // Populate metric only if it was enabled. |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 706 | for k := range OpticalPowerGroupMetrics { |
| 707 | switch k { |
| 708 | case "ani_g_instance_id": |
| 709 | if val, ok := meAttributes["ManagedEntityId"]; ok && val != nil { |
| 710 | opticalMetrics[k] = float32(val.(uint16)) |
| 711 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 712 | case "transmit_power": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 713 | if val, ok := meAttributes["TransmitOpticalLevel"]; ok && val != nil { |
| 714 | opticalMetrics[k] = float32(val.(uint16)) |
| 715 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 716 | case "receive_power": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 717 | if val, ok := meAttributes["OpticalSignalLevel"]; ok && val != nil { |
| 718 | opticalMetrics[k] = float32(val.(uint16)) |
| 719 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 720 | default: |
| 721 | // do nothing |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | // create slice of metrics given that there could be more than one ANI-G instance and |
| 726 | // optical metrics are collected per ANI-G instance |
| 727 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: opticalMetrics} |
| 728 | metricInfoSlice = append(metricInfoSlice, &metricInfo) |
| 729 | } |
| 730 | |
| 731 | return metricInfoSlice |
| 732 | } |
| 733 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 734 | // collectUniStatusMetrics collects UNI status group metric from various MEs (uni-g, pptp and veip). |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 735 | // nolint: gocyclo |
| 736 | func (mm *onuMetricsManager) collectUniStatusMetrics(ctx context.Context) []*voltha.MetricInformation { |
| 737 | logger.Debugw(ctx, "collectUniStatusMetrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 738 | mm.onuMetricsManagerLock.RLock() |
| 739 | if !mm.groupMetricMap[UniStatusGroupMetricName].enabled { |
| 740 | mm.onuMetricsManagerLock.RUnlock() |
| 741 | logger.Debugw(ctx, "uni status group metric is not enabled", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 742 | return nil |
| 743 | } |
| 744 | mm.onuMetricsManagerLock.RUnlock() |
| 745 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 746 | var metricInfoSlice []*voltha.MetricInformation |
| 747 | metricsContext := make(map[string]string) |
| 748 | metricsContext["onuID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.OnuId) |
| 749 | metricsContext["intfID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.ChannelId) |
| 750 | metricsContext["devicetype"] = mm.pDeviceHandler.DeviceType |
| 751 | |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 752 | raisedTs := time.Now().Unix() |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 753 | mmd := voltha.MetricMetaData{ |
| 754 | Title: "UniStatus", // Is this ok to hard code? |
| 755 | Ts: float64(raisedTs), |
| 756 | Context: metricsContext, |
| 757 | DeviceId: mm.pDeviceHandler.deviceID, |
| 758 | LogicalDeviceId: mm.pDeviceHandler.logicalDeviceID, |
| 759 | SerialNo: mm.pDeviceHandler.device.SerialNumber, |
| 760 | } |
| 761 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 762 | // get the UNI-G instance IDs |
| 763 | unigInstKeys := mm.pDeviceHandler.pOnuOmciDevice.pOnuDB.getSortedInstKeys(ctx, me.UniGClassID) |
| 764 | loop1: |
| 765 | for _, unigInstID := range unigInstKeys { |
| 766 | // TODO: Include additional information in the voltha.MetricMetaData - like portno, uni-id, instance-id |
| 767 | // to uniquely identify this ME instance and also to correlate the ME instance to physical instance |
| 768 | unigMetrics := make(map[string]float32) |
| 769 | var meAttributes me.AttributeValueMap |
| 770 | // Get the UNI-G instance optical power attributes |
| 771 | requestedAttributes := me.AttributeValueMap{"AdministrativeState": 0} |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 772 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, me.UniGClassID, unigInstID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 773 | // Wait for metrics or timeout |
| 774 | select { |
| 775 | case meAttributes = <-mm.uniStatusMetricsChan: |
| 776 | logger.Debugw(ctx, "received uni-g metrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 777 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 778 | logger.Errorw(ctx, "timeout waiting for omci-get response for uni status", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 779 | // The metrics could be empty in this case |
| 780 | break loop1 |
| 781 | } |
| 782 | // Populate metric only if it was enabled. |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 783 | for k := range UniStatusGroupMetrics { |
| 784 | switch k { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 785 | case "uni_admin_state": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 786 | if val, ok := meAttributes["AdministrativeState"]; ok && val != nil { |
| 787 | unigMetrics[k] = float32(val.(byte)) |
| 788 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 789 | default: |
| 790 | // do nothing |
| 791 | } |
| 792 | } |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 793 | var entityID uint32 |
| 794 | if val, ok := meAttributes["ManagedEntityId"]; ok && val != nil { |
| 795 | entityID = uint32(val.(uint16)) |
| 796 | } |
| 797 | // TODO: Rlock needed for reading uniEntityMap? |
| 798 | if uniPort, ok := mm.pDeviceHandler.uniEntityMap[entityID]; ok && uniPort != nil { |
| 799 | unigMetrics["uni_port_no"] = float32(uniPort.portNo) |
| 800 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 801 | // create slice of metrics given that there could be more than one UNI-G instance |
| 802 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: unigMetrics} |
| 803 | metricInfoSlice = append(metricInfoSlice, &metricInfo) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // get the PPTP instance IDs |
| 808 | pptpInstKeys := mm.pDeviceHandler.pOnuOmciDevice.pOnuDB.getSortedInstKeys(ctx, me.PhysicalPathTerminationPointEthernetUniClassID) |
| 809 | loop2: |
| 810 | for _, pptpInstID := range pptpInstKeys { |
| 811 | // TODO: Include additional information in the voltha.MetricMetaData - like portno, uni-id, instance-id |
| 812 | // to uniquely identify this ME instance and also to correlate the ME instance to physical instance |
| 813 | var meAttributes me.AttributeValueMap |
| 814 | pptpMetrics := make(map[string]float32) |
| 815 | |
| 816 | requestedAttributes := me.AttributeValueMap{"SensedType": 0, "OperationalState": 0, "AdministrativeState": 0} |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 817 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID, pptpInstID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 818 | // Wait for metrics or timeout |
| 819 | select { |
| 820 | case meAttributes = <-mm.uniStatusMetricsChan: |
| 821 | logger.Debugw(ctx, "received pptp metrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 822 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 823 | logger.Errorw(ctx, "timeout waiting for omci-get response for uni status", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 824 | // The metrics could be empty in this case |
| 825 | break loop2 |
| 826 | } |
| 827 | |
| 828 | // Populate metric only if it was enabled. |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 829 | for k := range UniStatusGroupMetrics { |
| 830 | switch k { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 831 | case "ethernet_type": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 832 | if val, ok := meAttributes["SensedType"]; ok && val != nil { |
| 833 | pptpMetrics[k] = float32(val.(byte)) |
| 834 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 835 | case "oper_status": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 836 | if val, ok := meAttributes["OperationalState"]; ok && val != nil { |
| 837 | pptpMetrics[k] = float32(val.(byte)) |
| 838 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 839 | case "uni_admin_state": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 840 | if val, ok := meAttributes["AdministrativeState"]; ok && val != nil { |
| 841 | pptpMetrics[k] = float32(val.(byte)) |
| 842 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 843 | default: |
| 844 | // do nothing |
| 845 | } |
| 846 | } |
| 847 | } |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 848 | var entityID uint32 |
| 849 | if val, ok := meAttributes["ManagedEntityId"]; ok && val != nil { |
| 850 | entityID = uint32(val.(uint16)) |
| 851 | } |
| 852 | // TODO: Rlock needed for reading uniEntityMap? |
| 853 | if uniPort, ok := mm.pDeviceHandler.uniEntityMap[entityID]; ok && uniPort != nil { |
| 854 | pptpMetrics["uni_port_no"] = float32(uniPort.portNo) |
| 855 | } |
| 856 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 857 | // create slice of metrics given that there could be more than one PPTP instance and |
| 858 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: pptpMetrics} |
| 859 | metricInfoSlice = append(metricInfoSlice, &metricInfo) |
| 860 | } |
| 861 | |
| 862 | // get the VEIP instance IDs |
| 863 | veipInstKeys := mm.pDeviceHandler.pOnuOmciDevice.pOnuDB.getSortedInstKeys(ctx, me.VirtualEthernetInterfacePointClassID) |
| 864 | loop3: |
| 865 | for _, veipInstID := range veipInstKeys { |
| 866 | // TODO: Include additional information in the voltha.MetricMetaData - like portno, uni-id, instance-id |
| 867 | // to uniquely identify this ME instance and also to correlate the ME instance to physical instance |
| 868 | var meAttributes me.AttributeValueMap |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 869 | veipMetrics := make(map[string]float32) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 870 | |
| 871 | requestedAttributes := me.AttributeValueMap{"OperationalState": 0, "AdministrativeState": 0} |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 872 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, me.VirtualEthernetInterfacePointClassID, veipInstID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 873 | // Wait for metrics or timeout |
| 874 | select { |
| 875 | case meAttributes = <-mm.uniStatusMetricsChan: |
| 876 | logger.Debugw(ctx, "received veip metrics", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 877 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 878 | logger.Errorw(ctx, "timeout waiting for omci-get response for uni status", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 879 | // The metrics could be empty in this case |
| 880 | break loop3 |
| 881 | } |
| 882 | |
| 883 | // Populate metric only if it was enabled. |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 884 | for k := range UniStatusGroupMetrics { |
| 885 | switch k { |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 886 | case "oper_status": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 887 | if val, ok := meAttributes["OperationalState"]; ok && val != nil { |
| 888 | veipMetrics[k] = float32(val.(byte)) |
| 889 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 890 | case "uni_admin_state": |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 891 | if val, ok := meAttributes["AdministrativeState"]; ok && val != nil { |
| 892 | veipMetrics[k] = float32(val.(byte)) |
| 893 | } |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 894 | default: |
| 895 | // do nothing |
| 896 | } |
| 897 | } |
| 898 | } |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 899 | |
| 900 | var entityID uint32 |
| 901 | if val, ok := meAttributes["ManagedEntityId"]; ok && val != nil { |
| 902 | entityID = uint32(meAttributes["ManagedEntityId"].(uint16)) |
| 903 | } |
| 904 | // TODO: Rlock needed for reading uniEntityMap? |
| 905 | if uniPort, ok := mm.pDeviceHandler.uniEntityMap[entityID]; ok && uniPort != nil { |
| 906 | veipMetrics["uni_port_no"] = float32(uniPort.portNo) |
| 907 | } |
| 908 | |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 909 | // create slice of metrics given that there could be more than one VEIP instance |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 910 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: veipMetrics} |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 911 | metricInfoSlice = append(metricInfoSlice, &metricInfo) |
| 912 | } |
| 913 | |
| 914 | return metricInfoSlice |
| 915 | } |
| 916 | |
| 917 | // publishMetrics publishes the metrics on kafka |
| 918 | func (mm *onuMetricsManager) publishMetrics(ctx context.Context, metricInfo []*voltha.MetricInformation) { |
| 919 | var ke voltha.KpiEvent2 |
Girish Gowdra | 5a7c492 | 2021-01-22 18:33:41 -0800 | [diff] [blame] | 920 | ts := time.Now().Unix() |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 921 | ke.SliceData = metricInfo |
| 922 | ke.Type = voltha.KpiEventType_slice |
| 923 | ke.Ts = float64(ts) |
| 924 | |
| 925 | if err := mm.pDeviceHandler.EventProxy.SendKpiEvent(ctx, "STATS_EVENT", &ke, voltha.EventCategory_EQUIPMENT, voltha.EventSubCategory_ONU, ts); err != nil { |
| 926 | logger.Errorw(ctx, "failed-to-send-pon-stats", log.Fields{"err": err}) |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | func (mm *onuMetricsManager) processOmciMessages(ctx context.Context) { |
| 931 | logger.Infow(ctx, "Start routine to process OMCI-GET messages for device-id", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 932 | // Flush metric collection channels to be safe. |
| 933 | // It is possible that there is stale data on this channel if the processOmciMessages routine |
| 934 | // is stopped right after issuing a OMCI-GET request and started again. |
| 935 | // The processOmciMessages routine will get stopped if startCollector routine (in device_handler.go) |
| 936 | // is stopped - as a result of ONU going down. |
| 937 | mm.flushMetricCollectionChannels(ctx) |
| 938 | |
| 939 | for { |
| 940 | select { |
| 941 | case <-mm.stopProcessingOmciResponses: // stop this routine |
| 942 | logger.Infow(ctx, "Stop routine to process OMCI-GET messages for device-id", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 943 | return |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 944 | case message, ok := <-mm.pAdaptFsm.commChan: |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 945 | if !ok { |
| 946 | logger.Errorw(ctx, "Message couldn't be read from channel", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 947 | continue |
| 948 | } |
| 949 | logger.Debugw(ctx, "Received message on ONU metrics channel", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 950 | |
| 951 | switch message.Type { |
| 952 | case OMCI: |
| 953 | msg, _ := message.Data.(OmciMessage) |
| 954 | mm.handleOmciMessage(ctx, msg) |
| 955 | default: |
| 956 | logger.Warn(ctx, "Unknown message type received", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "message.Type": message.Type}) |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | func (mm *onuMetricsManager) handleOmciMessage(ctx context.Context, msg OmciMessage) { |
| 963 | logger.Debugw(ctx, "omci Msg", log.Fields{"device-id": mm.pDeviceHandler.deviceID, |
| 964 | "msgType": msg.OmciMsg.MessageType, "msg": msg}) |
| 965 | switch msg.OmciMsg.MessageType { |
| 966 | case omci.GetResponseType: |
| 967 | //TODO: error handling |
| 968 | _ = mm.handleOmciGetResponseMessage(ctx, msg) |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 969 | case omci.SynchronizeTimeResponseType: |
| 970 | _ = mm.handleOmciSynchronizeTimeResponseMessage(ctx, msg) |
| 971 | case omci.CreateResponseType: |
| 972 | _ = mm.handleOmciCreateResponseMessage(ctx, msg) |
| 973 | case omci.DeleteResponseType: |
| 974 | _ = mm.handleOmciDeleteResponseMessage(ctx, msg) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 975 | default: |
| 976 | logger.Warnw(ctx, "Unknown Message Type", log.Fields{"msgType": msg.OmciMsg.MessageType}) |
| 977 | |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | func (mm *onuMetricsManager) handleOmciGetResponseMessage(ctx context.Context, msg OmciMessage) error { |
| 982 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse) |
| 983 | if msgLayer == nil { |
| 984 | logger.Errorw(ctx, "omci Msg layer could not be detected for GetResponse - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 985 | return fmt.Errorf("omci Msg layer could not be detected for GetResponse - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 986 | } |
| 987 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 988 | if !msgOk { |
| 989 | logger.Errorw(ctx, "omci Msg layer could not be assigned for GetResponse - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 990 | return fmt.Errorf("omci Msg layer could not be assigned for GetResponse - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 991 | } |
| 992 | logger.Debugw(ctx, "OMCI GetResponse Data", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "data-fields": msgObj}) |
| 993 | if msgObj.Result == me.Success { |
| 994 | meAttributes := msgObj.Attributes |
| 995 | switch msgObj.EntityClass { |
| 996 | case me.AniGClassID: |
| 997 | mm.opticalMetricsChan <- meAttributes |
| 998 | return nil |
| 999 | case me.UniGClassID: |
| 1000 | mm.uniStatusMetricsChan <- meAttributes |
| 1001 | return nil |
| 1002 | case me.PhysicalPathTerminationPointEthernetUniClassID: |
| 1003 | mm.uniStatusMetricsChan <- meAttributes |
| 1004 | return nil |
| 1005 | case me.VirtualEthernetInterfacePointClassID: |
| 1006 | mm.uniStatusMetricsChan <- meAttributes |
| 1007 | return nil |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 1008 | case me.EthernetFramePerformanceMonitoringHistoryDataUpstreamClassID, |
| 1009 | me.EthernetFramePerformanceMonitoringHistoryDataDownstreamClassID, |
| 1010 | me.EthernetPerformanceMonitoringHistoryDataClassID: |
| 1011 | mm.l2PmChan <- meAttributes |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 1012 | default: |
| 1013 | logger.Errorw(ctx, "unhandled omci get response message", |
| 1014 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1015 | } |
| 1016 | } |
| 1017 | |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 1018 | return fmt.Errorf("unhandled-omci-get-response-message") |
| 1019 | } |
| 1020 | |
| 1021 | func (mm *onuMetricsManager) handleOmciSynchronizeTimeResponseMessage(ctx context.Context, msg OmciMessage) error { |
| 1022 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSynchronizeTimeResponse) |
| 1023 | if msgLayer == nil { |
| 1024 | logger.Errorw(ctx, "omci Msg layer could not be detected for synchronize time response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1025 | return fmt.Errorf("omci Msg layer could not be detected for synchronize time response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1026 | } |
| 1027 | msgObj, msgOk := msgLayer.(*omci.SynchronizeTimeResponse) |
| 1028 | if !msgOk { |
| 1029 | logger.Errorw(ctx, "omci Msg layer could not be assigned for synchronize time response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1030 | return fmt.Errorf("omci Msg layer could not be assigned for synchronize time response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1031 | } |
| 1032 | logger.Debugw(ctx, "OMCI synchronize time response Data", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "data-fields": msgObj}) |
| 1033 | if msgObj.Result == me.Success { |
| 1034 | switch msgObj.EntityClass { |
| 1035 | case me.OnuGClassID: |
| 1036 | logger.Infow(ctx, "omci synchronize time success", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1037 | mm.syncTimeResponseChan <- true |
| 1038 | return nil |
| 1039 | default: |
| 1040 | logger.Errorw(ctx, "unhandled omci message", |
| 1041 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1042 | } |
| 1043 | } |
| 1044 | mm.syncTimeResponseChan <- false |
| 1045 | logger.Errorf(ctx, "unhandled-omci-synchronize-time-response-message--error-code-%v", msgObj.Result) |
| 1046 | return fmt.Errorf("unhandled-omci-synchronize-time-response-message--error-code-%v", msgObj.Result) |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | // flushMetricCollectionChannels flushes all metric collection channels for any stale OMCI responses |
| 1050 | func (mm *onuMetricsManager) flushMetricCollectionChannels(ctx context.Context) { |
| 1051 | // flush commMetricsChan |
| 1052 | select { |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 1053 | case <-mm.pAdaptFsm.commChan: |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 1054 | logger.Debug(ctx, "flushed common metrics channel") |
| 1055 | default: |
| 1056 | } |
| 1057 | |
| 1058 | // flush opticalMetricsChan |
| 1059 | select { |
| 1060 | case <-mm.opticalMetricsChan: |
| 1061 | logger.Debug(ctx, "flushed optical metrics channel") |
| 1062 | default: |
| 1063 | } |
| 1064 | |
| 1065 | // flush uniStatusMetricsChan |
| 1066 | select { |
| 1067 | case <-mm.uniStatusMetricsChan: |
| 1068 | logger.Debug(ctx, "flushed uni status metrics channel") |
| 1069 | default: |
| 1070 | } |
Girish Gowdra | e0140f0 | 2021-02-02 16:55:09 -0800 | [diff] [blame] | 1071 | |
| 1072 | // flush syncTimeResponseChan |
| 1073 | select { |
| 1074 | case <-mm.syncTimeResponseChan: |
| 1075 | logger.Debug(ctx, "flushed sync time response channel") |
| 1076 | default: |
| 1077 | } |
| 1078 | |
| 1079 | // flush l2PmChan |
| 1080 | select { |
| 1081 | case <-mm.l2PmChan: |
| 1082 | logger.Debug(ctx, "flushed L2 PM collection channel") |
| 1083 | default: |
| 1084 | } |
| 1085 | |
| 1086 | // flush stopTicks |
| 1087 | select { |
| 1088 | case <-mm.stopTicks: |
| 1089 | logger.Debug(ctx, "flushed stopTicks channel") |
| 1090 | default: |
| 1091 | } |
| 1092 | |
| 1093 | } |
| 1094 | |
| 1095 | // ** L2 PM FSM Handlers start ** |
| 1096 | |
| 1097 | func (mm *onuMetricsManager) l2PMFsmStarting(ctx context.Context, e *fsm.Event) { |
| 1098 | // Loop through all the group metrics |
| 1099 | // If it is a L2 PM Interval metric and it is enabled, then if it is not in the |
| 1100 | // list of active L2 PM list then mark it for creation |
| 1101 | // It it is a L2 PM Interval metric and it is disabled, then if it is in the |
| 1102 | // list of active L2 PM list then mark it for deletion |
| 1103 | mm.onuMetricsManagerLock.Lock() |
| 1104 | for n, g := range mm.groupMetricMap { |
| 1105 | if g.isL2PMCounter { // it is a l2 pm counter |
| 1106 | if g.enabled { // metric enabled. |
| 1107 | found := false |
| 1108 | inner1: |
| 1109 | for _, v := range mm.activeL2Pms { |
| 1110 | if v == n { |
| 1111 | found = true // metric already present in active l2 pm list |
| 1112 | break inner1 |
| 1113 | } |
| 1114 | } |
| 1115 | if !found { // metric not in active l2 pm list. Mark this to be added later |
| 1116 | mm.l2PmToAdd = mm.appendIfMissing(mm.l2PmToAdd, n) |
| 1117 | } |
| 1118 | } else { // metric not enabled. |
| 1119 | found := false |
| 1120 | inner2: |
| 1121 | for _, v := range mm.activeL2Pms { |
| 1122 | if v == n { |
| 1123 | found = true // metric is found in active l2 pm list |
| 1124 | break inner2 |
| 1125 | } |
| 1126 | } |
| 1127 | if found { // metric is found in active l2 pm list. Mark this to be deleted later |
| 1128 | mm.l2PmToDelete = mm.appendIfMissing(mm.l2PmToDelete, n) |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | mm.onuMetricsManagerLock.Unlock() |
| 1134 | logger.Debugw(ctx, "pms to add and delete", |
| 1135 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "pms-to-add": mm.l2PmToAdd, "pms-to-delete": mm.l2PmToDelete}) |
| 1136 | go func() { |
| 1137 | // push a tick event to move to next state |
| 1138 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventTick); err != nil { |
| 1139 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1140 | } |
| 1141 | }() |
| 1142 | } |
| 1143 | |
| 1144 | func (mm *onuMetricsManager) l2PMFsmSyncTime(ctx context.Context, e *fsm.Event) { |
| 1145 | // Sync time with the ONU to establish 15min boundary for PM collection. |
| 1146 | if err := mm.syncTime(ctx); err != nil { |
| 1147 | go func() { |
| 1148 | time.Sleep(SyncTimeRetryInterval * time.Second) // retry to sync time after this timeout |
| 1149 | // This will result in FSM attempting to sync time again |
| 1150 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventFailure); err != nil { |
| 1151 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1152 | } |
| 1153 | }() |
| 1154 | } |
| 1155 | // Initiate a tick generation routine every L2PmCollectionInterval |
| 1156 | go mm.generateTicks(ctx) |
| 1157 | |
| 1158 | go func() { |
| 1159 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventSuccess); err != nil { |
| 1160 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1161 | } |
| 1162 | }() |
| 1163 | } |
| 1164 | |
| 1165 | func (mm *onuMetricsManager) l2PMFsmNull(ctx context.Context, e *fsm.Event) { |
| 1166 | // We need to reset the local data so that the L2 PM MEs are re-provisioned once the ONU is back up based on the latest PM CONFIG |
| 1167 | mm.onuMetricsManagerLock.Lock() |
| 1168 | mm.activeL2Pms = nil |
| 1169 | mm.l2PmToAdd = nil |
| 1170 | mm.l2PmToDelete = nil |
| 1171 | mm.onuMetricsManagerLock.Unlock() |
| 1172 | } |
| 1173 | func (mm *onuMetricsManager) l2PMFsmIdle(ctx context.Context, e *fsm.Event) { |
| 1174 | logger.Debugw(ctx, "Enter state idle", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1175 | |
| 1176 | mm.onuMetricsManagerLock.RLock() |
| 1177 | numOfPmToDelete := len(mm.l2PmToDelete) |
| 1178 | numOfPmToAdd := len(mm.l2PmToAdd) |
| 1179 | mm.onuMetricsManagerLock.RUnlock() |
| 1180 | |
| 1181 | if numOfPmToDelete > 0 { |
| 1182 | logger.Debugw(ctx, "state idle - pms to delete", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "pms-to-delete": numOfPmToDelete}) |
| 1183 | go func() { |
| 1184 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventDeleteMe); err != nil { |
| 1185 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1186 | } |
| 1187 | }() |
| 1188 | } else if numOfPmToAdd > 0 { |
| 1189 | logger.Debugw(ctx, "state idle - pms to add", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "pms-to-add": numOfPmToAdd}) |
| 1190 | go func() { |
| 1191 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventAddMe); err != nil { |
| 1192 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1193 | } |
| 1194 | }() |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | func (mm *onuMetricsManager) l2PmFsmCollectData(ctx context.Context, e *fsm.Event) { |
| 1199 | logger.Debugw(ctx, "state collect data", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1200 | // Copy the activeL2Pms for which we want to collect the metrics since activeL2Pms can change dynamically |
| 1201 | mm.onuMetricsManagerLock.RLock() |
| 1202 | copyOfActiveL2Pms := make([]string, len(mm.activeL2Pms)) |
| 1203 | _ = copy(copyOfActiveL2Pms, mm.activeL2Pms) |
| 1204 | mm.onuMetricsManagerLock.RUnlock() |
| 1205 | |
| 1206 | for _, n := range copyOfActiveL2Pms { |
| 1207 | switch n { |
| 1208 | case EthernetBridgeHistoryName: |
| 1209 | logger.Debugw(ctx, "state collect data - collecting data for EthernetFramePerformanceMonitoringHistoryData ME", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1210 | var metricInfoSlice []*voltha.MetricInformation |
| 1211 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1212 | // Attach the EthernetFramePerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1213 | entityID := macBridgePortAniEID + uniPort.entityID |
| 1214 | if metricInfo := mm.collectEthernetFramePerformanceMonitoringHistoryData(ctx, true, entityID); metricInfo != nil { // upstream |
| 1215 | metricInfoSlice = append(metricInfoSlice, metricInfo) |
| 1216 | } |
| 1217 | if metricInfo := mm.collectEthernetFramePerformanceMonitoringHistoryData(ctx, false, entityID); metricInfo != nil { // downstream |
| 1218 | metricInfoSlice = append(metricInfoSlice, metricInfo) |
| 1219 | } |
| 1220 | } |
| 1221 | // Publish metrics if it is valid |
| 1222 | if metricInfoSlice != nil { |
| 1223 | mm.publishMetrics(ctx, metricInfoSlice) |
| 1224 | } else { |
| 1225 | // If collectAttempts exceeds L2PmCollectAttempts then remove it from activeL2Pms |
| 1226 | // slice so that we do not collect data from that PM ME anymore |
| 1227 | mm.onuMetricsManagerLock.Lock() |
| 1228 | mm.groupMetricMap[n].collectAttempts++ |
| 1229 | if mm.groupMetricMap[n].collectAttempts > L2PmCollectAttempts { |
| 1230 | mm.removeIfFound(mm.activeL2Pms, n) |
| 1231 | } |
| 1232 | logger.Warnw(ctx, "state collect data - no metrics collected", |
| 1233 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": n, "collectAttempts": mm.groupMetricMap[n].collectAttempts}) |
| 1234 | mm.onuMetricsManagerLock.Unlock() |
| 1235 | } |
| 1236 | case EthernetUniHistoryName: |
| 1237 | logger.Debugw(ctx, "state collect data - collecting data for EthernetPerformanceMonitoringHistoryData ME", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1238 | var metricInfoSlice []*voltha.MetricInformation |
| 1239 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1240 | if uniPort.portType == uniPPTP { // This metric is only applicable for PPTP Uni Type |
| 1241 | // Attach the EthernetFramePerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1242 | entityID := uniPort.entityID |
| 1243 | if metricInfo := mm.collectEthernetUniHistoryData(ctx, entityID); metricInfo != nil { // upstream |
| 1244 | metricInfoSlice = append(metricInfoSlice, metricInfo) |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | // Publish metrics if it is valid |
| 1249 | if metricInfoSlice != nil { |
| 1250 | mm.publishMetrics(ctx, metricInfoSlice) |
| 1251 | } else { |
| 1252 | // If collectAttempts exceeds L2PmCollectAttempts then remove it from activeL2Pms |
| 1253 | // slice so that we do not collect data from that PM ME anymore |
| 1254 | mm.onuMetricsManagerLock.Lock() |
| 1255 | mm.groupMetricMap[n].collectAttempts++ |
| 1256 | if mm.groupMetricMap[n].collectAttempts > L2PmCollectAttempts { |
| 1257 | mm.removeIfFound(mm.activeL2Pms, n) |
| 1258 | } |
| 1259 | logger.Warnw(ctx, "state collect data - no metrics collected", |
| 1260 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": n, "collectAttempts": mm.groupMetricMap[n].collectAttempts}) |
| 1261 | mm.onuMetricsManagerLock.Unlock() |
| 1262 | } |
| 1263 | default: |
| 1264 | logger.Errorw(ctx, "unsupported l2 pm", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "name": n}) |
| 1265 | } |
| 1266 | } |
| 1267 | // Does not matter we send success or failure here. |
| 1268 | // Those PMs that we failed to collect data will be attempted to collect again in the next PM collection cycle (assuming |
| 1269 | // we have not exceed max attempts to collect the PM data) |
| 1270 | go func() { |
| 1271 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventSuccess); err != nil { |
| 1272 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1273 | } |
| 1274 | }() |
| 1275 | } |
| 1276 | |
| 1277 | func (mm *onuMetricsManager) l2PmFsmCreatePM(ctx context.Context, e *fsm.Event) { |
| 1278 | // Copy the l2PmToAdd for which we want to collect the metrics since l2PmToAdd can change dynamically |
| 1279 | mm.onuMetricsManagerLock.RLock() |
| 1280 | copyOfL2PmToAdd := make([]string, len(mm.l2PmToAdd)) |
| 1281 | _ = copy(copyOfL2PmToAdd, mm.l2PmToAdd) |
| 1282 | mm.onuMetricsManagerLock.RUnlock() |
| 1283 | |
| 1284 | logger.Debugw(ctx, "state create pm", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "pms-to-add": copyOfL2PmToAdd}) |
| 1285 | for _, n := range copyOfL2PmToAdd { |
| 1286 | resp := false |
| 1287 | switch n { |
| 1288 | case EthernetBridgeHistoryName: |
| 1289 | boolForDirection := make([]bool, 2) // stores true and false to indicate upstream and downstream directions. |
| 1290 | boolForDirection = append(boolForDirection, true, false) |
| 1291 | // Create ME twice, one for each direction. Boolean true is used to indicate upstream and false for downstream. |
| 1292 | for _, direction := range boolForDirection { |
| 1293 | inner1: |
| 1294 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1295 | // Attach the EthernetFramePerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1296 | entityID := macBridgePortAniEID + uniPort.entityID |
| 1297 | mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendCreateOrDeleteEthernetPerformanceMonitoringHistoryME( |
| 1298 | ctx, ConstDefaultOmciTimeout, true, direction, true, mm.pAdaptFsm.commChan, entityID) |
| 1299 | select { |
| 1300 | case resp = <-mm.l2PmCreateOrDeleteResponseChan: |
| 1301 | logger.Debugw(ctx, "received create EthernetFramePerformanceMonitoringHistoryData l2 pm me response", |
| 1302 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "resp": resp, "uni": uniPort.uniID}) |
| 1303 | if !resp { |
| 1304 | // We will attempt to create the MEs again in the next L2 PM Collection cycle |
| 1305 | break inner1 |
| 1306 | } |
| 1307 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1308 | logger.Errorw(ctx, "timeout waiting for create EthernetFramePerformanceMonitoringHistoryData l2 pm me response", |
| 1309 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "uni": uniPort.uniID}) |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | case EthernetUniHistoryName: |
| 1314 | |
| 1315 | inner2: |
| 1316 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1317 | if uniPort.portType == uniPPTP { // This metric is only applicable for PPTP Uni Type |
| 1318 | // Attach the EthernetPerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1319 | entityID := uniPort.entityID |
| 1320 | mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendCreateOrDeleteEthernetUniHistoryME( |
| 1321 | ctx, ConstDefaultOmciTimeout, true, true, mm.pAdaptFsm.commChan, entityID) |
| 1322 | select { |
| 1323 | case resp = <-mm.l2PmCreateOrDeleteResponseChan: |
| 1324 | logger.Debugw(ctx, "received create EthernetPerformanceMonitoringHistoryData l2 pm me response", |
| 1325 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "resp": resp, "uni": uniPort.uniID}) |
| 1326 | if !resp { |
| 1327 | // We will attempt to create the MEs again in the next L2 PM Collection cycle |
| 1328 | break inner2 |
| 1329 | } |
| 1330 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1331 | logger.Errorw(ctx, "timeout waiting for create EthernetPerformanceMonitoringHistoryData l2 pm me response", |
| 1332 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "uni": uniPort.uniID}) |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | default: |
| 1337 | logger.Errorw(ctx, "unsupported l2 pm", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "name": n}) |
| 1338 | } |
| 1339 | // On success Update the local list maintained for active PMs and PMs to add |
| 1340 | if resp { |
| 1341 | mm.onuMetricsManagerLock.Lock() |
| 1342 | mm.activeL2Pms = mm.appendIfMissing(mm.activeL2Pms, n) |
| 1343 | mm.l2PmToAdd = mm.removeIfFound(mm.l2PmToAdd, n) |
| 1344 | mm.onuMetricsManagerLock.Unlock() |
| 1345 | } else { |
| 1346 | // If createRetryAttempts exceeds L2PmCreateAttempts then locally disable the PM |
| 1347 | // and also remove it from l2PmToAdd slice so that we do not try to create the PM ME anymore |
| 1348 | mm.onuMetricsManagerLock.Lock() |
| 1349 | mm.groupMetricMap[n].createRetryAttempts++ |
| 1350 | if mm.groupMetricMap[n].createRetryAttempts > L2PmCreateAttempts { |
| 1351 | mm.groupMetricMap[n].enabled = false |
| 1352 | mm.removeIfFound(mm.l2PmToAdd, n) |
| 1353 | } |
| 1354 | logger.Warnw(ctx, "state create pm - failed to create pm", |
| 1355 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "metricName": n, "createRetryAttempts": mm.groupMetricMap[n].createRetryAttempts}) |
| 1356 | mm.onuMetricsManagerLock.Unlock() |
| 1357 | } |
| 1358 | } |
| 1359 | // Does not matter we send success or failure here. |
| 1360 | // Those PMs that we failed to create will be attempted to create again in the next PM creation cycle (assuming |
| 1361 | // we have not exceed max attempts to create the PM ME) |
| 1362 | go func() { |
| 1363 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventSuccess); err != nil { |
| 1364 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1365 | } |
| 1366 | }() |
| 1367 | } |
| 1368 | |
| 1369 | func (mm *onuMetricsManager) l2PmFsmDeletePM(ctx context.Context, e *fsm.Event) { |
| 1370 | // Copy the l2PmToDelete for which we want to collect the metrics since l2PmToDelete can change dynamically |
| 1371 | mm.onuMetricsManagerLock.RLock() |
| 1372 | copyOfL2PmToDelete := make([]string, len(mm.l2PmToDelete)) |
| 1373 | _ = copy(copyOfL2PmToDelete, mm.l2PmToDelete) |
| 1374 | mm.onuMetricsManagerLock.RUnlock() |
| 1375 | |
| 1376 | logger.Debugw(ctx, "state delete pm", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "pms-to-delete": mm.l2PmToDelete}) |
| 1377 | for _, n := range copyOfL2PmToDelete { |
| 1378 | resp := false |
| 1379 | switch n { |
| 1380 | case EthernetBridgeHistoryName: |
| 1381 | boolForDirection := make([]bool, 2) // stores true and false to indicate upstream and downstream directions. |
| 1382 | boolForDirection = append(boolForDirection, true, false) |
| 1383 | // Create ME twice, one for each direction. Boolean true is used to indicate upstream and false for downstream. |
| 1384 | for _, direction := range boolForDirection { |
| 1385 | inner1: |
| 1386 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1387 | // Attach the EthernetFramePerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1388 | entityID := macBridgePortAniEID + uniPort.entityID |
| 1389 | mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendCreateOrDeleteEthernetPerformanceMonitoringHistoryME( |
| 1390 | ctx, ConstDefaultOmciTimeout, true, direction, false, mm.pAdaptFsm.commChan, entityID) |
| 1391 | select { |
| 1392 | case resp = <-mm.l2PmCreateOrDeleteResponseChan: |
| 1393 | logger.Debugw(ctx, "received delete EthernetFramePerformanceMonitoringHistoryData l2 pm me response", |
| 1394 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "resp": resp, "uni": uniPort.uniID}) |
| 1395 | if !resp { |
| 1396 | // We will attempt to delete the MEs again in the next L2 PM Collection cycle |
| 1397 | break inner1 |
| 1398 | } |
| 1399 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1400 | logger.Errorw(ctx, "timeout waiting for delete EthernetFramePerformanceMonitoringHistoryData l2 pm me response", |
| 1401 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "uni": uniPort.uniID}) |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | case EthernetUniHistoryName: |
| 1406 | |
| 1407 | inner2: |
| 1408 | for _, uniPort := range mm.pDeviceHandler.uniEntityMap { |
| 1409 | if uniPort.portType == uniPPTP { // This metric is only applicable for PPTP Uni Type |
| 1410 | // Attach the EthernetPerformanceMonitoringHistoryData ME to MacBridgePortConfigData on the UNI port |
| 1411 | entityID := uniPort.entityID |
| 1412 | mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendCreateOrDeleteEthernetUniHistoryME( |
| 1413 | ctx, ConstDefaultOmciTimeout, true, false, mm.pAdaptFsm.commChan, entityID) |
| 1414 | select { |
| 1415 | case resp = <-mm.l2PmCreateOrDeleteResponseChan: |
| 1416 | logger.Debugw(ctx, "received delete EthernetPerformanceMonitoringHistoryData l2 pm me response", |
| 1417 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "resp": resp, "uni": uniPort.uniID}) |
| 1418 | if !resp { |
| 1419 | // We will attempt to delete the MEs again in the next L2 PM Collection cycle |
| 1420 | break inner2 |
| 1421 | } |
| 1422 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1423 | logger.Errorw(ctx, "timeout waiting for delete EthernetPerformanceMonitoringHistoryData l2 pm me response", |
| 1424 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "uni": uniPort.uniID}) |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | default: |
| 1429 | logger.Errorw(ctx, "unsupported l2 pm", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "name": n}) |
| 1430 | } |
| 1431 | // On success Update the local list maintained for active PMs and PMs to delete |
| 1432 | if resp { |
| 1433 | mm.onuMetricsManagerLock.Lock() |
| 1434 | mm.activeL2Pms = mm.removeIfFound(mm.activeL2Pms, n) |
| 1435 | mm.l2PmToDelete = mm.removeIfFound(mm.l2PmToDelete, n) |
| 1436 | mm.onuMetricsManagerLock.Unlock() |
| 1437 | } |
| 1438 | } |
| 1439 | // Does not matter we send success or failure here. |
| 1440 | // Those PMs that we failed to delete will be attempted to create again in the next PM collection cycle |
| 1441 | go func() { |
| 1442 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventSuccess); err != nil { |
| 1443 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1444 | } |
| 1445 | }() |
| 1446 | } |
| 1447 | |
| 1448 | // ** L2 PM FSM Handlers end ** |
| 1449 | |
| 1450 | // syncTime synchronizes time with the ONU to establish a 15 min boundary for PM collection and reporting. |
| 1451 | func (mm *onuMetricsManager) syncTime(ctx context.Context) error { |
| 1452 | if err := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendSyncTime(ctx, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); err != nil { |
| 1453 | logger.Errorw(ctx, "cannot send sync time request", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1454 | return err |
| 1455 | } |
| 1456 | |
| 1457 | select { |
| 1458 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1459 | logger.Errorf(ctx, "timed out waiting for sync time response from onu", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1460 | return fmt.Errorf("timed-out-waiting-for-sync-time-response-%v", mm.pDeviceHandler.deviceID) |
| 1461 | case syncTimeRes := <-mm.syncTimeResponseChan: |
| 1462 | if !syncTimeRes { |
| 1463 | return fmt.Errorf("failed-to-sync-time-%v", mm.pDeviceHandler.deviceID) |
| 1464 | } |
| 1465 | logger.Infow(ctx, "sync time success", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1466 | return nil |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | func (mm *onuMetricsManager) collectEthernetFramePerformanceMonitoringHistoryData(ctx context.Context, upstream bool, entityID uint16) *voltha.MetricInformation { |
| 1471 | var mEnt *me.ManagedEntity |
| 1472 | var omciErr me.OmciErrors |
| 1473 | var classID me.ClassID |
| 1474 | var meAttributes me.AttributeValueMap |
| 1475 | logger.Debugw(ctx, "collecting data for EthernetFramePerformanceMonitoringHistoryData", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream}) |
| 1476 | meParam := me.ParamData{EntityID: entityID} |
| 1477 | if upstream { |
| 1478 | if mEnt, omciErr = me.NewEthernetFramePerformanceMonitoringHistoryDataUpstream(meParam); omciErr == nil || mEnt == nil || omciErr.GetError() != nil { |
| 1479 | logger.Errorw(ctx, "error creating me", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream}) |
| 1480 | return nil |
| 1481 | } |
| 1482 | classID = me.EthernetFramePerformanceMonitoringHistoryDataUpstreamClassID |
| 1483 | } else { |
| 1484 | if mEnt, omciErr = me.NewEthernetFramePerformanceMonitoringHistoryDataDownstream(meParam); omciErr == nil || mEnt == nil || omciErr.GetError() != nil { |
| 1485 | logger.Errorw(ctx, "error creating me", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream}) |
| 1486 | return nil |
| 1487 | } |
| 1488 | classID = me.EthernetFramePerformanceMonitoringHistoryDataDownstreamClassID |
| 1489 | } |
| 1490 | |
| 1491 | requestedAttributes := make(me.AttributeValueMap) |
| 1492 | size := 0 |
| 1493 | intervalEndTime := -1 |
| 1494 | ethPMHistData := make(map[string]float32) |
| 1495 | |
| 1496 | for _, v := range mEnt.GetAttributeDefinitions() { |
| 1497 | if (v.Size + size) <= MaxL2PMGetPayLoadSize { |
| 1498 | requestedAttributes[v.Name] = v.DefValue |
| 1499 | size = v.Size + size |
| 1500 | } else { // We exceeded the allow omci get size |
| 1501 | // Let's collect the attributes via get now and collect remaining in the next iteration |
| 1502 | if err := mm.populateEthernetBridgeHistoryMetrics(ctx, upstream, classID, entityID, meAttributes, requestedAttributes, ethPMHistData, &intervalEndTime); err != nil { |
| 1503 | logger.Errorw(ctx, "error during metric collection", |
| 1504 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream, "err": err}) |
| 1505 | return nil |
| 1506 | } |
| 1507 | size = 0 // reset size |
| 1508 | requestedAttributes = make(me.AttributeValueMap) // reset map |
| 1509 | } |
| 1510 | } |
| 1511 | // Collect the omci get attributes for the last bunch of attributes. |
| 1512 | if len(requestedAttributes) > 0 { |
| 1513 | if err := mm.populateEthernetBridgeHistoryMetrics(ctx, upstream, classID, entityID, meAttributes, requestedAttributes, ethPMHistData, &intervalEndTime); err != nil { |
| 1514 | logger.Errorw(ctx, "error during metric collection", |
| 1515 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream, "err": err}) |
| 1516 | return nil |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | // Populate some relevant context for the EthernetFramePerformanceMonitoringHistoryData PM |
| 1521 | ethPMHistData["class_id"] = float32(classID) |
| 1522 | ethPMHistData["interval_end_time"] = float32(intervalEndTime) |
| 1523 | ethPMHistData["parent_class_id"] = float32(me.MacBridgeConfigurationDataClassID) // EthernetFramePerformanceMonitoringHistoryData is attached to MBPCD ME |
| 1524 | ethPMHistData["parent_entity_id"] = float32(entityID) |
| 1525 | if upstream { |
| 1526 | ethPMHistData["upstream"] = float32(1) |
| 1527 | } else { |
| 1528 | ethPMHistData["upstream"] = float32(0) |
| 1529 | } |
| 1530 | |
| 1531 | metricsContext := make(map[string]string) |
| 1532 | metricsContext["onuID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.OnuId) |
| 1533 | metricsContext["intfID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.ChannelId) |
| 1534 | metricsContext["devicetype"] = mm.pDeviceHandler.DeviceType |
| 1535 | |
| 1536 | raisedTs := time.Now().Unix() |
| 1537 | mmd := voltha.MetricMetaData{ |
| 1538 | Title: EthernetBridgeHistoryName, |
| 1539 | Ts: float64(raisedTs), |
| 1540 | Context: metricsContext, |
| 1541 | DeviceId: mm.pDeviceHandler.deviceID, |
| 1542 | LogicalDeviceId: mm.pDeviceHandler.logicalDeviceID, |
| 1543 | SerialNo: mm.pDeviceHandler.device.SerialNumber, |
| 1544 | } |
| 1545 | |
| 1546 | // create slice of metrics given that there could be more than one VEIP instance |
| 1547 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: ethPMHistData} |
| 1548 | logger.Debugw(ctx, "collecting data for EthernetFramePerformanceMonitoringHistoryData successful", |
| 1549 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "upstream": upstream, "metricInfo": metricInfo}) |
| 1550 | return &metricInfo |
| 1551 | } |
| 1552 | |
| 1553 | func (mm *onuMetricsManager) collectEthernetUniHistoryData(ctx context.Context, entityID uint16) *voltha.MetricInformation { |
| 1554 | var mEnt *me.ManagedEntity |
| 1555 | var omciErr me.OmciErrors |
| 1556 | var classID me.ClassID |
| 1557 | var meAttributes me.AttributeValueMap |
| 1558 | logger.Debugw(ctx, "collecting data for EthernetFramePerformanceMonitoringHistoryData", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID}) |
| 1559 | meParam := me.ParamData{EntityID: entityID} |
| 1560 | if mEnt, omciErr = me.NewEthernetPerformanceMonitoringHistoryData(meParam); omciErr == nil || mEnt == nil || omciErr.GetError() != nil { |
| 1561 | logger.Errorw(ctx, "error creating me", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID}) |
| 1562 | return nil |
| 1563 | } |
| 1564 | classID = me.EthernetPerformanceMonitoringHistoryDataClassID |
| 1565 | |
| 1566 | requestedAttributes := make(me.AttributeValueMap) |
| 1567 | size := 0 |
| 1568 | intervalEndTime := -1 |
| 1569 | ethUniHistData := make(map[string]float32) |
| 1570 | |
| 1571 | for _, v := range mEnt.GetAttributeDefinitions() { |
| 1572 | if (v.Size + size) <= MaxL2PMGetPayLoadSize { |
| 1573 | requestedAttributes[v.Name] = v.DefValue |
| 1574 | size = v.Size + size |
| 1575 | } else { // We exceeded the allow omci get size |
| 1576 | // Let's collect the attributes via get now and collect remaining in the next iteration |
| 1577 | if err := mm.populateEthernetUniHistoryMetrics(ctx, classID, entityID, meAttributes, requestedAttributes, ethUniHistData, &intervalEndTime); err != nil { |
| 1578 | logger.Errorw(ctx, "error during metric collection", |
| 1579 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "err": err}) |
| 1580 | return nil |
| 1581 | } |
| 1582 | size = 0 // reset size |
| 1583 | requestedAttributes = make(me.AttributeValueMap) // reset map |
| 1584 | } |
| 1585 | } |
| 1586 | // Collect the omci get attributes for the last bunch of attributes. |
| 1587 | if len(requestedAttributes) > 0 { |
| 1588 | if err := mm.populateEthernetUniHistoryMetrics(ctx, classID, entityID, meAttributes, requestedAttributes, ethUniHistData, &intervalEndTime); err != nil { |
| 1589 | logger.Errorw(ctx, "error during metric collection", |
| 1590 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "err": err}) |
| 1591 | return nil |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | // Populate some relevant context for the EthernetPerformanceMonitoringHistoryData PM |
| 1596 | ethUniHistData["class_id"] = float32(classID) |
| 1597 | ethUniHistData["interval_end_time"] = float32(intervalEndTime) |
| 1598 | |
| 1599 | metricsContext := make(map[string]string) |
| 1600 | metricsContext["onuID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.OnuId) |
| 1601 | metricsContext["intfID"] = fmt.Sprintf("%d", mm.pDeviceHandler.device.ProxyAddress.ChannelId) |
| 1602 | metricsContext["devicetype"] = mm.pDeviceHandler.DeviceType |
| 1603 | |
| 1604 | raisedTs := time.Now().Unix() |
| 1605 | mmd := voltha.MetricMetaData{ |
| 1606 | Title: EthernetUniHistoryName, |
| 1607 | Ts: float64(raisedTs), |
| 1608 | Context: metricsContext, |
| 1609 | DeviceId: mm.pDeviceHandler.deviceID, |
| 1610 | LogicalDeviceId: mm.pDeviceHandler.logicalDeviceID, |
| 1611 | SerialNo: mm.pDeviceHandler.device.SerialNumber, |
| 1612 | } |
| 1613 | |
| 1614 | // create slice of metrics given that there could be more than one PPTP instance |
| 1615 | metricInfo := voltha.MetricInformation{Metadata: &mmd, Metrics: ethUniHistData} |
| 1616 | logger.Debugw(ctx, "collecting data for EthernetPerformanceMonitoringHistoryData successful", |
| 1617 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, "metricInfo": metricInfo}) |
| 1618 | return &metricInfo |
| 1619 | } |
| 1620 | |
| 1621 | // nolint: gocyclo |
| 1622 | func (mm *onuMetricsManager) populateEthernetBridgeHistoryMetrics(ctx context.Context, upstream bool, classID me.ClassID, entityID uint16, |
| 1623 | meAttributes me.AttributeValueMap, requestedAttributes me.AttributeValueMap, ethPMHistData map[string]float32, intervalEndTime *int) error { |
| 1624 | // Make sure "IntervalEndTime" is part of the requested attributes as we need this to compare the get responses when get request is multipart |
| 1625 | if _, ok := requestedAttributes["IntervalEndTime"]; !ok { |
| 1626 | requestedAttributes["IntervalEndTime"] = 0 |
| 1627 | } |
| 1628 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, classID, entityID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
| 1629 | select { |
| 1630 | case meAttributes = <-mm.l2PmChan: |
| 1631 | logger.Debugw(ctx, "received ethernet pm history data metrics", |
| 1632 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "upstream": upstream, "entityID": entityID}) |
| 1633 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1634 | logger.Errorw(ctx, "timeout waiting for omci-get response for ethernet pm history data", |
| 1635 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "upstream": upstream, "entityID": entityID}) |
| 1636 | // The metrics will be empty in this case |
| 1637 | return fmt.Errorf("timeout-during-l2-pm-collection-for-ethernet-bridge-history-%v", mm.pDeviceHandler.deviceID) |
| 1638 | } |
| 1639 | // verify that interval end time has not changed during metric collection. If it changed, we abort the procedure |
| 1640 | if *intervalEndTime == -1 { // first time |
| 1641 | // Update the interval end time |
| 1642 | if val, ok := meAttributes["IntervalEndTime"]; ok && val != nil { |
| 1643 | *intervalEndTime = int(meAttributes["IntervalEndTime"].(uint8)) |
| 1644 | } |
| 1645 | } else { |
| 1646 | var currIntervalEndTime int |
| 1647 | if val, ok := meAttributes["IntervalEndTime"]; ok && val != nil { |
| 1648 | currIntervalEndTime = int(meAttributes["IntervalEndTime"].(uint8)) |
| 1649 | } |
| 1650 | if currIntervalEndTime != *intervalEndTime { // interval end time changed during metric collection |
| 1651 | logger.Errorw(ctx, "interval end time changed during metrics collection for ethernet pm history data", |
| 1652 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, |
| 1653 | "currIntervalEndTime": *intervalEndTime, "newIntervalEndTime": currIntervalEndTime}) |
| 1654 | return fmt.Errorf("interval-end-time-changed-during-metric-collection-for-ethernet-bridge-history-%v", mm.pDeviceHandler.deviceID) |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | for k := range EthernetBridgeHistory { |
| 1659 | // populate ethPMHistData only if metric key not already present (or populated), since it is possible that we populate |
| 1660 | // the attributes in multiple iterations for a given L2 PM ME as there is a limit on the max OMCI GET payload size. |
| 1661 | if _, ok := ethPMHistData[k]; !ok { |
| 1662 | switch k { |
| 1663 | case "drop_events": |
| 1664 | if val, ok := meAttributes["DropEvents"]; ok && val != nil { |
| 1665 | ethPMHistData[k] = float32(val.(uint32)) |
| 1666 | } |
| 1667 | case "octets": |
| 1668 | if val, ok := meAttributes["Octets"]; ok && val != nil { |
| 1669 | ethPMHistData[k] = float32(val.(uint32)) |
| 1670 | } |
| 1671 | case "packets": |
| 1672 | if val, ok := meAttributes["Packets"]; ok && val != nil { |
| 1673 | ethPMHistData[k] = float32(val.(uint32)) |
| 1674 | } |
| 1675 | case "broadcast_packets": |
| 1676 | if val, ok := meAttributes["BroadcastPackets"]; ok && val != nil { |
| 1677 | ethPMHistData[k] = float32(val.(uint32)) |
| 1678 | } |
| 1679 | case "multicast_packets": |
| 1680 | if val, ok := meAttributes["MulticastPackets"]; ok && val != nil { |
| 1681 | ethPMHistData[k] = float32(val.(uint32)) |
| 1682 | } |
| 1683 | case "crc_errored_packets": |
| 1684 | if val, ok := meAttributes["CrcErroredPackets"]; ok && val != nil { |
| 1685 | ethPMHistData[k] = float32(val.(uint32)) |
| 1686 | } |
| 1687 | case "undersize_packets": |
| 1688 | if val, ok := meAttributes["UndersizePackets"]; ok && val != nil { |
| 1689 | ethPMHistData[k] = float32(val.(uint32)) |
| 1690 | } |
| 1691 | case "oversize_packets": |
| 1692 | if val, ok := meAttributes["OversizePackets"]; ok && val != nil { |
| 1693 | ethPMHistData[k] = float32(val.(uint32)) |
| 1694 | } |
| 1695 | case "64_octets": |
| 1696 | if val, ok := meAttributes["Packets64Octets"]; ok && val != nil { |
| 1697 | ethPMHistData[k] = float32(val.(uint32)) |
| 1698 | } |
| 1699 | case "65_to_127_octets": |
| 1700 | if val, ok := meAttributes["Packets65To127Octets"]; ok && val != nil { |
| 1701 | ethPMHistData[k] = float32(val.(uint32)) |
| 1702 | } |
| 1703 | case "128_to_255_octets": |
| 1704 | if val, ok := meAttributes["Packets128To255Octets"]; ok && val != nil { |
| 1705 | ethPMHistData[k] = float32(val.(uint32)) |
| 1706 | } |
| 1707 | case "256_to_511_octets": |
| 1708 | if val, ok := meAttributes["Packets256To511Octets"]; ok && val != nil { |
| 1709 | ethPMHistData[k] = float32(val.(uint32)) |
| 1710 | } |
| 1711 | case "512_to_1023_octets": |
| 1712 | if val, ok := meAttributes["Packets512To1023Octets"]; ok && val != nil { |
| 1713 | ethPMHistData[k] = float32(val.(uint32)) |
| 1714 | } |
| 1715 | case "1024_to_1518_octets": |
| 1716 | if val, ok := meAttributes["Packets1024To1518Octets"]; ok && val != nil { |
| 1717 | ethPMHistData[k] = float32(val.(uint32)) |
| 1718 | } |
| 1719 | default: |
| 1720 | // do nothing |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | return nil |
| 1725 | } |
| 1726 | |
| 1727 | // nolint: gocyclo |
| 1728 | func (mm *onuMetricsManager) populateEthernetUniHistoryMetrics(ctx context.Context, classID me.ClassID, entityID uint16, |
| 1729 | meAttributes me.AttributeValueMap, requestedAttributes me.AttributeValueMap, ethPMUniHistData map[string]float32, intervalEndTime *int) error { |
| 1730 | // Make sure "IntervalEndTime" is part of the requested attributes as we need this to compare the get responses when get request is multipart |
| 1731 | if _, ok := requestedAttributes["IntervalEndTime"]; !ok { |
| 1732 | requestedAttributes["IntervalEndTime"] = 0 |
| 1733 | } |
| 1734 | if meInstance := mm.pDeviceHandler.pOnuOmciDevice.PDevOmciCC.sendGetMe(ctx, classID, entityID, requestedAttributes, ConstDefaultOmciTimeout, true, mm.pAdaptFsm.commChan); meInstance != nil { |
| 1735 | select { |
| 1736 | case meAttributes = <-mm.l2PmChan: |
| 1737 | logger.Debugw(ctx, "received ethernet uni history data metrics", |
| 1738 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID}) |
| 1739 | case <-time.After(time.Duration(ConstDefaultOmciTimeout) * time.Second): |
| 1740 | logger.Errorw(ctx, "timeout waiting for omci-get response for ethernet uni history data", |
| 1741 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID}) |
| 1742 | // The metrics will be empty in this case |
| 1743 | return fmt.Errorf("timeout-during-l2-pm-collection-for-ethernet-uni-history-%v", mm.pDeviceHandler.deviceID) |
| 1744 | } |
| 1745 | // verify that interval end time has not changed during metric collection. If it changed, we abort the procedure |
| 1746 | if *intervalEndTime == -1 { // first time |
| 1747 | // Update the interval end time |
| 1748 | if val, ok := meAttributes["IntervalEndTime"]; ok && val != nil { |
| 1749 | *intervalEndTime = int(meAttributes["IntervalEndTime"].(uint8)) |
| 1750 | } |
| 1751 | } else { |
| 1752 | var currIntervalEndTime int |
| 1753 | if val, ok := meAttributes["IntervalEndTime"]; ok && val != nil { |
| 1754 | currIntervalEndTime = int(meAttributes["IntervalEndTime"].(uint8)) |
| 1755 | } |
| 1756 | if currIntervalEndTime != *intervalEndTime { // interval end time changed during metric collection |
| 1757 | logger.Errorw(ctx, "interval end time changed during metrics collection for ethernet uni history data", |
| 1758 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "entityID": entityID, |
| 1759 | "currIntervalEndTime": *intervalEndTime, "newIntervalEndTime": currIntervalEndTime}) |
| 1760 | return fmt.Errorf("interval-end-time-changed-during-metric-collection-for-ethernet-uni-history-%v", mm.pDeviceHandler.deviceID) |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | for k := range EthernetUniHistory { |
| 1765 | // populate ethPMUniHistData only if metric key not already present (or populated), since it is possible that we populate |
| 1766 | // the attributes in multiple iterations for a given L2 PM ME as there is a limit on the max OMCI GET payload size. |
| 1767 | if _, ok := ethPMUniHistData[k]; !ok { |
| 1768 | switch k { |
| 1769 | case "fcs_errors": |
| 1770 | if val, ok := meAttributes["FcsErrors"]; ok && val != nil { |
| 1771 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1772 | } |
| 1773 | case "excessive_collision_counter": |
| 1774 | if val, ok := meAttributes["ExcessiveCollisionCounter"]; ok && val != nil { |
| 1775 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1776 | } |
| 1777 | case "late_collision_counter": |
| 1778 | if val, ok := meAttributes["LateCollisionCounter"]; ok && val != nil { |
| 1779 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1780 | } |
| 1781 | case "frames_too_long": |
| 1782 | if val, ok := meAttributes["FramesTooLong"]; ok && val != nil { |
| 1783 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1784 | } |
| 1785 | case "buffer_overflows_on_rx": |
| 1786 | if val, ok := meAttributes["BufferOverflowsOnReceive"]; ok && val != nil { |
| 1787 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1788 | } |
| 1789 | case "buffer_overflows_on_tx": |
| 1790 | if val, ok := meAttributes["BufferOverflowsOnTransmit"]; ok && val != nil { |
| 1791 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1792 | } |
| 1793 | case "single_collision_frame_counter": |
| 1794 | if val, ok := meAttributes["SingleCollisionFrameCounter"]; ok && val != nil { |
| 1795 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1796 | } |
| 1797 | case "multiple_collisions_frame_counter": |
| 1798 | if val, ok := meAttributes["MultipleCollisionsFrameCounter"]; ok && val != nil { |
| 1799 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1800 | } |
| 1801 | case "sqe_counter": |
| 1802 | if val, ok := meAttributes["SqeCounter"]; ok && val != nil { |
| 1803 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1804 | } |
| 1805 | case "deferred_tx_counter": |
| 1806 | if val, ok := meAttributes["DeferredTransmissionCounter"]; ok && val != nil { |
| 1807 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1808 | } |
| 1809 | case "internal_mac_tx_error_counter": |
| 1810 | if val, ok := meAttributes["InternalMacTransmitErrorCounter"]; ok && val != nil { |
| 1811 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1812 | } |
| 1813 | case "carrier_sense_error_counter": |
| 1814 | if val, ok := meAttributes["CarrierSenseErrorCounter"]; ok && val != nil { |
| 1815 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1816 | } |
| 1817 | case "alignment_error_counter": |
| 1818 | if val, ok := meAttributes["AlignmentErrorCounter"]; ok && val != nil { |
| 1819 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1820 | } |
| 1821 | case "internal_mac_rx_error_counter": |
| 1822 | if val, ok := meAttributes["InternalMacReceiveErrorCounter"]; ok && val != nil { |
| 1823 | ethPMUniHistData[k] = float32(val.(uint32)) |
| 1824 | } |
| 1825 | default: |
| 1826 | // do nothing |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | return nil |
| 1831 | } |
| 1832 | |
| 1833 | func (mm *onuMetricsManager) handleOmciCreateResponseMessage(ctx context.Context, msg OmciMessage) error { |
| 1834 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCreateResponse) |
| 1835 | if msgLayer == nil { |
| 1836 | logger.Errorw(ctx, "omci Msg layer could not be detected for create response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1837 | return fmt.Errorf("omci Msg layer could not be detected for create response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1838 | } |
| 1839 | msgObj, msgOk := msgLayer.(*omci.CreateResponse) |
| 1840 | if !msgOk { |
| 1841 | logger.Errorw(ctx, "omci Msg layer could not be assigned for create response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1842 | return fmt.Errorf("omci Msg layer could not be assigned for delete response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1843 | } |
| 1844 | logger.Debugw(ctx, "OMCI create response Data", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "data-fields": msgObj}) |
| 1845 | switch msgObj.EntityClass { |
| 1846 | case me.EthernetFramePerformanceMonitoringHistoryDataUpstreamClassID, |
| 1847 | me.EthernetFramePerformanceMonitoringHistoryDataDownstreamClassID, |
| 1848 | me.EthernetPerformanceMonitoringHistoryDataClassID: |
| 1849 | // If the result is me.InstanceExists it means the entity was already created. It is ok handled that as success |
| 1850 | if msgObj.Result == me.Success || msgObj.Result == me.InstanceExists { |
| 1851 | mm.l2PmCreateOrDeleteResponseChan <- true |
| 1852 | } else { |
| 1853 | logger.Warnw(ctx, "failed to create me", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1854 | mm.l2PmCreateOrDeleteResponseChan <- false |
| 1855 | } |
| 1856 | return nil |
| 1857 | default: |
| 1858 | logger.Errorw(ctx, "unhandled omci create response message", |
| 1859 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1860 | } |
| 1861 | return fmt.Errorf("unhandled-omci-create-response-message-%v", mm.pDeviceHandler.deviceID) |
| 1862 | } |
| 1863 | |
| 1864 | func (mm *onuMetricsManager) handleOmciDeleteResponseMessage(ctx context.Context, msg OmciMessage) error { |
| 1865 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDeleteResponse) |
| 1866 | if msgLayer == nil { |
| 1867 | logger.Errorw(ctx, "omci Msg layer could not be detected for delete response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1868 | return fmt.Errorf("omci Msg layer could not be detected for create response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1869 | } |
| 1870 | msgObj, msgOk := msgLayer.(*omci.DeleteResponse) |
| 1871 | if !msgOk { |
| 1872 | logger.Errorw(ctx, "omci Msg layer could not be assigned for delete response - handling stopped", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1873 | return fmt.Errorf("omci Msg layer could not be assigned for delete response - handling stopped: %s", mm.pDeviceHandler.deviceID) |
| 1874 | } |
| 1875 | logger.Debugw(ctx, "OMCI delete response Data", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "data-fields": msgObj}) |
| 1876 | switch msgObj.EntityClass { |
| 1877 | case me.EthernetFramePerformanceMonitoringHistoryDataUpstreamClassID, |
| 1878 | me.EthernetFramePerformanceMonitoringHistoryDataDownstreamClassID, |
| 1879 | me.EthernetPerformanceMonitoringHistoryDataClassID: |
| 1880 | // If the result is me.UnknownInstance it means the entity was already deleted. It is ok handled that as success |
| 1881 | if msgObj.Result == me.Success || msgObj.Result == me.UnknownInstance { |
| 1882 | mm.l2PmCreateOrDeleteResponseChan <- true |
| 1883 | } else { |
| 1884 | logger.Warnw(ctx, "failed to delete me", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1885 | mm.l2PmCreateOrDeleteResponseChan <- false |
| 1886 | } |
| 1887 | return nil |
| 1888 | default: |
| 1889 | logger.Errorw(ctx, "unhandled omci delete response message", |
| 1890 | log.Fields{"device-id": mm.pDeviceHandler.deviceID, "class-id": msgObj.EntityClass}) |
| 1891 | } |
| 1892 | return fmt.Errorf("unhandled-omci-delete-response-message-%v", mm.pDeviceHandler.deviceID) |
| 1893 | } |
| 1894 | |
| 1895 | func (mm *onuMetricsManager) generateTicks(ctx context.Context) { |
| 1896 | for { |
| 1897 | select { |
| 1898 | case <-time.After(L2PmCollectionInterval * time.Second): |
| 1899 | go func() { |
| 1900 | if err := mm.pAdaptFsm.pFsm.Event(l2PmEventTick); err != nil { |
| 1901 | logger.Errorw(ctx, "error calling event", log.Fields{"device-id": mm.pDeviceHandler.deviceID, "err": err}) |
| 1902 | } |
| 1903 | }() |
| 1904 | case <-mm.stopTicks: |
| 1905 | logger.Infow(ctx, "stopping ticks", log.Fields{"device-id": mm.pDeviceHandler.deviceID}) |
| 1906 | return |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | func (mm *onuMetricsManager) appendIfMissing(slice []string, n string) []string { |
| 1912 | for _, ele := range slice { |
| 1913 | if ele == n { |
| 1914 | return slice |
| 1915 | } |
| 1916 | } |
| 1917 | return append(slice, n) |
| 1918 | } |
| 1919 | |
| 1920 | func (mm *onuMetricsManager) removeIfFound(slice []string, n string) []string { |
| 1921 | for i, ele := range slice { |
| 1922 | if ele == n { |
| 1923 | return append(slice[:i], slice[i+1:]...) |
| 1924 | } |
| 1925 | } |
| 1926 | return slice |
Girish Gowdra | e09a620 | 2021-01-12 18:10:59 -0800 | [diff] [blame] | 1927 | } |