blob: 09e408073240cf67f9dae8eefc2bab201ea26b68 [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"
24 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070025 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070026 log "github.com/sirupsen/logrus"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010027 "google.golang.org/grpc/codes"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070028)
29
30var logger = log.WithFields(log.Fields{
31 "module": "GrpcApiServer",
32})
33
34var (
Matteo Scandolo8df63df2019-09-12 10:34:32 -070035 version string
36 buildTime string
37 commitHash string
38 gitStatus string
Matteo Scandolo84f7d482019-08-08 19:00:47 -070039)
40
41type BBSimServer struct {
42}
43
Matteo Scandolo8df63df2019-09-12 10:34:32 -070044func (s BBSimServer) Version(ctx context.Context, req *bbsim.Empty) (*bbsim.VersionNumber, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020045 // TODO add a flag to specify whether the tree was clean at this commit or not
Matteo Scandolo84f7d482019-08-08 19:00:47 -070046 return &bbsim.VersionNumber{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070047 Version: version,
48 BuildTime: buildTime,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070049 CommitHash: commitHash,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070050 GitStatus: gitStatus,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070051 }, nil
52}
53
54func (s BBSimServer) GetOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Olt, error) {
55 olt := devices.GetOLT()
56 nnis := []*bbsim.NNIPort{}
57 pons := []*bbsim.PONPort{}
58
59 for _, nni := range olt.Nnis {
60 n := bbsim.NNIPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070061 ID: int32(nni.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070062 OperState: nni.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070063 }
64 nnis = append(nnis, &n)
65 }
66
67 for _, pon := range olt.Pons {
68 p := bbsim.PONPort{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070069 ID: int32(pon.ID),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070070 OperState: pon.OperState.Current(),
Matteo Scandolo84f7d482019-08-08 19:00:47 -070071 }
72 pons = append(pons, &p)
73 }
74
75 res := bbsim.Olt{
Matteo Scandolo8df63df2019-09-12 10:34:32 -070076 ID: int32(olt.ID),
77 SerialNumber: olt.SerialNumber,
78 OperState: olt.OperState.Current(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070079 InternalState: olt.InternalState.Current(),
Matteo Scandolo8df63df2019-09-12 10:34:32 -070080 NNIPorts: nnis,
81 PONPorts: pons,
Matteo Scandolo84f7d482019-08-08 19:00:47 -070082 }
83 return &res, nil
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070084}
85
Zdravko Bozakov681364d2019-11-10 14:28:46 +010086func (s BBSimServer) PoweronOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
87 res := &bbsim.Response{}
88 o := devices.GetOLT()
89
90 if err := o.InternalState.Event("initialize"); err != nil {
91 log.Errorf("Error initializing OLT: %v", err)
92 res.StatusCode = int32(codes.FailedPrecondition)
93 return res, err
94 }
95
96 res.StatusCode = int32(codes.OK)
97 return res, nil
98}
99
100func (s BBSimServer) ShutdownOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
101 res := &bbsim.Response{}
102 o := devices.GetOLT()
103
104 if err := o.InternalState.Event("disable"); err != nil {
105 log.Errorf("Error disabling OLT: %v", err)
106 res.StatusCode = int32(codes.FailedPrecondition)
107 return res, err
108 }
109
110 res.StatusCode = int32(codes.OK)
111 return res, nil
112}
113
114func (s BBSimServer) RebootOlt(ctx context.Context, req *bbsim.Empty) (*bbsim.Response, error) {
115 res := &bbsim.Response{}
116 o := devices.GetOLT()
117 go o.RestartOLT()
118 res.StatusCode = int32(codes.OK)
119 res.Message = fmt.Sprintf("OLT restart triggered.")
120 return res, nil
121}
122
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700123func (s BBSimServer) SetLogLevel(ctx context.Context, req *bbsim.LogLevel) (*bbsim.LogLevel, error) {
124
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700125 common.SetLogLevel(log.StandardLogger(), req.Level, req.Caller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700126
127 return &bbsim.LogLevel{
128 Level: log.StandardLogger().Level.String(),
129 Caller: log.StandardLogger().ReportCaller,
130 }, nil
131}