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" |
| 22 | "github.com/opencord/bbsim/api/bbsim" |
| 23 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 24 | "github.com/opencord/voltha-protos/v2/go/openolt" |
| 25 | "strconv" |
| 26 | ) |
| 27 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame^] | 28 | // AlarmNameMap string to enum map |
| 29 | var AlarmNameMap = map[string]bbsim.AlarmType_Types{ |
| 30 | "DyingGasp": bbsim.AlarmType_DYING_GASP, |
| 31 | "StartupFailure": bbsim.AlarmType_ONU_STARTUP_FAILURE, |
| 32 | "SignalDegrade": bbsim.AlarmType_ONU_SIGNAL_DEGRADE, |
| 33 | "DriftOfWindow": bbsim.AlarmType_ONU_DRIFT_OF_WINDOW, |
| 34 | "LossOfOmciChannel": bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL, |
| 35 | "SignalsFailure": bbsim.AlarmType_ONU_SIGNALS_FAILURE, |
| 36 | "TransmissionInterference": bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING, |
| 37 | "ActivationFailure": bbsim.AlarmType_ONU_ACTIVATION_FAILURE, |
| 38 | "ProcessingError": bbsim.AlarmType_ONU_PROCESSING_ERROR, |
| 39 | "LossOfKeySyncFailure": bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE, |
| 40 | |
| 41 | // Break out OnuAlarm into its subcases. |
| 42 | "LossOfSignal": bbsim.AlarmType_ONU_ALARM_LOS, |
| 43 | "LossOfBurst": bbsim.AlarmType_ONU_ALARM_LOB, |
| 44 | "LOPC_MISS": bbsim.AlarmType_ONU_ALARM_LOPC_MISS, |
| 45 | "LOPC_MIC_ERROR": bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR, |
| 46 | "LossOfFrame": bbsim.AlarmType_ONU_ALARM_LOFI, |
| 47 | "LossOfPloam": bbsim.AlarmType_ONU_ALARM_LOAMI, |
| 48 | |
| 49 | // Whole-PON / Non-onu-specific |
| 50 | "PonLossOfSignal": bbsim.AlarmType_LOS, |
| 51 | } |
| 52 | |
| 53 | func AlarmNameToEnum(name string) (*bbsim.AlarmType_Types, error) { |
| 54 | v, okay := AlarmNameMap[name] |
| 55 | if !okay { |
| 56 | return nil, fmt.Errorf("Unknown Alarm Name: %v", name) |
| 57 | } |
| 58 | |
| 59 | return &v, nil |
| 60 | } |
| 61 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 62 | // Find a key in the optional AlarmParameters, convert it to an integer, |
| 63 | // return 'def' if no key exists or it cannot be converted. |
| 64 | func extractInt(params []*bbsim.AlarmParameter, name string, def int) int { |
| 65 | for _, kv := range params { |
| 66 | if kv.Key == name { |
| 67 | i, err := strconv.Atoi(kv.Value) |
| 68 | if err == nil { |
| 69 | return i |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return def |
| 74 | } |
| 75 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame^] | 76 | // BuildAlarmIndication function forms openolt alarmIndication as per AlarmRequest |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 77 | func BuildAlarmIndication(req *bbsim.AlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) { |
| 78 | var alarm *openolt.AlarmIndication |
| 79 | var onu *devices.Onu |
| 80 | var err error |
| 81 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame^] | 82 | alarmType, err := AlarmNameToEnum(req.AlarmType) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
| 87 | if *alarmType != bbsim.AlarmType_LOS { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 88 | // No ONU Id for LOS |
| 89 | onu, err = o.FindOnuBySn(req.SerialNumber) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | } |
| 94 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame^] | 95 | switch *alarmType { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 96 | case bbsim.AlarmType_LOS: |
| 97 | alarm = &openolt.AlarmIndication{ |
| 98 | Data: &openolt.AlarmIndication_LosInd{&openolt.LosIndication{ |
| 99 | // No ONU Id for LOS |
| 100 | Status: req.Status, |
| 101 | IntfId: uint32(extractInt(req.Parameters, "InterfaceId", 0)), |
| 102 | }}, |
| 103 | } |
| 104 | case bbsim.AlarmType_DYING_GASP: |
| 105 | alarm = &openolt.AlarmIndication{ |
| 106 | Data: &openolt.AlarmIndication_DyingGaspInd{&openolt.DyingGaspIndication{ |
| 107 | Status: req.Status, |
| 108 | OnuId: onu.ID, |
| 109 | IntfId: onu.PonPortID, |
| 110 | }}, |
| 111 | } |
| 112 | case bbsim.AlarmType_ONU_STARTUP_FAILURE: |
| 113 | alarm = &openolt.AlarmIndication{ |
| 114 | Data: &openolt.AlarmIndication_OnuStartupFailInd{&openolt.OnuStartupFailureIndication{ |
| 115 | Status: req.Status, |
| 116 | OnuId: onu.ID, |
| 117 | IntfId: onu.PonPortID, |
| 118 | }}, |
| 119 | } |
| 120 | case bbsim.AlarmType_ONU_SIGNAL_DEGRADE: |
| 121 | alarm = &openolt.AlarmIndication{ |
| 122 | Data: &openolt.AlarmIndication_OnuSignalDegradeInd{&openolt.OnuSignalDegradeIndication{ |
| 123 | Status: req.Status, |
| 124 | OnuId: onu.ID, |
| 125 | IntfId: onu.PonPortID, |
| 126 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 127 | }}, |
| 128 | } |
Scott Baker | 8099ef8 | 2020-02-05 12:02:57 -0800 | [diff] [blame] | 129 | case bbsim.AlarmType_ONU_SIGNALS_FAILURE: |
| 130 | alarm = &openolt.AlarmIndication{ |
| 131 | Data: &openolt.AlarmIndication_OnuSignalsFailInd{&openolt.OnuSignalsFailureIndication{ |
| 132 | Status: req.Status, |
| 133 | OnuId: onu.ID, |
| 134 | IntfId: onu.PonPortID, |
| 135 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 136 | }}, |
| 137 | } |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 138 | case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW: |
| 139 | alarm = &openolt.AlarmIndication{ |
| 140 | Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{&openolt.OnuDriftOfWindowIndication{ |
| 141 | Status: req.Status, |
| 142 | OnuId: onu.ID, |
| 143 | IntfId: onu.PonPortID, |
| 144 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 145 | NewEqd: uint32(extractInt(req.Parameters, "NewEqd", 0)), |
| 146 | }}, |
| 147 | } |
| 148 | case bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL: |
| 149 | alarm = &openolt.AlarmIndication{ |
| 150 | Data: &openolt.AlarmIndication_OnuLossOmciInd{&openolt.OnuLossOfOmciChannelIndication{ |
| 151 | Status: req.Status, |
| 152 | OnuId: onu.ID, |
| 153 | IntfId: onu.PonPortID, |
| 154 | }}, |
| 155 | } |
| 156 | case bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING: |
| 157 | alarm = &openolt.AlarmIndication{ |
| 158 | Data: &openolt.AlarmIndication_OnuTiwiInd{&openolt.OnuTransmissionInterferenceWarning{ |
| 159 | Status: req.Status, |
| 160 | OnuId: onu.ID, |
| 161 | IntfId: onu.PonPortID, |
| 162 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 163 | }}, |
| 164 | } |
| 165 | case bbsim.AlarmType_ONU_ACTIVATION_FAILURE: |
| 166 | alarm = &openolt.AlarmIndication{ |
| 167 | Data: &openolt.AlarmIndication_OnuActivationFailInd{&openolt.OnuActivationFailureIndication{ |
| 168 | OnuId: onu.ID, |
| 169 | IntfId: onu.PonPortID, |
| 170 | FailReason: uint32(extractInt(req.Parameters, "FailReason", 0)), |
| 171 | }}, |
| 172 | } |
| 173 | case bbsim.AlarmType_ONU_PROCESSING_ERROR: |
| 174 | alarm = &openolt.AlarmIndication{ |
| 175 | Data: &openolt.AlarmIndication_OnuProcessingErrorInd{&openolt.OnuProcessingErrorIndication{ |
| 176 | OnuId: onu.ID, |
| 177 | IntfId: onu.PonPortID, |
| 178 | }}, |
| 179 | } |
| 180 | case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE: |
| 181 | alarm = &openolt.AlarmIndication{ |
| 182 | Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{&openolt.OnuLossOfKeySyncFailureIndication{ |
| 183 | OnuId: onu.ID, |
| 184 | IntfId: onu.PonPortID, |
| 185 | Status: req.Status, |
| 186 | }}, |
| 187 | } |
| 188 | case bbsim.AlarmType_ONU_ITU_PON_STATS: |
| 189 | alarm = &openolt.AlarmIndication{ |
| 190 | Data: &openolt.AlarmIndication_OnuItuPonStatsInd{&openolt.OnuItuPonStatsIndication{ |
| 191 | OnuId: onu.ID, |
| 192 | IntfId: onu.PonPortID, |
| 193 | RdiErrors: uint32(extractInt(req.Parameters, "RdiErrors", 0)), |
| 194 | }}, |
| 195 | } |
| 196 | case bbsim.AlarmType_ONU_ALARM_LOS: |
| 197 | alarm = &openolt.AlarmIndication{ |
| 198 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 199 | LosStatus: req.Status, |
| 200 | OnuId: onu.ID, |
| 201 | IntfId: onu.PonPortID, |
| 202 | }}, |
| 203 | } |
| 204 | case bbsim.AlarmType_ONU_ALARM_LOB: |
| 205 | alarm = &openolt.AlarmIndication{ |
| 206 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 207 | LobStatus: req.Status, |
| 208 | OnuId: onu.ID, |
| 209 | IntfId: onu.PonPortID, |
| 210 | }}, |
| 211 | } |
| 212 | case bbsim.AlarmType_ONU_ALARM_LOPC_MISS: |
| 213 | alarm = &openolt.AlarmIndication{ |
| 214 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 215 | LopcMissStatus: req.Status, |
| 216 | OnuId: onu.ID, |
| 217 | IntfId: onu.PonPortID, |
| 218 | }}, |
| 219 | } |
| 220 | case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR: |
| 221 | alarm = &openolt.AlarmIndication{ |
| 222 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 223 | LopcMicErrorStatus: req.Status, |
| 224 | OnuId: onu.ID, |
| 225 | IntfId: onu.PonPortID, |
| 226 | }}, |
| 227 | } |
| 228 | case bbsim.AlarmType_ONU_ALARM_LOFI: |
| 229 | alarm = &openolt.AlarmIndication{ |
| 230 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 231 | LofiStatus: req.Status, |
| 232 | OnuId: onu.ID, |
| 233 | IntfId: onu.PonPortID, |
| 234 | }}, |
| 235 | } |
| 236 | case bbsim.AlarmType_ONU_ALARM_LOAMI: |
| 237 | alarm = &openolt.AlarmIndication{ |
| 238 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 239 | LoamiStatus: req.Status, |
| 240 | OnuId: onu.ID, |
| 241 | IntfId: onu.PonPortID, |
| 242 | }}, |
| 243 | } |
| 244 | default: |
| 245 | return nil, fmt.Errorf("Unknown alarm type %v", req.AlarmType) |
| 246 | } |
| 247 | |
| 248 | return alarm, nil |
| 249 | } |
| 250 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame^] | 251 | // SimulateAlarm accept request for alarms and send proper alarmIndication to openolt stream |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 252 | func SimulateAlarm(ctx context.Context, req *bbsim.AlarmRequest, o *devices.OltDevice) error { |
| 253 | alarmIndication, err := BuildAlarmIndication(req, o) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | |
| 258 | err = o.SendAlarmIndication(ctx, alarmIndication) |
| 259 | if err != nil { |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | return nil |
| 264 | } |