Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "github.com/jhump/protoreflect/desc" |
| 20 | "github.com/jhump/protoreflect/dynamic" |
| 21 | ) |
| 22 | |
| 23 | type ReturnValueRow struct { |
| 24 | Name string `json:"name"` |
| 25 | Result interface{} `json:"result"` |
| 26 | } |
| 27 | |
| 28 | type ReturnValues struct { |
| 29 | Set uint32 `json:"set"` |
| 30 | Unsupported uint32 `json:"unsupported"` |
| 31 | Error uint32 `json:"error"` |
| 32 | Distance uint32 `json:"distance"` |
| 33 | } |
| 34 | |
| 35 | func (r *ReturnValues) PopulateFrom(val *dynamic.Message) { |
| 36 | r.Set = val.GetFieldByName("Set").(uint32) |
| 37 | r.Unsupported = val.GetFieldByName("Unsupported").(uint32) |
| 38 | r.Error = val.GetFieldByName("Error").(uint32) |
| 39 | r.Distance = val.GetFieldByName("Distance").(uint32) |
| 40 | } |
| 41 | |
| 42 | // Given a list of allowed enum values, check each one of the values against the |
| 43 | // bitmaps, and fill out an array of result rows as follows: |
| 44 | // "Error", if the enum is set in the Error bitmap |
| 45 | // "Unsupported", if the enum is set in the Unsupported bitmap |
| 46 | // An interface containing the value, if the enum is set in the Set bitmap |
| 47 | |
| 48 | func (r *ReturnValues) GetKeyValuePairs(enumValues []*desc.EnumValueDescriptor) []ReturnValueRow { |
| 49 | var rows []ReturnValueRow |
| 50 | |
| 51 | for _, v := range enumValues { |
| 52 | num := uint32(v.GetNumber()) |
| 53 | if num == 0 { |
| 54 | // EMPTY is not a real value |
| 55 | continue |
| 56 | } |
| 57 | name := v.GetName() |
| 58 | if (r.Error & num) != 0 { |
| 59 | row := ReturnValueRow{Name: name, Result: "Error"} |
| 60 | rows = append(rows, row) |
| 61 | } |
| 62 | if (r.Unsupported & num) != 0 { |
| 63 | row := ReturnValueRow{Name: name, Result: "Unsupported"} |
| 64 | rows = append(rows, row) |
| 65 | } |
| 66 | if (r.Set & num) != 0 { |
| 67 | switch name { |
| 68 | case "DISTANCE": |
| 69 | row := ReturnValueRow{Name: name, Result: r.Distance} |
| 70 | rows = append(rows, row) |
| 71 | default: |
| 72 | row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"} |
| 73 | rows = append(rows, row) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return rows |
| 79 | } |