blob: 6de98109b55752a4c129252929e6b840092e2cd3 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
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 */
16package commands
17
18import (
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
25type MethodNotFoundError struct {
26 Name string
27}
28
29func (e *MethodNotFoundError) Error() string {
30 return fmt.Sprintf("Method '%s' not found in function map", e.Name)
31}
32
33type MethodVersionNotFoundError struct {
34 Name string
35 Version string
36}
37
38func (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
42type DescriptorNotFoundError struct {
43 Version string
44}
45
46func (e *DescriptorNotFoundError) Error() string {
47 return fmt.Sprintf("Protocol buffer descriptor for API version '%s' not found", e.Version)
48}
49
50type UnableToParseDescriptorErrror struct {
51 err error
52 Version string
53}
54
55func (e *UnableToParseDescriptorErrror) Error() string {
56 return fmt.Sprintf("Unable to parse protocal buffer descriptor for version '%s': %s", e.Version, e.err)
57}
58
59var descriptorMap = map[string][]byte{
60 "v1": V1Descriptor,
61 "v2": V2Descriptor,
kesavand12cd8eb2020-01-20 22:25:22 -050062 "v3": V3Descriptor,
Zack Williamse940c7a2019-08-21 14:25:39 -070063}
64
65var functionMap = map[string]map[string]string{
66 "version": {
67 "v1": "voltha.VolthaGlobalService/GetVoltha",
68 "v2": "voltha.VolthaService/GetVoltha",
kesavand12cd8eb2020-01-20 22:25:22 -050069 "v3": "voltha.VolthaService/GetVoltha",
Zack Williamse940c7a2019-08-21 14:25:39 -070070 },
71 "adapter-list": {
72 "v1": "voltha.VolthaGlobalService/ListAdapters",
73 "v2": "voltha.VolthaService/ListAdapters",
kesavand12cd8eb2020-01-20 22:25:22 -050074 "v3": "voltha.VolthaService/ListAdapters",
Zack Williamse940c7a2019-08-21 14:25:39 -070075 },
76 "device-list": {
77 "v1": "voltha.VolthaGlobalService/ListDevices",
78 "v2": "voltha.VolthaService/ListDevices",
kesavand12cd8eb2020-01-20 22:25:22 -050079 "v3": "voltha.VolthaService/ListDevices",
Zack Williamse940c7a2019-08-21 14:25:39 -070080 },
81 "device-ports": {
82 "v1": "voltha.VolthaGlobalService/ListDevicePorts",
83 "v2": "voltha.VolthaService/ListDevicePorts",
kesavand12cd8eb2020-01-20 22:25:22 -050084 "v3": "voltha.VolthaService/ListDevicePorts",
Zack Williamse940c7a2019-08-21 14:25:39 -070085 },
86 "device-create": {
87 "v1": "voltha.VolthaGlobalService/CreateDevice",
88 "v2": "voltha.VolthaService/CreateDevice",
kesavand12cd8eb2020-01-20 22:25:22 -050089 "v3": "voltha.VolthaService/CreateDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -070090 },
91 "device-delete": {
92 "v1": "voltha.VolthaGlobalService/DeleteDevice",
93 "v2": "voltha.VolthaService/DeleteDevice",
kesavand12cd8eb2020-01-20 22:25:22 -050094 "v3": "voltha.VolthaService/DeleteDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -070095 },
96 "device-enable": {
97 "v1": "voltha.VolthaGlobalService/EnableDevice",
98 "v2": "voltha.VolthaService/EnableDevice",
kesavand12cd8eb2020-01-20 22:25:22 -050099 "v3": "voltha.VolthaService/EnableDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -0700100 },
101 "device-disable": {
102 "v1": "voltha.VolthaGlobalService/DisableDevice",
103 "v2": "voltha.VolthaService/DisableDevice",
kesavand12cd8eb2020-01-20 22:25:22 -0500104 "v3": "voltha.VolthaService/DisableDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -0700105 },
106 "device-reboot": {
107 "v1": "voltha.VolthaGlobalService/RebootDevice",
108 "v2": "voltha.VolthaService/RebootDevice",
kesavand12cd8eb2020-01-20 22:25:22 -0500109 "v3": "voltha.VolthaService/RebootDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -0700110 },
111 "device-inspect": {
112 "v1": "voltha.VolthaGlobalService/GetDevice",
113 "v2": "voltha.VolthaService/GetDevice",
kesavand12cd8eb2020-01-20 22:25:22 -0500114 "v3": "voltha.VolthaService/GetDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -0700115 },
David Bainbridgea6722342019-10-24 23:55:53 +0000116 "device-flows": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700117 "v1": "voltha.VolthaGlobalService/ListDeviceFlows",
118 "v2": "voltha.VolthaService/ListDeviceFlows",
kesavand12cd8eb2020-01-20 22:25:22 -0500119 "v3": "voltha.VolthaService/ListDeviceFlows",
Zack Williamse940c7a2019-08-21 14:25:39 -0700120 },
121 "logical-device-list": {
122 "v1": "voltha.VolthaGlobalService/ListLogicalDevices",
123 "v2": "voltha.VolthaService/ListLogicalDevices",
kesavand12cd8eb2020-01-20 22:25:22 -0500124 "v3": "voltha.VolthaService/ListLogicalDevices",
Zack Williamse940c7a2019-08-21 14:25:39 -0700125 },
126 "logical-device-ports": {
127 "v1": "voltha.VolthaGlobalService/ListLogicalDevicePorts",
128 "v2": "voltha.VolthaService/ListLogicalDevicePorts",
kesavand12cd8eb2020-01-20 22:25:22 -0500129 "v3": "voltha.VolthaService/ListLogicalDevicePorts",
Zack Williamse940c7a2019-08-21 14:25:39 -0700130 },
David Bainbridgea6722342019-10-24 23:55:53 +0000131 "logical-device-flows": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700132 "v1": "voltha.VolthaGlobalService/ListLogicalDeviceFlows",
133 "v2": "voltha.VolthaService/ListLogicalDeviceFlows",
kesavand12cd8eb2020-01-20 22:25:22 -0500134 "v3": "voltha.VolthaService/ListLogicalDeviceFlows",
Zack Williamse940c7a2019-08-21 14:25:39 -0700135 },
136 "logical-device-inspect": {
137 "v1": "voltha.VolthaGlobalService/GetLogicalDevice",
138 "v2": "voltha.VolthaService/GetLogicalDevice",
kesavand12cd8eb2020-01-20 22:25:22 -0500139 "v3": "voltha.VolthaService/GetLogicalDevice",
Zack Williamse940c7a2019-08-21 14:25:39 -0700140 },
David Bainbridgea6722342019-10-24 23:55:53 +0000141 "device-group-list": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700142 "v1": "voltha.VolthaGlobalService/ListDeviceGroups",
143 "v2": "voltha.VolthaService/ListDeviceGroups",
kesavand12cd8eb2020-01-20 22:25:22 -0500144 "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 Williamse940c7a2019-08-21 14:25:39 -0700151 },
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800152 "get-ext-value": {
153 "v3": "voltha.VolthaService/GetExtValue",
154 },
Zack Williamse940c7a2019-08-21 14:25:39 -0700155}
156
Scott Baker2fe436a2020-02-10 17:21:47 -0800157// Get the descriptor source using the current ApiVersion setting
158func 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 Williamse940c7a2019-08-21 14:25:39 -0700177func 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 Baker2fe436a2020-02-10 17:21:47 -0800187 desc, err := GetDescriptorSource()
Zack Williamse940c7a2019-08-21 14:25:39 -0700188 if err != nil {
189 return nil, "", err
190 }
191
192 return desc, m, nil
193}