Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [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 commands |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "github.com/fullstorydev/grpcurl" |
| 21 | "github.com/golang/protobuf/proto" |
| 22 | descpb "github.com/golang/protobuf/protoc-gen-go/descriptor" |
| 23 | ) |
| 24 | |
| 25 | type MethodNotFoundError struct { |
| 26 | Name string |
| 27 | } |
| 28 | |
| 29 | func (e *MethodNotFoundError) Error() string { |
| 30 | return fmt.Sprintf("Method '%s' not found in function map", e.Name) |
| 31 | } |
| 32 | |
| 33 | type MethodVersionNotFoundError struct { |
| 34 | Name string |
| 35 | Version string |
| 36 | } |
| 37 | |
| 38 | func (e *MethodVersionNotFoundError) Error() string { |
| 39 | return fmt.Sprintf("Method '%s' does not have a verison for '%s' specfied in function map", e.Name, e.Version) |
| 40 | } |
| 41 | |
| 42 | type DescriptorNotFoundError struct { |
| 43 | Version string |
| 44 | } |
| 45 | |
| 46 | func (e *DescriptorNotFoundError) Error() string { |
| 47 | return fmt.Sprintf("Protocol buffer descriptor for API version '%s' not found", e.Version) |
| 48 | } |
| 49 | |
| 50 | type UnableToParseDescriptorErrror struct { |
| 51 | err error |
| 52 | Version string |
| 53 | } |
| 54 | |
| 55 | func (e *UnableToParseDescriptorErrror) Error() string { |
| 56 | return fmt.Sprintf("Unable to parse protocal buffer descriptor for version '%s': %s", e.Version, e.err) |
| 57 | } |
| 58 | |
| 59 | var descriptorMap = map[string][]byte{ |
| 60 | "v1": V1Descriptor, |
| 61 | "v2": V2Descriptor, |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 62 | "v3": V3Descriptor, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | var functionMap = map[string]map[string]string{ |
| 66 | "version": { |
| 67 | "v1": "voltha.VolthaGlobalService/GetVoltha", |
| 68 | "v2": "voltha.VolthaService/GetVoltha", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 69 | "v3": "voltha.VolthaService/GetVoltha", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 70 | }, |
| 71 | "adapter-list": { |
| 72 | "v1": "voltha.VolthaGlobalService/ListAdapters", |
| 73 | "v2": "voltha.VolthaService/ListAdapters", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 74 | "v3": "voltha.VolthaService/ListAdapters", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 75 | }, |
| 76 | "device-list": { |
| 77 | "v1": "voltha.VolthaGlobalService/ListDevices", |
| 78 | "v2": "voltha.VolthaService/ListDevices", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 79 | "v3": "voltha.VolthaService/ListDevices", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 80 | }, |
| 81 | "device-ports": { |
| 82 | "v1": "voltha.VolthaGlobalService/ListDevicePorts", |
| 83 | "v2": "voltha.VolthaService/ListDevicePorts", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 84 | "v3": "voltha.VolthaService/ListDevicePorts", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 85 | }, |
| 86 | "device-create": { |
| 87 | "v1": "voltha.VolthaGlobalService/CreateDevice", |
| 88 | "v2": "voltha.VolthaService/CreateDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 89 | "v3": "voltha.VolthaService/CreateDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 90 | }, |
| 91 | "device-delete": { |
| 92 | "v1": "voltha.VolthaGlobalService/DeleteDevice", |
| 93 | "v2": "voltha.VolthaService/DeleteDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 94 | "v3": "voltha.VolthaService/DeleteDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 95 | }, |
| 96 | "device-enable": { |
| 97 | "v1": "voltha.VolthaGlobalService/EnableDevice", |
| 98 | "v2": "voltha.VolthaService/EnableDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 99 | "v3": "voltha.VolthaService/EnableDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 100 | }, |
| 101 | "device-disable": { |
| 102 | "v1": "voltha.VolthaGlobalService/DisableDevice", |
| 103 | "v2": "voltha.VolthaService/DisableDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 104 | "v3": "voltha.VolthaService/DisableDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 105 | }, |
| 106 | "device-reboot": { |
| 107 | "v1": "voltha.VolthaGlobalService/RebootDevice", |
| 108 | "v2": "voltha.VolthaService/RebootDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 109 | "v3": "voltha.VolthaService/RebootDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 110 | }, |
| 111 | "device-inspect": { |
| 112 | "v1": "voltha.VolthaGlobalService/GetDevice", |
| 113 | "v2": "voltha.VolthaService/GetDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 114 | "v3": "voltha.VolthaService/GetDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 115 | }, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 116 | "device-flows": { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 117 | "v1": "voltha.VolthaGlobalService/ListDeviceFlows", |
| 118 | "v2": "voltha.VolthaService/ListDeviceFlows", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 119 | "v3": "voltha.VolthaService/ListDeviceFlows", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 120 | }, |
| 121 | "logical-device-list": { |
| 122 | "v1": "voltha.VolthaGlobalService/ListLogicalDevices", |
| 123 | "v2": "voltha.VolthaService/ListLogicalDevices", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 124 | "v3": "voltha.VolthaService/ListLogicalDevices", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 125 | }, |
| 126 | "logical-device-ports": { |
| 127 | "v1": "voltha.VolthaGlobalService/ListLogicalDevicePorts", |
| 128 | "v2": "voltha.VolthaService/ListLogicalDevicePorts", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 129 | "v3": "voltha.VolthaService/ListLogicalDevicePorts", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 130 | }, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 131 | "logical-device-flows": { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 132 | "v1": "voltha.VolthaGlobalService/ListLogicalDeviceFlows", |
| 133 | "v2": "voltha.VolthaService/ListLogicalDeviceFlows", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 134 | "v3": "voltha.VolthaService/ListLogicalDeviceFlows", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 135 | }, |
| 136 | "logical-device-inspect": { |
| 137 | "v1": "voltha.VolthaGlobalService/GetLogicalDevice", |
| 138 | "v2": "voltha.VolthaService/GetLogicalDevice", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 139 | "v3": "voltha.VolthaService/GetLogicalDevice", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 140 | }, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 141 | "device-group-list": { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 142 | "v1": "voltha.VolthaGlobalService/ListDeviceGroups", |
| 143 | "v2": "voltha.VolthaService/ListDeviceGroups", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 144 | "v3": "voltha.VolthaService/ListDeviceGroups", |
| 145 | }, |
| 146 | "device-port-enable": { |
| 147 | "v3": "voltha.VolthaService/EnablePort", |
| 148 | }, |
| 149 | "device-port-disable": { |
| 150 | "v3": "voltha.VolthaService/DisablePort", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 151 | }, |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 152 | "get-goroutine-count": { |
| 153 | "v2": "afrouter.Configuration.GetGoroutineCount", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 154 | "v3": "afrouter.Configuration.GetGoroutineCount", |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 155 | }, |
| 156 | "apiserver-update-log-level": { |
| 157 | "v2": "afrouter.Configuration.UpdateLogLevel", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 158 | "v3": "afrouter.Configuration.UpdateLogLevel", |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 159 | }, |
| 160 | "apiserver-get-log-levels": { |
| 161 | "v2": "afrouter.Configuration.GetLogLevels", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 162 | "v3": "afrouter.Configuration.GetLogLevels", |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 163 | }, |
| 164 | "update-log-level": { |
| 165 | "v2": "voltha.VolthaService/UpdateLogLevel", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 166 | "v3": "voltha.VolthaService/UpdateLogLevel", |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 167 | }, |
| 168 | "get-log-levels": { |
| 169 | "v2": "voltha.VolthaService/GetLogLevels", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 170 | "v3": "voltha.VolthaService/GetLogLevels", |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 171 | }, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | func GetMethod(name string) (grpcurl.DescriptorSource, string, error) { |
| 175 | version := GlobalConfig.ApiVersion |
| 176 | f, ok := functionMap[name] |
| 177 | if !ok { |
| 178 | return nil, "", &MethodNotFoundError{name} |
| 179 | } |
| 180 | m, ok := f[version] |
| 181 | if !ok { |
| 182 | return nil, "", &MethodVersionNotFoundError{name, version} |
| 183 | } |
| 184 | filename, ok := descriptorMap[version] |
| 185 | if !ok { |
| 186 | return nil, "", &DescriptorNotFoundError{version} |
| 187 | } |
| 188 | |
| 189 | var fds descpb.FileDescriptorSet |
| 190 | err := proto.Unmarshal(filename, &fds) |
| 191 | if err != nil { |
| 192 | return nil, "", &UnableToParseDescriptorErrror{err, version} |
| 193 | } |
| 194 | desc, err := grpcurl.DescriptorSourceFromFileDescriptorSet(&fds) |
| 195 | if err != nil { |
| 196 | return nil, "", err |
| 197 | } |
| 198 | |
| 199 | return desc, m, nil |
| 200 | } |