blob: eb4813cf5f90bf3b39d6da21ce3ea62cd537c018 [file] [log] [blame]
Scott Baker41724b82020-01-21 19:54:53 -08001/*
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"
22 "fmt"
23 "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 "os"
29 "strings"
30)
31
32const (
33 DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}"
34)
35
Scott Baker0c4c3752020-01-23 14:16:24 -080036type AlarmNameString string
37
Scott Baker41724b82020-01-21 19:54:53 -080038type AlarmListOutput struct {
39 Name string
40}
41
42type AlarmRaise struct {
43 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
44 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080045 Name AlarmNameString
46 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080047 } `positional-args:"yes" required:"yes"`
48}
49
50type AlarmClear struct {
51 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
52 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080053 Name AlarmNameString
54 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080055 } `positional-args:"yes" required:"yes"`
56}
57
58type AlarmList struct{}
59
60type AlarmOptions struct {
61 Raise AlarmRaise `command:"raise"`
62 Clear AlarmClear `command:"clear"`
63 List AlarmList `command:"list"`
64}
65
66var AlarmNameMap = map[string]pb.AlarmType_Types{"DyingGasp": pb.AlarmType_DYING_GASP,
67 "StartupFailure": pb.AlarmType_ONU_STARTUP_FAILURE,
68 "SignalDegrade": pb.AlarmType_ONU_SIGNAL_DEGRADE,
69 "DriftOfWindow": pb.AlarmType_ONU_DRIFT_OF_WINDOW,
70 "LossOfOmciChannel": pb.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL,
71 "SignalsFailure": pb.AlarmType_ONU_SIGNALS_FAILURE,
72 "TransmissionInterference": pb.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING,
73 "ActivationFailure": pb.AlarmType_ONU_ACTIVATION_FAILURE,
74 "ProcessingError": pb.AlarmType_ONU_PROCESSING_ERROR,
75 "LossOfKeySyncFailure": pb.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE,
76
77 // Break out OnuAlarm into its subcases.
78 "LossOfSignal": pb.AlarmType_ONU_ALARM_LOS,
79 "LossOfBurst": pb.AlarmType_ONU_ALARM_LOB,
80 "LOPC_MISS": pb.AlarmType_ONU_ALARM_LOPC_MISS,
81 "LOPC_MIC_ERROR": pb.AlarmType_ONU_ALARM_LOPC_MIC_ERROR,
82 "LossOfFrame": pb.AlarmType_ONU_ALARM_LOFI,
83 "LossOfPloam": pb.AlarmType_ONU_ALARM_LOAMI,
84
85 // Whole-PON / Non-onu-specific
86 "PonLossOfSignal": pb.AlarmType_LOS,
87}
88
89func alarmNameToEnum(name string) (*pb.AlarmType_Types, error) {
90 v, okay := AlarmNameMap[name]
91 if !okay {
92 return nil, fmt.Errorf("Unknown Alarm Name: %v", name)
93 }
94
95 return &v, nil
96}
97
98// add optional parameters from the command-line to the AlarmRequest
99func addParameters(parameters []string, req *pb.AlarmRequest) error {
100 req.Parameters = make([]*pb.AlarmParameter, len(parameters))
101 for i, kv := range parameters {
102 parts := strings.Split(kv, "=")
103 if len(parts) != 2 {
104 return fmt.Errorf("Invalid parameter %v", kv)
105 }
106 req.Parameters[i] = &pb.AlarmParameter{Key: parts[0], Value: parts[1]}
107 }
108 return nil
109}
110
111func RegisterAlarmCommands(parser *flags.Parser) {
112 parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{})
113}
114
115func (o *AlarmRaise) Execute(args []string) error {
Scott Baker0c4c3752020-01-23 14:16:24 -0800116 alarmType, err := alarmNameToEnum(string(o.Args.Name))
Scott Baker41724b82020-01-21 19:54:53 -0800117 if err != nil {
118 return err
119 }
120
121 client, conn := connect()
122 defer conn.Close()
123
124 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
125 defer cancel()
126
127 req := pb.AlarmRequest{AlarmType: *alarmType,
Scott Baker0c4c3752020-01-23 14:16:24 -0800128 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -0800129 Status: "on"}
130
131 err = addParameters(o.Parameters, &req)
132 if err != nil {
133 return err
134 }
135
136 res, err := client.SetAlarmIndication(ctx, &req)
137
138 if err != nil {
139 log.Fatalf("Cannot raise alarm: %v", err)
140 return err
141 }
142
143 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
144 return nil
145}
146
147func (o *AlarmClear) Execute(args []string) error {
Scott Baker0c4c3752020-01-23 14:16:24 -0800148 alarmType, err := alarmNameToEnum(string(o.Args.Name))
Scott Baker41724b82020-01-21 19:54:53 -0800149 if err != nil {
150 return err
151 }
152
153 client, conn := connect()
154 defer conn.Close()
155
156 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
157 defer cancel()
158
159 req := pb.AlarmRequest{AlarmType: *alarmType,
Scott Baker0c4c3752020-01-23 14:16:24 -0800160 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -0800161 Status: "off"}
162
163 err = addParameters(o.Parameters, &req)
164 if err != nil {
165 return err
166 }
167
168 res, err := client.SetAlarmIndication(ctx, &req)
169
170 if err != nil {
171 log.Fatalf("Cannot clear alarm: %v", err)
172 return err
173 }
174
175 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
176 return nil
177}
178
179func (o *AlarmList) Execute(args []string) error {
180 alarmNames := make([]AlarmListOutput, len(AlarmNameMap))
181 i := 0
182 for k := range AlarmNameMap {
183 alarmNames[i] = AlarmListOutput{Name: k}
184 i++
185 }
186 // print out
187 tableFormat := format.Format(DEFAULT_ALARM_LIST_FORMAT)
188 tableFormat.Execute(os.Stdout, true, alarmNames)
189 return nil
190}
Scott Baker0c4c3752020-01-23 14:16:24 -0800191
192func (onuSn *AlarmNameString) Complete(match string) []flags.Completion {
193 list := make([]flags.Completion, 0)
194 for k := range AlarmNameMap {
195 if strings.HasPrefix(k, match) {
196 list = append(list, flags.Completion{Item: k})
197 }
198 }
199
200 return list
201}