Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package commands |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "fmt" |
| 23 | "github.com/jessevdk/go-flags" |
| 24 | pb "github.com/opencord/bbsim/api/bbsim" |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 25 | "github.com/opencord/bbsim/internal/bbsim/alarmsim" |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 26 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 27 | "github.com/opencord/cordctl/pkg/format" |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | "os" |
| 30 | "strings" |
| 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}" |
| 35 | ) |
| 36 | |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 37 | type AlarmNameString string |
| 38 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 39 | type AlarmListOutput struct { |
| 40 | Name string |
| 41 | } |
| 42 | |
| 43 | type AlarmRaise struct { |
| 44 | Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"` |
| 45 | Args struct { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 46 | Name AlarmNameString |
| 47 | SerialNumber OnuSnString |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 48 | } `positional-args:"yes" required:"yes"` |
| 49 | } |
| 50 | |
| 51 | type AlarmClear struct { |
| 52 | Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"` |
| 53 | Args struct { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 54 | Name AlarmNameString |
| 55 | SerialNumber OnuSnString |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 56 | } `positional-args:"yes" required:"yes"` |
| 57 | } |
| 58 | |
| 59 | type AlarmList struct{} |
| 60 | |
| 61 | type AlarmOptions struct { |
| 62 | Raise AlarmRaise `command:"raise"` |
| 63 | Clear AlarmClear `command:"clear"` |
| 64 | List AlarmList `command:"list"` |
| 65 | } |
| 66 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 67 | // add optional parameters from the command-line to the AlarmRequest |
| 68 | func 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 | |
| 80 | func RegisterAlarmCommands(parser *flags.Parser) { |
| 81 | parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{}) |
| 82 | } |
| 83 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 84 | // Execute alarm raise |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 85 | func (o *AlarmRaise) Execute(args []string) error { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 86 | client, conn := connect() |
| 87 | defer conn.Close() |
| 88 | |
| 89 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 90 | defer cancel() |
| 91 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 92 | req := pb.AlarmRequest{AlarmType: string(o.Args.Name), |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 93 | SerialNumber: string(o.Args.SerialNumber), |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 94 | Status: "on"} |
| 95 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 96 | err := addParameters(o.Parameters, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | res, err := client.SetAlarmIndication(ctx, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 102 | 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 Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 111 | // Execute alarm clear |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 112 | func (o *AlarmClear) Execute(args []string) error { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 113 | client, conn := connect() |
| 114 | defer conn.Close() |
| 115 | |
| 116 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 117 | defer cancel() |
| 118 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 119 | req := pb.AlarmRequest{AlarmType: string(o.Args.Name), |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 120 | SerialNumber: string(o.Args.SerialNumber), |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 121 | Status: "off"} |
| 122 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 123 | err := addParameters(o.Parameters, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 124 | 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 Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 139 | // Execute alarm list |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 140 | func (o *AlarmList) Execute(args []string) error { |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 141 | alarmNames := make([]AlarmListOutput, len(alarmsim.AlarmNameMap)) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 142 | i := 0 |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 143 | for k := range alarmsim.AlarmNameMap { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 144 | 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 Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 152 | |
| 153 | func (onuSn *AlarmNameString) Complete(match string) []flags.Completion { |
| 154 | list := make([]flags.Completion, 0) |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 155 | for k := range alarmsim.AlarmNameMap { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 156 | if strings.HasPrefix(k, match) { |
| 157 | list = append(list, flags.Completion{Item: k}) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return list |
| 162 | } |