blob: ba1c7954a597fdbf77c5deb4066076bfbea868af [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"
23
24 "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"
29)
30
31const (
32 DEFAULT_UNI_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}"
33 DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}"
34)
35
36type UniIdInt string
37
38type UNIList struct {
39 Verbose bool `short:"v" long:"verbose" description:"Print all the Unis from all the ONUs"`
40}
41
42type UNIGet struct {
43 Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about UNIs"`
44 Args struct {
45 OnuSn OnuSnString
46 } `positional-args:"yes" required:"yes"`
47}
48
49type UNIServices struct {
50 Verbose bool `short:"v" long:"verbose" description:"Print all the Services from the specified UNI"`
51 Args struct {
52 OnuSn OnuSnString
53 UniId UniIdInt
54 } `positional-args:"yes"`
55}
56
57type UNIOptions struct {
58 List UNIList `command:"list"`
59 Get UNIGet `command:"get"`
60 Services UNIServices `command:"services"`
61}
62
63func RegisterUNICommands(parser *flags.Parser) {
64 _, _ = parser.AddCommand("uni", "UNI Commands", "Commands to query and manipulate UNIs", &UNIOptions{})
65}
66
67func (options *UNIList) Execute(args []string) error {
68 client, conn := connect()
69 defer conn.Close()
70
71 // Contact the server and print out its response.
72 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
73 defer cancel()
74
75 unis, err := client.GetUnis(ctx, &pb.Empty{})
76 if err != nil {
77 log.Fatalf("could not get UNIs: %v", err)
78 return err
79 }
80
81 var tableFormat format.Format
82 if options.Verbose {
83 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
84 } else {
85 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
86 }
87 if err := tableFormat.Execute(os.Stdout, true, unis.Items); err != nil {
88 log.Fatalf("Error while formatting Unis table: %s", err)
89 }
90
91 return nil
92}
93
94func (options *UNIGet) Execute(args []string) error {
95
96 client, conn := connect()
97 defer conn.Close()
98
99 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
100 defer cancel()
101 req := pb.ONURequest{
102 SerialNumber: string(options.Args.OnuSn),
103 }
104 res, err := client.GetOnuUnis(ctx, &req)
105
106 if err != nil {
107 log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err)
108 return err
109 }
110
111 var tableFormat format.Format
112 if options.Verbose {
113 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
114 } else {
115 tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
116 }
117 if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
118 log.Fatalf("Error while formatting Unis table: %s", err)
119 }
120
121 return nil
122}
123
124//Get Services for specified UNI
125//First get UNIs from specified ONU SN
126//Then get Services from appropriate UNI
127func (options *UNIServices) Execute(args []string) error {
128 services, err := getServices(string(options.Args.OnuSn), string(options.Args.UniId))
129
130 if err != nil {
131 log.Fatalf("Cannot not get services for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err)
132 return err
133 }
134
135 tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
136 if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
137 log.Fatalf("Error while formatting ONUs table: %s", err)
138 }
139
140 return nil
141}