blob: 84d3478e0cdd2e1b0684c65ae4ed7e5dd948ad21 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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 Scandolo82c16d02019-09-24 09:34:32 -070017package api
Matteo Scandolo84f7d482019-08-08 19:00:47 -070018
19import (
20 "context"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010021 "fmt"
rajeshf921f882020-03-06 18:24:28 +053022 "strings"
Matteo Scandolo88c204a2020-11-03 10:34:24 -080023 "time"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020024
Matteo Scandolo11006992019-08-28 11:29:46 -070025 "github.com/opencord/bbsim/api/bbsim"
Scott Baker41724b82020-01-21 19:54:53 -080026 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Matteo Scandolo11006992019-08-28 11:29:46 -070027 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070028 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070029 log "github.com/sirupsen/logrus"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010030 "google.golang.org/grpc/codes"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070031)
32
33var logger = log.WithFields(log.Fields{
34 "module": "GrpcApiServer",
35})
36
37var (
Matteo Scandolo8df63df2019-09-12 10:34:32 -070038 version string
39 buildTime string
40 commitHash string
41 gitStatus string
Matteo Scandolo84f7d482019-08-08 19:00:47 -070042)
43
44type BBSimServer struct {
45}
46
Matteo Scandolo8df63df2019-09-12 10:34:32 -070047func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020048 // TODO add a flag to specify whether the tree was clean at this commit or not
Matteo Scandolo84f7d482019-08-08 19:00:47 -070049 return &bbsim.VersionNumber{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070050 Version: version,
51 BuildTime: buildTime,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070052 CommitHash: commitHash,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070053 GitStatus: gitStatus,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070054 }, nil
55}
56
57func (s BBSimServer) GetOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Olt, error) {
58 olt := devices.GetOLT()
59 nnis := []*bbsim.NNIPort{}
60 pons := []*bbsim.PONPort{}
61
62 for _, nni := range olt.Nnis {
63 n := bbsim.NNIPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070064 ID: int32(nni.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070065 OperState: nni.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070066 }
67 nnis = append(nnis, &n)
68 }
69
70 for _, pon := range olt.Pons {
71 p := bbsim.PONPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070072 ID: int32(pon.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070073 OperState: pon.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070074 }
75 pons = append(pons, &p)
76 }
77
Matteo Scandolo4a036262020-08-17 15:56:13 -070078 oltAddress := strings.Split(common.Config.BBSim.OpenOltAddress, ":")[0]
rajeshf921f882020-03-06 18:24:28 +053079 if oltAddress == "" {
80 oltAddress = getOltIP().String()
81 }
82
Matteo Scandolo84f7d482019-08-08 19:00:47 -070083 res := bbsim.Olt{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070084 ID: int32(olt.ID),
85 SerialNumber: olt.SerialNumber,
86 OperState: olt.OperState.Current(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070087 InternalState: olt.InternalState.Current(),
rajeshf921f882020-03-06 18:24:28 +053088 IP: oltAddress,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070089 NNIPorts: nnis,
90 PONPorts: pons,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070091 }
92 return &res, nil
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070093}
94
Zdravko Bozakov681364d2019-11-10 14:28:46 +010095func (s BBSimServer) PoweronOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
96 res := &bbsim.Response{}
97 o := devices.GetOLT()
98
99 if err := o.InternalState.Event("initialize"); err != nil {
100 log.Errorf("Error initializing OLT: %v", err)
101 res.StatusCode = int32(codes.FailedPrecondition)
102 return res, err
103 }
104
105 res.StatusCode = int32(codes.OK)
106 return res, nil
107}
108
109func (s BBSimServer) ShutdownOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
110 res := &bbsim.Response{}
111 o := devices.GetOLT()
112
113 if err := o.InternalState.Event("disable"); err != nil {
114 log.Errorf("Error disabling OLT: %v", err)
115 res.StatusCode = int32(codes.FailedPrecondition)
116 return res, err
117 }
118
119 res.StatusCode = int32(codes.OK)
120 return res, nil
121}
122
123func (s BBSimServer) RebootOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
124 res := &bbsim.Response{}
125 o := devices.GetOLT()
Shrey Baid688b4242020-07-10 20:40:10 +0530126 go func() { _ = o.RestartOLT() }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100127 res.StatusCode = int32(codes.OK)
128 res.Message = fmt.Sprintf("OLT restart triggered.")
129 return res, nil
130}
131
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800132func (s BBSimServer) StopgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
133 res := &bbsim.Response{}
134 res.StatusCode = int32(codes.OK)
135 res.Message = fmt.Sprintf("Openolt gRPC server stopped")
136 o := devices.GetOLT()
137
138 logger.Infof("Received request to stop Openolt gRPC Server")
139
140 o.StopOltServer()
141
142 return res, nil
143}
144
145func (s BBSimServer) StartgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
146 res := &bbsim.Response{}
147 res.StatusCode = int32(codes.OK)
148 res.Message = fmt.Sprintf("Openolt gRPC server started")
149 o := devices.GetOLT()
150
151 logger.Infof("Received request to start Openolt gRPC Server")
152
153 _, err := o.StartOltServer()
154 if err != nil {
155 return nil, err
156 }
157
158 return res, nil
159}
160
161func (s BBSimServer) RestartgRPCServer(ctx context.Context, req *bbsim.Timeout) (*bbsim.Response, error) {
162 o := devices.GetOLT()
163 logger.Infof("Received request to restart Openolt gRPC Server in %v seconds", req.Delay)
164 o.StopOltServer()
165
166 res := &bbsim.Response{}
167 res.StatusCode = int32(codes.OK)
168 res.Message = fmt.Sprintf("Openolt gRPC server stopped, restarting in %v", req.Delay)
169
170 go func() {
171 time.Sleep(time.Duration(req.Delay) * time.Second)
172 _, err := o.StartOltServer()
173 if err != nil {
174 logger.WithFields(log.Fields{
175 "err": err,
176 }).Error("Cannot restart Openolt gRPC server")
177 }
178 logger.Infof("Openolt gRPC Server restarted after %v seconds", req.Delay)
179 }()
180
181 return res, nil
182}
183
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700184func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
185
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700186 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700187
188 return &bbsim.LogLevel{
189 Level: log.StandardLogger().Level.String(),
190 Caller: log.StandardLogger().ReportCaller,
191 }, nil
192}
Scott Baker41724b82020-01-21 19:54:53 -0800193
Anand S Katti86552f92020-03-03 21:56:32 +0530194func (s BBSimServer) SetOnuAlarmIndication(ctx context.Context, req *bbsim.ONUAlarmRequest) (*bbsim.Response, error) {
Scott Baker41724b82020-01-21 19:54:53 -0800195 o := devices.GetOLT()
Anand S Katti86552f92020-03-03 21:56:32 +0530196 err := alarmsim.SimulateOnuAlarm(ctx, req, o)
Scott Baker41724b82020-01-21 19:54:53 -0800197 if err != nil {
198 return nil, err
199 }
200
201 res := &bbsim.Response{}
202 res.StatusCode = int32(codes.OK)
Anand S Katti86552f92020-03-03 21:56:32 +0530203 res.Message = fmt.Sprintf("Onu Alarm Indication Sent.")
204 return res, nil
205}
206
207// SetOltAlarmIndication generates OLT Alarms for LOS
208func (s BBSimServer) SetOltAlarmIndication(ctx context.Context, req *bbsim.OLTAlarmRequest) (*bbsim.Response, error) {
209 o := devices.GetOLT()
210 err := alarmsim.SimulateOltAlarm(ctx, req, o)
211 if err != nil {
212 return nil, err
213 }
214
215 res := &bbsim.Response{}
216 res.StatusCode = int32(codes.OK)
217 res.Message = fmt.Sprintf("Olt Alarm Indication Sent.")
Scott Baker41724b82020-01-21 19:54:53 -0800218 return res, nil
219}