blob: dcf53a3519d1cd115c54bb90f3f2516c33096b41 [file] [log] [blame]
Scott Baker41724b82020-01-21 19:54:53 -08001/*
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
18package commands
19
20import (
21 "context"
22 "fmt"
Anand S Katti86552f92020-03-03 21:56:32 +053023 "os"
24 "strings"
25
Scott Baker41724b82020-01-21 19:54:53 -080026 "github.com/jessevdk/go-flags"
Anand S Katti86552f92020-03-03 21:56:32 +053027 "github.com/olekukonko/tablewriter"
Scott Baker41724b82020-01-21 19:54:53 -080028 pb "github.com/opencord/bbsim/api/bbsim"
Pragya Arya694ece02020-02-07 13:03:47 +053029 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Matteo Scandolo583f17d2020-02-13 10:35:17 -080030 "github.com/opencord/bbsim/internal/bbsimctl/config"
Scott Baker41724b82020-01-21 19:54:53 -080031 log "github.com/sirupsen/logrus"
Scott Baker41724b82020-01-21 19:54:53 -080032)
33
Scott Baker0c4c3752020-01-23 14:16:24 -080034type AlarmNameString string
35
Scott Baker41724b82020-01-21 19:54:53 -080036type AlarmListOutput struct {
37 Name string
38}
39
40type AlarmRaise struct {
41 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
42 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080043 Name AlarmNameString
44 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080045 } `positional-args:"yes" required:"yes"`
46}
47
48type AlarmClear struct {
49 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
50 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080051 Name AlarmNameString
52 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080053 } `positional-args:"yes" required:"yes"`
54}
55
56type AlarmList struct{}
57
58type AlarmOptions struct {
59 Raise AlarmRaise `command:"raise"`
60 Clear AlarmClear `command:"clear"`
61 List AlarmList `command:"list"`
62}
63
Anand S Katti86552f92020-03-03 21:56:32 +053064// add optional parameters from the command-line to the ONUAlarmRequest
65func addParameters(parameters []string, req *pb.ONUAlarmRequest) error {
Scott Baker41724b82020-01-21 19:54:53 -080066 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 Arya694ece02020-02-07 13:03:47 +053077// Execute alarm raise
Scott Baker41724b82020-01-21 19:54:53 -080078func (o *AlarmRaise) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -080079 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 Katti86552f92020-03-03 21:56:32 +053085 req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -080086 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -080087 Status: "on"}
88
Pragya Arya694ece02020-02-07 13:03:47 +053089 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -080090 if err != nil {
91 return err
92 }
93
Anand S Katti86552f92020-03-03 21:56:32 +053094 res, err := client.SetOnuAlarmIndication(ctx, &req)
Scott Baker41724b82020-01-21 19:54:53 -080095 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 Arya694ece02020-02-07 13:03:47 +0530104// Execute alarm clear
Scott Baker41724b82020-01-21 19:54:53 -0800105func (o *AlarmClear) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -0800106 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 Katti86552f92020-03-03 21:56:32 +0530112 req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -0800113 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -0800114 Status: "off"}
115
Pragya Arya694ece02020-02-07 13:03:47 +0530116 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800117 if err != nil {
118 return err
119 }
120
Anand S Katti86552f92020-03-03 21:56:32 +0530121 res, err := client.SetOnuAlarmIndication(ctx, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800122
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 Katti86552f92020-03-03 21:56:32 +0530132// Execute OLT alarm list
Scott Baker41724b82020-01-21 19:54:53 -0800133func (o *AlarmList) Execute(args []string) error {
Anand S Katti86552f92020-03-03 21:56:32 +0530134 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 Baker41724b82020-01-21 19:54:53 -0800140 i := 0
Anand S Katti86552f92020-03-03 21:56:32 +0530141 for k := range alarmsim.OnuAlarmNameMap {
Scott Baker41724b82020-01-21 19:54:53 -0800142 alarmNames[i] = AlarmListOutput{Name: k}
Anand S Katti86552f92020-03-03 21:56:32 +0530143 OnuAlarmsValue = append(OnuAlarmsValue, []string{k})
Scott Baker41724b82020-01-21 19:54:53 -0800144 i++
145 }
Anand S Katti86552f92020-03-03 21:56:32 +0530146 for _, v := range OnuAlarmsValue {
147 OnuAlarmstable.Append(v)
148 }
149 OnuAlarmstable.Render()
Scott Baker41724b82020-01-21 19:54:53 -0800150 return nil
151}
Scott Baker0c4c3752020-01-23 14:16:24 -0800152
153func (onuSn *AlarmNameString) Complete(match string) []flags.Completion {
154 list := make([]flags.Completion, 0)
Anand S Katti86552f92020-03-03 21:56:32 +0530155 for k := range alarmsim.OnuAlarmNameMap {
Scott Baker0c4c3752020-01-23 14:16:24 -0800156 if strings.HasPrefix(k, match) {
157 list = append(list, flags.Completion{Item: k})
158 }
159 }
160
161 return list
162}