sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "errors" |
| 22 | "encoding/json" |
| 23 | "google.golang.org/grpc" |
| 24 | "golang.org/x/net/context" |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 25 | "google.golang.org/grpc/metadata" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/common/log" |
| 27 | // Values generated by the go template |
| 28 | {{range .Imports}} |
| 29 | "{{.}}" |
| 30 | {{end}} |
| 31 | {{range .ProtoImports}} |
| 32 | {{.Short}} "{{.Package}}" |
| 33 | {{end}} |
| 34 | // End go template values |
| 35 | ) |
| 36 | |
| 37 | {{if .Ct}}{{else}} |
| 38 | type clientCtl struct { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 39 | send func(string, context.Context, interface{}, string, metadata.MD) error |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 40 | cncl context.CancelFunc |
| 41 | ctx context.Context |
| 42 | } |
| 43 | {{end}} |
| 44 | |
| 45 | type {{.Name}}ClientConn struct { |
| 46 | conn * grpc.ClientConn |
| 47 | control * clientCtl |
| 48 | } |
| 49 | |
| 50 | |
| 51 | var {{.Name}}Client *{{.Name}}ClientConn |
| 52 | |
| 53 | |
| 54 | func {{.Name}}Connect() (*{{.Name}}ClientConn, error) { |
| 55 | log.Infof("Connecting client {{.Name}} to addr:127.0.0.1, port:{{.Port}}") |
| 56 | // Dial doesn't block, it just returns and continues connecting in the background. |
| 57 | // Check back later to confirm and increase the connection count. |
| 58 | cl := &{{.Name}}ClientConn{control:&clientCtl{}} |
| 59 | ctx, cnclFnc := context.WithCancel(context.Background()) |
| 60 | cl.control.cncl = cnclFnc |
| 61 | cl.control.ctx = ctx |
| 62 | if conn, err := grpc.Dial("127.0.0.1:{{.Port}}", grpc.WithInsecure()); err != nil { |
| 63 | log.Errorf("Dialng connection :%v",err) |
| 64 | return nil, err |
| 65 | } else { |
| 66 | cl.conn = conn |
| 67 | } |
| 68 | {{.Name}}Client = cl |
| 69 | cl.control.send = {{.Name}}Send |
| 70 | clients["{{.Name}}"] = cl.control |
| 71 | return cl, nil |
| 72 | } |
| 73 | |
| 74 | // This function will make the requested RPC with the supplied |
| 75 | // parameter and validate that the response matches the expected |
| 76 | // value provided. It will return nil if successful or an error |
| 77 | // if not. |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 78 | func {{.Name}}Send(mthd string, ctx context.Context, param interface{}, |
| 79 | expect string, expectMeta metadata.MD) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 80 | switch mthd { |
| 81 | {{range .Methods}} |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 82 | case "{{.Name}}": |
| 83 | var hdr metadata.MD |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 84 | switch t := param.(type) { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 85 | case *{{.Param}}: |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 86 | {{if .Ss}} |
| 87 | _=t |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 88 | _=hdr |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 89 | {{else if .Cs}} |
| 90 | _=t |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 91 | _=hdr |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 92 | {{else}} |
| 93 | client := {{.Pkg}}.New{{.Svc}}Client({{$.Name}}Client.conn) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 94 | res, err := client.{{.Name}}(ctx, t, grpc.Header(&hdr)) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 95 | if err != nil { |
| 96 | return errors.New("Error sending method {{.Name}}") |
| 97 | } |
| 98 | // Marshal the result and compare it to the expected |
| 99 | // value. |
| 100 | if resS,err := json.Marshal(res); err == nil { |
| 101 | if string(resS) != expect { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 102 | stats.testLog("Unexpected result returned expected '%s' got '%s'\n", expect, string(resS)) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 103 | return errors.New("Unexpected result on method {{.Name}}") |
| 104 | } |
| 105 | } else { |
| 106 | return errors.New("Error Marshaling the reply for method {{.Name}}") |
| 107 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 108 | // Now validate the metadata in the response |
| 109 | for k,v := range expectMeta { |
| 110 | if rv,ok := hdr[k]; ok == true { |
| 111 | if rv[0] != v[0] { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 112 | stats.testLog("Mismatch on returned metadata for key '%s' expected '%s' and got '%s'\n", k, v, rv) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 113 | err = errors.New("Failure on returned metadata") |
| 114 | } |
| 115 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 116 | stats.testLog("Returned metadata missing key '%s'; expected value '%s' at that key\n", k, v) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 117 | err = errors.New("Failure on returned metadata") |
| 118 | } |
| 119 | } |
| 120 | return err |
| 121 | default: |
| 122 | return errors.New("Invalid parameter type for method {{.Name}}") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 123 | {{end}} |
| 124 | } |
| 125 | {{end}} |
| 126 | default: |
| 127 | return errors.New(fmt.Sprintf("Unexpected method %s in send", mthd)) |
| 128 | } |
| 129 | return nil |
| 130 | } |
| 131 | |