blob: 01fb091b8f24373710b69b8a0dbc6b6ab5f182dc [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 "github.com/golang/protobuf/proto"
21 "github.com/jhump/protoreflect/desc"
22 "github.com/jhump/protoreflect/dynamic"
23 "google.golang.org/grpc/metadata"
24 "google.golang.org/grpc/status"
25 "io"
26)
27
28type RpcEventHandler struct {
29 Response proto.Message
30 Status *status.Status
31 Data []byte
32 Fields map[string]map[string]interface{}
33}
34
35func (h *RpcEventHandler) OnResolveMethod(*desc.MethodDescriptor) {
36}
37
38func (h *RpcEventHandler) OnSendHeaders(metadata.MD) {
39}
40
41func (h *RpcEventHandler) OnReceiveHeaders(metadata.MD) {
42}
43
44func (h *RpcEventHandler) OnReceiveResponse(m proto.Message) {
45 h.Response = m
46}
47
48func (h *RpcEventHandler) OnReceiveTrailers(s *status.Status, m metadata.MD) {
49 h.Status = s
50}
51
52func (h *RpcEventHandler) GetParams(msg proto.Message) error {
53 dmsg, err := dynamic.AsDynamicMessage(msg)
54 if err != nil {
55 return err
56 }
57
58 //fmt.Printf("MessageName: %s\n", dmsg.XXX_MessageName())
59
60 if h.Fields == nil || len(h.Fields) == 0 {
61 return io.EOF
62 }
63
64 fields, ok := h.Fields[dmsg.XXX_MessageName()]
65 if !ok {
66 return nil
67 }
68
69 for k, v := range fields {
70 dmsg.TrySetFieldByName(k, v)
71 }
72 delete(h.Fields, dmsg.XXX_MessageName())
73
74 return nil
75}