Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -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" |
Nitin Subramanian | 150f1bb | 2021-08-02 12:04:05 -0700 | [diff] [blame] | 22 | "fmt" |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 23 | "os" |
Nitin Subramanian | df9c2d8 | 2021-07-19 11:05:03 -0700 | [diff] [blame] | 24 | "strconv" |
| 25 | "strings" |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/jessevdk/go-flags" |
| 28 | pb "github.com/opencord/bbsim/api/bbsim" |
| 29 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 30 | "github.com/opencord/cordctl/pkg/format" |
| 31 | log "github.com/sirupsen/logrus" |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | DEFAULT_UNI_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}" |
| 36 | DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}" |
| 37 | ) |
| 38 | |
| 39 | type UniIdInt string |
| 40 | |
| 41 | type UNIList struct { |
| 42 | Verbose bool `short:"v" long:"verbose" description:"Print all the Unis from all the ONUs"` |
| 43 | } |
| 44 | |
| 45 | type UNIGet struct { |
| 46 | Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about UNIs"` |
| 47 | Args struct { |
| 48 | OnuSn OnuSnString |
| 49 | } `positional-args:"yes" required:"yes"` |
| 50 | } |
| 51 | |
| 52 | type UNIServices struct { |
| 53 | Verbose bool `short:"v" long:"verbose" description:"Print all the Services from the specified UNI"` |
| 54 | Args struct { |
| 55 | OnuSn OnuSnString |
| 56 | UniId UniIdInt |
| 57 | } `positional-args:"yes"` |
| 58 | } |
| 59 | |
Nitin Subramanian | 150f1bb | 2021-08-02 12:04:05 -0700 | [diff] [blame] | 60 | type UNIEapolRestart struct { |
| 61 | Args struct { |
| 62 | OnuSn OnuSnString |
| 63 | UniId UniIdInt |
| 64 | } `positional-args:"yes" required:"yes"` |
| 65 | } |
| 66 | |
| 67 | type UNIDhcpRestart struct { |
| 68 | Args struct { |
| 69 | OnuSn OnuSnString |
| 70 | UniId UniIdInt |
| 71 | } `positional-args:"yes" required:"yes"` |
| 72 | } |
| 73 | |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 74 | type UNIOptions struct { |
Nitin Subramanian | 150f1bb | 2021-08-02 12:04:05 -0700 | [diff] [blame] | 75 | List UNIList `command:"list"` |
| 76 | Get UNIGet `command:"get"` |
| 77 | Services UNIServices `command:"services"` |
| 78 | RestartEapol UNIEapolRestart `command:"auth_restart"` |
| 79 | RestartDchp UNIDhcpRestart `command:"dhcp_restart"` |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func RegisterUNICommands(parser *flags.Parser) { |
| 83 | _, _ = parser.AddCommand("uni", "UNI Commands", "Commands to query and manipulate UNIs", &UNIOptions{}) |
| 84 | } |
| 85 | |
| 86 | func (options *UNIList) Execute(args []string) error { |
| 87 | client, conn := connect() |
| 88 | defer conn.Close() |
| 89 | |
| 90 | // Contact the server and print out its response. |
| 91 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 92 | defer cancel() |
| 93 | |
| 94 | unis, err := client.GetUnis(ctx, &pb.Empty{}) |
| 95 | if err != nil { |
| 96 | log.Fatalf("could not get UNIs: %v", err) |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | var tableFormat format.Format |
| 101 | if options.Verbose { |
| 102 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES) |
| 103 | } else { |
| 104 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT) |
| 105 | } |
| 106 | if err := tableFormat.Execute(os.Stdout, true, unis.Items); err != nil { |
| 107 | log.Fatalf("Error while formatting Unis table: %s", err) |
| 108 | } |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func (options *UNIGet) Execute(args []string) error { |
| 114 | |
| 115 | client, conn := connect() |
| 116 | defer conn.Close() |
| 117 | |
| 118 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 119 | defer cancel() |
| 120 | req := pb.ONURequest{ |
| 121 | SerialNumber: string(options.Args.OnuSn), |
| 122 | } |
| 123 | res, err := client.GetOnuUnis(ctx, &req) |
| 124 | |
| 125 | if err != nil { |
| 126 | log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err) |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | var tableFormat format.Format |
| 131 | if options.Verbose { |
| 132 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES) |
| 133 | } else { |
| 134 | tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT) |
| 135 | } |
| 136 | if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil { |
| 137 | log.Fatalf("Error while formatting Unis table: %s", err) |
| 138 | } |
| 139 | |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | //Get Services for specified UNI |
| 144 | //First get UNIs from specified ONU SN |
| 145 | //Then get Services from appropriate UNI |
| 146 | func (options *UNIServices) Execute(args []string) error { |
| 147 | services, err := getServices(string(options.Args.OnuSn), string(options.Args.UniId)) |
| 148 | |
| 149 | if err != nil { |
| 150 | log.Fatalf("Cannot not get services for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err) |
| 151 | return err |
| 152 | } |
| 153 | |
| 154 | tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT) |
| 155 | if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil { |
| 156 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |
Nitin Subramanian | df9c2d8 | 2021-07-19 11:05:03 -0700 | [diff] [blame] | 161 | |
Nitin Subramanian | 150f1bb | 2021-08-02 12:04:05 -0700 | [diff] [blame] | 162 | func (options *UNIEapolRestart) Execute(args []string) error { |
| 163 | client, conn := connect() |
| 164 | defer conn.Close() |
| 165 | |
| 166 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 167 | defer cancel() |
| 168 | |
| 169 | req := pb.UNIRequest{ |
| 170 | OnuSerialNumber: string(options.Args.OnuSn), |
| 171 | UniID: string(options.Args.UniId), |
| 172 | } |
| 173 | res, err := client.RestartEapol(ctx, &req) |
| 174 | |
| 175 | if err != nil { |
| 176 | log.Fatalf("Cannot restart EAPOL for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err) |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 181 | |
| 182 | return nil |
| 183 | } |
| 184 | |
| 185 | func (options *UNIDhcpRestart) Execute(args []string) error { |
| 186 | client, conn := connect() |
| 187 | defer conn.Close() |
| 188 | |
| 189 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 190 | defer cancel() |
| 191 | |
| 192 | req := pb.UNIRequest{ |
| 193 | OnuSerialNumber: string(options.Args.OnuSn), |
| 194 | UniID: string(options.Args.UniId), |
| 195 | } |
| 196 | res, err := client.RestartDhcp(ctx, &req) |
| 197 | |
| 198 | if err != nil { |
| 199 | log.Fatalf("Cannot restart DHCP for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err) |
| 200 | return err |
| 201 | } |
| 202 | |
| 203 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 204 | |
| 205 | return nil |
| 206 | } |
| 207 | |
Nitin Subramanian | df9c2d8 | 2021-07-19 11:05:03 -0700 | [diff] [blame] | 208 | func (uniId *UniIdInt) Complete(match string) []flags.Completion { |
| 209 | client, conn := connect() |
| 210 | defer conn.Close() |
| 211 | |
| 212 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 213 | defer cancel() |
| 214 | |
| 215 | onus, err := client.GetONUs(ctx, &pb.Empty{}) |
| 216 | if err != nil { |
| 217 | log.Fatalf("could not get ONUs: %v", err) |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | // go-flag doesn't allow us to read the previous parameters to the command so we can't get the ONU Serial Number, |
| 222 | // but since all the ONUs have the same number of UNIs thus we can re-use the UNIs belonging to the first ONU in the list |
| 223 | // pending issue here https://github.com/jessevdk/go-flags/issues/305 |
| 224 | unis := onus.Items[0].Unis |
| 225 | |
| 226 | list := make([]flags.Completion, 0) |
| 227 | for _, uni := range unis { |
| 228 | strID := strconv.Itoa(int(uni.ID)) |
| 229 | if strings.HasPrefix(strID, match) { |
| 230 | list = append(list, flags.Completion{Item: strID}) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return list |
| 235 | } |