Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [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 | "os" |
| 24 | "strings" |
| 25 | |
| 26 | "github.com/jessevdk/go-flags" |
| 27 | "github.com/olekukonko/tablewriter" |
| 28 | pb "github.com/opencord/bbsim/api/bbsim" |
| 29 | "github.com/opencord/bbsim/internal/bbsim/alarmsim" |
| 30 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 31 | log "github.com/sirupsen/logrus" |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | DEFAULT_OLT_ALARM_LIST_FORMAT = "table{{ .Name }}" |
| 36 | ) |
| 37 | |
| 38 | type OltInterfaceStatus string |
| 39 | |
| 40 | type OltInterfaceID uint32 |
| 41 | |
| 42 | type OltAlarmNameString string |
| 43 | |
| 44 | type OltAlarmListOutput struct { |
| 45 | Name string |
| 46 | } |
| 47 | |
| 48 | type OltAlarmRaise struct { |
| 49 | Args struct { |
| 50 | Name OltAlarmNameString |
| 51 | IntfID OltInterfaceID |
| 52 | } `positional-args:"yes" required:"yes"` |
| 53 | } |
| 54 | |
| 55 | type OltAlarmClear struct { |
| 56 | Args struct { |
| 57 | Name OltAlarmNameString |
| 58 | IntfID OltInterfaceID |
| 59 | } `positional-args:"yes" required:"yes"` |
| 60 | } |
| 61 | |
| 62 | type OltAlarmList struct{} |
| 63 | |
| 64 | type OltAlarmOptions struct { |
| 65 | Raise OltAlarmRaise `command:"raise"` |
| 66 | Clear OltAlarmClear `command:"clear"` |
| 67 | List OltAlarmList `command:"list"` |
| 68 | } |
| 69 | |
| 70 | // Execute alarm raise |
| 71 | func (o *OltAlarmRaise) Execute(args []string) error { |
| 72 | client, conn := connect() |
| 73 | defer conn.Close() |
| 74 | |
| 75 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 76 | defer cancel() |
| 77 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 78 | req := pb.OLTAlarmRequest{ |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 79 | InterfaceID: uint32(o.Args.IntfID), |
| 80 | Status: "on"} |
| 81 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 82 | if string(o.Args.Name) == "PonLossOfSignal" { |
| 83 | req.InterfaceType = "pon" |
| 84 | } else if string(o.Args.Name) == "NniLossOfSignal" { |
| 85 | req.InterfaceType = "nni" |
| 86 | } else { |
| 87 | return fmt.Errorf("Unknown alarm type") |
| 88 | } |
| 89 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 90 | res, err := client.SetOltAlarmIndication(ctx, &req) |
| 91 | if err != nil { |
| 92 | log.Fatalf("Cannot raise OLT alarm: %v", err) |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | // Execute alarm clear |
| 101 | func (o *OltAlarmClear) Execute(args []string) error { |
| 102 | client, conn := connect() |
| 103 | defer conn.Close() |
| 104 | |
| 105 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 106 | defer cancel() |
| 107 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 108 | req := pb.OLTAlarmRequest{ |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 109 | InterfaceID: uint32(o.Args.IntfID), |
| 110 | Status: "off"} |
| 111 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 112 | if string(o.Args.Name) == "PonLossOfSignal" { |
| 113 | req.InterfaceType = "pon" |
| 114 | } else if string(o.Args.Name) == "NniLossOfSignal" { |
| 115 | req.InterfaceType = "nni" |
| 116 | } else { |
| 117 | return fmt.Errorf("Unknown alarm type") |
| 118 | } |
| 119 | |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 120 | res, err := client.SetOltAlarmIndication(ctx, &req) |
| 121 | |
| 122 | if err != nil { |
| 123 | log.Fatalf("Cannot clear OLT alarm: %v", err) |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | // Execute OLT alarm list |
| 132 | func (o *OltAlarmList) Execute(args []string) error { |
| 133 | OltAlarmsValue := [][]string{} |
| 134 | OltAlarmstable := tablewriter.NewWriter(os.Stdout) |
| 135 | fmt.Fprintf(os.Stdout, "OLT Alarms List:\n") |
| 136 | OltAlarmstable.SetHeader([]string{"OLT Alarms"}) |
| 137 | |
| 138 | alarmNames := make([]AlarmListOutput, len(alarmsim.OltAlarmNameMap)) |
| 139 | i := 0 |
| 140 | for k := range alarmsim.OltAlarmNameMap { |
| 141 | alarmNames[i] = AlarmListOutput{Name: k} |
| 142 | OltAlarmsValue = append(OltAlarmsValue, []string{k}) |
| 143 | i++ |
| 144 | } |
| 145 | for _, v := range OltAlarmsValue { |
| 146 | OltAlarmstable.Append(v) |
| 147 | } |
| 148 | OltAlarmstable.Render() |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | func (o *OltAlarmNameString) Complete(match string) []flags.Completion { |
| 153 | list := make([]flags.Completion, 0) |
| 154 | for k := range alarmsim.OltAlarmNameMap { |
| 155 | if strings.HasPrefix(k, match) { |
| 156 | list = append(list, flags.Completion{Item: k}) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return list |
| 161 | } |