blob: 716c7d363535b9d3c5fe6c820322e85af020c4e6 [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 Scandolo40e067f2019-10-16 16:59:41 -070034 DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .HwAddress }}\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{}
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070039
40type ONUGet struct {
41 Args struct {
42 OnuSn OnuSnString
43 } `positional-args:"yes" required:"yes"`
44}
45
Matteo Scandolo10f965c2019-09-24 10:40:46 -070046type ONUShutDown struct {
47 Args struct {
48 OnuSn OnuSnString
49 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070050}
51
Matteo Scandolo10f965c2019-09-24 10:40:46 -070052type ONUPowerOn struct {
53 Args struct {
54 OnuSn OnuSnString
55 } `positional-args:"yes" required:"yes"`
56}
57
58type ONUOptions struct {
59 List ONUList `command:"list"`
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070060 Get ONUGet `command:"get"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -070061 ShutDown ONUShutDown `command:"shutdown"`
62 PowerOn ONUPowerOn `command:"poweron"`
63}
64
65func RegisterONUCommands(parser *flags.Parser) {
66 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
67}
68
69func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070070 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
71
72 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070073 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070074 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -070075 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -070076 return pb.NewBBSimClient(conn), conn
77}
78
79func getONUs() *pb.ONUs {
80
81 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070082 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070083
84 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -070085 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
86 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -070087
88 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -070089 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070090 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070091 return nil
92 }
93 return onus
94}
95
Matteo Scandolo10f965c2019-09-24 10:40:46 -070096func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070097 onus := getONUs()
98
99 // print out
100 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
101 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
102 log.Fatalf("Error while formatting ONUs table: %s", err)
103 }
104
105 return nil
106}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700107
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700108func (options *ONUGet) Execute(args []string) error {
109 client, conn := connect()
110 defer conn.Close()
111
112 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
113 defer cancel()
114 req := pb.ONURequest{
115 SerialNumber: string(options.Args.OnuSn),
116 }
117 res, err := client.GetONU(ctx, &req)
118
119 if err != nil {
120 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
121 return err
122 }
123
124 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
125 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
126 log.Fatalf("Error while formatting ONUs table: %s", err)
127 }
128
129 return nil
130}
131
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700132func (options *ONUShutDown) Execute(args []string) error {
133
134 client, conn := connect()
135 defer conn.Close()
136
137 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
138 defer cancel()
139 req := pb.ONURequest{
140 SerialNumber: string(options.Args.OnuSn),
141 }
142 res, err := client.ShutdownONU(ctx, &req)
143
144 if err != nil {
145 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
146 return err
147 }
148
149 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
150
151 return nil
152}
153
154func (options *ONUPowerOn) Execute(args []string) error {
155 client, conn := connect()
156 defer conn.Close()
157
158 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
159 defer cancel()
160 req := pb.ONURequest{
161 SerialNumber: string(options.Args.OnuSn),
162 }
163 res, err := client.PoweronONU(ctx, &req)
164
165 if err != nil {
166 log.Fatalf("Cannot not power on ONU %s: %v", options.Args.OnuSn, err)
167 return err
168 }
169
170 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
171
172 return nil
173}
174
175func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
176 client, conn := connect()
177 defer conn.Close()
178
179 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
180 defer cancel()
181
182 onus, err := client.GetONUs(ctx, &pb.Empty{})
183 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700184 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700185 return nil
186 }
187
188 list := make([]flags.Completion, 0)
189 for _, k := range onus.Items {
190 if strings.HasPrefix(k.SerialNumber, match) {
191 list = append(list, flags.Completion{Item: k.SerialNumber})
192 }
193 }
194
195 return list
196}