blob: 834c2e7b3c1c728bcb695eb515b36b1b282ed9e6 [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"
Nitin Subramanian150f1bb2021-08-02 12:04:05 -070022 "fmt"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070023 "os"
Nitin Subramaniandf9c2d82021-07-19 11:05:03 -070024 "strconv"
25 "strings"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070026
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
34const (
Elia Battistonac63b112022-01-12 18:40:49 +010035 DEFAULT_UNI_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Type }}"
36 DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Type }}\t{{ .Services }}"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070037)
38
39type UniIdInt string
40
41type UNIList struct {
42 Verbose bool `short:"v" long:"verbose" description:"Print all the Unis from all the ONUs"`
43}
44
45type 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
52type 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 Subramanian150f1bb2021-08-02 12:04:05 -070060type UNIEapolRestart struct {
61 Args struct {
62 OnuSn OnuSnString
63 UniId UniIdInt
64 } `positional-args:"yes" required:"yes"`
65}
66
67type UNIDhcpRestart struct {
68 Args struct {
69 OnuSn OnuSnString
70 UniId UniIdInt
71 } `positional-args:"yes" required:"yes"`
72}
73
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070074type UNIOptions struct {
Nitin Subramanian150f1bb2021-08-02 12:04:05 -070075 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 Subramanianb0a333a2021-07-08 15:01:41 -070080}
81
82func RegisterUNICommands(parser *flags.Parser) {
83 _, _ = parser.AddCommand("uni", "UNI Commands", "Commands to query and manipulate UNIs", &UNIOptions{})
84}
85
86func (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
113func (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
Abhay Kumarc5723cc2023-06-08 12:09:30 +0530143// Get Services for specified UNI
144// First get UNIs from specified ONU SN
145// Then get Services from appropriate UNI
Nitin Subramanianb0a333a2021-07-08 15:01:41 -0700146func (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 Subramaniandf9c2d82021-07-19 11:05:03 -0700161
Nitin Subramanian150f1bb2021-08-02 12:04:05 -0700162func (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
185func (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 Subramaniandf9c2d82021-07-19 11:05:03 -0700208func (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}