Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | package commands |
| 18 | |
| 19 | import ( |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 20 | "fmt" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 21 | flags "github.com/jessevdk/go-flags" |
| 22 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | ) |
| 25 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 26 | type ModelNameString string |
| 27 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 28 | type ModelList struct { |
| 29 | OutputOptions |
Scott Baker | 5201c0b | 2019-05-15 15:35:56 -0700 | [diff] [blame] | 30 | 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 34 | Args struct { |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 35 | ModelName ModelNameString |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 36 | } `positional-args:"yes" required:"yes"` |
| 37 | } |
| 38 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 39 | type ModelUpdate struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 40 | OutputOptions |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 41 | 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 52 | type ModelOpts struct { |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 53 | List ModelList `command:"list"` |
| 54 | Update ModelUpdate `command:"update"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | var modelOpts = ModelOpts{} |
| 58 | |
| 59 | func RegisterModelCommands(parser *flags.Parser) { |
| 60 | parser.AddCommand("model", "model commands", "Commands to query and manipulate XOS models", &modelOpts) |
| 61 | } |
| 62 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 63 | func (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 Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 71 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 76 | queries, err := CommaSeparatedQueryToMap(options.Filter, true) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 81 | models, err := ListOrFilterModels(conn, descriptor, string(options.Args.ModelName), queries) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | field_names := make(map[string]bool) |
Scott Baker | 5201c0b | 2019-05-15 15:35:56 -0700 | [diff] [blame] | 87 | data := make([]map[string]interface{}, len(models)) |
| 88 | for i, val := range models { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 89 | data[i] = make(map[string]interface{}) |
| 90 | for _, field_desc := range val.GetKnownFields() { |
| 91 | field_name := field_desc.GetName() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 92 | |
| 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 105 | if isBookkeeping && (!options.ShowBookkeeping) { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 106 | continue |
| 107 | } |
| 108 | |
| 109 | if field_desc.IsRepeated() { |
| 110 | continue |
| 111 | } |
| 112 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 113 | data[i][field_name] = val.GetFieldByName(field_name) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 114 | |
| 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 Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 131 | FormatAndGenerateOutput(&options.OutputOptions, default_format.String(), "{{.id}}", data) |
| 132 | |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | func (options *ModelUpdate) Execute(args []string) error { |
| 137 | conn, descriptor, err := InitReflectionClient() |
| 138 | if err != nil { |
| 139 | return err |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 142 | defer conn.Close() |
| 143 | |
| 144 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
| 145 | if err != nil { |
| 146 | return err |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame^] | 149 | 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 214 | return nil |
| 215 | } |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 216 | |
| 217 | func (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 | } |