blob: 81f7c961ffcfd1ebc92cf27f6eb067be5a1771e3 [file] [log] [blame]
Nitin Subramanianb0a333a2021-07-08 15:01:41 -07001/*
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
18package commands
19
20import (
21 "context"
22 "os"
Nitin Subramaniandf9c2d82021-07-19 11:05:03 -070023 "strconv"
24 "strings"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070025
26 "github.com/jessevdk/go-flags"
27 pb "github.com/opencord/bbsim/api/bbsim"
28 "github.com/opencord/bbsim/internal/bbsimctl/config"
29 "github.com/opencord/cordctl/pkg/format"
30 log "github.com/sirupsen/logrus"
31)
32
33const (
34 DEFAULT_UNI_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}"
35 DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}"
36)
37
38type UniIdInt string
39
40type UNIList struct {
41 Verbose bool `short:"v" long:"verbose" description:"Print all the Unis from all the ONUs"`
42}
43
44type UNIGet struct {
45 Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about UNIs"`
46 Args struct {
47 OnuSn OnuSnString
48 } `positional-args:"yes" required:"yes"`
49}
50
51type UNIServices struct {
52 Verbose bool `short:"v" long:"verbose" description:"Print all the Services from the specified UNI"`
53 Args struct {
54 OnuSn OnuSnString
55 UniId UniIdInt
56 } `positional-args:"yes"`
57}
58
59type UNIOptions struct {
60 List UNIList `command:"list"`
61 Get UNIGet `command:"get"`
62 Services UNIServices `command:"services"`
63}
64
65func RegisterUNICommands(parser *flags.Parser) {
66 _, _ = parser.AddCommand("uni", "UNI Commands", "Commands to query and manipulate UNIs", &UNIOptions{})
67}
68
69func (options *UNIList) Execute(args []string) error {
70 client, conn := connect()
71 defer conn.Close()
72
73 // Contact the server and print out its response.
74 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
75 defer cancel()
76
77 unis, err := client.GetUnis(ctx, &pb.Empty{})
78 if err != nil {
79 log.Fatalf("could not get UNIs: %v", err)
80 return err
81 }
82
83 var tableFormat format.Format
84 if options.Verbose {
85 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
86 } else {
87 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
88 }
89 if err := tableFormat.Execute(os.Stdout, true, unis.Items); err != nil {
90 log.Fatalf("Error while formatting Unis table: %s", err)
91 }
92
93 return nil
94}
95
96func (options *UNIGet) Execute(args []string) error {
97
98 client, conn := connect()
99 defer conn.Close()
100
101 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
102 defer cancel()
103 req := pb.ONURequest{
104 SerialNumber: string(options.Args.OnuSn),
105 }
106 res, err := client.GetOnuUnis(ctx, &req)
107
108 if err != nil {
109 log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err)
110 return err
111 }
112
113 var tableFormat format.Format
114 if options.Verbose {
115 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
116 } else {
117 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
118 }
119 if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
120 log.Fatalf("Error while formatting Unis table: %s", err)
121 }
122
123 return nil
124}
125
126//Get Services for specified UNI
127//First get UNIs from specified ONU SN
128//Then get Services from appropriate UNI
129func (options *UNIServices) Execute(args []string) error {
130 services, err := getServices(string(options.Args.OnuSn), string(options.Args.UniId))
131
132 if err != nil {
133 log.Fatalf("Cannot not get services for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err)
134 return err
135 }
136
137 tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
138 if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
139 log.Fatalf("Error while formatting ONUs table: %s", err)
140 }
141
142 return nil
143}
Nitin Subramaniandf9c2d82021-07-19 11:05:03 -0700144
145func (uniId *UniIdInt) Complete(match string) []flags.Completion {
146 client, conn := connect()
147 defer conn.Close()
148
149 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
150 defer cancel()
151
152 onus, err := client.GetONUs(ctx, &pb.Empty{})
153 if err != nil {
154 log.Fatalf("could not get ONUs: %v", err)
155 return nil
156 }
157
158 // go-flag doesn't allow us to read the previous parameters to the command so we can't get the ONU Serial Number,
159 // 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
160 // pending issue here https://github.com/jessevdk/go-flags/issues/305
161 unis := onus.Items[0].Unis
162
163 list := make([]flags.Completion, 0)
164 for _, uni := range unis {
165 strID := strconv.Itoa(int(uni.ID))
166 if strings.HasPrefix(strID, match) {
167 list = append(list, flags.Completion{Item: strID})
168 }
169 }
170
171 return list
172}