blob: 2979cb2001638daafeb23e8cd77bc0da042a3c29 [file] [log] [blame]
Anand S Katti86552f92020-03-03 21:56:32 +05301/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Anand S Katti86552f92020-03-03 21:56:32 +05303 * 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 "fmt"
Kent Hagerman60d62302020-03-10 17:02:36 -040023 "github.com/opencord/bbsim/internal/common"
Anand S Katti86552f92020-03-03 21:56:32 +053024 "os"
25 "strings"
26
27 "github.com/jessevdk/go-flags"
28 "github.com/olekukonko/tablewriter"
29 pb "github.com/opencord/bbsim/api/bbsim"
Anand S Katti86552f92020-03-03 21:56:32 +053030 "github.com/opencord/bbsim/internal/bbsimctl/config"
31 log "github.com/sirupsen/logrus"
32)
33
34const (
35 DEFAULT_OLT_ALARM_LIST_FORMAT = "table{{ .Name }}"
36)
37
38type OltInterfaceStatus string
39
40type OltInterfaceID uint32
41
42type OltAlarmNameString string
43
44type OltAlarmListOutput struct {
45 Name string
46}
47
48type OltAlarmRaise struct {
49 Args struct {
50 Name OltAlarmNameString
51 IntfID OltInterfaceID
52 } `positional-args:"yes" required:"yes"`
53}
54
55type OltAlarmClear struct {
56 Args struct {
57 Name OltAlarmNameString
58 IntfID OltInterfaceID
59 } `positional-args:"yes" required:"yes"`
60}
61
62type OltAlarmList struct{}
63
64type OltAlarmOptions struct {
65 Raise OltAlarmRaise `command:"raise"`
66 Clear OltAlarmClear `command:"clear"`
67 List OltAlarmList `command:"list"`
68}
69
70// Execute alarm raise
71func (o *OltAlarmRaise) Execute(args []string) error {
72 client, conn := connect()
73 defer conn.Close()
74
75 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
76 defer cancel()
77
Pragya Arya996a0892020-03-09 21:47:52 +053078 req := pb.OLTAlarmRequest{
Anand S Katti86552f92020-03-03 21:56:32 +053079 InterfaceID: uint32(o.Args.IntfID),
80 Status: "on"}
81
Kent Hagerman60d62302020-03-10 17:02:36 -040082 if string(o.Args.Name) == common.OltPonLos {
Pragya Arya996a0892020-03-09 21:47:52 +053083 req.InterfaceType = "pon"
Kent Hagerman60d62302020-03-10 17:02:36 -040084 } else if string(o.Args.Name) == common.OltNniLos {
Pragya Arya996a0892020-03-09 21:47:52 +053085 req.InterfaceType = "nni"
86 } else {
Kent Hagerman60d62302020-03-10 17:02:36 -040087 return fmt.Errorf("Unknown OLT alarm type")
Pragya Arya996a0892020-03-09 21:47:52 +053088 }
89
Anand S Katti86552f92020-03-03 21:56:32 +053090 res, err := client.SetOltAlarmIndication(ctx, &req)
91 if err != nil {
92 log.Fatalf("Cannot raise OLT alarm: %v", err)
93 return err
94 }
95
96 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
97 return nil
98}
99
100// Execute alarm clear
101func (o *OltAlarmClear) Execute(args []string) error {
102 client, conn := connect()
103 defer conn.Close()
104
105 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
106 defer cancel()
107
Pragya Arya996a0892020-03-09 21:47:52 +0530108 req := pb.OLTAlarmRequest{
Anand S Katti86552f92020-03-03 21:56:32 +0530109 InterfaceID: uint32(o.Args.IntfID),
110 Status: "off"}
111
Kent Hagerman60d62302020-03-10 17:02:36 -0400112 if string(o.Args.Name) == common.OltPonLos {
Pragya Arya996a0892020-03-09 21:47:52 +0530113 req.InterfaceType = "pon"
Kent Hagerman60d62302020-03-10 17:02:36 -0400114 } else if string(o.Args.Name) == common.OltNniLos {
Pragya Arya996a0892020-03-09 21:47:52 +0530115 req.InterfaceType = "nni"
116 } else {
Kent Hagerman60d62302020-03-10 17:02:36 -0400117 return fmt.Errorf("Unknown OLT alarm type")
Pragya Arya996a0892020-03-09 21:47:52 +0530118 }
119
Anand S Katti86552f92020-03-03 21:56:32 +0530120 res, err := client.SetOltAlarmIndication(ctx, &req)
121
122 if err != nil {
123 log.Fatalf("Cannot clear OLT alarm: %v", err)
124 return err
125 }
126
127 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
128 return nil
129}
130
131// Execute OLT alarm list
132func (o *OltAlarmList) Execute(args []string) error {
133 OltAlarmsValue := [][]string{}
134 OltAlarmstable := tablewriter.NewWriter(os.Stdout)
135 fmt.Fprintf(os.Stdout, "OLT Alarms List:\n")
136 OltAlarmstable.SetHeader([]string{"OLT Alarms"})
137
Kent Hagerman60d62302020-03-10 17:02:36 -0400138 alarmNames := make([]AlarmListOutput, len(common.OLTAlarms))
Anand S Katti86552f92020-03-03 21:56:32 +0530139 i := 0
Kent Hagerman60d62302020-03-10 17:02:36 -0400140 for k := range common.OLTAlarms {
Anand S Katti86552f92020-03-03 21:56:32 +0530141 alarmNames[i] = AlarmListOutput{Name: k}
142 OltAlarmsValue = append(OltAlarmsValue, []string{k})
143 i++
144 }
145 for _, v := range OltAlarmsValue {
146 OltAlarmstable.Append(v)
147 }
148 OltAlarmstable.Render()
149 return nil
150}
151
152func (o *OltAlarmNameString) Complete(match string) []flags.Completion {
153 list := make([]flags.Completion, 0)
Kent Hagerman60d62302020-03-10 17:02:36 -0400154 for k := range common.OLTAlarms {
Anand S Katti86552f92020-03-03 21:56:32 +0530155 if strings.HasPrefix(k, match) {
156 list = append(list, flags.Completion{Item: k})
157 }
158 }
159
160 return list
161}