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" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 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 |
David Bainbridge | 142516e | 2019-04-19 01:34:58 +0000 | [diff] [blame] | 84 | switch 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}} |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 87 | _=hdr |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 88 | {{else if .Cs}} |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 89 | _=hdr |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 90 | {{else}} |
| 91 | client := {{.Pkg}}.New{{.Svc}}Client({{$.Name}}Client.conn) |
David Bainbridge | 142516e | 2019-04-19 01:34:58 +0000 | [diff] [blame] | 92 | res, err := client.{{.Name}}(ctx, param.(*{{.Param}}), grpc.Header(&hdr)) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 93 | if err != nil { |
| 94 | return errors.New("Error sending method {{.Name}}") |
| 95 | } |
| 96 | // Marshal the result and compare it to the expected |
| 97 | // value. |
| 98 | if resS,err := json.Marshal(res); err == nil { |
| 99 | if string(resS) != expect { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 100 | stats.testLog("Unexpected result returned expected '%s' got '%s'\n", expect, string(resS)) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 101 | return errors.New("Unexpected result on method {{.Name}}") |
| 102 | } |
| 103 | } else { |
| 104 | return errors.New("Error Marshaling the reply for method {{.Name}}") |
| 105 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 106 | // Now validate the metadata in the response |
| 107 | for k,v := range expectMeta { |
| 108 | if rv,ok := hdr[k]; ok == true { |
| 109 | if rv[0] != v[0] { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 110 | 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] | 111 | err = errors.New("Failure on returned metadata") |
| 112 | } |
| 113 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 114 | 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] | 115 | err = errors.New("Failure on returned metadata") |
| 116 | } |
| 117 | } |
| 118 | return err |
| 119 | default: |
| 120 | return errors.New("Invalid parameter type for method {{.Name}}") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 121 | {{end}} |
| 122 | } |
| 123 | {{end}} |
| 124 | default: |
| 125 | return errors.New(fmt.Sprintf("Unexpected method %s in send", mthd)) |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |