/
get.go
158 lines (137 loc) · 3.94 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
package uenib
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/onosproject/onos-api/go/onos/uenib"
"github.com/onosproject/onos-lib-go/pkg/cli"
"github.com/spf13/cobra"
)
func getGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get {ue|ues}",
Short: "Get UE information",
}
cmd.AddCommand(getGetUECommand())
cmd.AddCommand(getGetUEsCommand())
return cmd
}
func getGetUECommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ue ue-id [args]",
Args: cobra.ExactArgs(1),
Short: "Get UE information",
RunE: runGetUECommand,
}
cmd.Flags().StringSliceP("aspect", "a", []string{}, "UE aspects to get")
cmd.Flags().Bool("no-headers", false, "disables output headers")
cmd.Flags().BoolP("verbose", "v", false, "whether to print the change with verbose output")
return cmd
}
func getGetUEsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ues [args]",
Args: cobra.ExactArgs(0),
Short: "Get list of UE information",
RunE: runGetUEsCommand,
}
cmd.Flags().StringSliceP("aspect", "a", []string{}, "UE aspects to get")
cmd.Flags().Bool("no-headers", false, "disables output headers")
cmd.Flags().BoolP("verbose", "v", false, "whether to print the change with verbose output")
return cmd
}
func runGetUECommand(cmd *cobra.Command, args []string) error {
noHeaders, _ := cmd.Flags().GetBool("no-headers")
aspectTypes, _ := cmd.Flags().GetStringSlice("aspect")
verbose, _ := cmd.Flags().GetBool("verbose")
// headers do not make sense when printing flat
if verbose {
noHeaders = true
}
writer := os.Stdout
if !noHeaders {
printHeader(writer, false)
}
conn, err := cli.GetConnection(cmd)
if err != nil {
return err
}
defer conn.Close()
client := uenib.CreateUEServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
response, err := client.GetUE(ctx, &uenib.GetUERequest{ID: uenib.ID(args[0]), AspectTypes: aspectTypes})
if err != nil {
cli.Output("Unable to get UE aspects: %s", err)
return err
}
printUE(writer, response.UE, verbose)
return nil
}
func runGetUEsCommand(cmd *cobra.Command, _ []string) error {
noHeaders, _ := cmd.Flags().GetBool("no-headers")
aspectTypes, _ := cmd.Flags().GetStringSlice("aspect")
verbose, _ := cmd.Flags().GetBool("verbose")
// headers do not make sense when printing flat
if verbose {
noHeaders = true
}
writer := os.Stdout
if !noHeaders {
printHeader(writer, false)
}
conn, err := cli.GetConnection(cmd)
if err != nil {
return err
}
defer conn.Close()
client := uenib.CreateUEServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
response, err := client.ListUEs(ctx, &uenib.ListUERequest{AspectTypes: aspectTypes})
if err != nil {
cli.Output("Unable to list UEs: %s", err)
return err
}
for {
resp, err := response.Recv()
if err == io.EOF {
break
} else if err != nil {
cli.Output("Unable to read UE: %s", err)
return err
} else {
printUE(writer, resp.UE, verbose)
}
}
return nil
}
func printHeader(writer *os.File, replay bool) {
if replay {
_, _ = fmt.Fprintf(writer, "%-12s\t%-16s\t%-20s\t%s\n", "Event Type", "UE ID", "Aspect Type", "Aspect Value")
} else {
_, _ = fmt.Fprintf(writer, "%-16s\t%-20s\n", "UE ID", "Aspect Types")
}
}
func printUE(writer io.Writer, ue uenib.UE, verbose bool) {
if !verbose {
_, _ = fmt.Fprintf(writer, "%-16s\t", ue.ID)
aspectTypes := make([]string, 0, len(ue.Aspects))
for k := range ue.Aspects {
aspectTypes = append(aspectTypes, k)
}
_, _ = fmt.Fprintf(writer, "%s\n", strings.Join(aspectTypes[:], ","))
} else {
_, _ = fmt.Fprintf(writer, "ID: %s\n", ue.ID)
_, _ = fmt.Fprintf(writer, "Aspects:\n")
for aspectType, any := range ue.Aspects {
_, _ = fmt.Fprintf(writer, "- %s=%s\n", aspectType, any.Value)
}
}
}