blob: 4ea60e76bb8715bdc713f92c74f26b9b387d3d53 [file] [log] [blame]
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001/*
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 api
18
19import (
20 "context"
21 "fmt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080022 "github.com/opencord/bbsim/internal/bbsim/types"
Andrea Campanellabe8e12f2020-12-14 18:43:41 +010023 "github.com/opencord/voltha-protos/v4/go/openolt"
Pragya Arya1d5ffb82020-03-20 18:51:37 +053024
Matteo Scandolo10f965c2019-09-24 10:40:46 -070025 "github.com/opencord/bbsim/api/bbsim"
26 "github.com/opencord/bbsim/internal/bbsim/devices"
27 log "github.com/sirupsen/logrus"
28 "google.golang.org/grpc/codes"
29)
30
31func (s BBSimServer) GetONUs(ctx context.Context, req *bbsim.Empty) (*bbsim.ONUs, error) {
32 olt := devices.GetOLT()
33 onus := bbsim.ONUs{
34 Items: []*bbsim.ONU{},
35 }
36
37 for _, pon := range olt.Pons {
38 for _, o := range pon.Onus {
39 onu := bbsim.ONU{
Matteo Scandolocedde462021-03-09 17:37:16 -080040 ID: int32(o.ID),
41 SerialNumber: o.Sn(),
42 OperState: o.OperState.Current(),
43 InternalState: o.InternalState.Current(),
44 PonPortID: int32(o.PonPortID),
Matteo Scandolocedde462021-03-09 17:37:16 -080045 ImageSoftwareReceivedSections: int32(o.ImageSoftwareReceivedSections),
46 ImageSoftwareExpectedSections: int32(o.ImageSoftwareExpectedSections),
47 ActiveImageEntityId: int32(o.ActiveImageEntityId),
48 CommittedImageEntityId: int32(o.CommittedImageEntityId),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070049 Unis: convertBBsimUniPortsToProtoUniPorts(o.UniPorts),
Matteo Scandolo10f965c2019-09-24 10:40:46 -070050 }
51 onus.Items = append(onus.Items, &onu)
52 }
53 }
54 return &onus, nil
55}
56
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070057func (s BBSimServer) GetONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONU, error) {
58 olt := devices.GetOLT()
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070059 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070060
61 if err != nil {
62 res := bbsim.ONU{}
63 return &res, err
64 }
65
66 res := bbsim.ONU{
67 ID: int32(onu.ID),
68 SerialNumber: onu.Sn(),
69 OperState: onu.OperState.Current(),
70 InternalState: onu.InternalState.Current(),
71 PonPortID: int32(onu.PonPortID),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070072 Unis: convertBBsimUniPortsToProtoUniPorts(onu.UniPorts),
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070073 }
74 return &res, nil
75}
76
Pragya Aryabd731ec2020-02-11 16:38:17 +053077// ShutdownONU sends DyingGasp indication for specified ONUs and mark ONUs as disabled.
Matteo Scandolo10f965c2019-09-24 10:40:46 -070078func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
Anand S Katti09541352020-01-29 15:54:01 +053079 // NOTE this method is now sending a Dying Gasp and then disabling the device (operState: down, adminState: up),
Matteo Scandolo10f965c2019-09-24 10:40:46 -070080 // is this the only way to do? Should we address other cases?
81 // Investigate what happens when:
82 // - a fiber is pulled
83 // - ONU malfunction
84 // - ONU shutdown
Matteo Scandolo10f965c2019-09-24 10:40:46 -070085 logger.WithFields(log.Fields{
86 "OnuSn": req.SerialNumber,
87 }).Infof("Received request to shutdown ONU")
88
Pragya Aryabd731ec2020-02-11 16:38:17 +053089 res := &bbsim.Response{}
Matteo Scandolo10f965c2019-09-24 10:40:46 -070090 olt := devices.GetOLT()
91
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070092 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070093 if err != nil {
94 res.StatusCode = int32(codes.NotFound)
95 res.Message = err.Error()
96 return res, err
97 }
98
Pragya Aryabd731ec2020-02-11 16:38:17 +053099 return handleShutdownONU(onu)
100}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700101
Pragya Aryabd731ec2020-02-11 16:38:17 +0530102// ShutdownONUsOnPON sends DyingGasp indication for all ONUs under specified PON port
103func (s BBSimServer) ShutdownONUsOnPON(ctx context.Context, req *bbsim.PONRequest) (*bbsim.Response, error) {
104 logger.WithFields(log.Fields{
105 "IntfId": req.PonPortId,
106 }).Infof("Received request to shutdown all ONUs on PON")
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800107
Pragya Aryabd731ec2020-02-11 16:38:17 +0530108 res := &bbsim.Response{}
109 olt := devices.GetOLT()
110 pon, _ := olt.GetPonById(req.PonPortId)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800111
Pragya Aryabd731ec2020-02-11 16:38:17 +0530112 go func() {
113 for _, onu := range pon.Onus {
114 res, _ = handleShutdownONU(onu)
115 }
116 }()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700117 res.StatusCode = int32(codes.OK)
Pragya Aryabd731ec2020-02-11 16:38:17 +0530118 res.Message = fmt.Sprintf("Request accepted for shutdown all ONUs on PON port %d", pon.ID)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700119
120 return res, nil
121}
122
Pragya Aryabd731ec2020-02-11 16:38:17 +0530123// ShutdownAllONUs sends DyingGasp indication for all ONUs and mark ONUs as disabled.
124func (s BBSimServer) ShutdownAllONUs(context.Context, *bbsim.Empty) (*bbsim.Response, error) {
125 logger.Infof("Received request to shutdown all ONUs")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126 res := &bbsim.Response{}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530127 olt := devices.GetOLT()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700128
Pragya Aryabd731ec2020-02-11 16:38:17 +0530129 go func() {
130 for _, pon := range olt.Pons {
131 for _, onu := range pon.Onus {
132 res, _ = handleShutdownONU(onu)
133 }
134 }
135 }()
136 res.StatusCode = int32(codes.OK)
137 res.Message = fmt.Sprintf("Request Accepted for shutdown all ONUs in OLT %d", olt.ID)
138
139 return res, nil
140}
141
142// PoweronONU simulates ONU power on and start sending discovery indications to VOLTHA
143func (s BBSimServer) PoweronONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700144 logger.WithFields(log.Fields{
145 "OnuSn": req.SerialNumber,
146 }).Infof("Received request to poweron ONU")
147
Pragya Aryabd731ec2020-02-11 16:38:17 +0530148 res := &bbsim.Response{}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700149 olt := devices.GetOLT()
150
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700151 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700152 if err != nil {
153 res.StatusCode = int32(codes.NotFound)
154 res.Message = err.Error()
155 return res, err
156 }
157
Pragya Arya2225f202020-01-29 18:05:01 +0530158 pon, _ := olt.GetPonById(onu.PonPortID)
159 if pon.InternalState.Current() != "enabled" {
160 err := fmt.Errorf("PON port %d not enabled", onu.PonPortID)
161 logger.WithFields(log.Fields{
162 "OnuId": onu.ID,
163 "IntfId": onu.PonPortID,
164 "OnuSn": onu.Sn(),
165 }).Errorf("Cannot poweron ONU: %s", err.Error())
166
167 res.StatusCode = int32(codes.FailedPrecondition)
168 res.Message = err.Error()
169 return res, err
170 }
171
Pragya Aryabd731ec2020-02-11 16:38:17 +0530172 return handlePoweronONU(onu)
173}
174
175// PoweronONUsOnPON simulates ONU power on for all ONUs under specified PON port
176func (s BBSimServer) PoweronONUsOnPON(ctx context.Context, req *bbsim.PONRequest) (*bbsim.Response, error) {
177 logger.WithFields(log.Fields{
178 "IntfId": req.PonPortId,
179 }).Infof("Received request to poweron all ONUs on PON")
180
181 res := &bbsim.Response{}
182 olt := devices.GetOLT()
183
184 pon, _ := olt.GetPonById(req.PonPortId)
185 if pon.InternalState.Current() != "enabled" {
186 err := fmt.Errorf("PON port %d not enabled", pon.ID)
187 logger.WithFields(log.Fields{
188 "IntfId": pon.ID,
189 }).Errorf("Cannot poweron ONUs on PON: %s", err.Error())
190
191 res.StatusCode = int32(codes.FailedPrecondition)
192 res.Message = err.Error()
193 return res, err
194 }
195
196 go func() {
197 for _, onu := range pon.Onus {
198 res, _ = handlePoweronONU(onu)
Pragya Arya2225f202020-01-29 18:05:01 +0530199 }
Pragya Aryabd731ec2020-02-11 16:38:17 +0530200 }()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700201 res.StatusCode = int32(codes.OK)
Pragya Aryabd731ec2020-02-11 16:38:17 +0530202 res.Message = fmt.Sprintf("Request Accepted for power on all ONUs on PON port %d", pon.ID)
203
204 return res, nil
205}
206
207// PoweronAllONUs simulates ONU power on for all ONUs on all PON ports
208func (s BBSimServer) PoweronAllONUs(context.Context, *bbsim.Empty) (*bbsim.Response, error) {
209 logger.Infof("Received request to poweron all ONUs")
210
211 res := &bbsim.Response{}
212 olt := devices.GetOLT()
213
214 go func() {
215 for _, pon := range olt.Pons {
216 if pon.InternalState.Current() == "enabled" {
217 for _, onu := range pon.Onus {
218 res, _ = handlePoweronONU(onu)
219 }
220 }
221 }
222 }()
223 res.StatusCode = int32(codes.OK)
224 res.Message = fmt.Sprintf("Request Accepted for power on all ONUs in OLT %d", olt.ID)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700225
226 return res, nil
227}
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700228
Arjun E K57a7fcb2020-01-30 06:44:45 +0000229func (s BBSimServer) ChangeIgmpState(ctx context.Context, req *bbsim.IgmpRequest) (*bbsim.Response, error) {
Matteo Scandolo618a6582020-09-09 12:21:29 -0700230
Matteo Scandolo8a574812021-05-20 15:18:53 -0700231 // NOTE this API will change the IGMP state for all UNIs on the requested ONU
232 // TODO a new API needs to be created to individually manage the UNIs
Matteo Scandolo618a6582020-09-09 12:21:29 -0700233
Arjun E K57a7fcb2020-01-30 06:44:45 +0000234 res := &bbsim.Response{}
235
236 logger.WithFields(log.Fields{
Onur Kalinagac9f9faca2021-01-21 14:04:34 +0000237 "OnuSn": req.OnuReq.SerialNumber,
238 "subAction": req.SubActionVal,
239 "GroupAddress": req.GroupAddress,
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800240 }).Info("Received igmp request for ONU")
Arjun E K57a7fcb2020-01-30 06:44:45 +0000241
242 olt := devices.GetOLT()
243 onu, err := olt.FindOnuBySn(req.OnuReq.SerialNumber)
244
245 if err != nil {
246 res.StatusCode = int32(codes.NotFound)
247 res.Message = err.Error()
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800248 logger.WithFields(log.Fields{
249 "OnuSn": req.OnuReq.SerialNumber,
250 "subAction": req.SubActionVal,
251 "GroupAddress": req.GroupAddress,
252 }).Warn("ONU not found for sending igmp packet.")
Arjun E K57a7fcb2020-01-30 06:44:45 +0000253 return res, err
254 } else {
255 event := ""
256 switch req.SubActionVal {
257 case bbsim.SubActionTypes_JOIN:
258 event = "igmp_join_start"
259 case bbsim.SubActionTypes_LEAVE:
260 event = "igmp_leave"
Anand S Katti09541352020-01-29 15:54:01 +0530261 case bbsim.SubActionTypes_JOINV3:
262 event = "igmp_join_startv3"
Arjun E K57a7fcb2020-01-30 06:44:45 +0000263 }
264
Matteo Scandolo618a6582020-09-09 12:21:29 -0700265 errors := []string{}
266 startedOn := []string{}
267 success := true
268
Matteo Scandolo8a574812021-05-20 15:18:53 -0700269 for _, u := range onu.UniPorts {
270 uni := u.(*devices.UniPort)
271 if !uni.OperState.Is(devices.UniStateUp) {
272 // if the UNI is disabled, ignore it
273 continue
274 }
275 for _, s := range uni.Services {
276 service := s.(*devices.Service)
277 serviceKey := fmt.Sprintf("uni[%d]%s", uni.ID, service.Name)
278 if service.NeedsIgmp {
279 if !service.InternalState.Is(devices.ServiceStateInitialized) {
280 logger.WithFields(log.Fields{
281 "OnuId": onu.ID,
282 "UniId": uni.ID,
283 "IntfId": onu.PonPortID,
284 "OnuSn": onu.Sn(),
285 "Service": service.Name,
286 }).Warn("service-not-initialized-skipping-event")
287 continue
288 }
Matteo Scandolo618a6582020-09-09 12:21:29 -0700289 logger.WithFields(log.Fields{
290 "OnuId": onu.ID,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700291 "UniId": uni.ID,
Matteo Scandolo618a6582020-09-09 12:21:29 -0700292 "IntfId": onu.PonPortID,
293 "OnuSn": onu.Sn(),
294 "Service": service.Name,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700295 "Uni": uni.ID,
296 }).Debugf("Sending %s event on Service %s", event, service.Name)
297
298 if err := service.IGMPState.Event(event, types.IgmpMessage{GroupAddress: req.GroupAddress}); err != nil {
299 logger.WithFields(log.Fields{
300 "OnuId": onu.ID,
301 "UniId": uni.ID,
302 "IntfId": onu.PonPortID,
303 "OnuSn": onu.Sn(),
304 "Service": service.Name,
305 }).Errorf("IGMP request failed: %s", err.Error())
306 errors = append(errors, fmt.Sprintf("%s: %s", serviceKey, err.Error()))
307 success = false
308 }
309 startedOn = append(startedOn, serviceKey)
Matteo Scandolo618a6582020-09-09 12:21:29 -0700310 }
Matteo Scandolo618a6582020-09-09 12:21:29 -0700311 }
312 }
313
314 if success {
315 res.StatusCode = int32(codes.OK)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800316 if len(startedOn) > 0 {
317 res.Message = fmt.Sprintf("IGMP %s sent on Services %s for ONU %s.",
318 event, fmt.Sprintf("%v", startedOn), onu.Sn())
319 } else {
320 res.Message = "No service requires IGMP"
321 }
322 logger.WithFields(log.Fields{
323 "OnuSn": req.OnuReq.SerialNumber,
324 "subAction": req.SubActionVal,
325 "GroupAddress": req.GroupAddress,
326 "Message": res.Message,
327 }).Info("Processed IGMP request for ONU")
Matteo Scandolo618a6582020-09-09 12:21:29 -0700328 } else {
Arjun E K57a7fcb2020-01-30 06:44:45 +0000329 res.StatusCode = int32(codes.FailedPrecondition)
Matteo Scandolo618a6582020-09-09 12:21:29 -0700330 res.Message = fmt.Sprintf("%v", errors)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800331 logger.WithFields(log.Fields{
332 "OnuSn": req.OnuReq.SerialNumber,
333 "subAction": req.SubActionVal,
334 "GroupAddress": req.GroupAddress,
335 "Message": res.Message,
336 }).Error("Error while processing IGMP request for ONU")
Arjun E K57a7fcb2020-01-30 06:44:45 +0000337 }
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800338
Arjun E K57a7fcb2020-01-30 06:44:45 +0000339 }
340
341 return res, nil
342}
343
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700344func (s BBSimServer) RestartEapol(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700345 // NOTE this API will change the EAPOL state for all UNIs on the requested ONU
346 // TODO a new API needs to be created to individually manage the UNIs
347
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700348 res := &bbsim.Response{}
349
350 logger.WithFields(log.Fields{
351 "OnuSn": req.SerialNumber,
352 }).Infof("Received request to restart authentication ONU")
353
354 olt := devices.GetOLT()
355
356 onu, err := olt.FindOnuBySn(req.SerialNumber)
357
358 if err != nil {
359 res.StatusCode = int32(codes.NotFound)
360 res.Message = err.Error()
361 return res, err
362 }
363
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700364 errors := []string{}
Matteo Scandolo618a6582020-09-09 12:21:29 -0700365 startedOn := []string{}
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700366 success := true
367
Matteo Scandolo8a574812021-05-20 15:18:53 -0700368 for _, u := range onu.UniPorts {
369 uni := u.(*devices.UniPort)
370 if !uni.OperState.Is(devices.UniStateUp) {
371 // if the UNI is disabled, ignore it
372 continue
373 }
374 for _, s := range uni.Services {
375 service := s.(*devices.Service)
376 serviceKey := fmt.Sprintf("uni[%d]%s", uni.ID, service.Name)
377 if service.NeedsEapol {
378 if !service.InternalState.Is(devices.ServiceStateInitialized) {
379 logger.WithFields(log.Fields{
380 "OnuId": onu.ID,
381 "UniId": uni.ID,
382 "IntfId": onu.PonPortID,
383 "OnuSn": onu.Sn(),
384 "Service": service.Name,
385 }).Warn("service-not-initialized-skipping-event")
386 continue
387 }
388 if err := service.EapolState.Event("start_auth"); err != nil {
389 logger.WithFields(log.Fields{
390 "OnuId": onu.ID,
391 "IntfId": onu.PonPortID,
392 "OnuSn": onu.Sn(),
393 "UniId": uni.ID,
394 "Service": service.Name,
395 }).Errorf("Cannot restart authenticaton for Service: %s", err.Error())
396 errors = append(errors, fmt.Sprintf("%s: %s", serviceKey, err.Error()))
397 success = false
398 }
399 startedOn = append(startedOn, serviceKey)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700400 }
401 }
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700402 }
403
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700404 if success {
405 res.StatusCode = int32(codes.OK)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800406 if len(startedOn) > 0 {
407 res.Message = fmt.Sprintf("Authentication restarted on Services %s for ONU %s.",
408 fmt.Sprintf("%v", startedOn), onu.Sn())
409 } else {
410 res.Message = "No service requires EAPOL"
411 }
412
413 logger.WithFields(log.Fields{
414 "OnuSn": req.SerialNumber,
415 "Message": res.Message,
416 }).Info("Processed EAPOL restart request for ONU")
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700417 } else {
418 res.StatusCode = int32(codes.FailedPrecondition)
419 res.Message = fmt.Sprintf("%v", errors)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800420 logger.WithFields(log.Fields{
421 "OnuSn": req.SerialNumber,
422 "Message": res.Message,
423 }).Error("Error while processing DHCP restart request for ONU")
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700424 }
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700425
426 return res, nil
427}
428
429func (s BBSimServer) RestartDhcp(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700430 // NOTE this API will change the DHCP state for all UNIs on the requested ONU
431 // TODO a new API needs to be created to individually manage the UNIs
432
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700433 res := &bbsim.Response{}
434
435 logger.WithFields(log.Fields{
436 "OnuSn": req.SerialNumber,
437 }).Infof("Received request to restart DHCP on ONU")
438
439 olt := devices.GetOLT()
440
441 onu, err := olt.FindOnuBySn(req.SerialNumber)
442
443 if err != nil {
444 res.StatusCode = int32(codes.NotFound)
445 res.Message = err.Error()
446 return res, err
447 }
448
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700449 errors := []string{}
Matteo Scandolo618a6582020-09-09 12:21:29 -0700450 startedOn := []string{}
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700451 success := true
452
Matteo Scandolo8a574812021-05-20 15:18:53 -0700453 for _, u := range onu.UniPorts {
454 uni := u.(*devices.UniPort)
455 if !uni.OperState.Is(devices.UniStateUp) {
456 // if the UNI is disabled, ignore it
457 continue
458 }
459 for _, s := range uni.Services {
460 service := s.(*devices.Service)
461 serviceKey := fmt.Sprintf("uni[%d]%s", uni.ID, service.Name)
462 if service.NeedsDhcp {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700463
Matteo Scandolo8a574812021-05-20 15:18:53 -0700464 if err := service.DHCPState.Event("start_dhcp"); err != nil {
465 logger.WithFields(log.Fields{
466 "OnuId": onu.ID,
467 "IntfId": onu.PonPortID,
468 "OnuSn": onu.Sn(),
469 "UniId": uni.ID,
470 "Service": service.Name,
471 }).Errorf("Cannot restart DHCP for Service: %s", err.Error())
472 errors = append(errors, fmt.Sprintf("%s: %s", serviceKey, err.Error()))
473 success = false
474 }
475 startedOn = append(startedOn, serviceKey)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700476 }
477 }
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700478 }
479
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700480 if success {
481 res.StatusCode = int32(codes.OK)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800482 if len(startedOn) > 0 {
483 res.Message = fmt.Sprintf("DHCP restarted on Services %s for ONU %s.",
484 fmt.Sprintf("%v", startedOn), onu.Sn())
485
486 } else {
487 res.Message = "No service requires DHCP"
488 }
489 logger.WithFields(log.Fields{
490 "OnuSn": req.SerialNumber,
491 "Message": res.Message,
492 }).Info("Processed DHCP restart request for ONU")
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700493 } else {
494 res.StatusCode = int32(codes.FailedPrecondition)
495 res.Message = fmt.Sprintf("%v", errors)
Matteo Scandoloc08a6082021-02-05 11:12:01 -0800496 logger.WithFields(log.Fields{
497 "OnuSn": req.SerialNumber,
498 "Message": res.Message,
499 }).Error("Error while processing DHCP restart request for ONU")
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700500 }
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700501
502 return res, nil
503}
Anand S Katti09541352020-01-29 15:54:01 +0530504
Pragya Arya8bdb4532020-03-02 17:08:09 +0530505// GetFlows for OLT/ONUs
506func (s BBSimServer) GetFlows(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Flows, error) {
507 logger.WithFields(log.Fields{
508 "OnuSn": req.SerialNumber,
509 }).Info("Received GetFlows request")
510
511 olt := devices.GetOLT()
512 res := &bbsim.Flows{}
513
514 if req.SerialNumber == "" {
Andrea Campanellabe8e12f2020-12-14 18:43:41 +0100515 olt.Flows.Range(func(flowKey, flow interface{}) bool {
516 flowObj := flow.(openolt.Flow)
517 res.Flows = append(res.Flows, &flowObj)
518 return true
519 })
520 res.FlowCount = uint32(len(res.Flows))
Pragya Arya8bdb4532020-03-02 17:08:09 +0530521 } else {
522 onu, err := olt.FindOnuBySn(req.SerialNumber)
523 if err != nil {
524 logger.WithFields(log.Fields{
525 "OnuSn": req.SerialNumber,
526 }).Error("Can't get ONU in GetFlows request")
527 return nil, err
528 }
529 for _, flowKey := range onu.Flows {
Andrea Campanellabe8e12f2020-12-14 18:43:41 +0100530 flow, _ := olt.Flows.Load(flowKey)
531 flowObj := flow.(openolt.Flow)
532 res.Flows = append(res.Flows, &flowObj)
Pragya Arya8bdb4532020-03-02 17:08:09 +0530533 }
534 res.FlowCount = uint32(len(onu.Flows))
535 }
536 return res, nil
537}
538
Anand S Katti09541352020-01-29 15:54:01 +0530539func (s BBSimServer) GetOnuTrafficSchedulers(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONUTrafficSchedulers, error) {
540 olt := devices.GetOLT()
541 ts := bbsim.ONUTrafficSchedulers{}
542
543 onu, err := olt.FindOnuBySn(req.SerialNumber)
544 if err != nil {
545 return &ts, err
546 }
547
548 if onu.TrafficSchedulers != nil {
549 ts.TraffSchedulers = onu.TrafficSchedulers
550 return &ts, nil
551 } else {
552 ts.TraffSchedulers = nil
553 return &ts, nil
554 }
555}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530556
557func handlePoweronONU(onu *devices.Onu) (*bbsim.Response, error) {
558 res := &bbsim.Response{}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530559
Matteo Scandolof9d43412021-01-12 11:11:34 -0800560 if err := onu.HandlePowerOnONU(); err != nil {
Pragya Aryabd731ec2020-02-11 16:38:17 +0530561 res.StatusCode = int32(codes.FailedPrecondition)
562 res.Message = err.Error()
563 return res, err
564 }
565
Pragya Aryabd731ec2020-02-11 16:38:17 +0530566 res.StatusCode = int32(codes.OK)
567 res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn())
568
569 return res, nil
570}
571
572func handleShutdownONU(onu *devices.Onu) (*bbsim.Response, error) {
573 res := &bbsim.Response{}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530574
Matteo Scandolof9d43412021-01-12 11:11:34 -0800575 if err := onu.HandleShutdownONU(); err != nil {
Pragya Aryabd731ec2020-02-11 16:38:17 +0530576 res.StatusCode = int32(codes.FailedPrecondition)
577 res.Message = err.Error()
578 return res, err
579 }
580
581 res.StatusCode = int32(codes.OK)
582 res.Message = fmt.Sprintf("ONU %s successfully shut down.", onu.Sn())
583
584 return res, nil
585}
Nitin Subramanianb0a333a2021-07-08 15:01:41 -0700586
587func (s BBSimServer) GetUnis(ctx context.Context, req *bbsim.Empty) (*bbsim.UNIs, error) {
588 onus, err := s.GetONUs(ctx, req)
589
590 if err != nil {
591 return nil, err
592 }
593 unis := []*bbsim.UNI{}
594 for _, onu := range onus.Items {
595 unis = append(unis, onu.Unis...)
596 }
597 unis_ret := bbsim.UNIs{
598 Items: unis,
599 }
600 return &unis_ret, nil
601}