blob: 5f6747e7f3dc1f4ed2740c1184747dede236791d [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
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"
Pragya Arya8bdb4532020-03-02 17:08:09 +053023 "os"
24 "strconv"
25
Matteo Scandolo8df63df2019-09-12 10:34:32 -070026 "github.com/jessevdk/go-flags"
Pragya Arya8bdb4532020-03-02 17:08:09 +053027 "github.com/olekukonko/tablewriter"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070028 pb "github.com/opencord/bbsim/api/bbsim"
29 "github.com/opencord/bbsim/internal/bbsimctl/config"
30 "github.com/opencord/cordctl/pkg/format"
31 log "github.com/sirupsen/logrus"
32 "google.golang.org/grpc"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070033)
34
35const (
rajeshf921f882020-03-06 18:24:28 +053036 DEFAULT_OLT_DEVICE_HEADER_FORMAT = "table{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .IP }}"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070037 DEFAULT_PORT_HEADER_FORMAT = "table{{ .ID }}\t{{ .OperState }}"
38)
39
40type OltGet struct{}
41
42type OltNNIs struct{}
43
44type OltPONs struct{}
45
Zdravko Bozakov681364d2019-11-10 14:28:46 +010046type OltShutdown struct{}
47
48type OltPoweron struct{}
49
50type OltReboot struct{}
51
Pragya Arya8bdb4532020-03-02 17:08:09 +053052type OltFlows struct{}
53
Pragya Aryabd731ec2020-02-11 16:38:17 +053054type OltPoweronAllOnus struct{}
55
56type OltShutdownAllOnus struct{}
57
Matteo Scandolo8df63df2019-09-12 10:34:32 -070058type oltOptions struct {
Pragya Aryabd731ec2020-02-11 16:38:17 +053059 Get OltGet `command:"get"`
60 NNI OltNNIs `command:"nnis"`
61 PON OltPONs `command:"pons"`
62 Shutdown OltShutdown `command:"shutdown"`
63 Poweron OltPoweron `command:"poweron"`
64 Reboot OltReboot `command:"reboot"`
65 Alarms OltAlarmOptions `command:"alarms"`
66 Flows OltFlows `command:"flows"`
67 PoweronAllOnus OltPoweronAllOnus `command:"poweronAllONUs"`
68 ShutdownAllOnus OltShutdownAllOnus `command:"shutdownAllONUs"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070069}
70
71func RegisterOltCommands(parser *flags.Parser) {
Shrey Baid688b4242020-07-10 20:40:10 +053072 _, _ = parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -070073}
74
75func getOLT() *pb.Olt {
76 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
77 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070078 log.Fatalf("did not connect: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070079 return nil
80 }
81 defer conn.Close()
82 c := pb.NewBBSimClient(conn)
83
84 // Contact the server and print out its response.
85
86 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
87 defer cancel()
88 olt, err := c.GetOlt(ctx, &pb.Empty{})
89 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070090 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070091 return nil
92 }
93 return olt
94}
95
96func printOltHeader(prefix string, o *pb.Olt) {
97 fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber))
98 fmt.Println()
99}
100
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700101func (o *OltGet) Execute(args []string) error {
102 olt := getOLT()
103
104 // print out
105 tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530106 _ = tableFormat.Execute(os.Stdout, true, olt)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700107
108 return nil
109}
110
111func (o *OltNNIs) Execute(args []string) error {
112 olt := getOLT()
113
114 printOltHeader("NNI Ports for", olt)
115
116 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530117 _ = tableFormat.Execute(os.Stdout, true, olt.NNIPorts)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700118
119 return nil
120}
121
122func (o *OltPONs) Execute(args []string) error {
123 olt := getOLT()
124
125 printOltHeader("PON Ports for", olt)
126
127 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
Shrey Baid688b4242020-07-10 20:40:10 +0530128 _ = tableFormat.Execute(os.Stdout, true, olt.PONPorts)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700129
130 return nil
131}
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100132
133func (o *OltShutdown) Execute(args []string) error {
134 client, conn := connect()
135 defer conn.Close()
136
137 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
138 defer cancel()
139
140 res, err := client.ShutdownOlt(ctx, &pb.Empty{})
141
142 if err != nil {
143 log.Fatalf("Cannot shut down OLT: %v", err)
144 return err
145 }
146
147 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
148 return nil
149}
150
151func (o *OltPoweron) Execute(args []string) error {
152 client, conn := connect()
153 defer conn.Close()
154
155 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
156 defer cancel()
157
158 res, err := client.PoweronOlt(ctx, &pb.Empty{})
159
160 if err != nil {
161 log.Fatalf("Cannot power on OLT: %v", err)
162 return err
163 }
164
165 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
166 return nil
167}
168
169func (o *OltReboot) Execute(args []string) error {
170 client, conn := connect()
171 defer conn.Close()
172
173 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
174 defer cancel()
175
176 res, err := client.RebootOlt(ctx, &pb.Empty{})
177
178 if err != nil {
179 log.Fatalf("Cannot reboot OLT: %v", err)
180 return err
181 }
182
183 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
184 return nil
185}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530186
187func (o *OltFlows) Execute(args []string) error {
188 client, conn := connect()
189 defer conn.Close()
190
191 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
192 defer cancel()
193
194 req := pb.ONURequest{}
195 res, err := client.GetFlows(ctx, &req)
196 if err != nil {
197 log.Errorf("Cannot get flows for OLT: %v", err)
198 return err
199 }
200
201 if res.Flows == nil {
202 fmt.Println("OLT has no flows")
203 return nil
204 }
205
206 flowHeader := []string{
207 "access_intf_id",
208 "onu_id",
209 "uni_id",
210 "flow_id",
211 "flow_type",
212 "eth_type",
213 "alloc_id",
214 "network_intf_id",
215 "gemport_id",
216 "classifier",
217 "action",
218 "priority",
219 "cookie",
220 "port_no",
221 }
222
223 tableFlow := tablewriter.NewWriter(os.Stdout)
224 tableFlow.SetRowLine(true)
225 fmt.Fprintf(os.Stdout, "OLT Flows:\n")
226 tableFlow.SetHeader(flowHeader)
227
228 for _, flow := range res.Flows {
229 flowInfo := []string{}
230 flowInfo = append(flowInfo,
231 strconv.Itoa(int(flow.AccessIntfId)),
232 strconv.Itoa(int(flow.OnuId)),
233 strconv.Itoa(int(flow.UniId)),
234 strconv.Itoa(int(flow.FlowId)),
235 flow.FlowType,
236 fmt.Sprintf("%x", flow.Classifier.EthType),
237 strconv.Itoa(int(flow.AllocId)),
238 strconv.Itoa(int(flow.NetworkIntfId)),
239 strconv.Itoa(int(flow.GemportId)),
240 flow.Classifier.String(),
241 flow.Action.String(),
242 strconv.Itoa(int(flow.Priority)),
243 strconv.Itoa(int(flow.Cookie)),
244 strconv.Itoa(int(flow.PortNo)),
245 )
246 tableFlow.Append(flowInfo)
247 }
248 tableFlow.Render()
249 tableFlow.SetNewLine("")
250 return nil
251}
Pragya Aryabd731ec2020-02-11 16:38:17 +0530252
253func (o *OltPoweronAllOnus) Execute(args []string) error {
254 client, conn := connect()
255 defer conn.Close()
256
257 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
258 defer cancel()
259
260 res, err := client.PoweronAllONUs(ctx, &pb.Empty{})
261
262 if err != nil {
263 log.Errorf("Cannot poweron all ONUs: %v", err)
264 return err
265 }
266
267 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
268 return nil
269}
270
271func (o *OltShutdownAllOnus) Execute(args []string) error {
272 client, conn := connect()
273 defer conn.Close()
274
275 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
276 defer cancel()
277
278 res, err := client.ShutdownAllONUs(ctx, &pb.Empty{})
279
280 if err != nil {
281 log.Errorf("Cannot shutdown all ONUs: %v", err)
282 return err
283 }
284
285 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
286 return nil
287}