blob: d7c0b547b35356f9299099be9c2ee44e12b3baf5 [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"
28 "github.com/opencord/voltha-protos/v2/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{
Scott Baker41724b82020-01-21 19:54:53 -0800163 OnuId: onu.ID,
164 IntfId: onu.PonPortID,
165 RdiErrors: uint32(extractInt(req.Parameters, "RdiErrors", 0)),
166 }},
167 }
168 case bbsim.AlarmType_ONU_ALARM_LOS:
169 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530170 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800171 LosStatus: req.Status,
172 OnuId: onu.ID,
173 IntfId: onu.PonPortID,
174 }},
175 }
176 case bbsim.AlarmType_ONU_ALARM_LOB:
177 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530178 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800179 LobStatus: req.Status,
180 OnuId: onu.ID,
181 IntfId: onu.PonPortID,
182 }},
183 }
184 case bbsim.AlarmType_ONU_ALARM_LOPC_MISS:
185 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530186 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800187 LopcMissStatus: req.Status,
188 OnuId: onu.ID,
189 IntfId: onu.PonPortID,
190 }},
191 }
192 case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR:
193 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530194 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800195 LopcMicErrorStatus: req.Status,
196 OnuId: onu.ID,
197 IntfId: onu.PonPortID,
198 }},
199 }
200 case bbsim.AlarmType_ONU_ALARM_LOFI:
201 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530202 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800203 LofiStatus: req.Status,
204 OnuId: onu.ID,
205 IntfId: onu.PonPortID,
206 }},
207 }
208 case bbsim.AlarmType_ONU_ALARM_LOAMI:
209 alarm = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530210 Data: &openolt.AlarmIndication_OnuAlarmInd{OnuAlarmInd: &openolt.OnuAlarmIndication{
Scott Baker41724b82020-01-21 19:54:53 -0800211 LoamiStatus: req.Status,
212 OnuId: onu.ID,
213 IntfId: onu.PonPortID,
214 }},
215 }
216 default:
Kent Hagerman60d62302020-03-10 17:02:36 -0400217 return nil, fmt.Errorf("Unknown ONU alarm type %v", req.AlarmType)
Scott Baker41724b82020-01-21 19:54:53 -0800218 }
219
220 return alarm, nil
221}
222
Anand S Katti86552f92020-03-03 21:56:32 +0530223// SimulateOnuAlarm accept request for Onu alarms and send proper alarmIndication to openolt stream
224func SimulateOnuAlarm(ctx context.Context, req *bbsim.ONUAlarmRequest, o *devices.OltDevice) error {
225 alarmIndication, err := BuildOnuAlarmIndication(req, o)
Scott Baker41724b82020-01-21 19:54:53 -0800226 if err != nil {
227 return err
228 }
229
230 err = o.SendAlarmIndication(ctx, alarmIndication)
231 if err != nil {
232 return err
233 }
234
235 return nil
236}
Anand S Katti86552f92020-03-03 21:56:32 +0530237
Anand S Katti86552f92020-03-03 21:56:32 +0530238// IsPonPortPresentInOlt verifies if given Pon port is present in olt
239func IsPonPortPresentInOlt(PonPort uint32) bool {
240 o := devices.GetOLT()
241 for _, intf := range o.Pons {
242 if intf.ID == PonPort {
243 return true
244 }
245 }
246 return false
247}
248
249// IsNniPortPresentInOlt verifies if given nni port is present in olt
250func IsNniPortPresentInOlt(nniPort uint32) bool {
251 o := devices.GetOLT()
252 for _, intf := range o.Nnis {
253 if intf.ID == nniPort {
254 return true
255 }
256 }
257 return false
258}
259
260// SimulateOltAlarm accept request for Olt alarms and send proper alarmIndication to openolt stream
261func SimulateOltAlarm(ctx context.Context, req *bbsim.OLTAlarmRequest, o *devices.OltDevice) error {
262 var alarmIndication *openolt.AlarmIndication
263 var err error
264
265 //check if its a valid port id
266 switch req.InterfaceType {
267 case "nni":
268 if !IsNniPortPresentInOlt(uint32(req.InterfaceID)) {
269 return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" NNI not present in olt")
270 }
271
272 case "pon":
273 if !IsPonPortPresentInOlt(uint32(req.InterfaceID)) {
274 return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" PON not present in olt")
275 }
276 }
277 alarmIndication = &openolt.AlarmIndication{
Shrey Baid688b4242020-07-10 20:40:10 +0530278 Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{
Anand S Katti86552f92020-03-03 21:56:32 +0530279 Status: req.Status,
Pragya Arya996a0892020-03-09 21:47:52 +0530280 IntfId: devices.InterfaceIDToPortNo(req.InterfaceID, req.InterfaceType),
Anand S Katti86552f92020-03-03 21:56:32 +0530281 }},
282 }
283
284 err = o.SendAlarmIndication(ctx, alarmIndication)
285 if err != nil {
286 return err
287 }
288
289 return nil
290}