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 | "fmt" |
| 23 | "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" |
| 30 | ) |
| 31 | |
| 32 | const ( |
| 33 | DEFAULT_OLT_DEVICE_HEADER_FORMAT = "table{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}" |
| 34 | DEFAULT_PORT_HEADER_FORMAT = "table{{ .ID }}\t{{ .OperState }}" |
| 35 | ) |
| 36 | |
| 37 | type OltGet struct{} |
| 38 | |
| 39 | type OltNNIs struct{} |
| 40 | |
| 41 | type OltPONs struct{} |
| 42 | |
| 43 | // TODO add autocomplete |
| 44 | type oltOptions struct { |
| 45 | Get OltGet `command:"get"` |
| 46 | NNI OltNNIs `command:"nnis"` |
| 47 | PON OltPONs `command:"pons"` |
| 48 | } |
| 49 | |
| 50 | func RegisterOltCommands(parser *flags.Parser) { |
| 51 | parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{}) |
| 52 | } |
| 53 | |
| 54 | func getOLT() *pb.Olt { |
| 55 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 56 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 57 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 58 | return nil |
| 59 | } |
| 60 | defer conn.Close() |
| 61 | c := pb.NewBBSimClient(conn) |
| 62 | |
| 63 | // Contact the server and print out its response. |
| 64 | |
| 65 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 66 | defer cancel() |
| 67 | olt, err := c.GetOlt(ctx, &pb.Empty{}) |
| 68 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 69 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 70 | return nil |
| 71 | } |
| 72 | return olt |
| 73 | } |
| 74 | |
| 75 | func printOltHeader(prefix string, o *pb.Olt) { |
| 76 | fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber)) |
| 77 | fmt.Println() |
| 78 | } |
| 79 | |
| 80 | // TODO use voltctl or cordctl parser to print tables (this needs to be moved out of the internals package) |
| 81 | func (o *OltGet) Execute(args []string) error { |
| 82 | olt := getOLT() |
| 83 | |
| 84 | // print out |
| 85 | tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT) |
| 86 | tableFormat.Execute(os.Stdout, true, olt) |
| 87 | |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func (o *OltNNIs) Execute(args []string) error { |
| 92 | olt := getOLT() |
| 93 | |
| 94 | printOltHeader("NNI Ports for", olt) |
| 95 | |
| 96 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
| 97 | tableFormat.Execute(os.Stdout, true, olt.NNIPorts) |
| 98 | |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (o *OltPONs) Execute(args []string) error { |
| 103 | olt := getOLT() |
| 104 | |
| 105 | printOltHeader("PON Ports for", olt) |
| 106 | |
| 107 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
| 108 | tableFormat.Execute(os.Stdout, true, olt.PONPorts) |
| 109 | |
| 110 | return nil |
| 111 | } |