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