blob: 6b055342baaeb2d7c9a17d6a334fda03cc460ed7 [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -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 (
Shrey Baid688b4242020-07-10 20:40:10 +053019 "io"
20
Matteo Scandolo8df63df2019-09-12 10:34:32 -070021 "github.com/golang/protobuf/proto"
22 "github.com/jhump/protoreflect/desc"
23 "github.com/jhump/protoreflect/dynamic"
24 "google.golang.org/grpc/metadata"
25 "google.golang.org/grpc/status"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070026)
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 if h.Fields == nil || len(h.Fields) == 0 {
59 return io.EOF
60 }
61
62 fields, ok := h.Fields[dmsg.XXX_MessageName()]
63 if !ok {
64 return nil
65 }
66
67 for k, v := range fields {
Shrey Baid688b4242020-07-10 20:40:10 +053068 err := dmsg.TrySetFieldByName(k, v)
69 if err != nil {
70 return err
71 }
Matteo Scandolo8df63df2019-09-12 10:34:32 -070072 }
73 delete(h.Fields, dmsg.XXX_MessageName())
74
75 return nil
76}