blob: 7a2bdf2699da544367ba017bffe5eb82176b4513 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
20 "fmt"
21 "github.com/fullstorydev/grpcurl"
22 "github.com/jhump/protoreflect/dynamic"
23 "github.com/jhump/protoreflect/grpcreflect"
24 "golang.org/x/net/context"
25 "google.golang.org/grpc"
26 reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
27)
28
29type MethodNotFoundError struct {
30 Name string
31}
32
33func (e *MethodNotFoundError) Error() string {
34 return fmt.Sprintf("Method '%s' not found in function map", e.Name)
35}
36
37type MethodVersionNotFoundError struct {
38 Name string
39 Version string
40}
41
42func (e *MethodVersionNotFoundError) Error() string {
43 return fmt.Sprintf("Method '%s' does not have a verison for '%s' specfied in function map", e.Name, e.Version)
44}
45
46type DescriptorNotFoundError struct {
47 Version string
48}
49
50func (e *DescriptorNotFoundError) Error() string {
51 return fmt.Sprintf("Protocol buffer descriptor for API version '%s' not found", e.Version)
52}
53
54type UnableToParseDescriptorErrror struct {
55 err error
56 Version string
57}
58
59func (e *UnableToParseDescriptorErrror) Error() string {
60 return fmt.Sprintf("Unable to parse protocal buffer descriptor for version '%s': %s", e.Version, e.err)
61}
62
63func GetReflectionMethod(conn *grpc.ClientConn, name string) (grpcurl.DescriptorSource, string, error) {
64 refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn))
65 defer refClient.Reset()
66
67 desc := grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
68
69 return desc, name, nil
70}
71
Scott Baker5201c0b2019-05-15 15:35:56 -070072func GetEnumValue(msg *dynamic.Message, name string) string {
73 return msg.FindFieldDescriptorByName(name).GetEnumType().
74 FindValueByNumber(msg.GetFieldByName(name).(int32)).GetName()
75}
76
77func SetEnumValue(msg *dynamic.Message, name string, value string) {
78 eValue := msg.FindFieldDescriptorByName(name).GetEnumType().FindValueByName(value)
79 msg.SetFieldByName(name, eValue.GetNumber())
Scott Baker2c0ebda2019-05-06 16:55:47 -070080}