blob: 8543040b98513fe90f977167dc279113c5f8ca46 [file] [log] [blame]
sslobodrd6e07e72019-01-31 16:07:20 -05001/*
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
17package main
18
19import (
20 "fmt"
21 "errors"
22 "encoding/json"
23 "google.golang.org/grpc"
24 "golang.org/x/net/context"
sslobodrd9daabf2019-02-05 13:14:21 -050025 "google.golang.org/grpc/metadata"
Scott Baker807addd2019-10-24 15:16:21 -070026 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sslobodrd6e07e72019-01-31 16:07:20 -050027 // 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}}
38type clientCtl struct {
sslobodrd9daabf2019-02-05 13:14:21 -050039 send func(string, context.Context, interface{}, string, metadata.MD) error
sslobodrd6e07e72019-01-31 16:07:20 -050040 cncl context.CancelFunc
41 ctx context.Context
42}
43{{end}}
44
45type {{.Name}}ClientConn struct {
46 conn * grpc.ClientConn
47 control * clientCtl
48}
49
50
51var {{.Name}}Client *{{.Name}}ClientConn
52
53
54func {{.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.
sslobodrd9daabf2019-02-05 13:14:21 -050078func {{.Name}}Send(mthd string, ctx context.Context, param interface{},
79 expect string, expectMeta metadata.MD) error {
sslobodrd6e07e72019-01-31 16:07:20 -050080 switch mthd {
81 {{range .Methods}}
sslobodrd9daabf2019-02-05 13:14:21 -050082 case "{{.Name}}":
83 var hdr metadata.MD
David Bainbridge142516e2019-04-19 01:34:58 +000084 switch param.(type) {
sslobodrd9daabf2019-02-05 13:14:21 -050085 case *{{.Param}}:
sslobodrd6e07e72019-01-31 16:07:20 -050086 {{if .Ss}}
sslobodrd9daabf2019-02-05 13:14:21 -050087 _=hdr
sslobodrd6e07e72019-01-31 16:07:20 -050088 {{else if .Cs}}
sslobodrd9daabf2019-02-05 13:14:21 -050089 _=hdr
sslobodrd6e07e72019-01-31 16:07:20 -050090 {{else}}
91 client := {{.Pkg}}.New{{.Svc}}Client({{$.Name}}Client.conn)
David Bainbridge142516e2019-04-19 01:34:58 +000092 res, err := client.{{.Name}}(ctx, param.(*{{.Param}}), grpc.Header(&hdr))
sslobodrd6e07e72019-01-31 16:07:20 -050093 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 {
sslobodr1d1e50b2019-03-14 09:17:40 -0400100 stats.testLog("Unexpected result returned expected '%s' got '%s'\n", expect, string(resS))
sslobodrd6e07e72019-01-31 16:07:20 -0500101 return errors.New("Unexpected result on method {{.Name}}")
102 }
103 } else {
104 return errors.New("Error Marshaling the reply for method {{.Name}}")
105 }
sslobodrd9daabf2019-02-05 13:14:21 -0500106 // 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] {
sslobodr1d1e50b2019-03-14 09:17:40 -0400110 stats.testLog("Mismatch on returned metadata for key '%s' expected '%s' and got '%s'\n", k, v, rv)
sslobodrd9daabf2019-02-05 13:14:21 -0500111 err = errors.New("Failure on returned metadata")
112 }
113 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400114 stats.testLog("Returned metadata missing key '%s'; expected value '%s' at that key\n", k, v)
sslobodrd9daabf2019-02-05 13:14:21 -0500115 err = errors.New("Failure on returned metadata")
116 }
117 }
118 return err
119 default:
120 return errors.New("Invalid parameter type for method {{.Name}}")
sslobodrd6e07e72019-01-31 16:07:20 -0500121 {{end}}
122 }
123 {{end}}
124 default:
125 return errors.New(fmt.Sprintf("Unexpected method %s in send", mthd))
126 }
127 return nil
128}
129