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