Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "github.com/opencord/bbsim/api/bbsim" |
| 22 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame^] | 23 | "strconv" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func convertBBSimServiceToProtoService(s *devices.Service) *bbsim.Service { |
| 27 | return &bbsim.Service{ |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 28 | Name: s.Name, |
| 29 | InternalState: s.InternalState.Current(), |
| 30 | HwAddress: s.HwAddress.String(), |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 31 | OnuSn: s.UniPort.Onu.Sn(), |
| 32 | UniId: s.UniPort.ID, |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 33 | 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 Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 41 | IGMPState: s.IGMPState.Current(), |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | func 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 Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame^] | 54 | 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] | 55 | |
| 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 Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 64 | for _, u := range o.UniPorts { |
| 65 | uni := u.(*devices.UniPort) |
| 66 | s := convertBBsimServicesToProtoServices(uni.Services) |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame^] | 67 | 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 Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 77 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | return &services, nil |
| 82 | } |