Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -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" |
| 22 | "github.com/jessevdk/go-flags" |
| 23 | pb "github.com/opencord/bbsim/api/bbsim" |
| 24 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 25 | "github.com/opencord/cordctl/pkg/format" |
| 26 | log "github.com/sirupsen/logrus" |
| 27 | "os" |
| 28 | ) |
| 29 | |
| 30 | const ( |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 31 | DEFAULT_SERVICE_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .InternalState }}\t{{ .Name }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .CTag }}\t{{ .NeedsEapol }}\t{{ .NeedsDhcp }}\t{{ .NeedsIgmp }}\t{{ .GemPort }}\t{{ .EapolState }}\t{{ .DhcpState }}" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | type ServiceList struct{} |
| 35 | |
| 36 | type ServiceOptions struct { |
| 37 | List ServiceList `command:"list"` |
| 38 | } |
| 39 | |
| 40 | func RegisterServiceCommands(parser *flags.Parser) { |
| 41 | _, _ = parser.AddCommand("service", "Service Commands", "Commands to interact with ONU Services", &ServiceOptions{}) |
| 42 | } |
| 43 | |
| 44 | func getServices() *pb.Services { |
| 45 | |
| 46 | client, conn := connect() |
| 47 | defer conn.Close() |
| 48 | |
| 49 | // Contact the server and print out its response. |
| 50 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 51 | defer cancel() |
| 52 | |
| 53 | services, err := client.GetServices(ctx, &pb.Empty{}) |
| 54 | if err != nil { |
| 55 | log.Fatalf("could not get OLT: %v", err) |
| 56 | return nil |
| 57 | } |
| 58 | return services |
| 59 | } |
| 60 | |
| 61 | func (options *ServiceList) Execute(args []string) error { |
| 62 | services := getServices() |
| 63 | |
| 64 | // print out |
| 65 | tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT) |
| 66 | if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil { |
| 67 | log.Fatalf("Error while formatting ONUs table: %s", err) |
| 68 | } |
| 69 | |
| 70 | return nil |
| 71 | } |