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 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 52 | type OltFlows struct{} |
| 53 | |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 54 | type OltPoweronAllOnus struct{} |
| 55 | |
| 56 | type OltShutdownAllOnus struct{} |
| 57 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 58 | type oltOptions struct { |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 59 | Get OltGet `command:"get"` |
| 60 | NNI OltNNIs `command:"nnis"` |
| 61 | PON OltPONs `command:"pons"` |
| 62 | Shutdown OltShutdown `command:"shutdown"` |
| 63 | Poweron OltPoweron `command:"poweron"` |
| 64 | Reboot OltReboot `command:"reboot"` |
| 65 | Alarms OltAlarmOptions `command:"alarms"` |
| 66 | Flows OltFlows `command:"flows"` |
| 67 | PoweronAllOnus OltPoweronAllOnus `command:"poweronAllONUs"` |
| 68 | ShutdownAllOnus OltShutdownAllOnus `command:"shutdownAllONUs"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func RegisterOltCommands(parser *flags.Parser) { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 72 | _, _ = 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] | 73 | } |
| 74 | |
| 75 | func getOLT() *pb.Olt { |
| 76 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 77 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 78 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 79 | return nil |
| 80 | } |
| 81 | defer conn.Close() |
| 82 | c := pb.NewBBSimClient(conn) |
| 83 | |
| 84 | // Contact the server and print out its response. |
| 85 | |
| 86 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 87 | defer cancel() |
| 88 | olt, err := c.GetOlt(ctx, &pb.Empty{}) |
| 89 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 90 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 91 | return nil |
| 92 | } |
| 93 | return olt |
| 94 | } |
| 95 | |
| 96 | func printOltHeader(prefix string, o *pb.Olt) { |
| 97 | fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber)) |
| 98 | fmt.Println() |
| 99 | } |
| 100 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 101 | func (o *OltGet) Execute(args []string) error { |
| 102 | olt := getOLT() |
| 103 | |
| 104 | // print out |
| 105 | tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 106 | _ = tableFormat.Execute(os.Stdout, true, olt) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 107 | |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (o *OltNNIs) Execute(args []string) error { |
| 112 | olt := getOLT() |
| 113 | |
| 114 | printOltHeader("NNI Ports for", olt) |
| 115 | |
| 116 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 117 | _ = tableFormat.Execute(os.Stdout, true, olt.NNIPorts) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 118 | |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func (o *OltPONs) Execute(args []string) error { |
| 123 | olt := getOLT() |
| 124 | |
| 125 | printOltHeader("PON Ports for", olt) |
| 126 | |
| 127 | tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 128 | _ = tableFormat.Execute(os.Stdout, true, olt.PONPorts) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 129 | |
| 130 | return nil |
| 131 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 132 | |
| 133 | func (o *OltShutdown) Execute(args []string) error { |
| 134 | client, conn := connect() |
| 135 | defer conn.Close() |
| 136 | |
| 137 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 138 | defer cancel() |
| 139 | |
| 140 | res, err := client.ShutdownOlt(ctx, &pb.Empty{}) |
| 141 | |
| 142 | if err != nil { |
| 143 | log.Fatalf("Cannot shut down OLT: %v", err) |
| 144 | return err |
| 145 | } |
| 146 | |
| 147 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func (o *OltPoweron) Execute(args []string) error { |
| 152 | client, conn := connect() |
| 153 | defer conn.Close() |
| 154 | |
| 155 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 156 | defer cancel() |
| 157 | |
| 158 | res, err := client.PoweronOlt(ctx, &pb.Empty{}) |
| 159 | |
| 160 | if err != nil { |
| 161 | log.Fatalf("Cannot power on OLT: %v", err) |
| 162 | return err |
| 163 | } |
| 164 | |
| 165 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | func (o *OltReboot) Execute(args []string) error { |
| 170 | client, conn := connect() |
| 171 | defer conn.Close() |
| 172 | |
| 173 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 174 | defer cancel() |
| 175 | |
| 176 | res, err := client.RebootOlt(ctx, &pb.Empty{}) |
| 177 | |
| 178 | if err != nil { |
| 179 | log.Fatalf("Cannot reboot OLT: %v", err) |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 184 | return nil |
| 185 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 186 | |
| 187 | func (o *OltFlows) Execute(args []string) error { |
| 188 | client, conn := connect() |
| 189 | defer conn.Close() |
| 190 | |
| 191 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 192 | defer cancel() |
| 193 | |
| 194 | req := pb.ONURequest{} |
| 195 | res, err := client.GetFlows(ctx, &req) |
| 196 | if err != nil { |
| 197 | log.Errorf("Cannot get flows for OLT: %v", err) |
| 198 | return err |
| 199 | } |
| 200 | |
| 201 | if res.Flows == nil { |
| 202 | fmt.Println("OLT has no flows") |
| 203 | return nil |
| 204 | } |
| 205 | |
| 206 | flowHeader := []string{ |
| 207 | "access_intf_id", |
| 208 | "onu_id", |
| 209 | "uni_id", |
| 210 | "flow_id", |
| 211 | "flow_type", |
| 212 | "eth_type", |
| 213 | "alloc_id", |
| 214 | "network_intf_id", |
| 215 | "gemport_id", |
| 216 | "classifier", |
| 217 | "action", |
| 218 | "priority", |
| 219 | "cookie", |
| 220 | "port_no", |
| 221 | } |
| 222 | |
| 223 | tableFlow := tablewriter.NewWriter(os.Stdout) |
| 224 | tableFlow.SetRowLine(true) |
| 225 | fmt.Fprintf(os.Stdout, "OLT Flows:\n") |
| 226 | tableFlow.SetHeader(flowHeader) |
| 227 | |
| 228 | for _, flow := range res.Flows { |
| 229 | flowInfo := []string{} |
| 230 | flowInfo = append(flowInfo, |
| 231 | strconv.Itoa(int(flow.AccessIntfId)), |
| 232 | strconv.Itoa(int(flow.OnuId)), |
| 233 | strconv.Itoa(int(flow.UniId)), |
| 234 | strconv.Itoa(int(flow.FlowId)), |
| 235 | flow.FlowType, |
| 236 | fmt.Sprintf("%x", flow.Classifier.EthType), |
| 237 | strconv.Itoa(int(flow.AllocId)), |
| 238 | strconv.Itoa(int(flow.NetworkIntfId)), |
| 239 | strconv.Itoa(int(flow.GemportId)), |
| 240 | flow.Classifier.String(), |
| 241 | flow.Action.String(), |
| 242 | strconv.Itoa(int(flow.Priority)), |
| 243 | strconv.Itoa(int(flow.Cookie)), |
| 244 | strconv.Itoa(int(flow.PortNo)), |
| 245 | ) |
| 246 | tableFlow.Append(flowInfo) |
| 247 | } |
| 248 | tableFlow.Render() |
| 249 | tableFlow.SetNewLine("") |
| 250 | return nil |
| 251 | } |
Pragya Arya | bd731ec | 2020-02-11 16:38:17 +0530 | [diff] [blame] | 252 | |
| 253 | func (o *OltPoweronAllOnus) Execute(args []string) error { |
| 254 | client, conn := connect() |
| 255 | defer conn.Close() |
| 256 | |
| 257 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 258 | defer cancel() |
| 259 | |
| 260 | res, err := client.PoweronAllONUs(ctx, &pb.Empty{}) |
| 261 | |
| 262 | if err != nil { |
| 263 | log.Errorf("Cannot poweron all ONUs: %v", err) |
| 264 | return err |
| 265 | } |
| 266 | |
| 267 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 268 | return nil |
| 269 | } |
| 270 | |
| 271 | func (o *OltShutdownAllOnus) Execute(args []string) error { |
| 272 | client, conn := connect() |
| 273 | defer conn.Close() |
| 274 | |
| 275 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 276 | defer cancel() |
| 277 | |
| 278 | res, err := client.ShutdownAllONUs(ctx, &pb.Empty{}) |
| 279 | |
| 280 | if err != nil { |
| 281 | log.Errorf("Cannot shutdown all ONUs: %v", err) |
| 282 | return err |
| 283 | } |
| 284 | |
| 285 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 286 | return nil |
| 287 | } |