blob: bed373373c8dcdadfa6fed6bd86e4ef4e3411d3e [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package commands
19
20import (
21 "context"
22 "fmt"
Pragya Arya8bdb4532020-03-02 17:08:09 +053023 "os"
24 "strconv"
25
Matteo Scandolo8df63df2019-09-12 10:34:32 -070026 "github.com/jessevdk/go-flags"
Pragya Arya8bdb4532020-03-02 17:08:09 +053027 "github.com/olekukonko/tablewriter"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070028 pb "github.com/opencord/bbsim/api/bbsim"
29 "github.com/opencord/bbsim/internal/bbsimctl/config"
30 "github.com/opencord/cordctl/pkg/format"
31 log "github.com/sirupsen/logrus"
32 "google.golang.org/grpc"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070033)
34
35const (
rajeshf921f882020-03-06 18:24:28 +053036 DEFAULT_OLT_DEVICE_HEADER_FORMAT = "table{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .IP }}"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070037 DEFAULT_PORT_HEADER_FORMAT = "table{{ .ID }}\t{{ .OperState }}"
38)
39
40type OltGet struct{}
41
42type OltNNIs struct{}
43
44type OltPONs struct{}
45
Zdravko Bozakov681364d2019-11-10 14:28:46 +010046type OltShutdown struct{}
47
48type OltPoweron struct{}
49
50type OltReboot struct{}
51
Matteo Scandolo88c204a2020-11-03 10:34:24 -080052type StopGrpcServer struct{}
53
54type StartGrpcServer struct{}
55type RestartGrpcServer struct {
56 Args struct {
57 Delay uint32
58 } `positional-args:"yes" required:"yes"`
59}
60
Pragya Arya8bdb4532020-03-02 17:08:09 +053061type OltFlows struct{}
62
Pragya Aryabd731ec2020-02-11 16:38:17 +053063type OltPoweronAllOnus struct{}
64
65type OltShutdownAllOnus struct{}
66
Matteo Scandolo8df63df2019-09-12 10:34:32 -070067type oltOptions struct {
Pragya Aryabd731ec2020-02-11 16:38:17 +053068 Get OltGet `command:"get"`
69 NNI OltNNIs `command:"nnis"`
70 PON OltPONs `command:"pons"`
71 Shutdown OltShutdown `command:"shutdown"`
72 Poweron OltPoweron `command:"poweron"`
73 Reboot OltReboot `command:"reboot"`
74 Alarms OltAlarmOptions `command:"alarms"`
75 Flows OltFlows `command:"flows"`
76 PoweronAllOnus OltPoweronAllOnus `command:"poweronAllONUs"`
77 ShutdownAllOnus OltShutdownAllOnus `command:"shutdownAllONUs"`
Matteo Scandolo88c204a2020-11-03 10:34:24 -080078 StopServer StopGrpcServer `command:"stopServer"`
79 StartServer StartGrpcServer `command:"startServer"`
80 RestartServer RestartGrpcServer `command:"restartServer"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070081}
82
83func RegisterOltCommands(parser *flags.Parser) {
Shrey Baid688b4242020-07-10 20:40:10 +053084 _, _ = parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -070085}
86
87func getOLT() *pb.Olt {
88 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
89 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070090 log.Fatalf("did not connect: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070091 return nil
92 }
93 defer conn.Close()
94 c := pb.NewBBSimClient(conn)
95
96 // Contact the server and print out its response.
97
98 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
99 defer cancel()
100 olt, err := c.GetOlt(ctx, &pb.Empty{})
101 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700102 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700103 return nil
104 }
105 return olt
106}
107
108func printOltHeader(prefix string, o *pb.Olt) {
109 fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber))
110 fmt.Println()
111}
112
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700113func (o *OltGet) Execute(args []string) error {
114 olt := getOLT()
115
116 // print out
117 tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530118 _ = tableFormat.Execute(os.Stdout, true, olt)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700119
120 return nil
121}
122
123func (o *OltNNIs) Execute(args []string) error {
124 olt := getOLT()
125
126 printOltHeader("NNI Ports for", olt)
127
128 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530129 _ = tableFormat.Execute(os.Stdout, true, olt.NNIPorts)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700130
131 return nil
132}
133
134func (o *OltPONs) Execute(args []string) error {
135 olt := getOLT()
136
137 printOltHeader("PON Ports for", olt)
138
139 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530140 _ = tableFormat.Execute(os.Stdout, true, olt.PONPorts)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700141
142 return nil
143}
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100144
145func (o *OltShutdown) Execute(args []string) error {
146 client, conn := connect()
147 defer conn.Close()
148
149 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
150 defer cancel()
151
152 res, err := client.ShutdownOlt(ctx, &pb.Empty{})
153
154 if err != nil {
155 log.Fatalf("Cannot shut down OLT: %v", err)
156 return err
157 }
158
159 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
160 return nil
161}
162
163func (o *OltPoweron) Execute(args []string) error {
164 client, conn := connect()
165 defer conn.Close()
166
167 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
168 defer cancel()
169
170 res, err := client.PoweronOlt(ctx, &pb.Empty{})
171
172 if err != nil {
173 log.Fatalf("Cannot power on OLT: %v", err)
174 return err
175 }
176
177 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
178 return nil
179}
180
181func (o *OltReboot) Execute(args []string) error {
182 client, conn := connect()
183 defer conn.Close()
184
185 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
186 defer cancel()
187
188 res, err := client.RebootOlt(ctx, &pb.Empty{})
189
190 if err != nil {
191 log.Fatalf("Cannot reboot OLT: %v", err)
192 return err
193 }
194
195 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
196 return nil
197}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530198
Matteo Scandolo88c204a2020-11-03 10:34:24 -0800199func (o *StopGrpcServer) Execute(args []string) error {
200 client, conn := connect()
201 defer conn.Close()
202
203 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
204 defer cancel()
205
206 res, err := client.StopgRPCServer(ctx, &pb.Empty{})
207
208 if err != nil {
209 log.Fatalf("Cannot stop Openolt server: %v", err)
210 return err
211 }
212
213 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
214 return nil
215}
216
217func (o *StartGrpcServer) Execute(args []string) error {
218 client, conn := connect()
219 defer conn.Close()
220
221 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
222 defer cancel()
223
224 res, err := client.StartgRPCServer(ctx, &pb.Empty{})
225
226 if err != nil {
227 log.Fatalf("Cannot start Openolt server: %v", err)
228 return err
229 }
230
231 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
232 return nil
233}
234
235func (o *RestartGrpcServer) Execute(args []string) error {
236 req := &pb.Timeout{
237 Delay: o.Args.Delay,
238 }
239 client, conn := connect()
240 defer conn.Close()
241
242 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
243 defer cancel()
244
245 res, err := client.RestartgRPCServer(ctx, req)
246
247 if err != nil {
248 log.Fatalf("Cannot restart Openolt server: %v", err)
249 return err
250 }
251
252 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
253 return nil
254}
255
Pragya Arya8bdb4532020-03-02 17:08:09 +0530256func (o *OltFlows) Execute(args []string) error {
257 client, conn := connect()
258 defer conn.Close()
259
260 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
261 defer cancel()
262
263 req := pb.ONURequest{}
264 res, err := client.GetFlows(ctx, &req)
265 if err != nil {
266 log.Errorf("Cannot get flows for OLT: %v", err)
267 return err
268 }
269
270 if res.Flows == nil {
271 fmt.Println("OLT has no flows")
272 return nil
273 }
274
275 flowHeader := []string{
276 "access_intf_id",
277 "onu_id",
278 "uni_id",
279 "flow_id",
280 "flow_type",
281 "eth_type",
282 "alloc_id",
283 "network_intf_id",
284 "gemport_id",
285 "classifier",
286 "action",
287 "priority",
288 "cookie",
289 "port_no",
290 }
291
292 tableFlow := tablewriter.NewWriter(os.Stdout)
293 tableFlow.SetRowLine(true)
294 fmt.Fprintf(os.Stdout, "OLT Flows:\n")
295 tableFlow.SetHeader(flowHeader)
296
297 for _, flow := range res.Flows {
298 flowInfo := []string{}
299 flowInfo = append(flowInfo,
300 strconv.Itoa(int(flow.AccessIntfId)),
301 strconv.Itoa(int(flow.OnuId)),
302 strconv.Itoa(int(flow.UniId)),
303 strconv.Itoa(int(flow.FlowId)),
304 flow.FlowType,
305 fmt.Sprintf("%x", flow.Classifier.EthType),
306 strconv.Itoa(int(flow.AllocId)),
307 strconv.Itoa(int(flow.NetworkIntfId)),
308 strconv.Itoa(int(flow.GemportId)),
309 flow.Classifier.String(),
310 flow.Action.String(),
311 strconv.Itoa(int(flow.Priority)),
312 strconv.Itoa(int(flow.Cookie)),
313 strconv.Itoa(int(flow.PortNo)),
314 )
315 tableFlow.Append(flowInfo)
316 }
317 tableFlow.Render()
318 tableFlow.SetNewLine("")
319 return nil
320}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530321
322func (o *OltPoweronAllOnus) Execute(args []string) error {
323 client, conn := connect()
324 defer conn.Close()
325
326 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
327 defer cancel()
328
329 res, err := client.PoweronAllONUs(ctx, &pb.Empty{})
330
331 if err != nil {
332 log.Errorf("Cannot poweron all ONUs: %v", err)
333 return err
334 }
335
336 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
337 return nil
338}
339
340func (o *OltShutdownAllOnus) Execute(args []string) error {
341 client, conn := connect()
342 defer conn.Close()
343
344 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
345 defer cancel()
346
347 res, err := client.ShutdownAllONUs(ctx, &pb.Empty{})
348
349 if err != nil {
350 log.Errorf("Cannot shutdown all ONUs: %v", err)
351 return err
352 }
353
354 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
355 return nil
356}