blob: 669c173832a5b2d5e4bf2d7cd2f49f5956985131 [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
hkouser24361d42020-12-14 19:21:47 +053026 "github.com/opencord/bbsim/internal/common"
27
Scott Baker41724b82020-01-21 19:54:53 -080028 "github.com/jessevdk/go-flags"
Anand S Katti86552f92020-03-03 21:56:32 +053029 "github.com/olekukonko/tablewriter"
Scott Baker41724b82020-01-21 19:54:53 -080030 pb "github.com/opencord/bbsim/api/bbsim"
Matteo Scandolo583f17d2020-02-13 10:35:17 -080031 "github.com/opencord/bbsim/internal/bbsimctl/config"
Scott Baker41724b82020-01-21 19:54:53 -080032 log "github.com/sirupsen/logrus"
Scott Baker41724b82020-01-21 19:54:53 -080033)
34
Scott Baker0c4c3752020-01-23 14:16:24 -080035type AlarmNameString string
36
Scott Baker41724b82020-01-21 19:54:53 -080037type AlarmListOutput struct {
38 Name string
39}
40
41type AlarmRaise struct {
42 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
43 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080044 Name AlarmNameString
45 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080046 } `positional-args:"yes" required:"yes"`
47}
48
49type AlarmClear struct {
50 Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
51 Args struct {
Scott Baker0c4c3752020-01-23 14:16:24 -080052 Name AlarmNameString
53 SerialNumber OnuSnString
Scott Baker41724b82020-01-21 19:54:53 -080054 } `positional-args:"yes" required:"yes"`
55}
56
57type AlarmList struct{}
58
59type AlarmOptions struct {
60 Raise AlarmRaise `command:"raise"`
61 Clear AlarmClear `command:"clear"`
62 List AlarmList `command:"list"`
63}
64
Anand S Katti86552f92020-03-03 21:56:32 +053065// add optional parameters from the command-line to the ONUAlarmRequest
66func addParameters(parameters []string, req *pb.ONUAlarmRequest) error {
Scott Baker41724b82020-01-21 19:54:53 -080067 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 Arya694ece02020-02-07 13:03:47 +053078// Execute alarm raise
Scott Baker41724b82020-01-21 19:54:53 -080079func (o *AlarmRaise) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -080080 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 Katti86552f92020-03-03 21:56:32 +053086 req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -080087 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -080088 Status: "on"}
89
Pragya Arya694ece02020-02-07 13:03:47 +053090 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -080091 if err != nil {
92 return err
93 }
94
Anand S Katti86552f92020-03-03 21:56:32 +053095 res, err := client.SetOnuAlarmIndication(ctx, &req)
Scott Baker41724b82020-01-21 19:54:53 -080096 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 Arya694ece02020-02-07 13:03:47 +0530105// Execute alarm clear
Scott Baker41724b82020-01-21 19:54:53 -0800106func (o *AlarmClear) Execute(args []string) error {
Scott Baker41724b82020-01-21 19:54:53 -0800107 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 Katti86552f92020-03-03 21:56:32 +0530113 req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
Scott Baker0c4c3752020-01-23 14:16:24 -0800114 SerialNumber: string(o.Args.SerialNumber),
Scott Baker41724b82020-01-21 19:54:53 -0800115 Status: "off"}
116
Pragya Arya694ece02020-02-07 13:03:47 +0530117 err := addParameters(o.Parameters, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800118 if err != nil {
119 return err
120 }
121
Anand S Katti86552f92020-03-03 21:56:32 +0530122 res, err := client.SetOnuAlarmIndication(ctx, &req)
Scott Baker41724b82020-01-21 19:54:53 -0800123
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 Hagerman60d62302020-03-10 17:02:36 -0400133// Execute ONU alarm list
Scott Baker41724b82020-01-21 19:54:53 -0800134func (o *AlarmList) Execute(args []string) error {
Anand S Katti86552f92020-03-03 21:56:32 +0530135 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 Hagerman60d62302020-03-10 17:02:36 -0400140 alarmNames := make([]AlarmListOutput, len(common.ONUAlarms))
Scott Baker41724b82020-01-21 19:54:53 -0800141 i := 0
Kent Hagerman60d62302020-03-10 17:02:36 -0400142 for k := range common.ONUAlarms {
Scott Baker41724b82020-01-21 19:54:53 -0800143 alarmNames[i] = AlarmListOutput{Name: k}
Anand S Katti86552f92020-03-03 21:56:32 +0530144 OnuAlarmsValue = append(OnuAlarmsValue, []string{k})
Scott Baker41724b82020-01-21 19:54:53 -0800145 i++
146 }
Anand S Katti86552f92020-03-03 21:56:32 +0530147 for _, v := range OnuAlarmsValue {
148 OnuAlarmstable.Append(v)
149 }
150 OnuAlarmstable.Render()
Scott Baker41724b82020-01-21 19:54:53 -0800151 return nil
152}
Scott Baker0c4c3752020-01-23 14:16:24 -0800153
154func (onuSn *AlarmNameString) Complete(match string) []flags.Completion {
155 list := make([]flags.Completion, 0)
Kent Hagerman60d62302020-03-10 17:02:36 -0400156 for k := range common.ONUAlarms {
Scott Baker0c4c3752020-01-23 14:16:24 -0800157 if strings.HasPrefix(k, match) {
158 list = append(list, flags.Completion{Item: k})
159 }
160 }
161
162 return list
163}