blob: 044d74a380b29bdc06755422353cab518bff60ba [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"
Pragya Arya694ece02020-02-07 13:03:47 +053025 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Matteo Scandolo583f17d2020-02-13 10:35:17 -080026 "github.com/opencord/bbsim/internal/bbsimctl/config"
Scott Baker41724b82020-01-21 19:54:53 -080027 "github.com/opencord/cordctl/pkg/format"
28 log "github.com/sirupsen/logrus"
29 "os"
30 "strings"
31)
32
33const (
34 DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}"
35)
36
Scott Baker0c4c3752020-01-23 14:16:24 -080037type AlarmNameString string
38
Scott Baker41724b82020-01-21 19:54:53 -080039type AlarmListOutput struct {
40 Name string
41}
42
43type AlarmRaise struct {
44 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
45 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080046 Name AlarmNameString
47 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080048 } `positional-args:"yes" required:"yes"`
49}
50
51type AlarmClear struct {
52 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
53 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080054 Name AlarmNameString
55 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080056 } `positional-args:"yes" required:"yes"`
57}
58
59type AlarmList struct{}
60
61type AlarmOptions struct {
62 Raise AlarmRaise `command:"raise"`
63 Clear AlarmClear `command:"clear"`
64 List AlarmList `command:"list"`
65}
66
Scott Baker41724b82020-01-21 19:54:53 -080067// add optional parameters from the command-line to the AlarmRequest
68func addParameters(parameters []string, req *pb.AlarmRequest) error {
69 req.Parameters = make([]*pb.AlarmParameter, len(parameters))
70 for i, kv := range parameters {
71 parts := strings.Split(kv, "=")
72 if len(parts) != 2 {
73 return fmt.Errorf("Invalid parameter %v", kv)
74 }
75 req.Parameters[i] = &pb.AlarmParameter{Key: parts[0], Value: parts[1]}
76 }
77 return nil
78}
79
80func RegisterAlarmCommands(parser *flags.Parser) {
81 parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{})
82}
83
Pragya Arya694ece02020-02-07 13:03:47 +053084// Execute alarm raise
Scott Baker41724b82020-01-21 19:54:53 -080085func (o *AlarmRaise) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -080086 client, conn := connect()
87 defer conn.Close()
88
89 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
90 defer cancel()
91
Pragya Arya694ece02020-02-07 13:03:47 +053092 req := pb.AlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -080093 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -080094 Status: "on"}
95
Pragya Arya694ece02020-02-07 13:03:47 +053096 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -080097 if err != nil {
98 return err
99 }
100
101 res, err := client.SetAlarmIndication(ctx, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800102 if err != nil {
103 log.Fatalf("Cannot raise alarm: %v", err)
104 return err
105 }
106
107 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
108 return nil
109}
110
Pragya Arya694ece02020-02-07 13:03:47 +0530111// Execute alarm clear
Scott Baker41724b82020-01-21 19:54:53 -0800112func (o *AlarmClear) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -0800113 client, conn := connect()
114 defer conn.Close()
115
116 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
117 defer cancel()
118
Pragya Arya694ece02020-02-07 13:03:47 +0530119 req := pb.AlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -0800120 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -0800121 Status: "off"}
122
Pragya Arya694ece02020-02-07 13:03:47 +0530123 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800124 if err != nil {
125 return err
126 }
127
128 res, err := client.SetAlarmIndication(ctx, &req)
129
130 if err != nil {
131 log.Fatalf("Cannot clear alarm: %v", err)
132 return err
133 }
134
135 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
136 return nil
137}
138
Pragya Arya694ece02020-02-07 13:03:47 +0530139// Execute alarm list
Scott Baker41724b82020-01-21 19:54:53 -0800140func (o *AlarmList) Execute(args []string) error {
Pragya Arya694ece02020-02-07 13:03:47 +0530141 alarmNames := make([]AlarmListOutput, len(alarmsim.AlarmNameMap))
Scott Baker41724b82020-01-21 19:54:53 -0800142 i := 0
Pragya Arya694ece02020-02-07 13:03:47 +0530143 for k := range alarmsim.AlarmNameMap {
Scott Baker41724b82020-01-21 19:54:53 -0800144 alarmNames[i] = AlarmListOutput{Name: k}
145 i++
146 }
147 // print out
148 tableFormat := format.Format(DEFAULT_ALARM_LIST_FORMAT)
149 tableFormat.Execute(os.Stdout, true, alarmNames)
150 return nil
151}
Scott Baker0c4c3752020-01-23 14:16:24 -0800152
153func (onuSn *AlarmNameString) Complete(match string) []flags.Completion {
154 list := make([]flags.Completion, 0)
Pragya Arya694ece02020-02-07 13:03:47 +0530155 for k := range alarmsim.AlarmNameMap {
Scott Baker0c4c3752020-01-23 14:16:24 -0800156 if strings.HasPrefix(k, match) {
157 list = append(list, flags.Completion{Item: k})
158 }
159 }
160
161 return list
162}