blob: f60d2972160443592043efa50728102e12371066 [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,
62}
63
64var functionMap = map[string]map[string]string{
65 "version": {
66 "v1": "voltha.VolthaGlobalService/GetVoltha",
67 "v2": "voltha.VolthaService/GetVoltha",
68 },
69 "adapter-list": {
70 "v1": "voltha.VolthaGlobalService/ListAdapters",
71 "v2": "voltha.VolthaService/ListAdapters",
72 },
73 "device-list": {
74 "v1": "voltha.VolthaGlobalService/ListDevices",
75 "v2": "voltha.VolthaService/ListDevices",
76 },
77 "device-ports": {
78 "v1": "voltha.VolthaGlobalService/ListDevicePorts",
79 "v2": "voltha.VolthaService/ListDevicePorts",
80 },
81 "device-create": {
82 "v1": "voltha.VolthaGlobalService/CreateDevice",
83 "v2": "voltha.VolthaService/CreateDevice",
84 },
85 "device-delete": {
86 "v1": "voltha.VolthaGlobalService/DeleteDevice",
87 "v2": "voltha.VolthaService/DeleteDevice",
88 },
89 "device-enable": {
90 "v1": "voltha.VolthaGlobalService/EnableDevice",
91 "v2": "voltha.VolthaService/EnableDevice",
92 },
93 "device-disable": {
94 "v1": "voltha.VolthaGlobalService/DisableDevice",
95 "v2": "voltha.VolthaService/DisableDevice",
96 },
97 "device-reboot": {
98 "v1": "voltha.VolthaGlobalService/RebootDevice",
99 "v2": "voltha.VolthaService/RebootDevice",
100 },
101 "device-inspect": {
102 "v1": "voltha.VolthaGlobalService/GetDevice",
103 "v2": "voltha.VolthaService/GetDevice",
104 },
David Bainbridgea6722342019-10-24 23:55:53 +0000105 "device-flows": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700106 "v1": "voltha.VolthaGlobalService/ListDeviceFlows",
107 "v2": "voltha.VolthaService/ListDeviceFlows",
108 },
109 "logical-device-list": {
110 "v1": "voltha.VolthaGlobalService/ListLogicalDevices",
111 "v2": "voltha.VolthaService/ListLogicalDevices",
112 },
113 "logical-device-ports": {
114 "v1": "voltha.VolthaGlobalService/ListLogicalDevicePorts",
115 "v2": "voltha.VolthaService/ListLogicalDevicePorts",
116 },
David Bainbridgea6722342019-10-24 23:55:53 +0000117 "logical-device-flows": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700118 "v1": "voltha.VolthaGlobalService/ListLogicalDeviceFlows",
119 "v2": "voltha.VolthaService/ListLogicalDeviceFlows",
120 },
121 "logical-device-inspect": {
122 "v1": "voltha.VolthaGlobalService/GetLogicalDevice",
123 "v2": "voltha.VolthaService/GetLogicalDevice",
124 },
David Bainbridgea6722342019-10-24 23:55:53 +0000125 "device-group-list": {
Zack Williamse940c7a2019-08-21 14:25:39 -0700126 "v1": "voltha.VolthaGlobalService/ListDeviceGroups",
127 "v2": "voltha.VolthaService/ListDeviceGroups",
128 },
Scott Bakerd69e4222019-08-21 16:46:05 -0700129 "get-goroutine-count": {
130 "v2": "afrouter.Configuration.GetGoroutineCount",
131 },
132 "apiserver-update-log-level": {
133 "v2": "afrouter.Configuration.UpdateLogLevel",
134 },
135 "apiserver-get-log-levels": {
136 "v2": "afrouter.Configuration.GetLogLevels",
137 },
138 "update-log-level": {
139 "v2": "voltha.VolthaService/UpdateLogLevel",
140 },
141 "get-log-levels": {
142 "v2": "voltha.VolthaService/GetLogLevels",
143 },
Zack Williamse940c7a2019-08-21 14:25:39 -0700144}
145
146func GetMethod(name string) (grpcurl.DescriptorSource, string, error) {
147 version := GlobalConfig.ApiVersion
148 f, ok := functionMap[name]
149 if !ok {
150 return nil, "", &MethodNotFoundError{name}
151 }
152 m, ok := f[version]
153 if !ok {
154 return nil, "", &MethodVersionNotFoundError{name, version}
155 }
156 filename, ok := descriptorMap[version]
157 if !ok {
158 return nil, "", &DescriptorNotFoundError{version}
159 }
160
161 var fds descpb.FileDescriptorSet
162 err := proto.Unmarshal(filename, &fds)
163 if err != nil {
164 return nil, "", &UnableToParseDescriptorErrror{err, version}
165 }
166 desc, err := grpcurl.DescriptorSourceFromFileDescriptorSet(&fds)
167 if err != nil {
168 return nil, "", err
169 }
170
171 return desc, m, nil
172}