blob: 620e052ff9a6978230513625fd3ae42030869780 [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
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070058type ONUEapolRestart struct {
59 Args struct {
60 OnuSn OnuSnString
61 } `positional-args:"yes" required:"yes"`
62}
63
64type ONUDhcpRestart struct {
65 Args struct {
66 OnuSn OnuSnString
67 } `positional-args:"yes" required:"yes"`
68}
69
Matteo Scandolo10f965c2019-09-24 10:40:46 -070070type ONUOptions struct {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070071 List ONUList `command:"list"`
72 Get ONUGet `command:"get"`
73 ShutDown ONUShutDown `command:"shutdown"`
74 PowerOn ONUPowerOn `command:"poweron"`
75 RestartEapol ONUEapolRestart `command:"auth_restart"`
76 RestartDchp ONUDhcpRestart `command:"dhcp_restart"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -070077}
78
79func RegisterONUCommands(parser *flags.Parser) {
80 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
81}
82
83func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070084 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
85
86 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070087 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070088 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -070089 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -070090 return pb.NewBBSimClient(conn), conn
91}
92
93func getONUs() *pb.ONUs {
94
95 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070096 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -070097
98 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -070099 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
100 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700101
102 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700103 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700104 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700105 return nil
106 }
107 return onus
108}
109
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700110func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700111 onus := getONUs()
112
113 // print out
114 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
115 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
116 log.Fatalf("Error while formatting ONUs table: %s", err)
117 }
118
119 return nil
120}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700121
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700122func (options *ONUGet) 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.GetONU(ctx, &req)
132
133 if err != nil {
134 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
135 return err
136 }
137
138 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
139 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
140 log.Fatalf("Error while formatting ONUs table: %s", err)
141 }
142
143 return nil
144}
145
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700146func (options *ONUShutDown) Execute(args []string) error {
147
148 client, conn := connect()
149 defer conn.Close()
150
151 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
152 defer cancel()
153 req := pb.ONURequest{
154 SerialNumber: string(options.Args.OnuSn),
155 }
156 res, err := client.ShutdownONU(ctx, &req)
157
158 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700159 log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700160 return err
161 }
162
163 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
164
165 return nil
166}
167
168func (options *ONUPowerOn) Execute(args []string) error {
169 client, conn := connect()
170 defer conn.Close()
171
172 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
173 defer cancel()
174 req := pb.ONURequest{
175 SerialNumber: string(options.Args.OnuSn),
176 }
177 res, err := client.PoweronONU(ctx, &req)
178
179 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700180 log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err)
181 return err
182 }
183
184 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
185
186 return nil
187}
188
189func (options *ONUEapolRestart) Execute(args []string) error {
190 client, conn := connect()
191 defer conn.Close()
192
193 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
194 defer cancel()
195 req := pb.ONURequest{
196 SerialNumber: string(options.Args.OnuSn),
197 }
198 res, err := client.RestartEapol(ctx, &req)
199
200 if err != nil {
201 log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err)
202 return err
203 }
204
205 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
206
207 return nil
208}
209
210func (options *ONUDhcpRestart) Execute(args []string) error {
211 client, conn := connect()
212 defer conn.Close()
213
214 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
215 defer cancel()
216 req := pb.ONURequest{
217 SerialNumber: string(options.Args.OnuSn),
218 }
219 res, err := client.RestartDhcp(ctx, &req)
220
221 if err != nil {
222 log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700223 return err
224 }
225
226 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
227
228 return nil
229}
230
231func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
232 client, conn := connect()
233 defer conn.Close()
234
235 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
236 defer cancel()
237
238 onus, err := client.GetONUs(ctx, &pb.Empty{})
239 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700240 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700241 return nil
242 }
243
244 list := make([]flags.Completion, 0)
245 for _, k := range onus.Items {
246 if strings.HasPrefix(k.SerialNumber, match) {
247 list = append(list, flags.Completion{Item: k.SerialNumber})
248 }
249 }
250
251 return list
252}