blob: 515ba0705b704544b7c83330001b056a7a0c4e7b [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"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070022 "fmt"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070023 "github.com/jessevdk/go-flags"
24 pb "github.com/opencord/bbsim/api/bbsim"
25 "github.com/opencord/bbsim/internal/bbsimctl/config"
26 "github.com/opencord/cordctl/pkg/format"
27 log "github.com/sirupsen/logrus"
28 "google.golang.org/grpc"
29 "os"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070030 "strings"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070031)
32
33const (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .CTag }}\t{{ .OperState }}\t{{ .InternalState }}"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070035)
36
Matteo Scandolo10f965c2019-09-24 10:40:46 -070037type OnuSnString string
Arjun E K57a7fcb2020-01-30 06:44:45 +000038type IgmpSubAction string
39
40const IgmpJoinKey string = "join"
41const IgmpLeaveKey string = "leave"
42
Matteo Scandolo10f965c2019-09-24 10:40:46 -070043type ONUList struct{}
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070044
45type ONUGet struct {
46 Args struct {
47 OnuSn OnuSnString
48 } `positional-args:"yes" required:"yes"`
49}
50
Matteo Scandolo10f965c2019-09-24 10:40:46 -070051type ONUShutDown struct {
52 Args struct {
53 OnuSn OnuSnString
54 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070055}
56
Matteo Scandolo10f965c2019-09-24 10:40:46 -070057type ONUPowerOn struct {
58 Args struct {
59 OnuSn OnuSnString
60 } `positional-args:"yes" required:"yes"`
61}
62
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070063type ONUEapolRestart struct {
64 Args struct {
65 OnuSn OnuSnString
66 } `positional-args:"yes" required:"yes"`
67}
68
69type ONUDhcpRestart struct {
70 Args struct {
71 OnuSn OnuSnString
72 } `positional-args:"yes" required:"yes"`
73}
74
Arjun E K57a7fcb2020-01-30 06:44:45 +000075type ONUIgmp struct {
76 Args struct {
77 OnuSn OnuSnString
78 SubAction IgmpSubAction
79 } `positional-args:"yes" required:"yes"`
80}
81
Matteo Scandolo10f965c2019-09-24 10:40:46 -070082type ONUOptions struct {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070083 List ONUList `command:"list"`
84 Get ONUGet `command:"get"`
85 ShutDown ONUShutDown `command:"shutdown"`
86 PowerOn ONUPowerOn `command:"poweron"`
87 RestartEapol ONUEapolRestart `command:"auth_restart"`
88 RestartDchp ONUDhcpRestart `command:"dhcp_restart"`
Arjun E K57a7fcb2020-01-30 06:44:45 +000089 Igmp ONUIgmp `command:"igmp"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -070090}
91
92func RegisterONUCommands(parser *flags.Parser) {
93 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
94}
95
96func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070097 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
98
99 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700100 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700101 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700102 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700103 return pb.NewBBSimClient(conn), conn
104}
105
106func getONUs() *pb.ONUs {
107
108 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700109 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700110
111 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700112 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
113 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700114
115 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700116 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700117 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700118 return nil
119 }
120 return onus
121}
122
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700123func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700124 onus := getONUs()
125
126 // print out
127 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
128 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
129 log.Fatalf("Error while formatting ONUs table: %s", err)
130 }
131
132 return nil
133}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700134
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700135func (options *ONUGet) Execute(args []string) error {
136 client, conn := connect()
137 defer conn.Close()
138
139 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
140 defer cancel()
141 req := pb.ONURequest{
142 SerialNumber: string(options.Args.OnuSn),
143 }
144 res, err := client.GetONU(ctx, &req)
145
146 if err != nil {
147 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
148 return err
149 }
150
151 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
152 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
153 log.Fatalf("Error while formatting ONUs table: %s", err)
154 }
155
156 return nil
157}
158
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700159func (options *ONUShutDown) Execute(args []string) error {
160
161 client, conn := connect()
162 defer conn.Close()
163
164 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
165 defer cancel()
166 req := pb.ONURequest{
167 SerialNumber: string(options.Args.OnuSn),
168 }
169 res, err := client.ShutdownONU(ctx, &req)
170
171 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700172 log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700173 return err
174 }
175
176 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
177
178 return nil
179}
180
181func (options *ONUPowerOn) Execute(args []string) error {
182 client, conn := connect()
183 defer conn.Close()
184
185 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
186 defer cancel()
187 req := pb.ONURequest{
188 SerialNumber: string(options.Args.OnuSn),
189 }
190 res, err := client.PoweronONU(ctx, &req)
191
192 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700193 log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err)
194 return err
195 }
196
197 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
198
199 return nil
200}
201
202func (options *ONUEapolRestart) Execute(args []string) error {
203 client, conn := connect()
204 defer conn.Close()
205
206 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
207 defer cancel()
208 req := pb.ONURequest{
209 SerialNumber: string(options.Args.OnuSn),
210 }
211 res, err := client.RestartEapol(ctx, &req)
212
213 if err != nil {
214 log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err)
215 return err
216 }
217
218 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
219
220 return nil
221}
222
223func (options *ONUDhcpRestart) Execute(args []string) error {
224 client, conn := connect()
225 defer conn.Close()
226
227 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
228 defer cancel()
229 req := pb.ONURequest{
230 SerialNumber: string(options.Args.OnuSn),
231 }
232 res, err := client.RestartDhcp(ctx, &req)
233
234 if err != nil {
235 log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700236 return err
237 }
238
239 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
240
241 return nil
242}
243
Arjun E K57a7fcb2020-01-30 06:44:45 +0000244func (options *ONUIgmp) Execute(args []string) error {
245 client, conn := connect()
246 defer conn.Close()
247
248 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
249 defer cancel()
250
251 req := pb.ONURequest{
252 SerialNumber: string(options.Args.OnuSn),
253 }
254
255 var subActionVal pb.SubActionTypes
256 if string(options.Args.SubAction) == IgmpJoinKey {
257 subActionVal = pb.SubActionTypes_JOIN
258 } else if string(options.Args.SubAction) == IgmpLeaveKey {
259 subActionVal = pb.SubActionTypes_LEAVE
260 }
261
262 igmpReq := pb.IgmpRequest{
263 OnuReq: &req,
264 SubActionVal: subActionVal,
265 }
266 res, err := client.GetONU(ctx, igmpReq.OnuReq)
267 if err != nil {
268 log.WithFields(log.Fields{
269 "SerialNumber": options.Args.OnuSn,
270 }).Errorf("Cannot not get details on ONU error: %v", err)
271 }
272 log.WithFields(log.Fields{
273 "SerialNumber": igmpReq.OnuReq.SerialNumber,
274 }).Debugf("ONU has indentified : %s", res)
275
276 igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq)
277 if igmpErr != nil {
278 log.WithFields(log.Fields{
279 "SubAction": options.Args.SubAction,
280 }).Errorf("Could not process Action: error: %v", igmpErr)
281 } else {
282 log.WithFields(log.Fields{
283 "SubAction": options.Args.SubAction,
284 }).Debugf("igmp state has been changed with response: %s",
285 igmpRes.Message)
286 }
287
288 return nil
289}
290
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700291func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
292 client, conn := connect()
293 defer conn.Close()
294
295 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
296 defer cancel()
297
298 onus, err := client.GetONUs(ctx, &pb.Empty{})
299 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700300 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700301 return nil
302 }
303
304 list := make([]flags.Completion, 0)
305 for _, k := range onus.Items {
306 if strings.HasPrefix(k.SerialNumber, match) {
307 list = append(list, flags.Completion{Item: k.SerialNumber})
308 }
309 }
310
311 return list
312}