Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 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 | |
| 28 | type RpcEventHandler struct { |
| 29 | Response proto.Message |
| 30 | Status *status.Status |
| 31 | Data []byte |
| 32 | Fields map[string]map[string]interface{} |
| 33 | } |
| 34 | |
| 35 | func (h *RpcEventHandler) OnResolveMethod(*desc.MethodDescriptor) { |
| 36 | } |
| 37 | |
| 38 | func (h *RpcEventHandler) OnSendHeaders(metadata.MD) { |
| 39 | } |
| 40 | |
| 41 | func (h *RpcEventHandler) OnReceiveHeaders(metadata.MD) { |
| 42 | } |
| 43 | |
| 44 | func (h *RpcEventHandler) OnReceiveResponse(m proto.Message) { |
| 45 | h.Response = m |
| 46 | } |
| 47 | |
| 48 | func (h *RpcEventHandler) OnReceiveTrailers(s *status.Status, m metadata.MD) { |
| 49 | h.Status = s |
| 50 | } |
| 51 | |
| 52 | func (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 | } |