blob: 5fd6392bb110c63be331beaec48d43a57a014c36 [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"
26 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070028 log "github.com/sirupsen/logrus"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010029 "google.golang.org/grpc/codes"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070030)
31
32var logger = log.WithFields(log.Fields{
33 "module": "GrpcApiServer",
34})
35
36var (
Matteo Scandolo8df63df2019-09-12 10:34:32 -070037 version string
38 buildTime string
39 commitHash string
40 gitStatus string
Matteo Scandolo84f7d482019-08-08 19:00:47 -070041)
42
43type BBSimServer struct {
44}
45
Matteo Scandolo8df63df2019-09-12 10:34:32 -070046func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020047 // TODO add a flag to specify whether the tree was clean at this commit or not
Matteo Scandolo84f7d482019-08-08 19:00:47 -070048 return &bbsim.VersionNumber{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070049 Version: version,
50 BuildTime: buildTime,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070051 CommitHash: commitHash,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070052 GitStatus: gitStatus,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070053 }, nil
54}
55
56func (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 Scandolo8df63df2019-09-12 10:34:32 -070063 ID: int32(nni.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070064 OperState: nni.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070065 }
66 nnis = append(nnis, &n)
67 }
68
69 for _, pon := range olt.Pons {
70 p := bbsim.PONPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070071 ID: int32(pon.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070072 OperState: pon.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070073 }
74 pons = append(pons, &p)
75 }
76
Matteo Scandolo4a036262020-08-17 15:56:13 -070077 oltAddress := strings.Split(common.Config.BBSim.OpenOltAddress, ":")[0]
rajeshf921f882020-03-06 18:24:28 +053078 if oltAddress == "" {
79 oltAddress = getOltIP().String()
80 }
81
Matteo Scandolo84f7d482019-08-08 19:00:47 -070082 res := bbsim.Olt{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070083 ID: int32(olt.ID),
84 SerialNumber: olt.SerialNumber,
85 OperState: olt.OperState.Current(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070086 InternalState: olt.InternalState.Current(),
rajeshf921f882020-03-06 18:24:28 +053087 IP: oltAddress,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070088 NNIPorts: nnis,
89 PONPorts: pons,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070090 }
91 return &res, nil
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070092}
93
Zdravko Bozakov681364d2019-11-10 14:28:46 +010094func (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
108func (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
122func (s BBSimServer) RebootOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
123 res := &bbsim.Response{}
124 o := devices.GetOLT()
Shrey Baid688b4242020-07-10 20:40:10 +0530125 go func() { _ = o.RestartOLT() }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100126 res.StatusCode = int32(codes.OK)
127 res.Message = fmt.Sprintf("OLT restart triggered.")
128 return res, nil
129}
130
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800131func (s BBSimServer) StopgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
132 res := &bbsim.Response{}
133 res.StatusCode = int32(codes.OK)
134 res.Message = fmt.Sprintf("Openolt gRPC server stopped")
135 o := devices.GetOLT()
136
137 logger.Infof("Received request to stop Openolt gRPC Server")
138
139 o.StopOltServer()
140
141 return res, nil
142}
143
144func (s BBSimServer) StartgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
145 res := &bbsim.Response{}
146 res.StatusCode = int32(codes.OK)
147 res.Message = fmt.Sprintf("Openolt gRPC server started")
148 o := devices.GetOLT()
149
150 logger.Infof("Received request to start Openolt gRPC Server")
151
152 _, err := o.StartOltServer()
153 if err != nil {
154 return nil, err
155 }
156
157 return res, nil
158}
159
160func (s BBSimServer) RestartgRPCServer(ctx context.Context, req *bbsim.Timeout) (*bbsim.Response, error) {
161 o := devices.GetOLT()
162 logger.Infof("Received request to restart Openolt gRPC Server in %v seconds", req.Delay)
163 o.StopOltServer()
164
165 res := &bbsim.Response{}
166 res.StatusCode = int32(codes.OK)
167 res.Message = fmt.Sprintf("Openolt gRPC server stopped, restarting in %v", req.Delay)
168
169 go func() {
170 time.Sleep(time.Duration(req.Delay) * time.Second)
171 _, err := o.StartOltServer()
172 if err != nil {
173 logger.WithFields(log.Fields{
174 "err": err,
175 }).Error("Cannot restart Openolt gRPC server")
176 }
177 logger.Infof("Openolt gRPC Server restarted after %v seconds", req.Delay)
178 }()
179
180 return res, nil
181}
182
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700183func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
184
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700185 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700186
187 return &bbsim.LogLevel{
188 Level: log.StandardLogger().Level.String(),
189 Caller: log.StandardLogger().ReportCaller,
190 }, nil
191}
Scott Baker41724b82020-01-21 19:54:53 -0800192
Anand S Katti86552f92020-03-03 21:56:32 +0530193func (s BBSimServer) SetOnuAlarmIndication(ctx context.Context, req *bbsim.ONUAlarmRequest) (*bbsim.Response, error) {
Scott Baker41724b82020-01-21 19:54:53 -0800194 o := devices.GetOLT()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800195
196 res := &bbsim.Response{}
197
198 onu, err := o.FindOnuBySn(req.SerialNumber)
Scott Baker41724b82020-01-21 19:54:53 -0800199 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800200 res.StatusCode = int32(codes.NotFound)
201 res.Message = err.Error()
Scott Baker41724b82020-01-21 19:54:53 -0800202 return nil, err
203 }
204
Matteo Scandolof9d43412021-01-12 11:11:34 -0800205 if err := onu.SetAlarm(req.AlarmType, req.Status); err != nil {
206 res.StatusCode = int32(codes.Internal)
207 res.Message = err.Error()
208 return nil, err
209 }
210
Scott Baker41724b82020-01-21 19:54:53 -0800211 res.StatusCode = int32(codes.OK)
Anand S Katti86552f92020-03-03 21:56:32 +0530212 res.Message = fmt.Sprintf("Onu Alarm Indication Sent.")
213 return res, nil
214}
215
216// SetOltAlarmIndication generates OLT Alarms for LOS
217func (s BBSimServer) SetOltAlarmIndication(ctx context.Context, req *bbsim.OLTAlarmRequest) (*bbsim.Response, error) {
218 o := devices.GetOLT()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800219 res := &bbsim.Response{}
220
221 if err := o.SetAlarm(req.InterfaceID, req.InterfaceType, req.Status); err != nil {
222 res.StatusCode = int32(codes.Internal)
223 res.Message = err.Error()
Anand S Katti86552f92020-03-03 21:56:32 +0530224 return nil, err
225 }
226
Anand S Katti86552f92020-03-03 21:56:32 +0530227 res.StatusCode = int32(codes.OK)
228 res.Message = fmt.Sprintf("Olt Alarm Indication Sent.")
Scott Baker41724b82020-01-21 19:54:53 -0800229 return res, nil
230}