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 { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 61 | //fmt.Println("EOF") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 62 | return io.EOF |
| 63 | } |
| 64 | |
| 65 | fields, ok := h.Fields[dmsg.XXX_MessageName()] |
| 66 | if !ok { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 67 | //fmt.Println("nil") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 68 | return nil |
| 69 | } |
| 70 | |
| 71 | for k, v := range fields { |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 72 | // _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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 81 | } |
| 82 | delete(h.Fields, dmsg.XXX_MessageName()) |
| 83 | |
| 84 | return nil |
| 85 | } |