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 | }, |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame^] | 152 | "get-ext-value": { |
| 153 | "v3": "voltha.VolthaService/GetExtValue", |
| 154 | }, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Scott Baker | 2fe436a | 2020-02-10 17:21:47 -0800 | [diff] [blame] | 157 | // Get the descriptor source using the current ApiVersion setting |
| 158 | func GetDescriptorSource() (grpcurl.DescriptorSource, error) { |
| 159 | version := GlobalConfig.ApiVersion |
| 160 | filename, ok := descriptorMap[version] |
| 161 | if !ok { |
| 162 | return nil, &DescriptorNotFoundError{version} |
| 163 | } |
| 164 | var fds descpb.FileDescriptorSet |
| 165 | err := proto.Unmarshal(filename, &fds) |
| 166 | if err != nil { |
| 167 | return nil, &UnableToParseDescriptorErrror{err, version} |
| 168 | } |
| 169 | desc, err := grpcurl.DescriptorSourceFromFileDescriptorSet(&fds) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | |
| 174 | return desc, nil |
| 175 | } |
| 176 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 177 | func GetMethod(name string) (grpcurl.DescriptorSource, string, error) { |
| 178 | version := GlobalConfig.ApiVersion |
| 179 | f, ok := functionMap[name] |
| 180 | if !ok { |
| 181 | return nil, "", &MethodNotFoundError{name} |
| 182 | } |
| 183 | m, ok := f[version] |
| 184 | if !ok { |
| 185 | return nil, "", &MethodVersionNotFoundError{name, version} |
| 186 | } |
Scott Baker | 2fe436a | 2020-02-10 17:21:47 -0800 | [diff] [blame] | 187 | desc, err := GetDescriptorSource() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 188 | if err != nil { |
| 189 | return nil, "", err |
| 190 | } |
| 191 | |
| 192 | return desc, m, nil |
| 193 | } |