blob: 2815501c261240ed43015020f978e31c6e406684 [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"
Anand S Katti09541352020-01-29 15:54:01 +053023 "os"
24 "strconv"
25 "strings"
26
Matteo Scandolo8df63df2019-09-12 10:34:32 -070027 "github.com/jessevdk/go-flags"
Anand S Katti09541352020-01-29 15:54:01 +053028 "github.com/olekukonko/tablewriter"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070029 pb "github.com/opencord/bbsim/api/bbsim"
30 "github.com/opencord/bbsim/internal/bbsimctl/config"
31 "github.com/opencord/cordctl/pkg/format"
32 log "github.com/sirupsen/logrus"
33 "google.golang.org/grpc"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070034)
35
36const (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070037 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 -070038)
39
Matteo Scandolo10f965c2019-09-24 10:40:46 -070040type OnuSnString string
Arjun E K57a7fcb2020-01-30 06:44:45 +000041type IgmpSubAction string
42
43const IgmpJoinKey string = "join"
44const IgmpLeaveKey string = "leave"
Arjun E Kdd443f02020-02-07 15:24:01 +000045const IgmpJoinKeyV3 string = "joinv3"
Arjun E K57a7fcb2020-01-30 06:44:45 +000046
Matteo Scandolo10f965c2019-09-24 10:40:46 -070047type ONUList struct{}
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070048
49type ONUGet struct {
50 Args struct {
51 OnuSn OnuSnString
52 } `positional-args:"yes" required:"yes"`
53}
54
Matteo Scandolo10f965c2019-09-24 10:40:46 -070055type ONUShutDown struct {
56 Args struct {
57 OnuSn OnuSnString
58 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070059}
60
Matteo Scandolo10f965c2019-09-24 10:40:46 -070061type ONUPowerOn struct {
62 Args struct {
63 OnuSn OnuSnString
64 } `positional-args:"yes" required:"yes"`
65}
66
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070067type ONUEapolRestart struct {
68 Args struct {
69 OnuSn OnuSnString
70 } `positional-args:"yes" required:"yes"`
71}
72
73type ONUDhcpRestart struct {
74 Args struct {
75 OnuSn OnuSnString
76 } `positional-args:"yes" required:"yes"`
77}
78
Arjun E K57a7fcb2020-01-30 06:44:45 +000079type ONUIgmp struct {
80 Args struct {
81 OnuSn OnuSnString
82 SubAction IgmpSubAction
83 } `positional-args:"yes" required:"yes"`
84}
85
Matteo Scandolo10f965c2019-09-24 10:40:46 -070086type ONUOptions struct {
Anand S Katti09541352020-01-29 15:54:01 +053087 List ONUList `command:"list"`
88 Get ONUGet `command:"get"`
89 ShutDown ONUShutDown `command:"shutdown"`
90 PowerOn ONUPowerOn `command:"poweron"`
91 RestartEapol ONUEapolRestart `command:"auth_restart"`
92 RestartDchp ONUDhcpRestart `command:"dhcp_restart"`
93 Igmp ONUIgmp `command:"igmp"`
94 TrafficSchedulers ONUTrafficSchedulers `command:"traffic_schedulers"`
Anand S Katti86552f92020-03-03 21:56:32 +053095 Alarms AlarmOptions `command:"alarms"`
Anand S Katti09541352020-01-29 15:54:01 +053096}
97
98type ONUTrafficSchedulers struct {
99 Args struct {
100 OnuSn OnuSnString
101 } `positional-args:"yes" required:"yes"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700102}
103
104func RegisterONUCommands(parser *flags.Parser) {
105 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
106}
107
108func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700109 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
110
111 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700112 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700113 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700114 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700115 return pb.NewBBSimClient(conn), conn
116}
117
118func getONUs() *pb.ONUs {
119
120 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700121 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700122
123 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700124 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
125 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126
127 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700128 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700129 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700130 return nil
131 }
132 return onus
133}
134
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700135func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700136 onus := getONUs()
137
138 // print out
139 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
140 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
141 log.Fatalf("Error while formatting ONUs table: %s", err)
142 }
143
144 return nil
145}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700146
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700147func (options *ONUGet) Execute(args []string) error {
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.GetONU(ctx, &req)
157
158 if err != nil {
159 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
160 return err
161 }
162
163 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
164 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
165 log.Fatalf("Error while formatting ONUs table: %s", err)
166 }
167
168 return nil
169}
170
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700171func (options *ONUShutDown) Execute(args []string) error {
172
173 client, conn := connect()
174 defer conn.Close()
175
176 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
177 defer cancel()
178 req := pb.ONURequest{
179 SerialNumber: string(options.Args.OnuSn),
180 }
181 res, err := client.ShutdownONU(ctx, &req)
182
183 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700184 log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700185 return err
186 }
187
188 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
189
190 return nil
191}
192
193func (options *ONUPowerOn) Execute(args []string) error {
194 client, conn := connect()
195 defer conn.Close()
196
197 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
198 defer cancel()
199 req := pb.ONURequest{
200 SerialNumber: string(options.Args.OnuSn),
201 }
202 res, err := client.PoweronONU(ctx, &req)
203
204 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700205 log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err)
206 return err
207 }
208
209 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
210
211 return nil
212}
213
214func (options *ONUEapolRestart) Execute(args []string) error {
215 client, conn := connect()
216 defer conn.Close()
217
218 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
219 defer cancel()
220 req := pb.ONURequest{
221 SerialNumber: string(options.Args.OnuSn),
222 }
223 res, err := client.RestartEapol(ctx, &req)
224
225 if err != nil {
226 log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err)
227 return err
228 }
229
230 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
231
232 return nil
233}
234
235func (options *ONUDhcpRestart) Execute(args []string) error {
236 client, conn := connect()
237 defer conn.Close()
238
239 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
240 defer cancel()
241 req := pb.ONURequest{
242 SerialNumber: string(options.Args.OnuSn),
243 }
244 res, err := client.RestartDhcp(ctx, &req)
245
246 if err != nil {
247 log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700248 return err
249 }
250
251 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
252
253 return nil
254}
255
Arjun E K57a7fcb2020-01-30 06:44:45 +0000256func (options *ONUIgmp) 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 SerialNumber: string(options.Args.OnuSn),
265 }
266
267 var subActionVal pb.SubActionTypes
268 if string(options.Args.SubAction) == IgmpJoinKey {
269 subActionVal = pb.SubActionTypes_JOIN
270 } else if string(options.Args.SubAction) == IgmpLeaveKey {
271 subActionVal = pb.SubActionTypes_LEAVE
Anand S Katti09541352020-01-29 15:54:01 +0530272 } else if string(options.Args.SubAction) == IgmpJoinKeyV3 {
273 subActionVal = pb.SubActionTypes_JOINV3
274 }
Arjun E K57a7fcb2020-01-30 06:44:45 +0000275
276 igmpReq := pb.IgmpRequest{
277 OnuReq: &req,
278 SubActionVal: subActionVal,
279 }
280 res, err := client.GetONU(ctx, igmpReq.OnuReq)
281 if err != nil {
282 log.WithFields(log.Fields{
283 "SerialNumber": options.Args.OnuSn,
284 }).Errorf("Cannot not get details on ONU error: %v", err)
285 }
286 log.WithFields(log.Fields{
287 "SerialNumber": igmpReq.OnuReq.SerialNumber,
288 }).Debugf("ONU has indentified : %s", res)
289
290 igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq)
291 if igmpErr != nil {
292 log.WithFields(log.Fields{
293 "SubAction": options.Args.SubAction,
294 }).Errorf("Could not process Action: error: %v", igmpErr)
295 } else {
296 log.WithFields(log.Fields{
297 "SubAction": options.Args.SubAction,
298 }).Debugf("igmp state has been changed with response: %s",
299 igmpRes.Message)
300 }
301
302 return nil
303}
304
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700305func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
306 client, conn := connect()
307 defer conn.Close()
308
309 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
310 defer cancel()
311
312 onus, err := client.GetONUs(ctx, &pb.Empty{})
313 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700314 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700315 return nil
316 }
317
318 list := make([]flags.Completion, 0)
319 for _, k := range onus.Items {
320 if strings.HasPrefix(k.SerialNumber, match) {
321 list = append(list, flags.Completion{Item: k.SerialNumber})
322 }
323 }
324
325 return list
326}
Anand S Katti09541352020-01-29 15:54:01 +0530327
328func (options *ONUTrafficSchedulers) Execute(args []string) error {
329 client, conn := connect()
330 defer conn.Close()
331
332 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
333 defer cancel()
334 req := pb.ONURequest{
335 SerialNumber: string(options.Args.OnuSn),
336 }
337 res, err := client.GetOnuTrafficSchedulers(ctx, &req)
338 if err != nil {
339 log.Fatalf("Cannot get traffic schedulers for ONU %s: %v", options.Args.OnuSn, err)
340 return err
341 }
342
343 if res.TraffSchedulers == nil {
344 log.Fatalf("Cannot get traffic schedulers for ONU: %s (unavailable)", options.Args.OnuSn)
345 return nil
346 }
347
348 SchedulerHeader := []string{"Direction",
349 "AllocId",
350 "Scheduler.Direction",
351 "Scheduler.AdditionalBw",
352 "Scheduler.Priority",
353 "Scheduler.Weight",
354 "Scheduler.SchedPolicy",
355 }
356
357 ShapingInfoHeader := []string{"InferredAdditionBwIndication",
358 "Cbs",
359 "Cir",
360 "Gir",
361 "Pbs",
362 "Pir",
363 }
364
365 SchedulerVals := []string{}
366 ShapingInfoVals := []string{}
367 for _, v := range res.TraffSchedulers.TrafficScheds {
368 SchedulerVals = append(SchedulerVals,
369 v.GetDirection().String(),
370 strconv.Itoa(int(v.GetAllocId())),
371 v.Scheduler.GetDirection().String(),
372 v.Scheduler.GetAdditionalBw().String(),
373 strconv.Itoa(int(v.Scheduler.GetPriority())),
374 strconv.Itoa(int(v.Scheduler.GetWeight())),
375 v.GetScheduler().GetSchedPolicy().String(),
376 )
377
378 ShapingInfoVals = append(ShapingInfoVals,
379 v.TrafficShapingInfo.GetAddBwInd().String(),
380 strconv.Itoa(int(v.TrafficShapingInfo.GetCbs())),
381 strconv.Itoa(int(v.TrafficShapingInfo.GetCir())),
382 strconv.Itoa(int(v.TrafficShapingInfo.GetGir())),
383 strconv.Itoa(int(v.TrafficShapingInfo.GetPbs())),
384 strconv.Itoa(int(v.TrafficShapingInfo.GetPir())),
385 )
386 }
387
388 fmt.Fprintf(os.Stdout, "OnuId: %d \n", int(res.TraffSchedulers.OnuId))
389 fmt.Fprintf(os.Stdout, "IntfId: %d \n", int(res.TraffSchedulers.IntfId))
390 fmt.Fprintf(os.Stdout, "UniId: %d \n", int(res.TraffSchedulers.UniId))
391 fmt.Fprintf(os.Stdout, "OnuPortNo: %d \n", int(res.TraffSchedulers.PortNo))
392
393 tableSched := tablewriter.NewWriter(os.Stdout)
394 tableSched.SetRowLine(true)
395 fmt.Fprintf(os.Stdout, "Traffic Schedulers Info:\n")
396 tableSched.SetHeader(SchedulerHeader)
397 tableSched.Append(SchedulerVals)
398 tableSched.Render()
399 tableSched.SetNewLine("")
400
401 tableShap := tablewriter.NewWriter(os.Stdout)
402 tableShap.SetRowLine(true)
403 fmt.Fprintf(os.Stdout, "Traffic Shaping Info:\n")
404 tableShap.SetHeader(ShapingInfoHeader)
405 tableShap.Append(ShapingInfoVals)
406 tableShap.Render()
407
408 return nil
409}