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 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 43 | type OltShutdown struct{} |
| 44 | |
| 45 | type OltPoweron struct{} |
| 46 | |
| 47 | type OltReboot struct{} |
| 48 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 49 | type oltOptions struct { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 50 | Get OltGet `command:"get"` |
| 51 | NNI OltNNIs `command:"nnis"` |
| 52 | PON OltPONs `command:"pons"` |
| 53 | Shutdown OltShutdown `command:"shutdown"` |
| 54 | Poweron OltPoweron `command:"poweron"` |
| 55 | Reboot OltReboot `command:"reboot"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func RegisterOltCommands(parser *flags.Parser) { |
| 59 | parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{}) |
| 60 | } |
| 61 | |
| 62 | func getOLT() *pb.Olt { |
| 63 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 64 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 65 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 66 | return nil |
| 67 | } |
| 68 | defer conn.Close() |
| 69 | c := pb.NewBBSimClient(conn) |
| 70 | |
| 71 | // Contact the server and print out its response. |
| 72 | |
| 73 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 74 | defer cancel() |
| 75 | olt, err := c.GetOlt(ctx, &pb.Empty{}) |
| 76 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 77 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 78 | return nil |
| 79 | } |
| 80 | return olt |
| 81 | } |
| 82 | |
| 83 | func printOltHeader(prefix string, o *pb.Olt) { |
| 84 | fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber)) |
| 85 | fmt.Println() |
| 86 | } |
| 87 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 88 | func (o *OltGet) Execute(args []string) error { |
| 89 | olt := getOLT() |
| 90 | |
| 91 | // print out |
| 92 | tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT) |
| 93 | tableFormat.Execute(os.Stdout, true, olt) |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (o *OltNNIs) Execute(args []string) error { |
| 99 | olt := getOLT() |
| 100 | |
| 101 | printOltHeader("NNI Ports for", olt) |
| 102 | |
| 103 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
| 104 | tableFormat.Execute(os.Stdout, true, olt.NNIPorts) |
| 105 | |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func (o *OltPONs) Execute(args []string) error { |
| 110 | olt := getOLT() |
| 111 | |
| 112 | printOltHeader("PON Ports for", olt) |
| 113 | |
| 114 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
| 115 | tableFormat.Execute(os.Stdout, true, olt.PONPorts) |
| 116 | |
| 117 | return nil |
| 118 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 119 | |
| 120 | func (o *OltShutdown) Execute(args []string) error { |
| 121 | client, conn := connect() |
| 122 | defer conn.Close() |
| 123 | |
| 124 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 125 | defer cancel() |
| 126 | |
| 127 | res, err := client.ShutdownOlt(ctx, &pb.Empty{}) |
| 128 | |
| 129 | if err != nil { |
| 130 | log.Fatalf("Cannot shut down OLT: %v", err) |
| 131 | return err |
| 132 | } |
| 133 | |
| 134 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 135 | return nil |
| 136 | } |
| 137 | |
| 138 | func (o *OltPoweron) Execute(args []string) error { |
| 139 | client, conn := connect() |
| 140 | defer conn.Close() |
| 141 | |
| 142 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 143 | defer cancel() |
| 144 | |
| 145 | res, err := client.PoweronOlt(ctx, &pb.Empty{}) |
| 146 | |
| 147 | if err != nil { |
| 148 | log.Fatalf("Cannot power on OLT: %v", err) |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | func (o *OltReboot) Execute(args []string) error { |
| 157 | client, conn := connect() |
| 158 | defer conn.Close() |
| 159 | |
| 160 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 161 | defer cancel() |
| 162 | |
| 163 | res, err := client.RebootOlt(ctx, &pb.Empty{}) |
| 164 | |
| 165 | if err != nil { |
| 166 | log.Fatalf("Cannot reboot OLT: %v", err) |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 171 | return nil |
| 172 | } |