Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package commands |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "github.com/jessevdk/go-flags" |
| 23 | pb "github.com/opencord/bbsim/api/bbsim" |
| 24 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 25 | "github.com/opencord/cordctl/pkg/format" |
| 26 | log "github.com/sirupsen/logrus" |
| 27 | "google.golang.org/grpc" |
| 28 | "os" |
| 29 | ) |
| 30 | |
| 31 | const ( |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 32 | DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .STag }}\t{{ .CTag }}\t{{ .OperState }}\t{{ .InternalState }}" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type ONUOptions struct{} |
| 36 | |
| 37 | func RegisterONUCommands(parser *flags.Parser) { |
| 38 | parser.AddCommand("onus", "List ONU Devices", "Commands to list the ONU devices and their internal state", &ONUOptions{}) |
| 39 | } |
| 40 | |
| 41 | func getONUs() *pb.ONUs { |
| 42 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 43 | |
| 44 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 45 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 46 | return nil |
| 47 | } |
| 48 | defer conn.Close() |
| 49 | c := pb.NewBBSimClient(conn) |
| 50 | |
| 51 | // Contact the server and print out its response. |
| 52 | |
| 53 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 54 | defer cancel() |
| 55 | onus, err := c.GetONUs(ctx, &pb.Empty{}) |
| 56 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 57 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 58 | return nil |
| 59 | } |
| 60 | return onus |
| 61 | } |
| 62 | |
| 63 | func (options *ONUOptions) Execute(args []string) error { |
| 64 | onus := getONUs() |
| 65 | |
| 66 | // print out |
| 67 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 68 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 69 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |