Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 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 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Elia Battiston | de6becb | 2022-01-28 16:45:31 +0100 | [diff] [blame] | 21 | "strconv" |
| 22 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/api/bbsim" |
| 24 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 25 | ) |
| 26 | |
| 27 | func convertBBSimServiceToProtoService(s *devices.Service) *bbsim.Service { |
| 28 | return &bbsim.Service{ |
Elia Battiston | 9a1da62 | 2022-02-09 14:43:52 +0100 | [diff] [blame] | 29 | Name: s.Name, |
| 30 | InternalState: s.InternalState.Current(), |
| 31 | HwAddress: s.HwAddress.String(), |
| 32 | OnuSn: s.UniPort.Onu.Sn(), |
| 33 | UniId: s.UniPort.ID, |
| 34 | UniTagMatch: int32(s.UniTagMatch), |
| 35 | CTag: int32(s.CTag), |
| 36 | UsCTagPriority: uint32(s.UsPonCTagPriority), |
| 37 | DsCTagPriority: uint32(s.DsPonCTagPriority), |
| 38 | STag: int32(s.STag), |
| 39 | UsSTagPriority: uint32(s.UsPonSTagPriority), |
| 40 | DsSTagPriority: uint32(s.DsPonSTagPriority), |
| 41 | NeedsEapol: s.NeedsEapol, |
| 42 | NeedsDhcp: s.NeedsDhcp, |
| 43 | NeedsIgmp: s.NeedsIgmp, |
| 44 | NeedsPPPoE: s.NeedsPPPoE, |
| 45 | ConfigureMacAddress: s.ConfigureMacAddress, |
| 46 | EnableMacLearning: s.EnableMacLearning, |
| 47 | GemPort: int32(s.GemPort), |
| 48 | EapolState: s.EapolState.Current(), |
| 49 | DhcpState: s.DHCPState.Current(), |
| 50 | IGMPState: s.IGMPState.Current(), |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | func convertBBsimServicesToProtoServices(list []devices.ServiceIf) []*bbsim.Service { |
| 55 | services := []*bbsim.Service{} |
| 56 | for _, service := range list { |
| 57 | s := service.(*devices.Service) |
| 58 | services = append(services, convertBBSimServiceToProtoService(s)) |
| 59 | } |
| 60 | return services |
| 61 | } |
| 62 | |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 63 | func (s BBSimServer) GetServices(ctx context.Context, req *bbsim.UNIRequest) (*bbsim.Services, error) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 64 | |
| 65 | services := bbsim.Services{ |
| 66 | Items: []*bbsim.Service{}, |
| 67 | } |
| 68 | |
| 69 | olt := devices.GetOLT() |
| 70 | |
| 71 | for _, pon := range olt.Pons { |
| 72 | for _, o := range pon.Onus { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 73 | for _, u := range o.UniPorts { |
| 74 | uni := u.(*devices.UniPort) |
| 75 | s := convertBBsimServicesToProtoServices(uni.Services) |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 76 | for _, service := range s { |
| 77 | intVar, err := strconv.Atoi(req.UniID) |
| 78 | if req.UniID == "" && req.OnuSerialNumber == "" { |
Elia Battiston | de6becb | 2022-01-28 16:45:31 +0100 | [diff] [blame] | 79 | services.Items = append(services.Items, service) |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 80 | } else if err == nil && service.UniId == uint32(intVar) && service.OnuSn == req.OnuSerialNumber { |
Elia Battiston | de6becb | 2022-01-28 16:45:31 +0100 | [diff] [blame] | 81 | services.Items = append(services.Items, service) |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 82 | } else if req.UniID == "" && service.OnuSn == req.OnuSerialNumber { |
Elia Battiston | de6becb | 2022-01-28 16:45:31 +0100 | [diff] [blame] | 83 | services.Items = append(services.Items, service) |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 86 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | return &services, nil |
| 91 | } |