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 |
| 38 | type ONUList struct{} |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 39 | |
| 40 | type ONUGet struct { |
| 41 | Args struct { |
| 42 | OnuSn OnuSnString |
| 43 | } `positional-args:"yes" required:"yes"` |
| 44 | } |
| 45 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 46 | type ONUShutDown struct { |
| 47 | Args struct { |
| 48 | OnuSn OnuSnString |
| 49 | } `positional-args:"yes" required:"yes"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 52 | type ONUPowerOn struct { |
| 53 | Args struct { |
| 54 | OnuSn OnuSnString |
| 55 | } `positional-args:"yes" required:"yes"` |
| 56 | } |
| 57 | |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 58 | type ONUEapolRestart struct { |
| 59 | Args struct { |
| 60 | OnuSn OnuSnString |
| 61 | } `positional-args:"yes" required:"yes"` |
| 62 | } |
| 63 | |
| 64 | type ONUDhcpRestart struct { |
| 65 | Args struct { |
| 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 ONUOptions struct { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 71 | List ONUList `command:"list"` |
| 72 | Get ONUGet `command:"get"` |
| 73 | ShutDown ONUShutDown `command:"shutdown"` |
| 74 | PowerOn ONUPowerOn `command:"poweron"` |
| 75 | RestartEapol ONUEapolRestart `command:"auth_restart"` |
| 76 | RestartDchp ONUDhcpRestart `command:"dhcp_restart"` |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func RegisterONUCommands(parser *flags.Parser) { |
| 80 | parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{}) |
| 81 | } |
| 82 | |
| 83 | func connect() (pb.BBSimClient, *grpc.ClientConn) { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 84 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 85 | |
| 86 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 87 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 88 | return nil, conn |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 89 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 90 | return pb.NewBBSimClient(conn), conn |
| 91 | } |
| 92 | |
| 93 | func getONUs() *pb.ONUs { |
| 94 | |
| 95 | client, conn := connect() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 96 | defer conn.Close() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 97 | |
| 98 | // Contact the server and print out its response. |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 99 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 100 | defer cancel() |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 101 | |
| 102 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 103 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 104 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 105 | return nil |
| 106 | } |
| 107 | return onus |
| 108 | } |
| 109 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 110 | func (options *ONUList) Execute(args []string) error { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 111 | onus := getONUs() |
| 112 | |
| 113 | // print out |
| 114 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 115 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 116 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 117 | } |
| 118 | |
| 119 | return nil |
| 120 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 121 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 122 | func (options *ONUGet) Execute(args []string) error { |
| 123 | client, conn := connect() |
| 124 | defer conn.Close() |
| 125 | |
| 126 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 127 | defer cancel() |
| 128 | req := pb.ONURequest{ |
| 129 | SerialNumber: string(options.Args.OnuSn), |
| 130 | } |
| 131 | res, err := client.GetONU(ctx, &req) |
| 132 | |
| 133 | if err != nil { |
| 134 | log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err) |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 139 | if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil { |
| 140 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 141 | } |
| 142 | |
| 143 | return nil |
| 144 | } |
| 145 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 146 | func (options *ONUShutDown) Execute(args []string) error { |
| 147 | |
| 148 | client, conn := connect() |
| 149 | defer conn.Close() |
| 150 | |
| 151 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 152 | defer cancel() |
| 153 | req := pb.ONURequest{ |
| 154 | SerialNumber: string(options.Args.OnuSn), |
| 155 | } |
| 156 | res, err := client.ShutdownONU(ctx, &req) |
| 157 | |
| 158 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 159 | log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 160 | return err |
| 161 | } |
| 162 | |
| 163 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 164 | |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | func (options *ONUPowerOn) Execute(args []string) error { |
| 169 | client, conn := connect() |
| 170 | defer conn.Close() |
| 171 | |
| 172 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 173 | defer cancel() |
| 174 | req := pb.ONURequest{ |
| 175 | SerialNumber: string(options.Args.OnuSn), |
| 176 | } |
| 177 | res, err := client.PoweronONU(ctx, &req) |
| 178 | |
| 179 | if err != nil { |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 180 | log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err) |
| 181 | return err |
| 182 | } |
| 183 | |
| 184 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 185 | |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | func (options *ONUEapolRestart) Execute(args []string) error { |
| 190 | client, conn := connect() |
| 191 | defer conn.Close() |
| 192 | |
| 193 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 194 | defer cancel() |
| 195 | req := pb.ONURequest{ |
| 196 | SerialNumber: string(options.Args.OnuSn), |
| 197 | } |
| 198 | res, err := client.RestartEapol(ctx, &req) |
| 199 | |
| 200 | if err != nil { |
| 201 | log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err) |
| 202 | return err |
| 203 | } |
| 204 | |
| 205 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 206 | |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | func (options *ONUDhcpRestart) Execute(args []string) error { |
| 211 | client, conn := connect() |
| 212 | defer conn.Close() |
| 213 | |
| 214 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 215 | defer cancel() |
| 216 | req := pb.ONURequest{ |
| 217 | SerialNumber: string(options.Args.OnuSn), |
| 218 | } |
| 219 | res, err := client.RestartDhcp(ctx, &req) |
| 220 | |
| 221 | if err != nil { |
| 222 | 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] | 223 | return err |
| 224 | } |
| 225 | |
| 226 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 227 | |
| 228 | return nil |
| 229 | } |
| 230 | |
| 231 | func (onuSn *OnuSnString) Complete(match string) []flags.Completion { |
| 232 | client, conn := connect() |
| 233 | defer conn.Close() |
| 234 | |
| 235 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 236 | defer cancel() |
| 237 | |
| 238 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 239 | if err != nil { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 240 | log.Fatalf("could not get ONUs: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 241 | return nil |
| 242 | } |
| 243 | |
| 244 | list := make([]flags.Completion, 0) |
| 245 | for _, k := range onus.Items { |
| 246 | if strings.HasPrefix(k.SerialNumber, match) { |
| 247 | list = append(list, flags.Completion{Item: k.SerialNumber}) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return list |
| 252 | } |