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