blob: f251b8355ee1bc3544b55f5cd1e06a564bfc56eb [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
Pragya Arya996a0892020-03-09 21:47:52 +053078 req := pb.OLTAlarmRequest{
Anand S Katti86552f92020-03-03 21:56:32 +053079 InterfaceID: uint32(o.Args.IntfID),
80 Status: "on"}
81
Pragya Arya996a0892020-03-09 21:47:52 +053082 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 Katti86552f92020-03-03 21:56:32 +053090 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
101func (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 Arya996a0892020-03-09 21:47:52 +0530108 req := pb.OLTAlarmRequest{
Anand S Katti86552f92020-03-03 21:56:32 +0530109 InterfaceID: uint32(o.Args.IntfID),
110 Status: "off"}
111
Pragya Arya996a0892020-03-09 21:47:52 +0530112 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 Katti86552f92020-03-03 21:56:32 +0530120 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
132func (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
152func (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}