blob: 7ed8f8d4877b8fd4735eb5915d1941549bc92683 [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"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020023
Matteo Scandolo11006992019-08-28 11:29:46 -070024 "github.com/opencord/bbsim/api/bbsim"
Scott Baker41724b82020-01-21 19:54:53 -080025 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Matteo Scandolo11006992019-08-28 11:29:46 -070026 "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 Scandolo2bf742a2019-10-01 11:33:34 -0700131func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
132
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700133 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700134
135 return &bbsim.LogLevel{
136 Level: log.StandardLogger().Level.String(),
137 Caller: log.StandardLogger().ReportCaller,
138 }, nil
139}
Scott Baker41724b82020-01-21 19:54:53 -0800140
Anand S Katti86552f92020-03-03 21:56:32 +0530141func (s BBSimServer) SetOnuAlarmIndication(ctx context.Context, req *bbsim.ONUAlarmRequest) (*bbsim.Response, error) {
Scott Baker41724b82020-01-21 19:54:53 -0800142 o := devices.GetOLT()
Anand S Katti86552f92020-03-03 21:56:32 +0530143 err := alarmsim.SimulateOnuAlarm(ctx, req, o)
Scott Baker41724b82020-01-21 19:54:53 -0800144 if err != nil {
145 return nil, err
146 }
147
148 res := &bbsim.Response{}
149 res.StatusCode = int32(codes.OK)
Anand S Katti86552f92020-03-03 21:56:32 +0530150 res.Message = fmt.Sprintf("Onu Alarm Indication Sent.")
151 return res, nil
152}
153
154// SetOltAlarmIndication generates OLT Alarms for LOS
155func (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 Baker41724b82020-01-21 19:54:53 -0800165 return res, nil
166}