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" |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 23 | "os" |
| 24 | "strconv" |
| 25 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 26 | "github.com/jessevdk/go-flags" |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 27 | "github.com/olekukonko/tablewriter" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 28 | pb "github.com/opencord/bbsim/api/bbsim" |
| 29 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 30 | "github.com/opencord/cordctl/pkg/format" |
| 31 | log "github.com/sirupsen/logrus" |
| 32 | "google.golang.org/grpc" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | const ( |
rajesh | f921f88 | 2020-03-06 18:24:28 +0530 | [diff] [blame] | 36 | DEFAULT_OLT_DEVICE_HEADER_FORMAT = "table{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .IP }}" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 37 | DEFAULT_PORT_HEADER_FORMAT = "table{{ .ID }}\t{{ .OperState }}" |
| 38 | ) |
| 39 | |
| 40 | type OltGet struct{} |
| 41 | |
| 42 | type OltNNIs struct{} |
| 43 | |
| 44 | type OltPONs struct{} |
| 45 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 46 | type OltShutdown struct{} |
| 47 | |
| 48 | type OltPoweron struct{} |
| 49 | |
| 50 | type OltReboot struct{} |
| 51 | |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 52 | type StopGrpcServer struct{} |
| 53 | |
| 54 | type StartGrpcServer struct{} |
| 55 | type RestartGrpcServer struct { |
| 56 | Args struct { |
| 57 | Delay uint32 |
| 58 | } `positional-args:"yes" required:"yes"` |
| 59 | } |
| 60 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 61 | type OltFlows struct{} |
| 62 | |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 63 | type OltPoweronAllOnus struct{} |
| 64 | |
| 65 | type OltShutdownAllOnus struct{} |
| 66 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 67 | type oltOptions struct { |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 68 | Get OltGet `command:"get"` |
| 69 | NNI OltNNIs `command:"nnis"` |
| 70 | PON OltPONs `command:"pons"` |
| 71 | Shutdown OltShutdown `command:"shutdown"` |
| 72 | Poweron OltPoweron `command:"poweron"` |
| 73 | Reboot OltReboot `command:"reboot"` |
| 74 | Alarms OltAlarmOptions `command:"alarms"` |
| 75 | Flows OltFlows `command:"flows"` |
| 76 | PoweronAllOnus OltPoweronAllOnus `command:"poweronAllONUs"` |
| 77 | ShutdownAllOnus OltShutdownAllOnus `command:"shutdownAllONUs"` |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 78 | StopServer StopGrpcServer `command:"stopServer"` |
| 79 | StartServer StartGrpcServer `command:"startServer"` |
| 80 | RestartServer RestartGrpcServer `command:"restartServer"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func RegisterOltCommands(parser *flags.Parser) { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 84 | _, _ = parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func getOLT() *pb.Olt { |
| 88 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 89 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 90 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 91 | return nil |
| 92 | } |
| 93 | defer conn.Close() |
| 94 | c := pb.NewBBSimClient(conn) |
| 95 | |
| 96 | // Contact the server and print out its response. |
| 97 | |
| 98 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 99 | defer cancel() |
| 100 | olt, err := c.GetOlt(ctx, &pb.Empty{}) |
| 101 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 102 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 103 | return nil |
| 104 | } |
| 105 | return olt |
| 106 | } |
| 107 | |
| 108 | func printOltHeader(prefix string, o *pb.Olt) { |
| 109 | fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber)) |
| 110 | fmt.Println() |
| 111 | } |
| 112 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 113 | func (o *OltGet) Execute(args []string) error { |
| 114 | olt := getOLT() |
| 115 | |
| 116 | // print out |
| 117 | tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 118 | _ = tableFormat.Execute(os.Stdout, true, olt) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 119 | |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | func (o *OltNNIs) Execute(args []string) error { |
| 124 | olt := getOLT() |
| 125 | |
| 126 | printOltHeader("NNI Ports for", olt) |
| 127 | |
| 128 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 129 | _ = tableFormat.Execute(os.Stdout, true, olt.NNIPorts) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 130 | |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func (o *OltPONs) Execute(args []string) error { |
| 135 | olt := getOLT() |
| 136 | |
| 137 | printOltHeader("PON Ports for", olt) |
| 138 | |
| 139 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 140 | _ = tableFormat.Execute(os.Stdout, true, olt.PONPorts) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 141 | |
| 142 | return nil |
| 143 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 144 | |
| 145 | func (o *OltShutdown) Execute(args []string) error { |
| 146 | client, conn := connect() |
| 147 | defer conn.Close() |
| 148 | |
| 149 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 150 | defer cancel() |
| 151 | |
| 152 | res, err := client.ShutdownOlt(ctx, &pb.Empty{}) |
| 153 | |
| 154 | if err != nil { |
| 155 | log.Fatalf("Cannot shut down OLT: %v", err) |
| 156 | return err |
| 157 | } |
| 158 | |
| 159 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | func (o *OltPoweron) Execute(args []string) error { |
| 164 | client, conn := connect() |
| 165 | defer conn.Close() |
| 166 | |
| 167 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 168 | defer cancel() |
| 169 | |
| 170 | res, err := client.PoweronOlt(ctx, &pb.Empty{}) |
| 171 | |
| 172 | if err != nil { |
| 173 | log.Fatalf("Cannot power on OLT: %v", err) |
| 174 | return err |
| 175 | } |
| 176 | |
| 177 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | func (o *OltReboot) 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 | |
| 188 | res, err := client.RebootOlt(ctx, &pb.Empty{}) |
| 189 | |
| 190 | if err != nil { |
| 191 | log.Fatalf("Cannot reboot OLT: %v", err) |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 196 | return nil |
| 197 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 198 | |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 199 | func (o *StopGrpcServer) Execute(args []string) error { |
| 200 | client, conn := connect() |
| 201 | defer conn.Close() |
| 202 | |
| 203 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 204 | defer cancel() |
| 205 | |
| 206 | res, err := client.StopgRPCServer(ctx, &pb.Empty{}) |
| 207 | |
| 208 | if err != nil { |
| 209 | log.Fatalf("Cannot stop Openolt server: %v", err) |
| 210 | return err |
| 211 | } |
| 212 | |
| 213 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 214 | return nil |
| 215 | } |
| 216 | |
| 217 | func (o *StartGrpcServer) Execute(args []string) error { |
| 218 | client, conn := connect() |
| 219 | defer conn.Close() |
| 220 | |
| 221 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 222 | defer cancel() |
| 223 | |
| 224 | res, err := client.StartgRPCServer(ctx, &pb.Empty{}) |
| 225 | |
| 226 | if err != nil { |
| 227 | log.Fatalf("Cannot start Openolt server: %v", err) |
| 228 | return err |
| 229 | } |
| 230 | |
| 231 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 232 | return nil |
| 233 | } |
| 234 | |
| 235 | func (o *RestartGrpcServer) Execute(args []string) error { |
| 236 | req := &pb.Timeout{ |
| 237 | Delay: o.Args.Delay, |
| 238 | } |
| 239 | client, conn := connect() |
| 240 | defer conn.Close() |
| 241 | |
| 242 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 243 | defer cancel() |
| 244 | |
| 245 | res, err := client.RestartgRPCServer(ctx, req) |
| 246 | |
| 247 | if err != nil { |
| 248 | log.Fatalf("Cannot restart Openolt server: %v", err) |
| 249 | return err |
| 250 | } |
| 251 | |
| 252 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 253 | return nil |
| 254 | } |
| 255 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 256 | func (o *OltFlows) Execute(args []string) error { |
| 257 | client, conn := connect() |
| 258 | defer conn.Close() |
| 259 | |
| 260 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 261 | defer cancel() |
| 262 | |
| 263 | req := pb.ONURequest{} |
| 264 | res, err := client.GetFlows(ctx, &req) |
| 265 | if err != nil { |
| 266 | log.Errorf("Cannot get flows for OLT: %v", err) |
| 267 | return err |
| 268 | } |
| 269 | |
| 270 | if res.Flows == nil { |
| 271 | fmt.Println("OLT has no flows") |
| 272 | return nil |
| 273 | } |
| 274 | |
| 275 | flowHeader := []string{ |
| 276 | "access_intf_id", |
| 277 | "onu_id", |
| 278 | "uni_id", |
| 279 | "flow_id", |
| 280 | "flow_type", |
| 281 | "eth_type", |
| 282 | "alloc_id", |
| 283 | "network_intf_id", |
| 284 | "gemport_id", |
| 285 | "classifier", |
| 286 | "action", |
| 287 | "priority", |
| 288 | "cookie", |
| 289 | "port_no", |
| 290 | } |
| 291 | |
| 292 | tableFlow := tablewriter.NewWriter(os.Stdout) |
| 293 | tableFlow.SetRowLine(true) |
| 294 | fmt.Fprintf(os.Stdout, "OLT Flows:\n") |
| 295 | tableFlow.SetHeader(flowHeader) |
| 296 | |
| 297 | for _, flow := range res.Flows { |
| 298 | flowInfo := []string{} |
| 299 | flowInfo = append(flowInfo, |
| 300 | strconv.Itoa(int(flow.AccessIntfId)), |
| 301 | strconv.Itoa(int(flow.OnuId)), |
| 302 | strconv.Itoa(int(flow.UniId)), |
| 303 | strconv.Itoa(int(flow.FlowId)), |
| 304 | flow.FlowType, |
| 305 | fmt.Sprintf("%x", flow.Classifier.EthType), |
| 306 | strconv.Itoa(int(flow.AllocId)), |
| 307 | strconv.Itoa(int(flow.NetworkIntfId)), |
| 308 | strconv.Itoa(int(flow.GemportId)), |
| 309 | flow.Classifier.String(), |
| 310 | flow.Action.String(), |
| 311 | strconv.Itoa(int(flow.Priority)), |
| 312 | strconv.Itoa(int(flow.Cookie)), |
| 313 | strconv.Itoa(int(flow.PortNo)), |
| 314 | ) |
| 315 | tableFlow.Append(flowInfo) |
| 316 | } |
| 317 | tableFlow.Render() |
| 318 | tableFlow.SetNewLine("") |
| 319 | return nil |
| 320 | } |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 321 | |
| 322 | func (o *OltPoweronAllOnus) Execute(args []string) error { |
| 323 | client, conn := connect() |
| 324 | defer conn.Close() |
| 325 | |
| 326 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 327 | defer cancel() |
| 328 | |
| 329 | res, err := client.PoweronAllONUs(ctx, &pb.Empty{}) |
| 330 | |
| 331 | if err != nil { |
| 332 | log.Errorf("Cannot poweron all ONUs: %v", err) |
| 333 | return err |
| 334 | } |
| 335 | |
| 336 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 337 | return nil |
| 338 | } |
| 339 | |
| 340 | func (o *OltShutdownAllOnus) Execute(args []string) error { |
| 341 | client, conn := connect() |
| 342 | defer conn.Close() |
| 343 | |
| 344 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 345 | defer cancel() |
| 346 | |
| 347 | res, err := client.ShutdownAllONUs(ctx, &pb.Empty{}) |
| 348 | |
| 349 | if err != nil { |
| 350 | log.Errorf("Cannot shutdown all ONUs: %v", err) |
| 351 | return err |
| 352 | } |
| 353 | |
| 354 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 355 | return nil |
| 356 | } |