blob: 4749915cce439931631313c8a9f5497c0061e619 [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 },
105 "device-flow-list": {
106 "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 },
117 "logical-device-flow-list": {
118 "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 },
125 "devicegroup-list": {
126 "v1": "voltha.VolthaGlobalService/ListDeviceGroups",
127 "v2": "voltha.VolthaService/ListDeviceGroups",
128 },
129}
130
131func GetMethod(name string) (grpcurl.DescriptorSource, string, error) {
132 version := GlobalConfig.ApiVersion
133 f, ok := functionMap[name]
134 if !ok {
135 return nil, "", &MethodNotFoundError{name}
136 }
137 m, ok := f[version]
138 if !ok {
139 return nil, "", &MethodVersionNotFoundError{name, version}
140 }
141 filename, ok := descriptorMap[version]
142 if !ok {
143 return nil, "", &DescriptorNotFoundError{version}
144 }
145
146 var fds descpb.FileDescriptorSet
147 err := proto.Unmarshal(filename, &fds)
148 if err != nil {
149 return nil, "", &UnableToParseDescriptorErrror{err, version}
150 }
151 desc, err := grpcurl.DescriptorSourceFromFileDescriptorSet(&fds)
152 if err != nil {
153 return nil, "", err
154 }
155
156 return desc, m, nil
157}