blob: 71f7d9de622a25182eb0e0af48cb19b14bc3540b [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 },
Scott Bakerd69e4222019-08-21 16:46:05 -0700152 "get-goroutine-count": {
153 "v2": "afrouter.Configuration.GetGoroutineCount",
kesavand12cd8eb2020-01-20 22:25:22 -0500154 "v3": "afrouter.Configuration.GetGoroutineCount",
Scott Bakerd69e4222019-08-21 16:46:05 -0700155 },
156 "apiserver-update-log-level": {
157 "v2": "afrouter.Configuration.UpdateLogLevel",
kesavand12cd8eb2020-01-20 22:25:22 -0500158 "v3": "afrouter.Configuration.UpdateLogLevel",
Scott Bakerd69e4222019-08-21 16:46:05 -0700159 },
160 "apiserver-get-log-levels": {
161 "v2": "afrouter.Configuration.GetLogLevels",
kesavand12cd8eb2020-01-20 22:25:22 -0500162 "v3": "afrouter.Configuration.GetLogLevels",
Scott Bakerd69e4222019-08-21 16:46:05 -0700163 },
164 "update-log-level": {
165 "v2": "voltha.VolthaService/UpdateLogLevel",
kesavand12cd8eb2020-01-20 22:25:22 -0500166 "v3": "voltha.VolthaService/UpdateLogLevel",
Scott Bakerd69e4222019-08-21 16:46:05 -0700167 },
168 "get-log-levels": {
169 "v2": "voltha.VolthaService/GetLogLevels",
kesavand12cd8eb2020-01-20 22:25:22 -0500170 "v3": "voltha.VolthaService/GetLogLevels",
Scott Bakerd69e4222019-08-21 16:46:05 -0700171 },
Zack Williamse940c7a2019-08-21 14:25:39 -0700172}
173
174func 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}