blob: cbb51da115f3fa24320f4bd8a7af7d10550ab9a0 [file] [log] [blame]
Scott Baker41724b82020-01-21 19:54:53 -08001/*
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
17package alarmsim
18
19import (
20 "context"
21 "fmt"
Anand S Katti86552f92020-03-03 21:56:32 +053022 "strconv"
23
Shrey Baid688b4242020-07-10 20:40:10 +053024 "github.com/opencord/bbsim/internal/common"
25
Scott Baker41724b82020-01-21 19:54:53 -080026 "github.com/opencord/bbsim/api/bbsim"
27 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070028 "github.com/opencord/voltha-protos/v4/go/openolt"
Anand S Katti86552f92020-03-03 21:56:32 +053029 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/status"
Scott Baker41724b82020-01-21 19:54:53 -080031)
32
Kent Hagerman60d62302020-03-10 17:02:36 -040033func AlarmNameToEnum(name string) (bbsim.AlarmType_Types, error) {
34 v, okay := common.ONUAlarms[name]
Pragya Arya694ece02020-02-07 13:03:47 +053035 if !okay {
Kent Hagerman60d62302020-03-10 17:02:36 -040036 return 0, fmt.Errorf("Unknown Alarm Name: %v", name)
Pragya Arya694ece02020-02-07 13:03:47 +053037 }
38
Kent Hagerman60d62302020-03-10 17:02:36 -040039 return v, nil
Pragya Arya694ece02020-02-07 13:03:47 +053040}
41
Scott Baker41724b82020-01-21 19:54:53 -080042// Find a key in the optional AlarmParameters, convert it to an integer,
43// return 'def' if no key exists or it cannot be converted.
44func 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 Katti86552f92020-03-03 21:56:32 +053056// BuildOnuAlarmIndication function forms openolt alarmIndication as per ONUAlarmRequest
57func BuildOnuAlarmIndication(req *bbsim.ONUAlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) {
Scott Baker41724b82020-01-21 19:54:53 -080058 var alarm *openolt.AlarmIndication
59 var onu *devices.Onu
60 var err error
61
Pragya Arya694ece02020-02-07 13:03:47 +053062 alarmType, err := AlarmNameToEnum(req.AlarmType)
63 if err != nil {
64 return nil, err
65 }
66
Kent Hagerman60d62302020-03-10 17:02:36 -040067 if alarmType != bbsim.AlarmType_LOS {
Scott Baker41724b82020-01-21 19:54:53 -080068 // No ONU Id for LOS
69 onu, err = o.FindOnuBySn(req.SerialNumber)
70 if err != nil {
71 return nil, err
72 }
73 }
74
Kent Hagerman60d62302020-03-10 17:02:36 -040075 switch alarmType {
Scott Baker41724b82020-01-21 19:54:53 -080076 case bbsim.AlarmType_DYING_GASP:
77 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +053078 Data: &openolt.AlarmIndication_DyingGaspInd{DyingGaspInd: &openolt.DyingGaspIndication{
Scott Baker41724b82020-01-21 19:54:53 -080079 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 Baid688b4242020-07-10 20:40:10 +053086 Data: &openolt.AlarmIndication_OnuStartupFailInd{OnuStartupFailInd: &openolt.OnuStartupFailureIndication{
Scott Baker41724b82020-01-21 19:54:53 -080087 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 Baid688b4242020-07-10 20:40:10 +053094 Data: &openolt.AlarmIndication_OnuSignalDegradeInd{OnuSignalDegradeInd: &openolt.OnuSignalDegradeIndication{
Scott Baker41724b82020-01-21 19:54:53 -080095 Status: req.Status,
96 OnuId: onu.ID,
97 IntfId: onu.PonPortID,
98 InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)),
99 }},
100 }
Scott Baker8099ef82020-02-05 12:02:57 -0800101 case bbsim.AlarmType_ONU_SIGNALS_FAILURE:
102 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530103 Data: &openolt.AlarmIndication_OnuSignalsFailInd{OnuSignalsFailInd: &openolt.OnuSignalsFailureIndication{
Scott Baker8099ef82020-02-05 12:02:57 -0800104 Status: req.Status,
105 OnuId: onu.ID,
106 IntfId: onu.PonPortID,
107 InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)),
108 }},
109 }
Scott Baker41724b82020-01-21 19:54:53 -0800110 case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW:
111 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530112 Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{OnuDriftOfWindowInd: &openolt.OnuDriftOfWindowIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800113 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 Baid688b4242020-07-10 20:40:10 +0530122 Data: &openolt.AlarmIndication_OnuLossOmciInd{OnuLossOmciInd: &openolt.OnuLossOfOmciChannelIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800123 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 Baid688b4242020-07-10 20:40:10 +0530130 Data: &openolt.AlarmIndication_OnuTiwiInd{OnuTiwiInd: &openolt.OnuTransmissionInterferenceWarning{
Scott Baker41724b82020-01-21 19:54:53 -0800131 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 Baid688b4242020-07-10 20:40:10 +0530139 Data: &openolt.AlarmIndication_OnuActivationFailInd{OnuActivationFailInd: &openolt.OnuActivationFailureIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800140 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 Baid688b4242020-07-10 20:40:10 +0530147 Data: &openolt.AlarmIndication_OnuProcessingErrorInd{OnuProcessingErrorInd: &openolt.OnuProcessingErrorIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800148 OnuId: onu.ID,
149 IntfId: onu.PonPortID,
150 }},
151 }
152 case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE:
153 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530154 Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{OnuLossOfSyncFailInd: &openolt.OnuLossOfKeySyncFailureIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800155 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 Baid688b4242020-07-10 20:40:10 +0530162 Data: &openolt.AlarmIndication_OnuItuPonStatsInd{OnuItuPonStatsInd: &openolt.OnuItuPonStatsIndication{
Matteo Scandolo618a6582020-09-09 12:21:29 -0700163 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 Baker41724b82020-01-21 19:54:53 -0800171 }},
172 }
173 case bbsim.AlarmType_ONU_ALARM_LOS:
174 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530175 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800176 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 Baid688b4242020-07-10 20:40:10 +0530183 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800184 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 Baid688b4242020-07-10 20:40:10 +0530191 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800192 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 Baid688b4242020-07-10 20:40:10 +0530199 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800200 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 Baid688b4242020-07-10 20:40:10 +0530207 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800208 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 Baid688b4242020-07-10 20:40:10 +0530215 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800216 LoamiStatus: req.Status,
217 OnuId: onu.ID,
218 IntfId: onu.PonPortID,
219 }},
220 }
221 default:
Kent Hagerman60d62302020-03-10 17:02:36 -0400222 return nil, fmt.Errorf("Unknown ONU alarm type %v", req.AlarmType)
Scott Baker41724b82020-01-21 19:54:53 -0800223 }
224
225 return alarm, nil
226}
227
Anand S Katti86552f92020-03-03 21:56:32 +0530228// SimulateOnuAlarm accept request for Onu alarms and send proper alarmIndication to openolt stream
229func SimulateOnuAlarm(ctx context.Context, req *bbsim.ONUAlarmRequest, o *devices.OltDevice) error {
230 alarmIndication, err := BuildOnuAlarmIndication(req, o)
Scott Baker41724b82020-01-21 19:54:53 -0800231 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 Katti86552f92020-03-03 21:56:32 +0530242
Anand S Katti86552f92020-03-03 21:56:32 +0530243// IsPonPortPresentInOlt verifies if given Pon port is present in olt
244func 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
255func 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
266func 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 Baid688b4242020-07-10 20:40:10 +0530283 Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{
Anand S Katti86552f92020-03-03 21:56:32 +0530284 Status: req.Status,
Pragya Arya996a0892020-03-09 21:47:52 +0530285 IntfId: devices.InterfaceIDToPortNo(req.InterfaceID, req.InterfaceType),
Anand S Katti86552f92020-03-03 21:56:32 +0530286 }},
287 }
288
289 err = o.SendAlarmIndication(ctx, alarmIndication)
290 if err != nil {
291 return err
292 }
293
294 return nil
295}