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{} |
| 39 | type ONUShutDown struct { |
| 40 | Args struct { |
| 41 | OnuSn OnuSnString |
| 42 | } `positional-args:"yes" required:"yes"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 45 | type ONUPowerOn struct { |
| 46 | Args struct { |
| 47 | OnuSn OnuSnString |
| 48 | } `positional-args:"yes" required:"yes"` |
| 49 | } |
| 50 | |
| 51 | type ONUOptions struct { |
| 52 | List ONUList `command:"list"` |
| 53 | ShutDown ONUShutDown `command:"shutdown"` |
| 54 | PowerOn ONUPowerOn `command:"poweron"` |
| 55 | } |
| 56 | |
| 57 | func RegisterONUCommands(parser *flags.Parser) { |
| 58 | parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{}) |
| 59 | } |
| 60 | |
| 61 | func connect() (pb.BBSimClient, *grpc.ClientConn) { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 62 | conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure()) |
| 63 | |
| 64 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 65 | log.Fatalf("did not connect: %v", err) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 66 | return nil, conn |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 67 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 68 | return pb.NewBBSimClient(conn), conn |
| 69 | } |
| 70 | |
| 71 | func getONUs() *pb.ONUs { |
| 72 | |
| 73 | client, conn := connect() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 74 | defer conn.Close() |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 75 | |
| 76 | // Contact the server and print out its response. |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 77 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 78 | defer cancel() |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 79 | |
| 80 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 81 | if err != nil { |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 82 | log.Fatalf("could not get OLT: %v", err) |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 83 | return nil |
| 84 | } |
| 85 | return onus |
| 86 | } |
| 87 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 88 | func (options *ONUList) Execute(args []string) error { |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 89 | onus := getONUs() |
| 90 | |
| 91 | // print out |
| 92 | tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT) |
| 93 | if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil { |
| 94 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 95 | } |
| 96 | |
| 97 | return nil |
| 98 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 99 | |
| 100 | func (options *ONUShutDown) Execute(args []string) error { |
| 101 | |
| 102 | client, conn := connect() |
| 103 | defer conn.Close() |
| 104 | |
| 105 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 106 | defer cancel() |
| 107 | req := pb.ONURequest{ |
| 108 | SerialNumber: string(options.Args.OnuSn), |
| 109 | } |
| 110 | res, err := client.ShutdownONU(ctx, &req) |
| 111 | |
| 112 | if err != nil { |
| 113 | log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err) |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 118 | |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func (options *ONUPowerOn) 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.PoweronONU(ctx, &req) |
| 132 | |
| 133 | if err != nil { |
| 134 | log.Fatalf("Cannot not power on ONU %s: %v", options.Args.OnuSn, err) |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 139 | |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | func (onuSn *OnuSnString) Complete(match string) []flags.Completion { |
| 144 | client, conn := connect() |
| 145 | defer conn.Close() |
| 146 | |
| 147 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 148 | defer cancel() |
| 149 | |
| 150 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 151 | if err != nil { |
| 152 | log.Fatal("could not get ONUs: %v", err) |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | list := make([]flags.Completion, 0) |
| 157 | for _, k := range onus.Items { |
| 158 | if strings.HasPrefix(k.SerialNumber, match) { |
| 159 | list = append(list, flags.Completion{Item: k.SerialNumber}) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return list |
| 164 | } |