blob: 526cbad3ad6baa1a77bbf5b8d15f4380188d8117 [file] [log] [blame]
Elia Battistone8d1fa42022-04-01 10:47:37 +02001/*
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
17package commands
18
19import (
20 "context"
21 "fmt"
22 "os"
23
24 "github.com/jessevdk/go-flags"
25 "github.com/opencord/bbsim/api/bbsim"
26 pb "github.com/opencord/bbsim/api/bbsim"
27 "github.com/opencord/bbsim/internal/bbsimctl/config"
28 "github.com/opencord/cordctl/pkg/format"
29 log "github.com/sirupsen/logrus"
30 "google.golang.org/grpc"
31)
32
33const (
34 DEFAULT_TRANSCEIVER_HEADER_FORMAT = "table{{ .ID }}\t{{ .UUID }}\t{{ .Name }}\t{{ .Technology }}\t{{ .PluggedIn }}\t{{ .PonIds }}"
35)
36
37type DMIOptions struct {
38 Events DmiEventOptions `command:"events"`
39 Transceiver DmiTransceiverOptions `command:"transceiver"`
40}
41
42type DmiEventOptions struct {
43 Create DmiEventCreate `command:"create"`
44}
45
46type DmiEventCreate struct {
47 Args struct {
48 Name string
49 } `positional-args:"yes" required:"yes"`
50}
51
52type DmiTransceiverOptions struct {
53 PlugIn DmiTransceiverPlugIn `command:"plug_in"`
54 PlugOut DmiTransceiverPlugOut `command:"plug_out"`
55 List DmiTransceiversList `command:"list"`
56}
57
58type DmiTransceiversList struct {
59}
60
61type DmiTransceiverPlugIn struct {
62 Args struct {
63 TransceiverId uint32
64 } `positional-args:"yes" required:"yes"`
65}
66
67type DmiTransceiverPlugOut struct {
68 Args struct {
69 TransceiverId uint32
70 } `positional-args:"yes" required:"yes"`
71}
72
73func RegisterDMICommands(parser *flags.Parser) {
74 _, _ = parser.AddCommand("dmi", "DMI Commands", "Commands to create events", &DMIOptions{})
75}
76
77func dmiEventGrpcClient() (bbsim.BBsimDmiClient, *grpc.ClientConn) {
78 conn, err := grpc.Dial(config.DmiConfig.Server, grpc.WithInsecure())
79 if err != nil {
80 log.Errorf("BBsimDmiClient connection failed : %v", err)
81 return nil, conn
82 }
83 return bbsim.NewBBsimDmiClient(conn), conn
84}
85
86// Execute create event
87func (o *DmiEventCreate) Execute(args []string) error {
88 client, conn := dmiEventGrpcClient()
89 defer conn.Close()
90
91 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
92 defer cancel()
93
94 req := bbsim.DmiEvent{EventName: o.Args.Name}
95 res, err := client.CreateEvent(ctx, &req)
96 if err != nil {
97 log.Errorf("Cannot create DMI event: %v", err)
98 return err
99 }
100
101 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
102 return nil
103}
104
105//Print a list of the transceivers and their state
106func (pon *DmiTransceiversList) Execute(args []string) error {
107 client, conn := dmiEventGrpcClient()
108 defer conn.Close()
109
110 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
111 defer cancel()
112
113 transceivers, err := client.GetTransceivers(ctx, &pb.DmiEmpty{})
114 if err != nil {
115 log.Errorf("Cannot get transceivers list: %v", err)
116 return err
117 }
118
119 // print out
120 tableFormat := format.Format(DEFAULT_TRANSCEIVER_HEADER_FORMAT)
121
122 if err := tableFormat.Execute(os.Stdout, true, transceivers.Items); err != nil {
123 log.Fatalf("Error while formatting transceivers table: %s", err)
124 }
125
126 return nil
127}
128
129//Plug in the specified transceiver
130func (pon *DmiTransceiverPlugIn) Execute(args []string) error {
131 client, conn := dmiEventGrpcClient()
132 defer conn.Close()
133
134 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
135 defer cancel()
136
137 req := pb.TransceiverRequest{
138 TransceiverId: uint32(pon.Args.TransceiverId),
139 }
140
141 res, err := client.PlugInTransceiver(ctx, &req)
142 if err != nil {
143 log.Errorf("Cannot plug in PON transceiver: %v", err)
144 return err
145 }
146
147 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
148 return nil
149}
150
151//Plug out the specified transceiver
152func (pon *DmiTransceiverPlugOut) Execute(args []string) error {
153 client, conn := dmiEventGrpcClient()
154 defer conn.Close()
155
156 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
157 defer cancel()
158
159 req := pb.TransceiverRequest{
160 TransceiverId: uint32(pon.Args.TransceiverId),
161 }
162
163 res, err := client.PlugOutTransceiver(ctx, &req)
164 if err != nil {
165 log.Errorf("Cannot plug out PON transceiver: %v", err)
166 return err
167 }
168
169 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
170 return nil
171}