blob: 645aade32d8fcb234917ef6c2644e5dee34c8745 [file] [log] [blame]
Anand S Katti86552f92020-03-03 21:56:32 +05301/*
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"
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
34const (
35 DEFAULT_OLT_ALARM_LIST_FORMAT = "table{{ .Name }}"
36)
37
38type OltInterfaceStatus string
39
40type OltInterfaceID uint32
41
42type OltAlarmNameString string
43
44type OltAlarmListOutput struct {
45 Name string
46}
47
48type OltAlarmRaise struct {
49 Args struct {
50 Name OltAlarmNameString
51 IntfID OltInterfaceID
52 } `positional-args:"yes" required:"yes"`
53}
54
55type OltAlarmClear struct {
56 Args struct {
57 Name OltAlarmNameString
58 IntfID OltInterfaceID
59 } `positional-args:"yes" required:"yes"`
60}
61
62type OltAlarmList struct{}
63
64type OltAlarmOptions struct {
65 Raise OltAlarmRaise `command:"raise"`
66 Clear OltAlarmClear `command:"clear"`
67 List OltAlarmList `command:"list"`
68}
69
70// Execute alarm raise
71func (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
78 req := pb.OLTAlarmRequest{InterfaceType: string(o.Args.Name),
79 InterfaceID: uint32(o.Args.IntfID),
80 Status: "on"}
81
82 res, err := client.SetOltAlarmIndication(ctx, &req)
83 if err != nil {
84 log.Fatalf("Cannot raise OLT alarm: %v", err)
85 return err
86 }
87
88 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
89 return nil
90}
91
92// Execute alarm clear
93func (o *OltAlarmClear) Execute(args []string) error {
94 client, conn := connect()
95 defer conn.Close()
96
97 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
98 defer cancel()
99
100 req := pb.OLTAlarmRequest{InterfaceType: string(o.Args.Name),
101 InterfaceID: uint32(o.Args.IntfID),
102 Status: "off"}
103
104 res, err := client.SetOltAlarmIndication(ctx, &req)
105
106 if err != nil {
107 log.Fatalf("Cannot clear OLT alarm: %v", err)
108 return err
109 }
110
111 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
112 return nil
113}
114
115// Execute OLT alarm list
116func (o *OltAlarmList) Execute(args []string) error {
117 OltAlarmsValue := [][]string{}
118 OltAlarmstable := tablewriter.NewWriter(os.Stdout)
119 fmt.Fprintf(os.Stdout, "OLT Alarms List:\n")
120 OltAlarmstable.SetHeader([]string{"OLT Alarms"})
121
122 alarmNames := make([]AlarmListOutput, len(alarmsim.OltAlarmNameMap))
123 i := 0
124 for k := range alarmsim.OltAlarmNameMap {
125 alarmNames[i] = AlarmListOutput{Name: k}
126 OltAlarmsValue = append(OltAlarmsValue, []string{k})
127 i++
128 }
129 for _, v := range OltAlarmsValue {
130 OltAlarmstable.Append(v)
131 }
132 OltAlarmstable.Render()
133 return nil
134}
135
136func (o *OltAlarmNameString) Complete(match string) []flags.Completion {
137 list := make([]flags.Completion, 0)
138 for k := range alarmsim.OltAlarmNameMap {
139 if strings.HasPrefix(k, match) {
140 list = append(list, flags.Completion{Item: k})
141 }
142 }
143
144 return list
145}