blob: d384baf0f509c4f24d8d560391943091a60e70bf [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"
25 "github.com/opencord/voltha-go/common/log"
26 // Values generated by the go template
27 {{range .Imports}}
28 "{{.}}"
29 {{end}}
30 {{range .ProtoImports}}
31 {{.Short}} "{{.Package}}"
32 {{end}}
33 // End go template values
34)
35
36{{if .Ct}}{{else}}
37type clientCtl struct {
38 send func(string, interface{}, string) error
39 cncl context.CancelFunc
40 ctx context.Context
41}
42{{end}}
43
44type {{.Name}}ClientConn struct {
45 conn * grpc.ClientConn
46 control * clientCtl
47}
48
49
50var {{.Name}}Client *{{.Name}}ClientConn
51
52
53func {{.Name}}Connect() (*{{.Name}}ClientConn, error) {
54 log.Infof("Connecting client {{.Name}} to addr:127.0.0.1, port:{{.Port}}")
55 // Dial doesn't block, it just returns and continues connecting in the background.
56 // Check back later to confirm and increase the connection count.
57 cl := &{{.Name}}ClientConn{control:&clientCtl{}}
58 ctx, cnclFnc := context.WithCancel(context.Background())
59 cl.control.cncl = cnclFnc
60 cl.control.ctx = ctx
61 if conn, err := grpc.Dial("127.0.0.1:{{.Port}}", grpc.WithInsecure()); err != nil {
62 log.Errorf("Dialng connection :%v",err)
63 return nil, err
64 } else {
65 cl.conn = conn
66 }
67 {{.Name}}Client = cl
68 cl.control.send = {{.Name}}Send
69 clients["{{.Name}}"] = cl.control
70 return cl, nil
71}
72
73// This function will make the requested RPC with the supplied
74// parameter and validate that the response matches the expected
75// value provided. It will return nil if successful or an error
76// if not.
77func {{.Name}}Send(mthd string, param interface{}, expect string) error {
78 switch mthd {
79 {{range .Methods}}
80 case "{{.Name}}":
81 switch t := param.(type) {
82 case *{{.Param}}:
83 {{if .Ss}}
84 _=t
85 {{else if .Cs}}
86 _=t
87 {{else}}
88 client := {{.Pkg}}.New{{.Svc}}Client({{$.Name}}Client.conn)
89 res, err := client.{{.Name}}(context.Background(), t)
90 if err != nil {
91 return errors.New("Error sending method {{.Name}}")
92 }
93 // Marshal the result and compare it to the expected
94 // value.
95 if resS,err := json.Marshal(res); err == nil {
96 if string(resS) != expect {
97 return errors.New("Unexpected result on method {{.Name}}")
98 }
99 } else {
100 return errors.New("Error Marshaling the reply for method {{.Name}}")
101 }
102 default:
103 return errors.New("Invalid parameter type for method {{.Name}}")
104 {{end}}
105 }
106 {{end}}
107 default:
108 return errors.New(fmt.Sprintf("Unexpected method %s in send", mthd))
109 }
110 return nil
111}
112