blob: fec47eda6a2cb99d4a1914d30544f0d03252268e [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"
Arjun E Kdd443f02020-02-07 15:24:01 +000042const IgmpJoinKeyV3 string = "joinv3"
Arjun E K57a7fcb2020-01-30 06:44:45 +000043
Matteo Scandolo10f965c2019-09-24 10:40:46 -070044type ONUList struct{}
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070045
46type ONUGet struct {
47 Args struct {
48 OnuSn OnuSnString
49 } `positional-args:"yes" required:"yes"`
50}
51
Matteo Scandolo10f965c2019-09-24 10:40:46 -070052type ONUShutDown struct {
53 Args struct {
54 OnuSn OnuSnString
55 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070056}
57
Matteo Scandolo10f965c2019-09-24 10:40:46 -070058type ONUPowerOn struct {
59 Args struct {
60 OnuSn OnuSnString
61 } `positional-args:"yes" required:"yes"`
62}
63
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070064type ONUEapolRestart struct {
65 Args struct {
66 OnuSn OnuSnString
67 } `positional-args:"yes" required:"yes"`
68}
69
70type ONUDhcpRestart struct {
71 Args struct {
72 OnuSn OnuSnString
73 } `positional-args:"yes" required:"yes"`
74}
75
Arjun E K57a7fcb2020-01-30 06:44:45 +000076type ONUIgmp struct {
77 Args struct {
78 OnuSn OnuSnString
79 SubAction IgmpSubAction
80 } `positional-args:"yes" required:"yes"`
81}
82
Matteo Scandolo10f965c2019-09-24 10:40:46 -070083type ONUOptions struct {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070084 List ONUList `command:"list"`
85 Get ONUGet `command:"get"`
86 ShutDown ONUShutDown `command:"shutdown"`
87 PowerOn ONUPowerOn `command:"poweron"`
88 RestartEapol ONUEapolRestart `command:"auth_restart"`
89 RestartDchp ONUDhcpRestart `command:"dhcp_restart"`
Arjun E K57a7fcb2020-01-30 06:44:45 +000090 Igmp ONUIgmp `command:"igmp"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -070091}
92
93func RegisterONUCommands(parser *flags.Parser) {
94 parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
95}
96
97func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -070098 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
99
100 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700101 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700102 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700103 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700104 return pb.NewBBSimClient(conn), conn
105}
106
107func getONUs() *pb.ONUs {
108
109 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700110 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700111
112 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700113 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
114 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700115
116 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700117 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700118 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700119 return nil
120 }
121 return onus
122}
123
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700124func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700125 onus := getONUs()
126
127 // print out
128 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
129 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
130 log.Fatalf("Error while formatting ONUs table: %s", err)
131 }
132
133 return nil
134}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700135
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700136func (options *ONUGet) Execute(args []string) error {
137 client, conn := connect()
138 defer conn.Close()
139
140 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
141 defer cancel()
142 req := pb.ONURequest{
143 SerialNumber: string(options.Args.OnuSn),
144 }
145 res, err := client.GetONU(ctx, &req)
146
147 if err != nil {
148 log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
149 return err
150 }
151
152 tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
153 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
154 log.Fatalf("Error while formatting ONUs table: %s", err)
155 }
156
157 return nil
158}
159
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700160func (options *ONUShutDown) Execute(args []string) error {
161
162 client, conn := connect()
163 defer conn.Close()
164
165 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
166 defer cancel()
167 req := pb.ONURequest{
168 SerialNumber: string(options.Args.OnuSn),
169 }
170 res, err := client.ShutdownONU(ctx, &req)
171
172 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700173 log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700174 return err
175 }
176
177 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
178
179 return nil
180}
181
182func (options *ONUPowerOn) Execute(args []string) error {
183 client, conn := connect()
184 defer conn.Close()
185
186 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
187 defer cancel()
188 req := pb.ONURequest{
189 SerialNumber: string(options.Args.OnuSn),
190 }
191 res, err := client.PoweronONU(ctx, &req)
192
193 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700194 log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err)
195 return err
196 }
197
198 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
199
200 return nil
201}
202
203func (options *ONUEapolRestart) Execute(args []string) error {
204 client, conn := connect()
205 defer conn.Close()
206
207 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
208 defer cancel()
209 req := pb.ONURequest{
210 SerialNumber: string(options.Args.OnuSn),
211 }
212 res, err := client.RestartEapol(ctx, &req)
213
214 if err != nil {
215 log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err)
216 return err
217 }
218
219 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
220
221 return nil
222}
223
224func (options *ONUDhcpRestart) Execute(args []string) error {
225 client, conn := connect()
226 defer conn.Close()
227
228 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
229 defer cancel()
230 req := pb.ONURequest{
231 SerialNumber: string(options.Args.OnuSn),
232 }
233 res, err := client.RestartDhcp(ctx, &req)
234
235 if err != nil {
236 log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700237 return err
238 }
239
240 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
241
242 return nil
243}
244
Arjun E K57a7fcb2020-01-30 06:44:45 +0000245func (options *ONUIgmp) Execute(args []string) error {
246 client, conn := connect()
247 defer conn.Close()
248
249 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
250 defer cancel()
251
252 req := pb.ONURequest{
253 SerialNumber: string(options.Args.OnuSn),
254 }
255
256 var subActionVal pb.SubActionTypes
257 if string(options.Args.SubAction) == IgmpJoinKey {
258 subActionVal = pb.SubActionTypes_JOIN
259 } else if string(options.Args.SubAction) == IgmpLeaveKey {
260 subActionVal = pb.SubActionTypes_LEAVE
Arjun E Kdd443f02020-02-07 15:24:01 +0000261 } else if string(options.Args.SubAction) == IgmpJoinKeyV3 {
262 subActionVal = pb.SubActionTypes_JOINV3
263 }
Arjun E K57a7fcb2020-01-30 06:44:45 +0000264
265 igmpReq := pb.IgmpRequest{
266 OnuReq: &req,
267 SubActionVal: subActionVal,
268 }
269 res, err := client.GetONU(ctx, igmpReq.OnuReq)
270 if err != nil {
271 log.WithFields(log.Fields{
272 "SerialNumber": options.Args.OnuSn,
273 }).Errorf("Cannot not get details on ONU error: %v", err)
274 }
275 log.WithFields(log.Fields{
276 "SerialNumber": igmpReq.OnuReq.SerialNumber,
277 }).Debugf("ONU has indentified : %s", res)
278
279 igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq)
280 if igmpErr != nil {
281 log.WithFields(log.Fields{
282 "SubAction": options.Args.SubAction,
283 }).Errorf("Could not process Action: error: %v", igmpErr)
284 } else {
285 log.WithFields(log.Fields{
286 "SubAction": options.Args.SubAction,
287 }).Debugf("igmp state has been changed with response: %s",
288 igmpRes.Message)
289 }
290
291 return nil
292}
293
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700294func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
295 client, conn := connect()
296 defer conn.Close()
297
298 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
299 defer cancel()
300
301 onus, err := client.GetONUs(ctx, &pb.Empty{})
302 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700303 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700304 return nil
305 }
306
307 list := make([]flags.Completion, 0)
308 for _, k := range onus.Items {
309 if strings.HasPrefix(k.SerialNumber, match) {
310 list = append(list, flags.Completion{Item: k.SerialNumber})
311 }
312 }
313
314 return list
315}