blob: 3c81265a72a081d0eef4c4403519b96f525db9e1 [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"
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.
30func 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
42func 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 }
89 case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW:
90 alarm = &openolt.AlarmIndication{
91 Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{&openolt.OnuDriftOfWindowIndication{
92 Status: req.Status,
93 OnuId: onu.ID,
94 IntfId: onu.PonPortID,
95 Drift: uint32(extractInt(req.Parameters, "Drift", 0)),
96 NewEqd: uint32(extractInt(req.Parameters, "NewEqd", 0)),
97 }},
98 }
99 case bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL:
100 alarm = &openolt.AlarmIndication{
101 Data: &openolt.AlarmIndication_OnuLossOmciInd{&openolt.OnuLossOfOmciChannelIndication{
102 Status: req.Status,
103 OnuId: onu.ID,
104 IntfId: onu.PonPortID,
105 }},
106 }
107 case bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING:
108 alarm = &openolt.AlarmIndication{
109 Data: &openolt.AlarmIndication_OnuTiwiInd{&openolt.OnuTransmissionInterferenceWarning{
110 Status: req.Status,
111 OnuId: onu.ID,
112 IntfId: onu.PonPortID,
113 Drift: uint32(extractInt(req.Parameters, "Drift", 0)),
114 }},
115 }
116 case bbsim.AlarmType_ONU_ACTIVATION_FAILURE:
117 alarm = &openolt.AlarmIndication{
118 Data: &openolt.AlarmIndication_OnuActivationFailInd{&openolt.OnuActivationFailureIndication{
119 OnuId: onu.ID,
120 IntfId: onu.PonPortID,
121 FailReason: uint32(extractInt(req.Parameters, "FailReason", 0)),
122 }},
123 }
124 case bbsim.AlarmType_ONU_PROCESSING_ERROR:
125 alarm = &openolt.AlarmIndication{
126 Data: &openolt.AlarmIndication_OnuProcessingErrorInd{&openolt.OnuProcessingErrorIndication{
127 OnuId: onu.ID,
128 IntfId: onu.PonPortID,
129 }},
130 }
131 case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE:
132 alarm = &openolt.AlarmIndication{
133 Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{&openolt.OnuLossOfKeySyncFailureIndication{
134 OnuId: onu.ID,
135 IntfId: onu.PonPortID,
136 Status: req.Status,
137 }},
138 }
139 case bbsim.AlarmType_ONU_ITU_PON_STATS:
140 alarm = &openolt.AlarmIndication{
141 Data: &openolt.AlarmIndication_OnuItuPonStatsInd{&openolt.OnuItuPonStatsIndication{
142 OnuId: onu.ID,
143 IntfId: onu.PonPortID,
144 RdiErrors: uint32(extractInt(req.Parameters, "RdiErrors", 0)),
145 }},
146 }
147 case bbsim.AlarmType_ONU_ALARM_LOS:
148 alarm = &openolt.AlarmIndication{
149 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
150 LosStatus: req.Status,
151 OnuId: onu.ID,
152 IntfId: onu.PonPortID,
153 }},
154 }
155 case bbsim.AlarmType_ONU_ALARM_LOB:
156 alarm = &openolt.AlarmIndication{
157 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
158 LobStatus: req.Status,
159 OnuId: onu.ID,
160 IntfId: onu.PonPortID,
161 }},
162 }
163 case bbsim.AlarmType_ONU_ALARM_LOPC_MISS:
164 alarm = &openolt.AlarmIndication{
165 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
166 LopcMissStatus: req.Status,
167 OnuId: onu.ID,
168 IntfId: onu.PonPortID,
169 }},
170 }
171 case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR:
172 alarm = &openolt.AlarmIndication{
173 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
174 LopcMicErrorStatus: req.Status,
175 OnuId: onu.ID,
176 IntfId: onu.PonPortID,
177 }},
178 }
179 case bbsim.AlarmType_ONU_ALARM_LOFI:
180 alarm = &openolt.AlarmIndication{
181 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
182 LofiStatus: req.Status,
183 OnuId: onu.ID,
184 IntfId: onu.PonPortID,
185 }},
186 }
187 case bbsim.AlarmType_ONU_ALARM_LOAMI:
188 alarm = &openolt.AlarmIndication{
189 Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
190 LoamiStatus: req.Status,
191 OnuId: onu.ID,
192 IntfId: onu.PonPortID,
193 }},
194 }
195 default:
196 return nil, fmt.Errorf("Unknown alarm type %v", req.AlarmType)
197 }
198
199 return alarm, nil
200}
201
202func SimulateAlarm(ctx context.Context, req *bbsim.AlarmRequest, o *devices.OltDevice) error {
203 alarmIndication, err := BuildAlarmIndication(req, o)
204 if err != nil {
205 return err
206 }
207
208 err = o.SendAlarmIndication(ctx, alarmIndication)
209 if err != nil {
210 return err
211 }
212
213 return nil
214}