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