blob: f016919f3ff58174972d51426d19a4ee59ca7a07 [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"
21 "github.com/opencord/bbsim/api/bbsim"
22 "github.com/opencord/bbsim/internal/bbsim/devices"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070023 "strconv"
Matteo Scandolo4a036262020-08-17 15:56:13 -070024)
25
26func convertBBSimServiceToProtoService(s *devices.Service) *bbsim.Service {
27 return &bbsim.Service{
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070028 Name: s.Name,
29 InternalState: s.InternalState.Current(),
30 HwAddress: s.HwAddress.String(),
Matteo Scandolo8a574812021-05-20 15:18:53 -070031 OnuSn: s.UniPort.Onu.Sn(),
32 UniId: s.UniPort.ID,
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070033 CTag: int32(s.CTag),
34 STag: int32(s.STag),
35 NeedsEapol: s.NeedsEapol,
36 NeedsDhcp: s.NeedsDhcp,
37 NeedsIgmp: s.NeedsIgmp,
38 GemPort: int32(s.GemPort),
39 EapolState: s.EapolState.Current(),
40 DhcpState: s.DHCPState.Current(),
Matteo Scandolo618a6582020-09-09 12:21:29 -070041 IGMPState: s.IGMPState.Current(),
Matteo Scandolo4a036262020-08-17 15:56:13 -070042 }
43}
44
45func convertBBsimServicesToProtoServices(list []devices.ServiceIf) []*bbsim.Service {
46 services := []*bbsim.Service{}
47 for _, service := range list {
48 s := service.(*devices.Service)
49 services = append(services, convertBBSimServiceToProtoService(s))
50 }
51 return services
52}
53
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070054func (s BBSimServer) GetServices(ctx context.Context, req *bbsim.UNIRequest) (*bbsim.Services, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070055
56 services := bbsim.Services{
57 Items: []*bbsim.Service{},
58 }
59
60 olt := devices.GetOLT()
61
62 for _, pon := range olt.Pons {
63 for _, o := range pon.Onus {
Matteo Scandolo8a574812021-05-20 15:18:53 -070064 for _, u := range o.UniPorts {
65 uni := u.(*devices.UniPort)
66 s := convertBBsimServicesToProtoServices(uni.Services)
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070067 for _, service := range s {
68 intVar, err := strconv.Atoi(req.UniID)
69 if req.UniID == "" && req.OnuSerialNumber == "" {
70 services.Items = append(services.Items, s...)
71 } else if err == nil && service.UniId == uint32(intVar) && service.OnuSn == req.OnuSerialNumber {
72 services.Items = append(services.Items, s...)
73 } else if req.UniID == "" && service.OnuSn == req.OnuSerialNumber {
74 services.Items = append(services.Items, s...)
75 }
76 }
Matteo Scandolo8a574812021-05-20 15:18:53 -070077 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070078 }
79 }
80
81 return &services, nil
82}