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 | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame^] | 21 | "fmt" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 22 | |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/api/bbsim" |
| 24 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 25 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 26 | log "github.com/sirupsen/logrus" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame^] | 27 | "google.golang.org/grpc/codes" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var logger = log.WithFields(log.Fields{ |
| 31 | "module": "GrpcApiServer", |
| 32 | }) |
| 33 | |
| 34 | var ( |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 35 | version string |
| 36 | buildTime string |
| 37 | commitHash string |
| 38 | gitStatus string |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | type BBSimServer struct { |
| 42 | } |
| 43 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 44 | 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] | 45 | // 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] | 46 | return &bbsim.VersionNumber{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 47 | Version: version, |
| 48 | BuildTime: buildTime, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 49 | CommitHash: commitHash, |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 50 | GitStatus: gitStatus, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 51 | }, nil |
| 52 | } |
| 53 | |
| 54 | func (s BBSimServer) GetOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Olt, error) { |
| 55 | olt := devices.GetOLT() |
| 56 | nnis := []*bbsim.NNIPort{} |
| 57 | pons := []*bbsim.PONPort{} |
| 58 | |
| 59 | for _, nni := range olt.Nnis { |
| 60 | n := bbsim.NNIPort{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 61 | ID: int32(nni.ID), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 62 | OperState: nni.OperState.Current(), |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 63 | } |
| 64 | nnis = append(nnis, &n) |
| 65 | } |
| 66 | |
| 67 | for _, pon := range olt.Pons { |
| 68 | p := bbsim.PONPort{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 69 | ID: int32(pon.ID), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 70 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 71 | } |
| 72 | pons = append(pons, &p) |
| 73 | } |
| 74 | |
| 75 | res := bbsim.Olt{ |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 76 | ID: int32(olt.ID), |
| 77 | SerialNumber: olt.SerialNumber, |
| 78 | OperState: olt.OperState.Current(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 79 | InternalState: olt.InternalState.Current(), |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 80 | NNIPorts: nnis, |
| 81 | PONPorts: pons, |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 82 | } |
| 83 | return &res, nil |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame^] | 86 | func (s BBSimServer) PoweronOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) { |
| 87 | res := &bbsim.Response{} |
| 88 | o := devices.GetOLT() |
| 89 | |
| 90 | if err := o.InternalState.Event("initialize"); err != nil { |
| 91 | log.Errorf("Error initializing OLT: %v", err) |
| 92 | res.StatusCode = int32(codes.FailedPrecondition) |
| 93 | return res, err |
| 94 | } |
| 95 | |
| 96 | res.StatusCode = int32(codes.OK) |
| 97 | return res, nil |
| 98 | } |
| 99 | |
| 100 | func (s BBSimServer) ShutdownOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) { |
| 101 | res := &bbsim.Response{} |
| 102 | o := devices.GetOLT() |
| 103 | |
| 104 | if err := o.InternalState.Event("disable"); err != nil { |
| 105 | log.Errorf("Error disabling OLT: %v", err) |
| 106 | res.StatusCode = int32(codes.FailedPrecondition) |
| 107 | return res, err |
| 108 | } |
| 109 | |
| 110 | res.StatusCode = int32(codes.OK) |
| 111 | return res, nil |
| 112 | } |
| 113 | |
| 114 | func (s BBSimServer) RebootOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) { |
| 115 | res := &bbsim.Response{} |
| 116 | o := devices.GetOLT() |
| 117 | go o.RestartOLT() |
| 118 | res.StatusCode = int32(codes.OK) |
| 119 | res.Message = fmt.Sprintf("OLT restart triggered.") |
| 120 | return res, nil |
| 121 | } |
| 122 | |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 123 | func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) { |
| 124 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 125 | common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 126 | |
| 127 | return &bbsim.LogLevel{ |
| 128 | Level: log.StandardLogger().Level.String(), |
| 129 | Caller: log.StandardLogger().ReportCaller, |
| 130 | }, nil |
| 131 | } |