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