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 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 24 | "github.com/opencord/bbsim/api/bbsim" |
| 25 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 26 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 31 | // OnuAlarmNameMap string to enum map |
| 32 | var OnuAlarmNameMap = map[string]bbsim.AlarmType_Types{ |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 33 | "DyingGasp": bbsim.AlarmType_DYING_GASP, |
| 34 | "StartupFailure": bbsim.AlarmType_ONU_STARTUP_FAILURE, |
| 35 | "SignalDegrade": bbsim.AlarmType_ONU_SIGNAL_DEGRADE, |
| 36 | "DriftOfWindow": bbsim.AlarmType_ONU_DRIFT_OF_WINDOW, |
| 37 | "LossOfOmciChannel": bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL, |
| 38 | "SignalsFailure": bbsim.AlarmType_ONU_SIGNALS_FAILURE, |
| 39 | "TransmissionInterference": bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING, |
| 40 | "ActivationFailure": bbsim.AlarmType_ONU_ACTIVATION_FAILURE, |
| 41 | "ProcessingError": bbsim.AlarmType_ONU_PROCESSING_ERROR, |
| 42 | "LossOfKeySyncFailure": bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE, |
| 43 | |
| 44 | // Break out OnuAlarm into its subcases. |
| 45 | "LossOfSignal": bbsim.AlarmType_ONU_ALARM_LOS, |
| 46 | "LossOfBurst": bbsim.AlarmType_ONU_ALARM_LOB, |
| 47 | "LOPC_MISS": bbsim.AlarmType_ONU_ALARM_LOPC_MISS, |
| 48 | "LOPC_MIC_ERROR": bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR, |
| 49 | "LossOfFrame": bbsim.AlarmType_ONU_ALARM_LOFI, |
| 50 | "LossOfPloam": bbsim.AlarmType_ONU_ALARM_LOAMI, |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 51 | } |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 52 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 53 | // OltAlarmNameMap string to enum map |
| 54 | var OltAlarmNameMap = map[string]bbsim.AlarmType_Types{ |
| 55 | // PON / Non-onu-specific |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 56 | "PonLossOfSignal": bbsim.AlarmType_LOS, |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 57 | // NNI / Non-onu-specific |
| 58 | "NniLossOfSignal": bbsim.AlarmType_LOS, |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func AlarmNameToEnum(name string) (*bbsim.AlarmType_Types, error) { |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 62 | v, okay := OnuAlarmNameMap[name] |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 63 | if !okay { |
| 64 | return nil, fmt.Errorf("Unknown Alarm Name: %v", name) |
| 65 | } |
| 66 | |
| 67 | return &v, nil |
| 68 | } |
| 69 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 70 | // Find a key in the optional AlarmParameters, convert it to an integer, |
| 71 | // return 'def' if no key exists or it cannot be converted. |
| 72 | func extractInt(params []*bbsim.AlarmParameter, name string, def int) int { |
| 73 | for _, kv := range params { |
| 74 | if kv.Key == name { |
| 75 | i, err := strconv.Atoi(kv.Value) |
| 76 | if err == nil { |
| 77 | return i |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return def |
| 82 | } |
| 83 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 84 | // BuildOnuAlarmIndication function forms openolt alarmIndication as per ONUAlarmRequest |
| 85 | func BuildOnuAlarmIndication(req *bbsim.ONUAlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 86 | var alarm *openolt.AlarmIndication |
| 87 | var onu *devices.Onu |
| 88 | var err error |
| 89 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 90 | alarmType, err := AlarmNameToEnum(req.AlarmType) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | |
| 95 | if *alarmType != bbsim.AlarmType_LOS { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 96 | // No ONU Id for LOS |
| 97 | onu, err = o.FindOnuBySn(req.SerialNumber) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | } |
| 102 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 103 | switch *alarmType { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 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 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 251 | // SimulateOnuAlarm accept request for Onu alarms and send proper alarmIndication to openolt stream |
| 252 | func SimulateOnuAlarm(ctx context.Context, req *bbsim.ONUAlarmRequest, o *devices.OltDevice) error { |
| 253 | alarmIndication, err := BuildOnuAlarmIndication(req, o) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 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 | } |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame^] | 265 | |
| 266 | // InterfaceIDToPortNo converts InterfaceID to voltha PortID |
| 267 | // Refer openolt adapter code(master) voltha-openolt-adapter/adaptercore/olt_platform.go: IntfIDToPortNo() |
| 268 | func InterfaceIDToPortNo(req *bbsim.OLTAlarmRequest) uint32 { |
| 269 | // Converts interface-id to port-numbers that can be understood by the VOLTHA |
| 270 | if req.InterfaceType == "nni" || req.InterfaceType == "PonLossOfSignal" { |
| 271 | // nni at voltha starts with 1,048,576 |
| 272 | // nni = 1,048,576 + InterfaceID |
| 273 | return 0x1<<20 + req.InterfaceID |
| 274 | } else if req.InterfaceType == "pon" || req.InterfaceType == "NniLossOfSignal" { |
| 275 | // pon = 536,870,912 + InterfaceID |
| 276 | return (0x2 << 28) + req.InterfaceID |
| 277 | // In bbsim, pon starts from 1 |
| 278 | } |
| 279 | return 0 |
| 280 | } |
| 281 | |
| 282 | // IsPonPortPresentInOlt verifies if given Pon port is present in olt |
| 283 | func IsPonPortPresentInOlt(PonPort uint32) bool { |
| 284 | o := devices.GetOLT() |
| 285 | for _, intf := range o.Pons { |
| 286 | if intf.ID == PonPort { |
| 287 | return true |
| 288 | } |
| 289 | } |
| 290 | return false |
| 291 | } |
| 292 | |
| 293 | // IsNniPortPresentInOlt verifies if given nni port is present in olt |
| 294 | func IsNniPortPresentInOlt(nniPort uint32) bool { |
| 295 | o := devices.GetOLT() |
| 296 | for _, intf := range o.Nnis { |
| 297 | if intf.ID == nniPort { |
| 298 | return true |
| 299 | } |
| 300 | } |
| 301 | return false |
| 302 | } |
| 303 | |
| 304 | // SimulateOltAlarm accept request for Olt alarms and send proper alarmIndication to openolt stream |
| 305 | func SimulateOltAlarm(ctx context.Context, req *bbsim.OLTAlarmRequest, o *devices.OltDevice) error { |
| 306 | var alarmIndication *openolt.AlarmIndication |
| 307 | var err error |
| 308 | |
| 309 | //check if its a valid port id |
| 310 | switch req.InterfaceType { |
| 311 | case "nni": |
| 312 | if !IsNniPortPresentInOlt(uint32(req.InterfaceID)) { |
| 313 | return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" NNI not present in olt") |
| 314 | } |
| 315 | |
| 316 | case "pon": |
| 317 | if !IsPonPortPresentInOlt(uint32(req.InterfaceID)) { |
| 318 | return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" PON not present in olt") |
| 319 | } |
| 320 | } |
| 321 | alarmIndication = &openolt.AlarmIndication{ |
| 322 | Data: &openolt.AlarmIndication_LosInd{&openolt.LosIndication{ |
| 323 | Status: req.Status, |
| 324 | IntfId: InterfaceIDToPortNo(req), |
| 325 | }}, |
| 326 | } |
| 327 | |
| 328 | err = o.SendAlarmIndication(ctx, alarmIndication) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 | |
| 333 | return nil |
| 334 | } |