blob: 7d6da8e2bad6a9b2058c75e688e3f879fc925844 [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 "github.com/jessevdk/go-flags"
23 pb "github.com/opencord/bbsim/api/bbsim"
24 "github.com/opencord/bbsim/internal/bbsimctl/config"
25 "github.com/opencord/cordctl/pkg/format"
26 log "github.com/sirupsen/logrus"
27 "google.golang.org/grpc"
28 "os"
29)
30
31const (
32 DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}"
33)
34
35type ONUOptions struct{}
36
37func RegisterONUCommands(parser *flags.Parser) {
38 parser.AddCommand("onus", "List ONU Devices", "Commands to list the ONU devices and their internal state", &ONUOptions{})
39}
40
41func getONUs() *pb.ONUs {
42 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
43
44 if err != nil {
45 log.Fatal("did not connect: %v", err)
46 return nil
47 }
48 defer conn.Close()
49 c := pb.NewBBSimClient(conn)
50
51 // Contact the server and print out its response.
52
53 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
54 defer cancel()
55 onus, err := c.GetONUs(ctx, &pb.Empty{})
56 if err != nil {
57 log.Fatal("could not get OLT: %v", err)
58 return nil
59 }
60 return onus
61}
62
63func (options *ONUOptions) Execute(args []string) error {
64 onus := getONUs()
65
66 // print out
67 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
68 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
69 log.Fatalf("Error while formatting ONUs table: %s", err)
70 }
71
72 return nil
73}