blob: ca311cec9e95baf7a0c97f167f58f82515867420 [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"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020022
Matteo Scandolo11006992019-08-28 11:29:46 -070023 "github.com/opencord/bbsim/api/bbsim"
Scott Baker41724b82020-01-21 19:54:53 -080024 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Matteo Scandolo11006992019-08-28 11:29:46 -070025 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070026 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070027 log "github.com/sirupsen/logrus"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010028 "google.golang.org/grpc/codes"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070029)
30
31var logger = log.WithFields(log.Fields{
32 "module": "GrpcApiServer",
33})
34
35var (
Matteo Scandolo8df63df2019-09-12 10:34:32 -070036 version string
37 buildTime string
38 commitHash string
39 gitStatus string
Matteo Scandolo84f7d482019-08-08 19:00:47 -070040)
41
42type BBSimServer struct {
43}
44
Matteo Scandolo8df63df2019-09-12 10:34:32 -070045func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020046 // TODO add a flag to specify whether the tree was clean at this commit or not
Matteo Scandolo84f7d482019-08-08 19:00:47 -070047 return &bbsim.VersionNumber{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070048 Version: version,
49 BuildTime: buildTime,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070050 CommitHash: commitHash,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070051 GitStatus: gitStatus,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070052 }, nil
53}
54
55func (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 Scandolo8df63df2019-09-12 10:34:32 -070062 ID: int32(nni.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070063 OperState: nni.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070064 }
65 nnis = append(nnis, &n)
66 }
67
68 for _, pon := range olt.Pons {
69 p := bbsim.PONPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070070 ID: int32(pon.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070071 OperState: pon.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070072 }
73 pons = append(pons, &p)
74 }
75
76 res := bbsim.Olt{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070077 ID: int32(olt.ID),
78 SerialNumber: olt.SerialNumber,
79 OperState: olt.OperState.Current(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070080 InternalState: olt.InternalState.Current(),
Matteo Scandolo8df63df2019-09-12 10:34:32 -070081 NNIPorts: nnis,
82 PONPorts: pons,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070083 }
84 return &res, nil
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070085}
86
Zdravko Bozakov681364d2019-11-10 14:28:46 +010087func (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
101func (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
115func (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 Scandolo2bf742a2019-10-01 11:33:34 -0700124func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
125
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700126 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700127
128 return &bbsim.LogLevel{
129 Level: log.StandardLogger().Level.String(),
130 Caller: log.StandardLogger().ReportCaller,
131 }, nil
132}
Scott Baker41724b82020-01-21 19:54:53 -0800133
134func (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}