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