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 | |
| 28 | // Find a key in the optional AlarmParameters, convert it to an integer, |
| 29 | // return 'def' if no key exists or it cannot be converted. |
| 30 | func extractInt(params []*bbsim.AlarmParameter, name string, def int) int { |
| 31 | for _, kv := range params { |
| 32 | if kv.Key == name { |
| 33 | i, err := strconv.Atoi(kv.Value) |
| 34 | if err == nil { |
| 35 | return i |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | return def |
| 40 | } |
| 41 | |
| 42 | func BuildAlarmIndication(req *bbsim.AlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) { |
| 43 | var alarm *openolt.AlarmIndication |
| 44 | var onu *devices.Onu |
| 45 | var err error |
| 46 | |
| 47 | if req.AlarmType != bbsim.AlarmType_LOS { |
| 48 | // No ONU Id for LOS |
| 49 | onu, err = o.FindOnuBySn(req.SerialNumber) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | switch req.AlarmType { |
| 56 | case bbsim.AlarmType_LOS: |
| 57 | alarm = &openolt.AlarmIndication{ |
| 58 | Data: &openolt.AlarmIndication_LosInd{&openolt.LosIndication{ |
| 59 | // No ONU Id for LOS |
| 60 | Status: req.Status, |
| 61 | IntfId: uint32(extractInt(req.Parameters, "InterfaceId", 0)), |
| 62 | }}, |
| 63 | } |
| 64 | case bbsim.AlarmType_DYING_GASP: |
| 65 | alarm = &openolt.AlarmIndication{ |
| 66 | Data: &openolt.AlarmIndication_DyingGaspInd{&openolt.DyingGaspIndication{ |
| 67 | Status: req.Status, |
| 68 | OnuId: onu.ID, |
| 69 | IntfId: onu.PonPortID, |
| 70 | }}, |
| 71 | } |
| 72 | case bbsim.AlarmType_ONU_STARTUP_FAILURE: |
| 73 | alarm = &openolt.AlarmIndication{ |
| 74 | Data: &openolt.AlarmIndication_OnuStartupFailInd{&openolt.OnuStartupFailureIndication{ |
| 75 | Status: req.Status, |
| 76 | OnuId: onu.ID, |
| 77 | IntfId: onu.PonPortID, |
| 78 | }}, |
| 79 | } |
| 80 | case bbsim.AlarmType_ONU_SIGNAL_DEGRADE: |
| 81 | alarm = &openolt.AlarmIndication{ |
| 82 | Data: &openolt.AlarmIndication_OnuSignalDegradeInd{&openolt.OnuSignalDegradeIndication{ |
| 83 | Status: req.Status, |
| 84 | OnuId: onu.ID, |
| 85 | IntfId: onu.PonPortID, |
| 86 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 87 | }}, |
| 88 | } |
Scott Baker | 8099ef8 | 2020-02-05 12:02:57 -0800 | [diff] [blame^] | 89 | case bbsim.AlarmType_ONU_SIGNALS_FAILURE: |
| 90 | alarm = &openolt.AlarmIndication{ |
| 91 | Data: &openolt.AlarmIndication_OnuSignalsFailInd{&openolt.OnuSignalsFailureIndication{ |
| 92 | Status: req.Status, |
| 93 | OnuId: onu.ID, |
| 94 | IntfId: onu.PonPortID, |
| 95 | InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)), |
| 96 | }}, |
| 97 | } |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 98 | case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW: |
| 99 | alarm = &openolt.AlarmIndication{ |
| 100 | Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{&openolt.OnuDriftOfWindowIndication{ |
| 101 | Status: req.Status, |
| 102 | OnuId: onu.ID, |
| 103 | IntfId: onu.PonPortID, |
| 104 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 105 | NewEqd: uint32(extractInt(req.Parameters, "NewEqd", 0)), |
| 106 | }}, |
| 107 | } |
| 108 | case bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL: |
| 109 | alarm = &openolt.AlarmIndication{ |
| 110 | Data: &openolt.AlarmIndication_OnuLossOmciInd{&openolt.OnuLossOfOmciChannelIndication{ |
| 111 | Status: req.Status, |
| 112 | OnuId: onu.ID, |
| 113 | IntfId: onu.PonPortID, |
| 114 | }}, |
| 115 | } |
| 116 | case bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING: |
| 117 | alarm = &openolt.AlarmIndication{ |
| 118 | Data: &openolt.AlarmIndication_OnuTiwiInd{&openolt.OnuTransmissionInterferenceWarning{ |
| 119 | Status: req.Status, |
| 120 | OnuId: onu.ID, |
| 121 | IntfId: onu.PonPortID, |
| 122 | Drift: uint32(extractInt(req.Parameters, "Drift", 0)), |
| 123 | }}, |
| 124 | } |
| 125 | case bbsim.AlarmType_ONU_ACTIVATION_FAILURE: |
| 126 | alarm = &openolt.AlarmIndication{ |
| 127 | Data: &openolt.AlarmIndication_OnuActivationFailInd{&openolt.OnuActivationFailureIndication{ |
| 128 | OnuId: onu.ID, |
| 129 | IntfId: onu.PonPortID, |
| 130 | FailReason: uint32(extractInt(req.Parameters, "FailReason", 0)), |
| 131 | }}, |
| 132 | } |
| 133 | case bbsim.AlarmType_ONU_PROCESSING_ERROR: |
| 134 | alarm = &openolt.AlarmIndication{ |
| 135 | Data: &openolt.AlarmIndication_OnuProcessingErrorInd{&openolt.OnuProcessingErrorIndication{ |
| 136 | OnuId: onu.ID, |
| 137 | IntfId: onu.PonPortID, |
| 138 | }}, |
| 139 | } |
| 140 | case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE: |
| 141 | alarm = &openolt.AlarmIndication{ |
| 142 | Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{&openolt.OnuLossOfKeySyncFailureIndication{ |
| 143 | OnuId: onu.ID, |
| 144 | IntfId: onu.PonPortID, |
| 145 | Status: req.Status, |
| 146 | }}, |
| 147 | } |
| 148 | case bbsim.AlarmType_ONU_ITU_PON_STATS: |
| 149 | alarm = &openolt.AlarmIndication{ |
| 150 | Data: &openolt.AlarmIndication_OnuItuPonStatsInd{&openolt.OnuItuPonStatsIndication{ |
| 151 | OnuId: onu.ID, |
| 152 | IntfId: onu.PonPortID, |
| 153 | RdiErrors: uint32(extractInt(req.Parameters, "RdiErrors", 0)), |
| 154 | }}, |
| 155 | } |
| 156 | case bbsim.AlarmType_ONU_ALARM_LOS: |
| 157 | alarm = &openolt.AlarmIndication{ |
| 158 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 159 | LosStatus: req.Status, |
| 160 | OnuId: onu.ID, |
| 161 | IntfId: onu.PonPortID, |
| 162 | }}, |
| 163 | } |
| 164 | case bbsim.AlarmType_ONU_ALARM_LOB: |
| 165 | alarm = &openolt.AlarmIndication{ |
| 166 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 167 | LobStatus: req.Status, |
| 168 | OnuId: onu.ID, |
| 169 | IntfId: onu.PonPortID, |
| 170 | }}, |
| 171 | } |
| 172 | case bbsim.AlarmType_ONU_ALARM_LOPC_MISS: |
| 173 | alarm = &openolt.AlarmIndication{ |
| 174 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 175 | LopcMissStatus: req.Status, |
| 176 | OnuId: onu.ID, |
| 177 | IntfId: onu.PonPortID, |
| 178 | }}, |
| 179 | } |
| 180 | case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR: |
| 181 | alarm = &openolt.AlarmIndication{ |
| 182 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 183 | LopcMicErrorStatus: req.Status, |
| 184 | OnuId: onu.ID, |
| 185 | IntfId: onu.PonPortID, |
| 186 | }}, |
| 187 | } |
| 188 | case bbsim.AlarmType_ONU_ALARM_LOFI: |
| 189 | alarm = &openolt.AlarmIndication{ |
| 190 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 191 | LofiStatus: req.Status, |
| 192 | OnuId: onu.ID, |
| 193 | IntfId: onu.PonPortID, |
| 194 | }}, |
| 195 | } |
| 196 | case bbsim.AlarmType_ONU_ALARM_LOAMI: |
| 197 | alarm = &openolt.AlarmIndication{ |
| 198 | Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{ |
| 199 | LoamiStatus: req.Status, |
| 200 | OnuId: onu.ID, |
| 201 | IntfId: onu.PonPortID, |
| 202 | }}, |
| 203 | } |
| 204 | default: |
| 205 | return nil, fmt.Errorf("Unknown alarm type %v", req.AlarmType) |
| 206 | } |
| 207 | |
| 208 | return alarm, nil |
| 209 | } |
| 210 | |
| 211 | func SimulateAlarm(ctx context.Context, req *bbsim.AlarmRequest, o *devices.OltDevice) error { |
| 212 | alarmIndication, err := BuildAlarmIndication(req, o) |
| 213 | if err != nil { |
| 214 | return err |
| 215 | } |
| 216 | |
| 217 | err = o.SendAlarmIndication(ctx, alarmIndication) |
| 218 | if err != nil { |
| 219 | return err |
| 220 | } |
| 221 | |
| 222 | return nil |
| 223 | } |