Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -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 | |
Matteo Scandolo | 82c16d0 | 2019-09-24 09:34:32 -0700 | [diff] [blame^] | 17 | package api |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 21 | "github.com/opencord/bbsim/api/bbsim" |
| 22 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 23 | log "github.com/sirupsen/logrus" |
| 24 | ) |
| 25 | |
| 26 | var logger = log.WithFields(log.Fields{ |
| 27 | "module": "GrpcApiServer", |
| 28 | }) |
| 29 | |
| 30 | var ( |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 31 | version string |
| 32 | buildTime string |
| 33 | commitHash string |
| 34 | gitStatus string |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type BBSimServer struct { |
| 38 | } |
| 39 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 40 | func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) { |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 41 | // TODO add a flag to specofy whether the tree was clean at this commit or not |
| 42 | return &bbsim.VersionNumber{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 43 | Version: version, |
| 44 | BuildTime: buildTime, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 45 | CommitHash: commitHash, |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 46 | GitStatus: gitStatus, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 47 | }, nil |
| 48 | } |
| 49 | |
| 50 | func (s BBSimServer) GetOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Olt, error) { |
| 51 | olt := devices.GetOLT() |
| 52 | nnis := []*bbsim.NNIPort{} |
| 53 | pons := []*bbsim.PONPort{} |
| 54 | |
| 55 | for _, nni := range olt.Nnis { |
| 56 | n := bbsim.NNIPort{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 57 | ID: int32(nni.ID), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 58 | OperState: nni.OperState.Current(), |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 59 | } |
| 60 | nnis = append(nnis, &n) |
| 61 | } |
| 62 | |
| 63 | for _, pon := range olt.Pons { |
| 64 | p := bbsim.PONPort{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 65 | ID: int32(pon.ID), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 66 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 67 | } |
| 68 | pons = append(pons, &p) |
| 69 | } |
| 70 | |
| 71 | res := bbsim.Olt{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 72 | ID: int32(olt.ID), |
| 73 | SerialNumber: olt.SerialNumber, |
| 74 | OperState: olt.OperState.Current(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 75 | InternalState: olt.InternalState.Current(), |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 76 | NNIPorts: nnis, |
| 77 | PONPorts: pons, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 78 | } |
| 79 | return &res, nil |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 82 | func (s BBSimServer) GetONUs(ctx context.Context, req *bbsim.Empty) (*bbsim.ONUs, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 83 | olt := devices.GetOLT() |
| 84 | onus := bbsim.ONUs{ |
| 85 | Items: []*bbsim.ONU{}, |
| 86 | } |
| 87 | |
| 88 | for _, pon := range olt.Pons { |
| 89 | for _, o := range pon.Onus { |
| 90 | onu := bbsim.ONU{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 91 | ID: int32(o.ID), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 92 | SerialNumber: o.Sn(), |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 93 | OperState: o.OperState.Current(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 94 | InternalState: o.InternalState.Current(), |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 95 | PonPortID: int32(o.PonPortID), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 96 | STag: int32(o.STag), |
| 97 | CTag: int32(o.CTag), |
| 98 | HwAddress: o.HwAddress.String(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 99 | } |
| 100 | onus.Items = append(onus.Items, &onu) |
| 101 | } |
| 102 | } |
| 103 | return &onus, nil |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 104 | } |