blob: 8aadb4427b3a72af0fb45109853036bb81625e40 [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 {
Scott Baker6cf525a2019-05-09 12:25:08 -070061 //fmt.Println("EOF")
Scott Baker2c0ebda2019-05-06 16:55:47 -070062 return io.EOF
63 }
64
65 fields, ok := h.Fields[dmsg.XXX_MessageName()]
66 if !ok {
Scott Baker6cf525a2019-05-09 12:25:08 -070067 //fmt.Println("nil")
Scott Baker2c0ebda2019-05-06 16:55:47 -070068 return nil
69 }
70
71 for k, v := range fields {
72 dmsg.TrySetFieldByName(k, v)
73 }
74 delete(h.Fields, dmsg.XXX_MessageName())
75
76 return nil
77}