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" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 22 | "fmt" |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 23 | "os" |
| 24 | "strconv" |
| 25 | "strings" |
| 26 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 27 | "github.com/jessevdk/go-flags" |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 28 | "github.com/olekukonko/tablewriter" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 29 | pb "github.com/opencord/bbsim/api/bbsim" |
| 30 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 31 | "github.com/opencord/cordctl/pkg/format" |
| 32 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | const ( |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 36 | DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .ImageSoftwareExpectedSections }}\t{{ .ImageSoftwareReceivedSections }}\t{{ .ActiveImageEntityId }}\t{{ .CommittedImageEntityId }}" |
| 37 | DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .ImageSoftwareExpectedSections }}\t{{ .ImageSoftwareReceivedSections }}\t{{ .ActiveImageEntityId }}\t{{ .CommittedImageEntityId }}\t{{ .Unis }}" |
| 38 | DEFAULT_UNI_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}" |
| 39 | DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 42 | type OnuSnString string |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 43 | type IgmpSubAction string |
Onur Kalinagac | 9f9faca | 2021-01-21 14:04:34 +0000 | [diff] [blame] | 44 | type GroupAddress string |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 45 | |
| 46 | const IgmpJoinKey string = "join" |
| 47 | const IgmpLeaveKey string = "leave" |
Arjun E K | dd443f0 | 2020-02-07 15:24:01 +0000 | [diff] [blame] | 48 | const IgmpJoinKeyV3 string = "joinv3" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 49 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 50 | var IgmpAllowedActions = []string{IgmpJoinKey, IgmpLeaveKey, IgmpJoinKeyV3} |
| 51 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 52 | type ONUList struct { |
| 53 | Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"` |
| 54 | } |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 55 | |
| 56 | type ONUGet struct { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 57 | Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"` |
| 58 | Args struct { |
| 59 | OnuSn OnuSnString |
| 60 | } `positional-args:"yes" required:"yes"` |
| 61 | } |
| 62 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 63 | type ONUUnis struct { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 64 | Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about UNIs"` |
| 65 | Args struct { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 66 | OnuSn OnuSnString |
| 67 | } `positional-args:"yes" required:"yes"` |
| 68 | } |
| 69 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 70 | type ONUShutDown struct { |
| 71 | Args struct { |
| 72 | OnuSn OnuSnString |
| 73 | } `positional-args:"yes" required:"yes"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 76 | type ONUPowerOn struct { |
| 77 | Args struct { |
| 78 | OnuSn OnuSnString |
| 79 | } `positional-args:"yes" required:"yes"` |
| 80 | } |
| 81 | |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 82 | type ONUEapolRestart struct { |
| 83 | Args struct { |
| 84 | OnuSn OnuSnString |
| 85 | } `positional-args:"yes" required:"yes"` |
| 86 | } |
| 87 | |
| 88 | type ONUDhcpRestart struct { |
| 89 | Args struct { |
| 90 | OnuSn OnuSnString |
| 91 | } `positional-args:"yes" required:"yes"` |
| 92 | } |
| 93 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 94 | type ONUIgmp struct { |
| 95 | Args struct { |
Onur Kalinagac | 9f9faca | 2021-01-21 14:04:34 +0000 | [diff] [blame] | 96 | OnuSn OnuSnString |
| 97 | SubAction IgmpSubAction |
| 98 | GroupAddress GroupAddress |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 99 | } `positional-args:"yes" required:"yes"` |
| 100 | } |
| 101 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 102 | type ONUTrafficSchedulers struct { |
| 103 | Args struct { |
| 104 | OnuSn OnuSnString |
| 105 | } `positional-args:"yes" required:"yes"` |
| 106 | } |
| 107 | |
| 108 | type ONUFlows struct { |
| 109 | Args struct { |
| 110 | OnuSn OnuSnString |
| 111 | } `positional-args:"yes" required:"yes"` |
| 112 | } |
| 113 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 114 | type ONUOptions struct { |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 115 | List ONUList `command:"list"` |
| 116 | Get ONUGet `command:"get"` |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 117 | Unis ONUUnis `command:"unis"` |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 118 | ShutDown ONUShutDown `command:"shutdown"` |
| 119 | PowerOn ONUPowerOn `command:"poweron"` |
| 120 | RestartEapol ONUEapolRestart `command:"auth_restart"` |
| 121 | RestartDchp ONUDhcpRestart `command:"dhcp_restart"` |
| 122 | Igmp ONUIgmp `command:"igmp"` |
| 123 | TrafficSchedulers ONUTrafficSchedulers `command:"traffic_schedulers"` |
Anand S Katti | 86552f9 | 2020-03-03 21:56:32 +0530 | [diff] [blame] | 124 | Alarms AlarmOptions `command:"alarms"` |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 125 | Flows ONUFlows `command:"flows"` |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | func RegisterONUCommands(parser *flags.Parser) { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 129 | _, _ = parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{}) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 132 | func getONUs() *pb.ONUs { |
| 133 | |
| 134 | client, conn := connect() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 135 | defer conn.Close() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 136 | |
| 137 | // Contact the server and print out its response. |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 138 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 139 | defer cancel() |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 140 | |
| 141 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 142 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 143 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 144 | return nil |
| 145 | } |
| 146 | return onus |
| 147 | } |
| 148 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 149 | func (options *ONUList) Execute(args []string) error { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 150 | onus := getONUs() |
| 151 | |
| 152 | // print out |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 153 | var tableFormat format.Format |
| 154 | if options.Verbose { |
| 155 | tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES) |
| 156 | } else { |
| 157 | tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 158 | } |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 159 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 160 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 161 | } |
| 162 | |
| 163 | return nil |
| 164 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 165 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 166 | func (options *ONUGet) Execute(args []string) error { |
| 167 | client, conn := connect() |
| 168 | defer conn.Close() |
| 169 | |
| 170 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 171 | defer cancel() |
| 172 | req := pb.ONURequest{ |
| 173 | SerialNumber: string(options.Args.OnuSn), |
| 174 | } |
| 175 | res, err := client.GetONU(ctx, &req) |
| 176 | |
| 177 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 178 | log.Fatalf("Cannot not get ONU %s: %v", options.Args.OnuSn, err) |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 179 | return err |
| 180 | } |
| 181 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 182 | var tableFormat format.Format |
| 183 | if options.Verbose { |
| 184 | tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES) |
| 185 | } else { |
| 186 | tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 187 | } |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 188 | if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil { |
| 189 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 190 | } |
| 191 | |
| 192 | return nil |
| 193 | } |
| 194 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 195 | func (options *ONUUnis) Execute(args []string) error { |
| 196 | |
| 197 | client, conn := connect() |
| 198 | defer conn.Close() |
| 199 | |
| 200 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 201 | defer cancel() |
| 202 | req := pb.ONURequest{ |
| 203 | SerialNumber: string(options.Args.OnuSn), |
| 204 | } |
| 205 | res, err := client.GetOnuUnis(ctx, &req) |
| 206 | |
| 207 | if err != nil { |
| 208 | log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err) |
| 209 | return err |
| 210 | } |
| 211 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 212 | var tableFormat format.Format |
| 213 | if options.Verbose { |
| 214 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES) |
| 215 | } else { |
| 216 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT) |
| 217 | } |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 218 | if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil { |
| 219 | log.Fatalf("Error while formatting Unis table: %s", err) |
| 220 | } |
| 221 | |
| 222 | return nil |
| 223 | } |
| 224 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 225 | func (options *ONUShutDown) Execute(args []string) error { |
| 226 | |
| 227 | client, conn := connect() |
| 228 | defer conn.Close() |
| 229 | |
| 230 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 231 | defer cancel() |
| 232 | req := pb.ONURequest{ |
| 233 | SerialNumber: string(options.Args.OnuSn), |
| 234 | } |
| 235 | res, err := client.ShutdownONU(ctx, &req) |
| 236 | |
| 237 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 238 | log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 239 | return err |
| 240 | } |
| 241 | |
| 242 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 243 | |
| 244 | return nil |
| 245 | } |
| 246 | |
| 247 | func (options *ONUPowerOn) Execute(args []string) error { |
| 248 | client, conn := connect() |
| 249 | defer conn.Close() |
| 250 | |
| 251 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 252 | defer cancel() |
| 253 | req := pb.ONURequest{ |
| 254 | SerialNumber: string(options.Args.OnuSn), |
| 255 | } |
| 256 | res, err := client.PoweronONU(ctx, &req) |
| 257 | |
| 258 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 259 | log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err) |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 264 | |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | func (options *ONUEapolRestart) Execute(args []string) error { |
| 269 | client, conn := connect() |
| 270 | defer conn.Close() |
| 271 | |
| 272 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 273 | defer cancel() |
| 274 | req := pb.ONURequest{ |
| 275 | SerialNumber: string(options.Args.OnuSn), |
| 276 | } |
| 277 | res, err := client.RestartEapol(ctx, &req) |
| 278 | |
| 279 | if err != nil { |
| 280 | log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err) |
| 281 | return err |
| 282 | } |
| 283 | |
| 284 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 285 | |
| 286 | return nil |
| 287 | } |
| 288 | |
| 289 | func (options *ONUDhcpRestart) Execute(args []string) error { |
| 290 | client, conn := connect() |
| 291 | defer conn.Close() |
| 292 | |
| 293 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 294 | defer cancel() |
| 295 | req := pb.ONURequest{ |
| 296 | SerialNumber: string(options.Args.OnuSn), |
| 297 | } |
| 298 | res, err := client.RestartDhcp(ctx, &req) |
| 299 | |
| 300 | if err != nil { |
| 301 | log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 302 | return err |
| 303 | } |
| 304 | |
| 305 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 306 | |
| 307 | return nil |
| 308 | } |
| 309 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 310 | func (options *ONUIgmp) Execute(args []string) error { |
| 311 | client, conn := connect() |
| 312 | defer conn.Close() |
| 313 | |
| 314 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 315 | defer cancel() |
| 316 | |
| 317 | req := pb.ONURequest{ |
| 318 | SerialNumber: string(options.Args.OnuSn), |
| 319 | } |
| 320 | |
| 321 | var subActionVal pb.SubActionTypes |
| 322 | if string(options.Args.SubAction) == IgmpJoinKey { |
| 323 | subActionVal = pb.SubActionTypes_JOIN |
| 324 | } else if string(options.Args.SubAction) == IgmpLeaveKey { |
| 325 | subActionVal = pb.SubActionTypes_LEAVE |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 326 | } else if string(options.Args.SubAction) == IgmpJoinKeyV3 { |
| 327 | subActionVal = pb.SubActionTypes_JOINV3 |
| 328 | } |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 329 | |
| 330 | igmpReq := pb.IgmpRequest{ |
| 331 | OnuReq: &req, |
| 332 | SubActionVal: subActionVal, |
Onur Kalinagac | 9f9faca | 2021-01-21 14:04:34 +0000 | [diff] [blame] | 333 | GroupAddress: string(options.Args.GroupAddress), |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 334 | } |
| 335 | res, err := client.GetONU(ctx, igmpReq.OnuReq) |
| 336 | if err != nil { |
| 337 | log.WithFields(log.Fields{ |
| 338 | "SerialNumber": options.Args.OnuSn, |
| 339 | }).Errorf("Cannot not get details on ONU error: %v", err) |
| 340 | } |
| 341 | log.WithFields(log.Fields{ |
| 342 | "SerialNumber": igmpReq.OnuReq.SerialNumber, |
Onur Kalinagac | 9f9faca | 2021-01-21 14:04:34 +0000 | [diff] [blame] | 343 | }).Debugf("ONU has identified : %s", res) |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 344 | |
| 345 | igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq) |
| 346 | if igmpErr != nil { |
| 347 | log.WithFields(log.Fields{ |
| 348 | "SubAction": options.Args.SubAction, |
| 349 | }).Errorf("Could not process Action: error: %v", igmpErr) |
| 350 | } else { |
| 351 | log.WithFields(log.Fields{ |
| 352 | "SubAction": options.Args.SubAction, |
| 353 | }).Debugf("igmp state has been changed with response: %s", |
| 354 | igmpRes.Message) |
| 355 | } |
| 356 | |
| 357 | return nil |
| 358 | } |
| 359 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 360 | func (options *ONUFlows) Execute(args []string) error { |
| 361 | client, conn := connect() |
| 362 | defer conn.Close() |
| 363 | |
| 364 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 365 | defer cancel() |
| 366 | req := pb.ONURequest{ |
| 367 | SerialNumber: string(options.Args.OnuSn), |
| 368 | } |
| 369 | res, err := client.GetFlows(ctx, &req) |
| 370 | if err != nil { |
| 371 | log.Errorf("Cannot get flows for ONU %s: %v", options.Args.OnuSn, err) |
| 372 | return err |
| 373 | } |
| 374 | |
| 375 | if res.Flows == nil { |
| 376 | fmt.Println(fmt.Sprintf("ONU %s has no flows", options.Args.OnuSn)) |
| 377 | return nil |
| 378 | } |
| 379 | |
| 380 | flowHeader := []string{ |
| 381 | "access_intf_id", |
| 382 | "onu_id", |
| 383 | "uni_id", |
| 384 | "flow_id", |
| 385 | "flow_type", |
| 386 | "eth_type", |
| 387 | "alloc_id", |
| 388 | "network_intf_id", |
| 389 | "gemport_id", |
| 390 | "classifier", |
| 391 | "action", |
| 392 | "priority", |
| 393 | "cookie", |
| 394 | "port_no", |
| 395 | } |
| 396 | |
| 397 | tableFlow := tablewriter.NewWriter(os.Stdout) |
| 398 | tableFlow.SetRowLine(true) |
| 399 | fmt.Fprintf(os.Stdout, "ONU Flows:\n") |
| 400 | tableFlow.SetHeader(flowHeader) |
| 401 | |
| 402 | for _, flow := range res.Flows { |
| 403 | flowInfo := []string{} |
| 404 | flowInfo = append(flowInfo, |
| 405 | strconv.Itoa(int(flow.AccessIntfId)), |
| 406 | strconv.Itoa(int(flow.OnuId)), |
| 407 | strconv.Itoa(int(flow.UniId)), |
| 408 | strconv.Itoa(int(flow.FlowId)), |
| 409 | flow.FlowType, |
| 410 | fmt.Sprintf("%x", flow.Classifier.EthType), |
| 411 | strconv.Itoa(int(flow.AllocId)), |
| 412 | strconv.Itoa(int(flow.NetworkIntfId)), |
| 413 | strconv.Itoa(int(flow.GemportId)), |
| 414 | flow.Classifier.String(), |
| 415 | flow.Action.String(), |
| 416 | strconv.Itoa(int(flow.Priority)), |
| 417 | strconv.Itoa(int(flow.Cookie)), |
| 418 | strconv.Itoa(int(flow.PortNo)), |
| 419 | ) |
| 420 | tableFlow.Append(flowInfo) |
| 421 | } |
| 422 | tableFlow.Render() |
| 423 | tableFlow.SetNewLine("") |
| 424 | return nil |
| 425 | } |
| 426 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 427 | func (onuSn *OnuSnString) Complete(match string) []flags.Completion { |
| 428 | client, conn := connect() |
| 429 | defer conn.Close() |
| 430 | |
| 431 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 432 | defer cancel() |
| 433 | |
| 434 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 435 | if err != nil { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 436 | log.Fatalf("could not get ONUs: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 437 | return nil |
| 438 | } |
| 439 | |
| 440 | list := make([]flags.Completion, 0) |
| 441 | for _, k := range onus.Items { |
| 442 | if strings.HasPrefix(k.SerialNumber, match) { |
| 443 | list = append(list, flags.Completion{Item: k.SerialNumber}) |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return list |
| 448 | } |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 449 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 450 | func (onuSn *IgmpSubAction) Complete(match string) []flags.Completion { |
| 451 | list := make([]flags.Completion, 0) |
| 452 | for _, k := range IgmpAllowedActions { |
| 453 | if strings.HasPrefix(k, match) { |
| 454 | list = append(list, flags.Completion{Item: k}) |
| 455 | } |
| 456 | } |
| 457 | return list |
| 458 | } |
| 459 | |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 460 | func (options *ONUTrafficSchedulers) Execute(args []string) error { |
| 461 | client, conn := connect() |
| 462 | defer conn.Close() |
| 463 | |
| 464 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 465 | defer cancel() |
| 466 | req := pb.ONURequest{ |
| 467 | SerialNumber: string(options.Args.OnuSn), |
| 468 | } |
| 469 | res, err := client.GetOnuTrafficSchedulers(ctx, &req) |
| 470 | if err != nil { |
| 471 | log.Fatalf("Cannot get traffic schedulers for ONU %s: %v", options.Args.OnuSn, err) |
| 472 | return err |
| 473 | } |
| 474 | |
| 475 | if res.TraffSchedulers == nil { |
| 476 | log.Fatalf("Cannot get traffic schedulers for ONU: %s (unavailable)", options.Args.OnuSn) |
| 477 | return nil |
| 478 | } |
| 479 | |
| 480 | SchedulerHeader := []string{"Direction", |
| 481 | "AllocId", |
| 482 | "Scheduler.Direction", |
| 483 | "Scheduler.AdditionalBw", |
| 484 | "Scheduler.Priority", |
| 485 | "Scheduler.Weight", |
| 486 | "Scheduler.SchedPolicy", |
| 487 | } |
| 488 | |
| 489 | ShapingInfoHeader := []string{"InferredAdditionBwIndication", |
| 490 | "Cbs", |
| 491 | "Cir", |
| 492 | "Gir", |
| 493 | "Pbs", |
| 494 | "Pir", |
| 495 | } |
| 496 | |
| 497 | SchedulerVals := []string{} |
| 498 | ShapingInfoVals := []string{} |
| 499 | for _, v := range res.TraffSchedulers.TrafficScheds { |
| 500 | SchedulerVals = append(SchedulerVals, |
| 501 | v.GetDirection().String(), |
| 502 | strconv.Itoa(int(v.GetAllocId())), |
| 503 | v.Scheduler.GetDirection().String(), |
| 504 | v.Scheduler.GetAdditionalBw().String(), |
| 505 | strconv.Itoa(int(v.Scheduler.GetPriority())), |
| 506 | strconv.Itoa(int(v.Scheduler.GetWeight())), |
| 507 | v.GetScheduler().GetSchedPolicy().String(), |
| 508 | ) |
| 509 | |
| 510 | ShapingInfoVals = append(ShapingInfoVals, |
| 511 | v.TrafficShapingInfo.GetAddBwInd().String(), |
| 512 | strconv.Itoa(int(v.TrafficShapingInfo.GetCbs())), |
| 513 | strconv.Itoa(int(v.TrafficShapingInfo.GetCir())), |
| 514 | strconv.Itoa(int(v.TrafficShapingInfo.GetGir())), |
| 515 | strconv.Itoa(int(v.TrafficShapingInfo.GetPbs())), |
| 516 | strconv.Itoa(int(v.TrafficShapingInfo.GetPir())), |
| 517 | ) |
| 518 | } |
| 519 | |
| 520 | fmt.Fprintf(os.Stdout, "OnuId: %d \n", int(res.TraffSchedulers.OnuId)) |
| 521 | fmt.Fprintf(os.Stdout, "IntfId: %d \n", int(res.TraffSchedulers.IntfId)) |
| 522 | fmt.Fprintf(os.Stdout, "UniId: %d \n", int(res.TraffSchedulers.UniId)) |
| 523 | fmt.Fprintf(os.Stdout, "OnuPortNo: %d \n", int(res.TraffSchedulers.PortNo)) |
| 524 | |
| 525 | tableSched := tablewriter.NewWriter(os.Stdout) |
| 526 | tableSched.SetRowLine(true) |
| 527 | fmt.Fprintf(os.Stdout, "Traffic Schedulers Info:\n") |
| 528 | tableSched.SetHeader(SchedulerHeader) |
| 529 | tableSched.Append(SchedulerVals) |
| 530 | tableSched.Render() |
| 531 | tableSched.SetNewLine("") |
| 532 | |
| 533 | tableShap := tablewriter.NewWriter(os.Stdout) |
| 534 | tableShap.SetRowLine(true) |
| 535 | fmt.Fprintf(os.Stdout, "Traffic Shaping Info:\n") |
| 536 | tableShap.SetHeader(ShapingInfoHeader) |
| 537 | tableShap.Append(ShapingInfoVals) |
| 538 | tableShap.Render() |
| 539 | |
| 540 | return nil |
| 541 | } |