Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 alarmsim |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 22 | "strconv" |
| 23 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 24 | "github.com/opencord/bbsim/internal/common" |
| 25 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 26 | "github.com/opencord/bbsim/api/bbsim" |
| 27 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v3/go/openolt" |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 33 | func AlarmNameToEnum(name string) (bbsim.AlarmType_Types, error) { |
| 34 | v, okay := common.ONUAlarms[name] |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 35 | if !okay { |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 36 | return 0, fmt.Errorf("Unknown Alarm Name: %v", name) |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 37 | } |
| 38 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 39 | return v, nil |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 40 | } |
| 41 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 42 | // Find a key in the optional AlarmParameters, convert it to an integer, |
| 43 | // return 'def' if no key exists or it cannot be converted. |
| 44 | func extractInt(params []*bbsim.AlarmParameter, name string, def int) int { |
| 45 | for _, kv := range params { |
| 46 | if kv.Key == name { |
| 47 | i, err := strconv.Atoi(kv.Value) |
| 48 | if err == nil { |
| 49 | return i |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return def |
| 54 | } |
| 55 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 56 | // BuildOnuAlarmIndication function forms openolt alarmIndication as per ONUAlarmRequest |
| 57 | func BuildOnuAlarmIndication(req *bbsim.ONUAlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 58 | var alarm *openolt.AlarmIndication |
| 59 | var onu *devices.Onu |
| 60 | var err error |
| 61 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 62 | alarmType, err := AlarmNameToEnum(req.AlarmType) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 67 | if alarmType != bbsim.AlarmType_LOS { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 68 | // No ONU Id for LOS |
| 69 | onu, err = o.FindOnuBySn(req.SerialNumber) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | } |
| 74 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 75 | switch alarmType { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 76 | case bbsim.AlarmType_DYING_GASP: |
| 77 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 78 | Data: &openolt.AlarmIndication_DyingGaspInd{DyingGaspInd: &openolt.DyingGaspIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 79 | Status: req.Status, |
| 80 | OnuId: onu.ID, |
| 81 | IntfId: onu.PonPortID, |
| 82 | }}, |
| 83 | } |
| 84 | case bbsim.AlarmType_ONU_STARTUP_FAILURE: |
| 85 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 86 | Data: &openolt.AlarmIndication_OnuStartupFailInd{OnuStartupFailInd: &openolt.OnuStartupFailureIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 87 | Status: req.Status, |
| 88 | OnuId: onu.ID, |
| 89 | IntfId: onu.PonPortID, |
| 90 | }}, |
| 91 | } |
| 92 | case bbsim.AlarmType_ONU_SIGNAL_DEGRADE: |
| 93 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 94 | Data: &openolt.AlarmIndication_OnuSignalDegradeInd{OnuSignalDegradeInd: &openolt.OnuSignalDegradeIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 95 | Status: req.Status, |
| 96 | OnuId: onu.ID, |
| 97 | IntfId: onu.PonPortID, |
| 98 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 99 | }}, |
| 100 | } |
Scott Baker | 8099ef8 | 2020-02-05 12:02:57 -0800 | [diff] [blame] | 101 | case bbsim.AlarmType_ONU_SIGNALS_FAILURE: |
| 102 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 103 | Data: &openolt.AlarmIndication_OnuSignalsFailInd{OnuSignalsFailInd: &openolt.OnuSignalsFailureIndication{ |
Scott Baker | 8099ef8 | 2020-02-05 12:02:57 -0800 | [diff] [blame] | 104 | Status: req.Status, |
| 105 | OnuId: onu.ID, |
| 106 | IntfId: onu.PonPortID, |
| 107 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 108 | }}, |
| 109 | } |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 110 | case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW: |
| 111 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 112 | Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{OnuDriftOfWindowInd: &openolt.OnuDriftOfWindowIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 113 | Status: req.Status, |
| 114 | OnuId: onu.ID, |
| 115 | IntfId: onu.PonPortID, |
| 116 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 117 | NewEqd: uint32(extractInt(req.Parameters, "NewEqd", 0)), |
| 118 | }}, |
| 119 | } |
| 120 | case bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL: |
| 121 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 122 | Data: &openolt.AlarmIndication_OnuLossOmciInd{OnuLossOmciInd: &openolt.OnuLossOfOmciChannelIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 123 | Status: req.Status, |
| 124 | OnuId: onu.ID, |
| 125 | IntfId: onu.PonPortID, |
| 126 | }}, |
| 127 | } |
| 128 | case bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING: |
| 129 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 130 | Data: &openolt.AlarmIndication_OnuTiwiInd{OnuTiwiInd: &openolt.OnuTransmissionInterferenceWarning{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 131 | Status: req.Status, |
| 132 | OnuId: onu.ID, |
| 133 | IntfId: onu.PonPortID, |
| 134 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 135 | }}, |
| 136 | } |
| 137 | case bbsim.AlarmType_ONU_ACTIVATION_FAILURE: |
| 138 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 139 | Data: &openolt.AlarmIndication_OnuActivationFailInd{OnuActivationFailInd: &openolt.OnuActivationFailureIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 140 | OnuId: onu.ID, |
| 141 | IntfId: onu.PonPortID, |
| 142 | FailReason: uint32(extractInt(req.Parameters, "FailReason", 0)), |
| 143 | }}, |
| 144 | } |
| 145 | case bbsim.AlarmType_ONU_PROCESSING_ERROR: |
| 146 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 147 | Data: &openolt.AlarmIndication_OnuProcessingErrorInd{OnuProcessingErrorInd: &openolt.OnuProcessingErrorIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 148 | OnuId: onu.ID, |
| 149 | IntfId: onu.PonPortID, |
| 150 | }}, |
| 151 | } |
| 152 | case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE: |
| 153 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 154 | Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{OnuLossOfSyncFailInd: &openolt.OnuLossOfKeySyncFailureIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 155 | OnuId: onu.ID, |
| 156 | IntfId: onu.PonPortID, |
| 157 | Status: req.Status, |
| 158 | }}, |
| 159 | } |
| 160 | case bbsim.AlarmType_ONU_ITU_PON_STATS: |
| 161 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 162 | Data: &openolt.AlarmIndication_OnuItuPonStatsInd{OnuItuPonStatsInd: &openolt.OnuItuPonStatsIndication{ |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 163 | OnuId: onu.ID, |
| 164 | IntfId: onu.PonPortID, |
| 165 | Stats: &openolt.OnuItuPonStatsIndication_RdiErrorInd{ |
| 166 | RdiErrorInd: &openolt.RdiErrorIndication{ |
| 167 | RdiErrorCount: uint64(extractInt(req.Parameters, "RdiErrors", 0)), |
| 168 | Status: req.Status, |
| 169 | }, |
| 170 | }, |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 171 | }}, |
| 172 | } |
| 173 | case bbsim.AlarmType_ONU_ALARM_LOS: |
| 174 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 175 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 176 | LosStatus: req.Status, |
| 177 | OnuId: onu.ID, |
| 178 | IntfId: onu.PonPortID, |
| 179 | }}, |
| 180 | } |
| 181 | case bbsim.AlarmType_ONU_ALARM_LOB: |
| 182 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 183 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 184 | LobStatus: req.Status, |
| 185 | OnuId: onu.ID, |
| 186 | IntfId: onu.PonPortID, |
| 187 | }}, |
| 188 | } |
| 189 | case bbsim.AlarmType_ONU_ALARM_LOPC_MISS: |
| 190 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 191 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 192 | LopcMissStatus: req.Status, |
| 193 | OnuId: onu.ID, |
| 194 | IntfId: onu.PonPortID, |
| 195 | }}, |
| 196 | } |
| 197 | case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR: |
| 198 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 199 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 200 | LopcMicErrorStatus: req.Status, |
| 201 | OnuId: onu.ID, |
| 202 | IntfId: onu.PonPortID, |
| 203 | }}, |
| 204 | } |
| 205 | case bbsim.AlarmType_ONU_ALARM_LOFI: |
| 206 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 207 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 208 | LofiStatus: req.Status, |
| 209 | OnuId: onu.ID, |
| 210 | IntfId: onu.PonPortID, |
| 211 | }}, |
| 212 | } |
| 213 | case bbsim.AlarmType_ONU_ALARM_LOAMI: |
| 214 | alarm = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 215 | Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{ |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 216 | LoamiStatus: req.Status, |
| 217 | OnuId: onu.ID, |
| 218 | IntfId: onu.PonPortID, |
| 219 | }}, |
| 220 | } |
| 221 | default: |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 222 | return nil, fmt.Errorf("Unknown ONU alarm type %v", req.AlarmType) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return alarm, nil |
| 226 | } |
| 227 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 228 | // SimulateOnuAlarm accept request for Onu alarms and send proper alarmIndication to openolt stream |
| 229 | func SimulateOnuAlarm(ctx context.Context, req *bbsim.ONUAlarmRequest, o *devices.OltDevice) error { |
| 230 | alarmIndication, err := BuildOnuAlarmIndication(req, o) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 231 | if err != nil { |
| 232 | return err |
| 233 | } |
| 234 | |
| 235 | err = o.SendAlarmIndication(ctx, alarmIndication) |
| 236 | if err != nil { |
| 237 | return err |
| 238 | } |
| 239 | |
| 240 | return nil |
| 241 | } |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 242 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 243 | // IsPonPortPresentInOlt verifies if given Pon port is present in olt |
| 244 | func IsPonPortPresentInOlt(PonPort uint32) bool { |
| 245 | o := devices.GetOLT() |
| 246 | for _, intf := range o.Pons { |
| 247 | if intf.ID == PonPort { |
| 248 | return true |
| 249 | } |
| 250 | } |
| 251 | return false |
| 252 | } |
| 253 | |
| 254 | // IsNniPortPresentInOlt verifies if given nni port is present in olt |
| 255 | func IsNniPortPresentInOlt(nniPort uint32) bool { |
| 256 | o := devices.GetOLT() |
| 257 | for _, intf := range o.Nnis { |
| 258 | if intf.ID == nniPort { |
| 259 | return true |
| 260 | } |
| 261 | } |
| 262 | return false |
| 263 | } |
| 264 | |
| 265 | // SimulateOltAlarm accept request for Olt alarms and send proper alarmIndication to openolt stream |
| 266 | func SimulateOltAlarm(ctx context.Context, req *bbsim.OLTAlarmRequest, o *devices.OltDevice) error { |
| 267 | var alarmIndication *openolt.AlarmIndication |
| 268 | var err error |
| 269 | |
| 270 | //check if its a valid port id |
| 271 | switch req.InterfaceType { |
| 272 | case "nni": |
| 273 | if !IsNniPortPresentInOlt(uint32(req.InterfaceID)) { |
| 274 | return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" NNI not present in olt") |
| 275 | } |
| 276 | |
| 277 | case "pon": |
| 278 | if !IsPonPortPresentInOlt(uint32(req.InterfaceID)) { |
| 279 | return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" PON not present in olt") |
| 280 | } |
| 281 | } |
| 282 | alarmIndication = &openolt.AlarmIndication{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 283 | Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{ |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 284 | Status: req.Status, |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 285 | IntfId: devices.InterfaceIDToPortNo(req.InterfaceID, req.InterfaceType), |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 286 | }}, |
| 287 | } |
| 288 | |
| 289 | err = o.SendAlarmIndication(ctx, alarmIndication) |
| 290 | if err != nil { |
| 291 | return err |
| 292 | } |
| 293 | |
| 294 | return nil |
| 295 | } |