blob: d7b91ecd61aeb6f3c28f465c1a1a0ee1b7ec1ecb [file] [log] [blame]
Matteo Scandolo4a036262020-08-17 15:56:13 -07001/*
Joey Armstrong3881b732022-12-27 07:55:37 -05002 * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo4a036262020-08-17 15:56:13 -07003 * 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
18package commands
19
20import (
21 "context"
Elia Battiston9a1da622022-02-09 14:43:52 +010022 "os"
23
Matteo Scandolo4a036262020-08-17 15:56:13 -070024 "github.com/jessevdk/go-flags"
25 pb "github.com/opencord/bbsim/api/bbsim"
26 "github.com/opencord/bbsim/internal/bbsimctl/config"
27 "github.com/opencord/cordctl/pkg/format"
28 log "github.com/sirupsen/logrus"
Matteo Scandolo4a036262020-08-17 15:56:13 -070029)
30
31const (
Elia Battiston9a1da622022-02-09 14:43:52 +010032 DEFAULT_SERVICE_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .UniId }}\t{{ .InternalState }}\t{{ .Name }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .UsSTagPriority }}\t{{ .DsSTagPriority }}\t{{ .CTag }}\t{{ .UsCTagPriority }}\t{{ .DsCTagPriority }}\t{{ .UniTagMatch }}\t{{ .NeedsEapol }}\t{{ .NeedsDhcp }}\t{{ .NeedsIgmp }}\t{{ .NeedsPPPoE }}\t{{ .ConfigureMacAddress }}\t{{ .EnableMacLearning }}\t{{ .GemPort }}\t{{ .EapolState }}\t{{ .DhcpState }}\t{{ .IGMPState }}"
Matteo Scandolo4a036262020-08-17 15:56:13 -070033)
34
35type ServiceList struct{}
36
37type ServiceOptions struct {
38 List ServiceList `command:"list"`
39}
40
41func RegisterServiceCommands(parser *flags.Parser) {
42 _, _ = parser.AddCommand("service", "Service Commands", "Commands to interact with ONU Services", &ServiceOptions{})
43}
44
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070045func getServices(OnuSn string, UniID string) (*pb.Services, error) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070046
47 client, conn := connect()
48 defer conn.Close()
49
50 // Contact the server and print out its response.
51 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
52 defer cancel()
53
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070054 req := pb.UNIRequest{
55 OnuSerialNumber: OnuSn,
56 UniID: UniID,
57 }
58
59 services, err := client.GetServices(ctx, &req)
60
61 return services, err
62}
63
64func (options *ServiceList) Execute(args []string) error {
65 services, err := getServices("", "")
66
Matteo Scandolo4a036262020-08-17 15:56:13 -070067 if err != nil {
68 log.Fatalf("could not get OLT: %v", err)
69 return nil
70 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070071 // print out
72 tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
73 if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
74 log.Fatalf("Error while formatting ONUs table: %s", err)
75 }
76
77 return nil
78}