blob: c7d7e3857631b9e435b5bd1269c577bd276ab213 [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
Elia Battiston67e9e4c2022-02-15 16:38:40 +010025 "google.golang.org/grpc/status"
26
Matteo Scandolo11006992019-08-28 11:29:46 -070027 "github.com/opencord/bbsim/api/bbsim"
28 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070029 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070030 log "github.com/sirupsen/logrus"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010031 "google.golang.org/grpc/codes"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070032)
33
34var logger = log.WithFields(log.Fields{
35 "module": "GrpcApiServer",
36})
37
38var (
Matteo Scandolo8df63df2019-09-12 10:34:32 -070039 version string
40 buildTime string
41 commitHash string
42 gitStatus string
Matteo Scandolo84f7d482019-08-08 19:00:47 -070043)
44
45type BBSimServer struct {
46}
47
Matteo Scandolo8df63df2019-09-12 10:34:32 -070048func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020049 // TODO add a flag to specify whether the tree was clean at this commit or not
Matteo Scandolo84f7d482019-08-08 19:00:47 -070050 return &bbsim.VersionNumber{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070051 Version: version,
52 BuildTime: buildTime,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070053 CommitHash: commitHash,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070054 GitStatus: gitStatus,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070055 }, nil
56}
57
58func (s BBSimServer) GetOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Olt, error) {
59 olt := devices.GetOLT()
60 nnis := []*bbsim.NNIPort{}
61 pons := []*bbsim.PONPort{}
62
63 for _, nni := range olt.Nnis {
64 n := bbsim.NNIPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070065 ID: int32(nni.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070066 OperState: nni.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070067 }
68 nnis = append(nnis, &n)
69 }
70
71 for _, pon := range olt.Pons {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080072
73 allocatedOnuIds := []*bbsim.PonAllocatedResources{}
74 allocatedAllocIds := []*bbsim.PonAllocatedResources{}
75 allocatedGemPorts := []*bbsim.PonAllocatedResources{}
76
77 for k, v := range pon.AllocatedOnuIds {
78 resource := &bbsim.PonAllocatedResources{
79 SerialNumber: common.OnuSnToString(v),
80 Id: int32(k),
81 }
82 allocatedOnuIds = append(allocatedOnuIds, resource)
83 }
84
85 for k, v := range pon.AllocatedGemPorts {
86 resource := &bbsim.PonAllocatedResources{
87 SerialNumber: common.OnuSnToString(v),
88 Id: int32(k),
89 }
90 allocatedGemPorts = append(allocatedGemPorts, resource)
91 }
92
Girish Gowdra574834a2022-02-04 15:15:15 -080093 for _, v := range pon.AllocatedAllocIds {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080094 resource := &bbsim.PonAllocatedResources{
Girish Gowdra574834a2022-02-04 15:15:15 -080095 SerialNumber: common.OnuSnToString(v.OnuSn),
96 Id: int32(v.AllocID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080097 }
98 allocatedAllocIds = append(allocatedAllocIds, resource)
99 }
100
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700101 p := bbsim.PONPort{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800102 ID: int32(pon.ID),
103 OperState: pon.OperState.Current(),
104 InternalState: pon.InternalState.Current(),
105 PacketCount: pon.PacketCount,
106 AllocatedOnuIds: allocatedOnuIds,
107 AllocatedAllocIds: allocatedAllocIds,
108 AllocatedGemPorts: allocatedGemPorts,
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700109 }
110 pons = append(pons, &p)
111 }
112
Matteo Scandolo4a036262020-08-17 15:56:13 -0700113 oltAddress := strings.Split(common.Config.BBSim.OpenOltAddress, ":")[0]
rajeshf921f882020-03-06 18:24:28 +0530114 if oltAddress == "" {
115 oltAddress = getOltIP().String()
116 }
117
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700118 res := bbsim.Olt{
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700119 ID: int32(olt.ID),
120 SerialNumber: olt.SerialNumber,
121 OperState: olt.OperState.Current(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700122 InternalState: olt.InternalState.Current(),
rajeshf921f882020-03-06 18:24:28 +0530123 IP: oltAddress,
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700124 NNIPorts: nnis,
125 PONPorts: pons,
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700126 }
127 return &res, nil
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700128}
129
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800130// takes a nested map and return a proto
131func resourcesMapToresourcesProto(resourceType bbsim.OltAllocatedResourceType_Type, resources map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool) *bbsim.OltAllocatedResources {
132 proto := &bbsim.OltAllocatedResources{
133 Resources: []*bbsim.OltAllocatedResource{},
134 }
135 for ponId, ponValues := range resources {
136 for onuId, onuValues := range ponValues {
137 for uniId, uniValues := range onuValues {
138 for allocId, flows := range uniValues {
139 for flow := range flows {
140 resource := &bbsim.OltAllocatedResource{
141 Type: resourceType.String(),
142 PonPortId: ponId,
143 OnuId: onuId,
144 PortNo: uniId,
145 ResourceId: allocId,
146 FlowId: flow,
147 }
148 proto.Resources = append(proto.Resources, resource)
149 }
150 }
151 }
152 }
153 }
154 return proto
155}
156
157func (s BBSimServer) GetOltAllocatedResources(ctx context.Context, req *bbsim.OltAllocatedResourceType) (*bbsim.OltAllocatedResources, error) {
158 o := devices.GetOLT()
159
160 switch req.Type {
161 case bbsim.OltAllocatedResourceType_UNKNOWN:
162 return nil, status.Errorf(codes.InvalidArgument, "resource-type-%s-is-invalid", req.Type)
163 case bbsim.OltAllocatedResourceType_ALLOC_ID:
164 return resourcesMapToresourcesProto(bbsim.OltAllocatedResourceType_ALLOC_ID, o.AllocIDs), nil
165 case bbsim.OltAllocatedResourceType_GEM_PORT:
166 return resourcesMapToresourcesProto(bbsim.OltAllocatedResourceType_GEM_PORT, o.GemPortIDs), nil
167 default:
168 return nil, status.Errorf(codes.InvalidArgument, "unkown-resource-type-%s", req.Type)
169 }
170}
171
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100172func (s BBSimServer) PoweronOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
173 res := &bbsim.Response{}
174 o := devices.GetOLT()
175
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100176 if err := o.InternalState.Event(devices.OltInternalTxInitialize); err != nil {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100177 log.Errorf("Error initializing OLT: %v", err)
178 res.StatusCode = int32(codes.FailedPrecondition)
179 return res, err
180 }
181
182 res.StatusCode = int32(codes.OK)
183 return res, nil
184}
185
186func (s BBSimServer) ShutdownOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
187 res := &bbsim.Response{}
188 o := devices.GetOLT()
189
Elia Battiston67e9e4c2022-02-15 16:38:40 +0100190 if err := o.InternalState.Event(devices.OltInternalTxDisable); err != nil {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100191 log.Errorf("Error disabling OLT: %v", err)
192 res.StatusCode = int32(codes.FailedPrecondition)
193 return res, err
194 }
195
196 res.StatusCode = int32(codes.OK)
197 return res, nil
198}
199
200func (s BBSimServer) RebootOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
201 res := &bbsim.Response{}
202 o := devices.GetOLT()
Shrey Baid688b4242020-07-10 20:40:10 +0530203 go func() { _ = o.RestartOLT() }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100204 res.StatusCode = int32(codes.OK)
205 res.Message = fmt.Sprintf("OLT restart triggered.")
206 return res, nil
207}
208
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800209func (s BBSimServer) StopgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
210 res := &bbsim.Response{}
211 res.StatusCode = int32(codes.OK)
212 res.Message = fmt.Sprintf("Openolt gRPC server stopped")
213 o := devices.GetOLT()
214
215 logger.Infof("Received request to stop Openolt gRPC Server")
216
217 o.StopOltServer()
218
219 return res, nil
220}
221
222func (s BBSimServer) StartgRPCServer(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
223 res := &bbsim.Response{}
224 res.StatusCode = int32(codes.OK)
225 res.Message = fmt.Sprintf("Openolt gRPC server started")
226 o := devices.GetOLT()
227
228 logger.Infof("Received request to start Openolt gRPC Server")
229
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000230 if o.OltServer != nil {
231 return nil, fmt.Errorf("Openolt gRPC server already running.")
232 }
233
234 oltGrpcServer, err := o.StartOltServer()
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800235 if err != nil {
236 return nil, err
237 }
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000238 o.OltServer = oltGrpcServer
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800239
240 return res, nil
241}
242
243func (s BBSimServer) RestartgRPCServer(ctx context.Context, req *bbsim.Timeout) (*bbsim.Response, error) {
244 o := devices.GetOLT()
245 logger.Infof("Received request to restart Openolt gRPC Server in %v seconds", req.Delay)
246 o.StopOltServer()
247
248 res := &bbsim.Response{}
249 res.StatusCode = int32(codes.OK)
250 res.Message = fmt.Sprintf("Openolt gRPC server stopped, restarting in %v", req.Delay)
251
252 go func() {
253 time.Sleep(time.Duration(req.Delay) * time.Second)
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000254 oltGrpcServer, err := o.StartOltServer()
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800255 if err != nil {
256 logger.WithFields(log.Fields{
257 "err": err,
258 }).Error("Cannot restart Openolt gRPC server")
259 }
Hardik Windlassefdb4b62021-03-18 10:33:24 +0000260 o.OltServer = oltGrpcServer
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800261 logger.Infof("Openolt gRPC Server restarted after %v seconds", req.Delay)
262 }()
263
264 return res, nil
265}
266
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700267func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
268
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700269 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700270
271 return &bbsim.LogLevel{
272 Level: log.StandardLogger().Level.String(),
273 Caller: log.StandardLogger().ReportCaller,
274 }, nil
275}
Scott Baker41724b82020-01-21 19:54:53 -0800276
Anand S Katti86552f92020-03-03 21:56:32 +0530277func (s BBSimServer) SetOnuAlarmIndication(ctx context.Context, req *bbsim.ONUAlarmRequest) (*bbsim.Response, error) {
Scott Baker41724b82020-01-21 19:54:53 -0800278 o := devices.GetOLT()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800279
280 res := &bbsim.Response{}
281
282 onu, err := o.FindOnuBySn(req.SerialNumber)
Scott Baker41724b82020-01-21 19:54:53 -0800283 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800284 res.StatusCode = int32(codes.NotFound)
285 res.Message = err.Error()
Scott Baker41724b82020-01-21 19:54:53 -0800286 return nil, err
287 }
288
Matteo Scandolof9d43412021-01-12 11:11:34 -0800289 if err := onu.SetAlarm(req.AlarmType, req.Status); err != nil {
290 res.StatusCode = int32(codes.Internal)
291 res.Message = err.Error()
292 return nil, err
293 }
294
Scott Baker41724b82020-01-21 19:54:53 -0800295 res.StatusCode = int32(codes.OK)
Anand S Katti86552f92020-03-03 21:56:32 +0530296 res.Message = fmt.Sprintf("Onu Alarm Indication Sent.")
297 return res, nil
298}
299
300// SetOltAlarmIndication generates OLT Alarms for LOS
301func (s BBSimServer) SetOltAlarmIndication(ctx context.Context, req *bbsim.OLTAlarmRequest) (*bbsim.Response, error) {
302 o := devices.GetOLT()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800303 res := &bbsim.Response{}
304
305 if err := o.SetAlarm(req.InterfaceID, req.InterfaceType, req.Status); err != nil {
306 res.StatusCode = int32(codes.Internal)
307 res.Message = err.Error()
Anand S Katti86552f92020-03-03 21:56:32 +0530308 return nil, err
309 }
310
Anand S Katti86552f92020-03-03 21:56:32 +0530311 res.StatusCode = int32(codes.OK)
312 res.Message = fmt.Sprintf("Olt Alarm Indication Sent.")
Scott Baker41724b82020-01-21 19:54:53 -0800313 return res, nil
314}