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