blob: 678883dda78dca3f6f49d9239a9c9d78b4ff2c89 [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 {
Scott Baker5281d002019-05-16 10:45:26 -070072 // _json is a special field name that indicates we should unmarshal json data
73 if k == "_json" {
74 err = dmsg.UnmarshalMergeJSON(v.([]byte))
75 if err != nil {
76 return err
77 }
78 } else {
79 dmsg.SetFieldByName(k, v)
80 }
Scott Baker2c0ebda2019-05-06 16:55:47 -070081 }
82 delete(h.Fields, dmsg.XXX_MessageName())
83
84 return nil
85}