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 | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 34 | DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\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 | |
| 58 | type ONUOptions struct { |
| 59 | List ONUList `command:"list"` |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 60 | Get ONUGet `command:"get"` |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 61 | ShutDown ONUShutDown `command:"shutdown"` |
| 62 | PowerOn ONUPowerOn `command:"poweron"` |
| 63 | } |
| 64 | |
| 65 | func RegisterONUCommands(parser *flags.Parser) { |
| 66 | parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{}) |
| 67 | } |
| 68 | |
| 69 | func connect() (pb.BBSimClient, *grpc.ClientConn) { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 70 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 71 | |
| 72 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 73 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 74 | return nil, conn |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 75 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 76 | return pb.NewBBSimClient(conn), conn |
| 77 | } |
| 78 | |
| 79 | func getONUs() *pb.ONUs { |
| 80 | |
| 81 | client, conn := connect() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 82 | defer conn.Close() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 83 | |
| 84 | // Contact the server and print out its response. |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 85 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 86 | defer cancel() |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 87 | |
| 88 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 89 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 90 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 91 | return nil |
| 92 | } |
| 93 | return onus |
| 94 | } |
| 95 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 96 | func (options *ONUList) Execute(args []string) error { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 97 | onus := getONUs() |
| 98 | |
| 99 | // print out |
| 100 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 101 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 102 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 103 | } |
| 104 | |
| 105 | return nil |
| 106 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 107 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 108 | func (options *ONUGet) Execute(args []string) error { |
| 109 | client, conn := connect() |
| 110 | defer conn.Close() |
| 111 | |
| 112 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 113 | defer cancel() |
| 114 | req := pb.ONURequest{ |
| 115 | SerialNumber: string(options.Args.OnuSn), |
| 116 | } |
| 117 | res, err := client.GetONU(ctx, &req) |
| 118 | |
| 119 | if err != nil { |
| 120 | log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err) |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 125 | if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil { |
| 126 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 127 | } |
| 128 | |
| 129 | return nil |
| 130 | } |
| 131 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 132 | func (options *ONUShutDown) Execute(args []string) error { |
| 133 | |
| 134 | client, conn := connect() |
| 135 | defer conn.Close() |
| 136 | |
| 137 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 138 | defer cancel() |
| 139 | req := pb.ONURequest{ |
| 140 | SerialNumber: string(options.Args.OnuSn), |
| 141 | } |
| 142 | res, err := client.ShutdownONU(ctx, &req) |
| 143 | |
| 144 | if err != nil { |
| 145 | log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err) |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 150 | |
| 151 | return nil |
| 152 | } |
| 153 | |
| 154 | func (options *ONUPowerOn) Execute(args []string) error { |
| 155 | client, conn := connect() |
| 156 | defer conn.Close() |
| 157 | |
| 158 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 159 | defer cancel() |
| 160 | req := pb.ONURequest{ |
| 161 | SerialNumber: string(options.Args.OnuSn), |
| 162 | } |
| 163 | res, err := client.PoweronONU(ctx, &req) |
| 164 | |
| 165 | if err != nil { |
| 166 | log.Fatalf("Cannot not power on ONU %s: %v", options.Args.OnuSn, err) |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 171 | |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | func (onuSn *OnuSnString) Complete(match string) []flags.Completion { |
| 176 | client, conn := connect() |
| 177 | defer conn.Close() |
| 178 | |
| 179 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 180 | defer cancel() |
| 181 | |
| 182 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 183 | if err != nil { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 184 | log.Fatalf("could not get ONUs: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 185 | return nil |
| 186 | } |
| 187 | |
| 188 | list := make([]flags.Completion, 0) |
| 189 | for _, k := range onus.Items { |
| 190 | if strings.HasPrefix(k.SerialNumber, match) { |
| 191 | list = append(list, flags.Completion{Item: k.SerialNumber}) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return list |
| 196 | } |