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" |
| 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 | |
| 32 | const ( |
| 33 | DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}" |
| 34 | ) |
| 35 | |
| 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 { |
| 43 | Name string |
| 44 | SerialNumber string |
| 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 { |
| 51 | Name string |
| 52 | SerialNumber string |
| 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 | |
| 64 | var AlarmNameMap = map[string]pb.AlarmType_Types{"DyingGasp": pb.AlarmType_DYING_GASP, |
| 65 | "StartupFailure": pb.AlarmType_ONU_STARTUP_FAILURE, |
| 66 | "SignalDegrade": pb.AlarmType_ONU_SIGNAL_DEGRADE, |
| 67 | "DriftOfWindow": pb.AlarmType_ONU_DRIFT_OF_WINDOW, |
| 68 | "LossOfOmciChannel": pb.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL, |
| 69 | "SignalsFailure": pb.AlarmType_ONU_SIGNALS_FAILURE, |
| 70 | "TransmissionInterference": pb.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING, |
| 71 | "ActivationFailure": pb.AlarmType_ONU_ACTIVATION_FAILURE, |
| 72 | "ProcessingError": pb.AlarmType_ONU_PROCESSING_ERROR, |
| 73 | "LossOfKeySyncFailure": pb.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE, |
| 74 | |
| 75 | // Break out OnuAlarm into its subcases. |
| 76 | "LossOfSignal": pb.AlarmType_ONU_ALARM_LOS, |
| 77 | "LossOfBurst": pb.AlarmType_ONU_ALARM_LOB, |
| 78 | "LOPC_MISS": pb.AlarmType_ONU_ALARM_LOPC_MISS, |
| 79 | "LOPC_MIC_ERROR": pb.AlarmType_ONU_ALARM_LOPC_MIC_ERROR, |
| 80 | "LossOfFrame": pb.AlarmType_ONU_ALARM_LOFI, |
| 81 | "LossOfPloam": pb.AlarmType_ONU_ALARM_LOAMI, |
| 82 | |
| 83 | // Whole-PON / Non-onu-specific |
| 84 | "PonLossOfSignal": pb.AlarmType_LOS, |
| 85 | } |
| 86 | |
| 87 | func alarmNameToEnum(name string) (*pb.AlarmType_Types, error) { |
| 88 | v, okay := AlarmNameMap[name] |
| 89 | if !okay { |
| 90 | return nil, fmt.Errorf("Unknown Alarm Name: %v", name) |
| 91 | } |
| 92 | |
| 93 | return &v, nil |
| 94 | } |
| 95 | |
| 96 | // add optional parameters from the command-line to the AlarmRequest |
| 97 | func addParameters(parameters []string, req *pb.AlarmRequest) error { |
| 98 | req.Parameters = make([]*pb.AlarmParameter, len(parameters)) |
| 99 | for i, kv := range parameters { |
| 100 | parts := strings.Split(kv, "=") |
| 101 | if len(parts) != 2 { |
| 102 | return fmt.Errorf("Invalid parameter %v", kv) |
| 103 | } |
| 104 | req.Parameters[i] = &pb.AlarmParameter{Key: parts[0], Value: parts[1]} |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func RegisterAlarmCommands(parser *flags.Parser) { |
| 110 | parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{}) |
| 111 | } |
| 112 | |
| 113 | func (o *AlarmRaise) Execute(args []string) error { |
| 114 | alarmType, err := alarmNameToEnum(o.Args.Name) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | client, conn := connect() |
| 120 | defer conn.Close() |
| 121 | |
| 122 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 123 | defer cancel() |
| 124 | |
| 125 | req := pb.AlarmRequest{AlarmType: *alarmType, |
| 126 | SerialNumber: o.Args.SerialNumber, |
| 127 | Status: "on"} |
| 128 | |
| 129 | err = addParameters(o.Parameters, &req) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
| 134 | res, err := client.SetAlarmIndication(ctx, &req) |
| 135 | |
| 136 | if err != nil { |
| 137 | log.Fatalf("Cannot raise alarm: %v", err) |
| 138 | return err |
| 139 | } |
| 140 | |
| 141 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | func (o *AlarmClear) Execute(args []string) error { |
| 146 | alarmType, err := alarmNameToEnum(o.Args.Name) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | client, conn := connect() |
| 152 | defer conn.Close() |
| 153 | |
| 154 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 155 | defer cancel() |
| 156 | |
| 157 | req := pb.AlarmRequest{AlarmType: *alarmType, |
| 158 | SerialNumber: o.Args.SerialNumber, |
| 159 | Status: "off"} |
| 160 | |
| 161 | err = addParameters(o.Parameters, &req) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | res, err := client.SetAlarmIndication(ctx, &req) |
| 167 | |
| 168 | if err != nil { |
| 169 | log.Fatalf("Cannot clear alarm: %v", err) |
| 170 | return err |
| 171 | } |
| 172 | |
| 173 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | func (o *AlarmList) Execute(args []string) error { |
| 178 | alarmNames := make([]AlarmListOutput, len(AlarmNameMap)) |
| 179 | i := 0 |
| 180 | for k := range AlarmNameMap { |
| 181 | alarmNames[i] = AlarmListOutput{Name: k} |
| 182 | i++ |
| 183 | } |
| 184 | // print out |
| 185 | tableFormat := format.Format(DEFAULT_ALARM_LIST_FORMAT) |
| 186 | tableFormat.Execute(os.Stdout, true, alarmNames) |
| 187 | return nil |
| 188 | } |