blob: 32b7794a862f17e96e6959b57510009f442ad88a [file] [log] [blame]
Matteo Scandolo4a036262020-08-17 15:56:13 -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"
Elia Battistonde6becb2022-01-28 16:45:31 +010021 "strconv"
22
Matteo Scandolo4a036262020-08-17 15:56:13 -070023 "github.com/opencord/bbsim/api/bbsim"
24 "github.com/opencord/bbsim/internal/bbsim/devices"
25)
26
27func convertBBSimServiceToProtoService(s *devices.Service) *bbsim.Service {
28 return &bbsim.Service{
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070029 Name: s.Name,
30 InternalState: s.InternalState.Current(),
31 HwAddress: s.HwAddress.String(),
Matteo Scandolo8a574812021-05-20 15:18:53 -070032 OnuSn: s.UniPort.Onu.Sn(),
33 UniId: s.UniPort.ID,
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070034 CTag: int32(s.CTag),
35 STag: int32(s.STag),
36 NeedsEapol: s.NeedsEapol,
37 NeedsDhcp: s.NeedsDhcp,
38 NeedsIgmp: s.NeedsIgmp,
39 GemPort: int32(s.GemPort),
40 EapolState: s.EapolState.Current(),
41 DhcpState: s.DHCPState.Current(),
Matteo Scandolo618a6582020-09-09 12:21:29 -070042 IGMPState: s.IGMPState.Current(),
Matteo Scandolo4a036262020-08-17 15:56:13 -070043 }
44}
45
46func convertBBsimServicesToProtoServices(list []devices.ServiceIf) []*bbsim.Service {
47 services := []*bbsim.Service{}
48 for _, service := range list {
49 s := service.(*devices.Service)
50 services = append(services, convertBBSimServiceToProtoService(s))
51 }
52 return services
53}
54
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070055func (s BBSimServer) GetServices(ctx context.Context, req *bbsim.UNIRequest) (*bbsim.Services, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070056
57 services := bbsim.Services{
58 Items: []*bbsim.Service{},
59 }
60
61 olt := devices.GetOLT()
62
63 for _, pon := range olt.Pons {
64 for _, o := range pon.Onus {
Matteo Scandolo8a574812021-05-20 15:18:53 -070065 for _, u := range o.UniPorts {
66 uni := u.(*devices.UniPort)
67 s := convertBBsimServicesToProtoServices(uni.Services)
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070068 for _, service := range s {
69 intVar, err := strconv.Atoi(req.UniID)
70 if req.UniID == "" && req.OnuSerialNumber == "" {
Elia Battistonde6becb2022-01-28 16:45:31 +010071 services.Items = append(services.Items, service)
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070072 } else if err == nil && service.UniId == uint32(intVar) && service.OnuSn == req.OnuSerialNumber {
Elia Battistonde6becb2022-01-28 16:45:31 +010073 services.Items = append(services.Items, service)
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070074 } else if req.UniID == "" && service.OnuSn == req.OnuSerialNumber {
Elia Battistonde6becb2022-01-28 16:45:31 +010075 services.Items = append(services.Items, service)
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070076 }
77 }
Matteo Scandolo8a574812021-05-20 15:18:53 -070078 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070079 }
80 }
81
82 return &services, nil
83}