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" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 23 | "github.com/jessevdk/go-flags" |
| 24 | pb "github.com/opencord/bbsim/api/bbsim" |
| 25 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 26 | "github.com/opencord/cordctl/pkg/format" |
| 27 | log "github.com/sirupsen/logrus" |
| 28 | "google.golang.org/grpc" |
| 29 | "os" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 30 | "strings" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | const ( |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 34 | DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .CTag }}\t{{ .OperState }}\t{{ .InternalState }}" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 37 | type OnuSnString string |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 38 | type IgmpSubAction string |
| 39 | |
| 40 | const IgmpJoinKey string = "join" |
| 41 | const IgmpLeaveKey string = "leave" |
Arjun E K | dd443f0 | 2020-02-07 15:24:01 +0000 | [diff] [blame^] | 42 | const IgmpJoinKeyV3 string = "joinv3" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 43 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 44 | type ONUList struct{} |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 45 | |
| 46 | type ONUGet struct { |
| 47 | Args struct { |
| 48 | OnuSn OnuSnString |
| 49 | } `positional-args:"yes" required:"yes"` |
| 50 | } |
| 51 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 52 | type ONUShutDown struct { |
| 53 | Args struct { |
| 54 | OnuSn OnuSnString |
| 55 | } `positional-args:"yes" required:"yes"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 58 | type ONUPowerOn struct { |
| 59 | Args struct { |
| 60 | OnuSn OnuSnString |
| 61 | } `positional-args:"yes" required:"yes"` |
| 62 | } |
| 63 | |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 64 | type ONUEapolRestart struct { |
| 65 | Args struct { |
| 66 | OnuSn OnuSnString |
| 67 | } `positional-args:"yes" required:"yes"` |
| 68 | } |
| 69 | |
| 70 | type ONUDhcpRestart struct { |
| 71 | Args struct { |
| 72 | OnuSn OnuSnString |
| 73 | } `positional-args:"yes" required:"yes"` |
| 74 | } |
| 75 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 76 | type ONUIgmp struct { |
| 77 | Args struct { |
| 78 | OnuSn OnuSnString |
| 79 | SubAction IgmpSubAction |
| 80 | } `positional-args:"yes" required:"yes"` |
| 81 | } |
| 82 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 83 | type ONUOptions struct { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 84 | List ONUList `command:"list"` |
| 85 | Get ONUGet `command:"get"` |
| 86 | ShutDown ONUShutDown `command:"shutdown"` |
| 87 | PowerOn ONUPowerOn `command:"poweron"` |
| 88 | RestartEapol ONUEapolRestart `command:"auth_restart"` |
| 89 | RestartDchp ONUDhcpRestart `command:"dhcp_restart"` |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 90 | Igmp ONUIgmp `command:"igmp"` |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func RegisterONUCommands(parser *flags.Parser) { |
| 94 | parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{}) |
| 95 | } |
| 96 | |
| 97 | func connect() (pb.BBSimClient, *grpc.ClientConn) { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 98 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 99 | |
| 100 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 101 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 102 | return nil, conn |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 103 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 104 | return pb.NewBBSimClient(conn), conn |
| 105 | } |
| 106 | |
| 107 | func getONUs() *pb.ONUs { |
| 108 | |
| 109 | client, conn := connect() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 110 | defer conn.Close() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 111 | |
| 112 | // Contact the server and print out its response. |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 113 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 114 | defer cancel() |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 115 | |
| 116 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 117 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 118 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 119 | return nil |
| 120 | } |
| 121 | return onus |
| 122 | } |
| 123 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 124 | func (options *ONUList) Execute(args []string) error { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 125 | onus := getONUs() |
| 126 | |
| 127 | // print out |
| 128 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 129 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 130 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 131 | } |
| 132 | |
| 133 | return nil |
| 134 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 136 | func (options *ONUGet) Execute(args []string) error { |
| 137 | client, conn := connect() |
| 138 | defer conn.Close() |
| 139 | |
| 140 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 141 | defer cancel() |
| 142 | req := pb.ONURequest{ |
| 143 | SerialNumber: string(options.Args.OnuSn), |
| 144 | } |
| 145 | res, err := client.GetONU(ctx, &req) |
| 146 | |
| 147 | if err != nil { |
| 148 | log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err) |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 153 | if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil { |
| 154 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 155 | } |
| 156 | |
| 157 | return nil |
| 158 | } |
| 159 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 160 | func (options *ONUShutDown) Execute(args []string) error { |
| 161 | |
| 162 | client, conn := connect() |
| 163 | defer conn.Close() |
| 164 | |
| 165 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 166 | defer cancel() |
| 167 | req := pb.ONURequest{ |
| 168 | SerialNumber: string(options.Args.OnuSn), |
| 169 | } |
| 170 | res, err := client.ShutdownONU(ctx, &req) |
| 171 | |
| 172 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 173 | log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 174 | return err |
| 175 | } |
| 176 | |
| 177 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 178 | |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | func (options *ONUPowerOn) Execute(args []string) error { |
| 183 | client, conn := connect() |
| 184 | defer conn.Close() |
| 185 | |
| 186 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 187 | defer cancel() |
| 188 | req := pb.ONURequest{ |
| 189 | SerialNumber: string(options.Args.OnuSn), |
| 190 | } |
| 191 | res, err := client.PoweronONU(ctx, &req) |
| 192 | |
| 193 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 194 | log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err) |
| 195 | return err |
| 196 | } |
| 197 | |
| 198 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 199 | |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | func (options *ONUEapolRestart) Execute(args []string) error { |
| 204 | client, conn := connect() |
| 205 | defer conn.Close() |
| 206 | |
| 207 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 208 | defer cancel() |
| 209 | req := pb.ONURequest{ |
| 210 | SerialNumber: string(options.Args.OnuSn), |
| 211 | } |
| 212 | res, err := client.RestartEapol(ctx, &req) |
| 213 | |
| 214 | if err != nil { |
| 215 | log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err) |
| 216 | return err |
| 217 | } |
| 218 | |
| 219 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 220 | |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | func (options *ONUDhcpRestart) Execute(args []string) error { |
| 225 | client, conn := connect() |
| 226 | defer conn.Close() |
| 227 | |
| 228 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 229 | defer cancel() |
| 230 | req := pb.ONURequest{ |
| 231 | SerialNumber: string(options.Args.OnuSn), |
| 232 | } |
| 233 | res, err := client.RestartDhcp(ctx, &req) |
| 234 | |
| 235 | if err != nil { |
| 236 | 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] | 237 | return err |
| 238 | } |
| 239 | |
| 240 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 241 | |
| 242 | return nil |
| 243 | } |
| 244 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 245 | func (options *ONUIgmp) Execute(args []string) error { |
| 246 | client, conn := connect() |
| 247 | defer conn.Close() |
| 248 | |
| 249 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 250 | defer cancel() |
| 251 | |
| 252 | req := pb.ONURequest{ |
| 253 | SerialNumber: string(options.Args.OnuSn), |
| 254 | } |
| 255 | |
| 256 | var subActionVal pb.SubActionTypes |
| 257 | if string(options.Args.SubAction) == IgmpJoinKey { |
| 258 | subActionVal = pb.SubActionTypes_JOIN |
| 259 | } else if string(options.Args.SubAction) == IgmpLeaveKey { |
| 260 | subActionVal = pb.SubActionTypes_LEAVE |
Arjun E K | dd443f0 | 2020-02-07 15:24:01 +0000 | [diff] [blame^] | 261 | } else if string(options.Args.SubAction) == IgmpJoinKeyV3 { |
| 262 | subActionVal = pb.SubActionTypes_JOINV3 |
| 263 | } |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 264 | |
| 265 | igmpReq := pb.IgmpRequest{ |
| 266 | OnuReq: &req, |
| 267 | SubActionVal: subActionVal, |
| 268 | } |
| 269 | res, err := client.GetONU(ctx, igmpReq.OnuReq) |
| 270 | if err != nil { |
| 271 | log.WithFields(log.Fields{ |
| 272 | "SerialNumber": options.Args.OnuSn, |
| 273 | }).Errorf("Cannot not get details on ONU error: %v", err) |
| 274 | } |
| 275 | log.WithFields(log.Fields{ |
| 276 | "SerialNumber": igmpReq.OnuReq.SerialNumber, |
| 277 | }).Debugf("ONU has indentified : %s", res) |
| 278 | |
| 279 | igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq) |
| 280 | if igmpErr != nil { |
| 281 | log.WithFields(log.Fields{ |
| 282 | "SubAction": options.Args.SubAction, |
| 283 | }).Errorf("Could not process Action: error: %v", igmpErr) |
| 284 | } else { |
| 285 | log.WithFields(log.Fields{ |
| 286 | "SubAction": options.Args.SubAction, |
| 287 | }).Debugf("igmp state has been changed with response: %s", |
| 288 | igmpRes.Message) |
| 289 | } |
| 290 | |
| 291 | return nil |
| 292 | } |
| 293 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 294 | func (onuSn *OnuSnString) Complete(match string) []flags.Completion { |
| 295 | client, conn := connect() |
| 296 | defer conn.Close() |
| 297 | |
| 298 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 299 | defer cancel() |
| 300 | |
| 301 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 302 | if err != nil { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 303 | log.Fatalf("could not get ONUs: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 304 | return nil |
| 305 | } |
| 306 | |
| 307 | list := make([]flags.Completion, 0) |
| 308 | for _, k := range onus.Items { |
| 309 | if strings.HasPrefix(k.SerialNumber, match) { |
| 310 | list = append(list, flags.Completion{Item: k.SerialNumber}) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return list |
| 315 | } |