Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 4d6878e | 2024-02-08 16:29:26 -0500 | [diff] [blame] | 2 | * Portions Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 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" |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 23 | "os" |
| 24 | "strings" |
| 25 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 26 | "github.com/opencord/bbsim/internal/common" |
| 27 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 28 | "github.com/jessevdk/go-flags" |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 29 | "github.com/olekukonko/tablewriter" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 30 | pb "github.com/opencord/bbsim/api/bbsim" |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 31 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 32 | log "github.com/sirupsen/logrus" |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 33 | ) |
| 34 | |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 35 | type AlarmNameString string |
| 36 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 37 | type AlarmListOutput struct { |
| 38 | Name string |
| 39 | } |
| 40 | |
| 41 | type AlarmRaise struct { |
| 42 | Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"` |
| 43 | Args struct { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 44 | Name AlarmNameString |
| 45 | SerialNumber OnuSnString |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 46 | } `positional-args:"yes" required:"yes"` |
| 47 | } |
| 48 | |
| 49 | type AlarmClear struct { |
| 50 | Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"` |
| 51 | Args struct { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 52 | Name AlarmNameString |
| 53 | SerialNumber OnuSnString |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 54 | } `positional-args:"yes" required:"yes"` |
| 55 | } |
| 56 | |
| 57 | type AlarmList struct{} |
| 58 | |
| 59 | type AlarmOptions struct { |
| 60 | Raise AlarmRaise `command:"raise"` |
| 61 | Clear AlarmClear `command:"clear"` |
| 62 | List AlarmList `command:"list"` |
| 63 | } |
| 64 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 65 | // add optional parameters from the command-line to the ONUAlarmRequest |
| 66 | func addParameters(parameters []string, req *pb.ONUAlarmRequest) error { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 67 | req.Parameters = make([]*pb.AlarmParameter, len(parameters)) |
| 68 | for i, kv := range parameters { |
| 69 | parts := strings.Split(kv, "=") |
| 70 | if len(parts) != 2 { |
| 71 | return fmt.Errorf("Invalid parameter %v", kv) |
| 72 | } |
| 73 | req.Parameters[i] = &pb.AlarmParameter{Key: parts[0], Value: parts[1]} |
| 74 | } |
| 75 | return nil |
| 76 | } |
| 77 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 78 | // Execute alarm raise |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 79 | func (o *AlarmRaise) Execute(args []string) error { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 80 | client, conn := connect() |
| 81 | defer conn.Close() |
| 82 | |
| 83 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 84 | defer cancel() |
| 85 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 86 | req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name), |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 87 | SerialNumber: string(o.Args.SerialNumber), |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 88 | Status: "on"} |
| 89 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 90 | err := addParameters(o.Parameters, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 95 | res, err := client.SetOnuAlarmIndication(ctx, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 96 | if err != nil { |
| 97 | log.Fatalf("Cannot raise alarm: %v", err) |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 102 | return nil |
| 103 | } |
| 104 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 105 | // Execute alarm clear |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 106 | func (o *AlarmClear) Execute(args []string) error { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 107 | client, conn := connect() |
| 108 | defer conn.Close() |
| 109 | |
| 110 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 111 | defer cancel() |
| 112 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 113 | req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name), |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 114 | SerialNumber: string(o.Args.SerialNumber), |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 115 | Status: "off"} |
| 116 | |
Pragya Arya | 694ece0 | 2020-02-07 13:03:47 +0530 | [diff] [blame] | 117 | err := addParameters(o.Parameters, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 122 | res, err := client.SetOnuAlarmIndication(ctx, &req) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 123 | |
| 124 | if err != nil { |
| 125 | log.Fatalf("Cannot clear alarm: %v", err) |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 130 | return nil |
| 131 | } |
| 132 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 133 | // Execute ONU alarm list |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 134 | func (o *AlarmList) Execute(args []string) error { |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 135 | OnuAlarmsValue := [][]string{} |
| 136 | OnuAlarmstable := tablewriter.NewWriter(os.Stdout) |
| 137 | fmt.Fprintf(os.Stdout, "ONU Alarms List:\n") |
| 138 | OnuAlarmstable.SetHeader([]string{"ONU Alarms"}) |
| 139 | |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 140 | alarmNames := make([]AlarmListOutput, len(common.ONUAlarms)) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 141 | i := 0 |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 142 | for k := range common.ONUAlarms { |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 143 | alarmNames[i] = AlarmListOutput{Name: k} |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 144 | OnuAlarmsValue = append(OnuAlarmsValue, []string{k}) |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 145 | i++ |
| 146 | } |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 147 | for _, v := range OnuAlarmsValue { |
| 148 | OnuAlarmstable.Append(v) |
| 149 | } |
| 150 | OnuAlarmstable.Render() |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 151 | return nil |
| 152 | } |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 153 | |
| 154 | func (onuSn *AlarmNameString) Complete(match string) []flags.Completion { |
| 155 | list := make([]flags.Completion, 0) |
Kent Hagerman | 60d6230 | 2020-03-10 17:02:36 -0400 | [diff] [blame] | 156 | for k := range common.ONUAlarms { |
Scott Baker | 0c4c375 | 2020-01-23 14:16:24 -0800 | [diff] [blame] | 157 | if strings.HasPrefix(k, match) { |
| 158 | list = append(list, flags.Completion{Item: k}) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return list |
| 163 | } |