blob: f99cb5cfd592c962769c58f5a17058074294462f [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"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070022 "fmt"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070023 "github.com/jessevdk/go-flags"
24 pb "github.com/opencord/bbsim/api/bbsim"
25 "github.com/opencord/bbsim/internal/bbsimctl/config"
26 "github.com/opencord/cordctl/pkg/format"
27 log "github.com/sirupsen/logrus"
28 "google.golang.org/grpc"
29 "os"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070030 "strings"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070031)
32
33const (
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070034 DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .STag }}\t{{ .CTag }}\t{{ .OperState }}\t{{ .InternalState }}"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070035)
36
Matteo Scandolo10f965c2019-09-24 10:40:46 -070037type OnuSnString string
38type ONUList struct{}
39type ONUShutDown struct {
40 Args struct {
41 OnuSn OnuSnString
42 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070043}
44
Matteo Scandolo10f965c2019-09-24 10:40:46 -070045type ONUPowerOn struct {
46 Args struct {
47 OnuSn OnuSnString
48 } `positional-args:"yes" required:"yes"`
49}
50
51type ONUOptions struct {
52 List ONUList `command:"list"`
53 ShutDown ONUShutDown `command:"shutdown"`
54 PowerOn ONUPowerOn `command:"poweron"`
55}
56
57func RegisterONUCommands(parser *flags.Parser) {
58 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
59}
60
61func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070062 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
63
64 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070065 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070066 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -070067 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -070068 return pb.NewBBSimClient(conn), conn
69}
70
71func getONUs() *pb.ONUs {
72
73 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070074 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070075
76 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -070077 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
78 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -070079
80 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -070081 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070082 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070083 return nil
84 }
85 return onus
86}
87
Matteo Scandolo10f965c2019-09-24 10:40:46 -070088func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070089 onus := getONUs()
90
91 // print out
92 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
93 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
94 log.Fatalf("Error while formatting ONUs table: %s", err)
95 }
96
97 return nil
98}
Matteo Scandolo10f965c2019-09-24 10:40:46 -070099
100func (options *ONUShutDown) Execute(args []string) error {
101
102 client, conn := connect()
103 defer conn.Close()
104
105 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
106 defer cancel()
107 req := pb.ONURequest{
108 SerialNumber: string(options.Args.OnuSn),
109 }
110 res, err := client.ShutdownONU(ctx, &req)
111
112 if err != nil {
113 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
114 return err
115 }
116
117 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
118
119 return nil
120}
121
122func (options *ONUPowerOn) Execute(args []string) error {
123 client, conn := connect()
124 defer conn.Close()
125
126 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
127 defer cancel()
128 req := pb.ONURequest{
129 SerialNumber: string(options.Args.OnuSn),
130 }
131 res, err := client.PoweronONU(ctx, &req)
132
133 if err != nil {
134 log.Fatalf("Cannot not power on ONU %s: %v", options.Args.OnuSn, err)
135 return err
136 }
137
138 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
139
140 return nil
141}
142
143func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
144 client, conn := connect()
145 defer conn.Close()
146
147 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
148 defer cancel()
149
150 onus, err := client.GetONUs(ctx, &pb.Empty{})
151 if err != nil {
152 log.Fatal("could not get ONUs: %v", err)
153 return nil
154 }
155
156 list := make([]flags.Completion, 0)
157 for _, k := range onus.Items {
158 if strings.HasPrefix(k.SerialNumber, match) {
159 list = append(list, flags.Completion{Item: k.SerialNumber})
160 }
161 }
162
163 return list
164}