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