blob: 26e9d9f23bf66c5490d76d5250632ee38f08f15f [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -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 */
17package commands
18
19import (
Scott Baker2c0ebda2019-05-06 16:55:47 -070020 "fmt"
Scott Baker2c0ebda2019-05-06 16:55:47 -070021 flags "github.com/jessevdk/go-flags"
22 "github.com/jhump/protoreflect/dynamic"
Scott Baker2c0ebda2019-05-06 16:55:47 -070023 "strings"
24)
25
Scott Baker63ce82e2019-05-15 09:01:42 -070026type ModelNameString string
27
Scott Baker2c0ebda2019-05-06 16:55:47 -070028type ModelList struct {
29 OutputOptions
Scott Baker5201c0b2019-05-15 15:35:56 -070030 ShowHidden bool `long:"showhidden" description:"Show hidden fields in default output"`
31 ShowFeedback bool `long:"showfeedback" description:"Show feedback fields in default output"`
32 ShowBookkeeping bool `long:"showbookkeeping" description:"Show bookkeeping fields in default output"`
33 Filter string `long:"filter" description:"Comma-separated list of filters"`
Scott Baker6cf525a2019-05-09 12:25:08 -070034 Args struct {
Scott Baker63ce82e2019-05-15 09:01:42 -070035 ModelName ModelNameString
Scott Baker2c0ebda2019-05-06 16:55:47 -070036 } `positional-args:"yes" required:"yes"`
37}
38
Scott Baker5281d002019-05-16 10:45:26 -070039type ModelUpdate struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070040 OutputOptions
Scott Baker5281d002019-05-16 10:45:26 -070041 Filter string `long:"filter" description:"Comma-separated list of filters"`
42 SetFields string `long:"set-field" description:"Comma-separated list of field=value to set"`
43 SetJSON string `long:"set-json" description:"JSON dictionary to use for settings fields"`
44 Args struct {
45 ModelName ModelNameString
46 } `positional-args:"yes" required:"yes"`
47 IDArgs struct {
48 ID []int32
49 } `positional-args:"yes" required:"no"`
Scott Baker6cf525a2019-05-09 12:25:08 -070050}
51
Scott Baker2c0ebda2019-05-06 16:55:47 -070052type ModelOpts struct {
Scott Baker5281d002019-05-16 10:45:26 -070053 List ModelList `command:"list"`
54 Update ModelUpdate `command:"update"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070055}
56
57var modelOpts = ModelOpts{}
58
59func RegisterModelCommands(parser *flags.Parser) {
60 parser.AddCommand("model", "model commands", "Commands to query and manipulate XOS models", &modelOpts)
61}
62
Scott Baker6cf525a2019-05-09 12:25:08 -070063func (options *ModelList) Execute(args []string) error {
64 conn, descriptor, err := InitReflectionClient()
65 if err != nil {
66 return err
67 }
68
69 defer conn.Close()
70
Scott Baker63ce82e2019-05-15 09:01:42 -070071 err = CheckModelName(descriptor, string(options.Args.ModelName))
Scott Baker6cf525a2019-05-09 12:25:08 -070072 if err != nil {
73 return err
74 }
75
Scott Baker5281d002019-05-16 10:45:26 -070076 queries, err := CommaSeparatedQueryToMap(options.Filter, true)
Scott Baker2c0ebda2019-05-06 16:55:47 -070077 if err != nil {
78 return err
79 }
80
Scott Baker5281d002019-05-16 10:45:26 -070081 models, err := ListOrFilterModels(conn, descriptor, string(options.Args.ModelName), queries)
Scott Baker2c0ebda2019-05-06 16:55:47 -070082 if err != nil {
83 return err
84 }
85
86 field_names := make(map[string]bool)
Scott Baker5201c0b2019-05-15 15:35:56 -070087 data := make([]map[string]interface{}, len(models))
88 for i, val := range models {
Scott Baker2c0ebda2019-05-06 16:55:47 -070089 data[i] = make(map[string]interface{})
90 for _, field_desc := range val.GetKnownFields() {
91 field_name := field_desc.GetName()
Scott Baker2c0ebda2019-05-06 16:55:47 -070092
93 isGuiHidden := strings.Contains(field_desc.GetFieldOptions().String(), "1005:1")
94 isFeedback := strings.Contains(field_desc.GetFieldOptions().String(), "1006:1")
95 isBookkeeping := strings.Contains(field_desc.GetFieldOptions().String(), "1007:1")
96
97 if isGuiHidden && (!options.ShowHidden) {
98 continue
99 }
100
101 if isFeedback && (!options.ShowFeedback) {
102 continue
103 }
104
Scott Baker6cf525a2019-05-09 12:25:08 -0700105 if isBookkeeping && (!options.ShowBookkeeping) {
Scott Baker2c0ebda2019-05-06 16:55:47 -0700106 continue
107 }
108
109 if field_desc.IsRepeated() {
110 continue
111 }
112
Scott Baker6cf525a2019-05-09 12:25:08 -0700113 data[i][field_name] = val.GetFieldByName(field_name)
Scott Baker2c0ebda2019-05-06 16:55:47 -0700114
115 field_names[field_name] = true
116 }
117 }
118
119 var default_format strings.Builder
120 default_format.WriteString("table")
121 first := true
122 for field_name, _ := range field_names {
123 if first {
124 fmt.Fprintf(&default_format, "{{ .%s }}", field_name)
125 first = false
126 } else {
127 fmt.Fprintf(&default_format, "\t{{ .%s }}", field_name)
128 }
129 }
130
Scott Baker5281d002019-05-16 10:45:26 -0700131 FormatAndGenerateOutput(&options.OutputOptions, default_format.String(), "{{.id}}", data)
132
133 return nil
134}
135
136func (options *ModelUpdate) Execute(args []string) error {
137 conn, descriptor, err := InitReflectionClient()
138 if err != nil {
139 return err
Scott Baker2c0ebda2019-05-06 16:55:47 -0700140 }
141
Scott Baker5281d002019-05-16 10:45:26 -0700142 defer conn.Close()
143
144 err = CheckModelName(descriptor, string(options.Args.ModelName))
145 if err != nil {
146 return err
Scott Baker2c0ebda2019-05-06 16:55:47 -0700147 }
148
Scott Baker5281d002019-05-16 10:45:26 -0700149 if (len(options.IDArgs.ID) == 0 && len(options.Filter) == 0) ||
150 (len(options.IDArgs.ID) != 0 && len(options.Filter) != 0) {
151 return fmt.Errorf("Use either an ID or a --filter to specify which models to update")
152 }
153
154 queries, err := CommaSeparatedQueryToMap(options.Filter, true)
155 if err != nil {
156 return err
157 }
158
159 updates, err := CommaSeparatedQueryToMap(options.SetFields, true)
160 if err != nil {
161 return err
162 }
163
164 modelName := string(options.Args.ModelName)
165
166 var models []*dynamic.Message
167
168 if len(options.IDArgs.ID) > 0 {
169 models = make([]*dynamic.Message, len(options.IDArgs.ID))
170 for i, id := range options.IDArgs.ID {
171 models[i], err = GetModel(conn, descriptor, modelName, id)
172 if err != nil {
173 return err
174 }
175 }
176 } else {
177 models, err = ListOrFilterModels(conn, descriptor, modelName, queries)
178 if err != nil {
179 return err
180 }
181 }
182
183 if len(models) == 0 {
184 return fmt.Errorf("Filter matches no objects")
185 } else if len(models) > 1 {
186 if !Confirmf("Filter matches %d objects. Continue [y/n] ? ", len(models)) {
187 return fmt.Errorf("Aborted by user")
188 }
189 }
190
191 fields := make(map[string]interface{})
192
193 if len(options.SetJSON) > 0 {
194 fields["_json"] = []byte(options.SetJSON)
195 }
196
197 for fieldName, value := range updates {
198 value = value[1:]
199 proto_value, err := TypeConvert(descriptor, modelName, fieldName, value)
200 if err != nil {
201 return err
202 }
203 fields[fieldName] = proto_value
204 }
205
206 for _, model := range models {
207 fields["id"] = model.GetFieldByName("id").(int32)
208 UpdateModel(conn, descriptor, modelName, fields)
209 }
210
211 count := len(models)
212 FormatAndGenerateOutput(&options.OutputOptions, "{{.}} models updated.", "{{.}}", count)
213
Scott Baker2c0ebda2019-05-06 16:55:47 -0700214 return nil
215}
Scott Baker63ce82e2019-05-15 09:01:42 -0700216
217func (modelName *ModelNameString) Complete(match string) []flags.Completion {
218 conn, descriptor, err := InitReflectionClient()
219 if err != nil {
220 return nil
221 }
222
223 defer conn.Close()
224
225 models, err := GetModelNames(descriptor)
226 if err != nil {
227 return nil
228 }
229
230 list := make([]flags.Completion, 0)
231 for k := range models {
232 if strings.HasPrefix(k, match) {
233 list = append(list, flags.Completion{Item: k})
234 }
235 }
236
237 return list
238}