blob: db34efdc0c389698cfafb694a4b3c5fe85685bd5 [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"
sslobodrd6e07e72019-01-31 16:07:20 -050026 "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}}
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
sslobodrd6e07e72019-01-31 16:07:20 -050084 switch t := param.(type) {
sslobodrd9daabf2019-02-05 13:14:21 -050085 case *{{.Param}}:
sslobodrd6e07e72019-01-31 16:07:20 -050086 {{if .Ss}}
87 _=t
sslobodrd9daabf2019-02-05 13:14:21 -050088 _=hdr
sslobodrd6e07e72019-01-31 16:07:20 -050089 {{else if .Cs}}
90 _=t
sslobodrd9daabf2019-02-05 13:14:21 -050091 _=hdr
sslobodrd6e07e72019-01-31 16:07:20 -050092 {{else}}
93 client := {{.Pkg}}.New{{.Svc}}Client({{$.Name}}Client.conn)
sslobodrd9daabf2019-02-05 13:14:21 -050094 res, err := client.{{.Name}}(ctx, t, grpc.Header(&hdr))
sslobodrd6e07e72019-01-31 16:07:20 -050095 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 {
sslobodr1d1e50b2019-03-14 09:17:40 -0400102 stats.testLog("Unexpected result returned expected '%s' got '%s'\n", expect, string(resS))
sslobodrd6e07e72019-01-31 16:07:20 -0500103 return errors.New("Unexpected result on method {{.Name}}")
104 }
105 } else {
106 return errors.New("Error Marshaling the reply for method {{.Name}}")
107 }
sslobodrd9daabf2019-02-05 13:14:21 -0500108 // 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] {
sslobodr1d1e50b2019-03-14 09:17:40 -0400112 stats.testLog("Mismatch on returned metadata for key '%s' expected '%s' and got '%s'\n", k, v, rv)
sslobodrd9daabf2019-02-05 13:14:21 -0500113 err = errors.New("Failure on returned metadata")
114 }
115 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400116 stats.testLog("Returned metadata missing key '%s'; expected value '%s' at that key\n", k, v)
sslobodrd9daabf2019-02-05 13:14:21 -0500117 err = errors.New("Failure on returned metadata")
118 }
119 }
120 return err
121 default:
122 return errors.New("Invalid parameter type for method {{.Name}}")
sslobodrd6e07e72019-01-31 16:07:20 -0500123 {{end}}
124 }
125 {{end}}
126 default:
127 return errors.New(fmt.Sprintf("Unexpected method %s in send", mthd))
128 }
129 return nil
130}
131