blob: 7ce70b06e5596ff7ab6d794da752dac2b51a4191 [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// The template for the tester.
17// This template is filled in by the
18// test driver based on the configuration.
19
20package main
21
22import (
23 "fmt"
24 "net"
25 "errors"
26 "encoding/json"
27 "google.golang.org/grpc"
28 "golang.org/x/net/context"
29 "google.golang.org/grpc/metadata"
Scott Baker807addd2019-10-24 15:16:21 -070030 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sslobodrd6e07e72019-01-31 16:07:20 -050031 // Values generated by the go template
32 {{range .Imports}}
33 "{{.}}"
34 {{end}}
35 {{range .ProtoImports}}
36 {{.Short}} "{{.Package}}"
37 {{end}}
38 // End go template values
39)
40
41// The channels to get fed the expected results by the test driver.
42//var {{.Name}}Meta <-chan string
43////var {{.Name}}Payload <-chan string
44
45{{if .Ct}}{{else}}
46type reply struct {
47 repl interface{}
48}
49type incoming struct {
50 meta metadata.MD
51 payload string
52}
53type serverCtl struct {
54 replyData chan * reply
55 incmg chan * incoming
56}
57{{end}}
58
59type {{.Name}}TestServer struct {
60 control *serverCtl
61 srv *grpc.Server
62}
63
64/*
65func init() {
66 {{if .Ct}}{{else}}
67 if _, err := log.SetDefaultLogger(log.JSON, 0, nil); err != nil {
68 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
69 }
70 {{end}}
71 {{.Name}}ListenAndServe()
72}
73*/
74
75
76func {{.Name}}ListenAndServe() error {
77 var s {{.Name}}TestServer
78
sslobodr13182842019-02-08 14:40:30 -050079 s.control = &serverCtl{replyData:make(chan *reply), incmg:make(chan *incoming)}
sslobodrd6e07e72019-01-31 16:07:20 -050080 servers["{{.Name}}"] = s.control
81
82 log.Debugf("Starting server %s on port %d", "{{.Name}}", {{.Port}})
83 // THe test head always uses localhost 127.0.0.1
84 addr := fmt.Sprintf("127.0.0.1:%d", {{.Port}})
85
86 // Create the gRPC server
87 s.srv = grpc.NewServer()
88
89{{range .ProtoImports}}
90 // Register the handler object
91 {{.Short}}.Register{{.Service}}Server(s.srv, s)
92{{end}}
93
94 // Create the channel to listen on
95 lis, err := net.Listen("tcp", addr)
96 if err != nil {
97 log.Errorf("could not listen on %s: %s", addr, err)
98 return err
99 }
100
101 // Serve and Listen
102 go func() {
103 if err = s.srv.Serve(lis); err != nil {
104 log.Errorf("grpc serve error: %s", err)
105 return
106 }
107 }()
108
109 return err
110}
111
112{{range .Methods}}
113{{if .Ss}}
114func (ts {{$.Name}}TestServer) {{.Name}}(in *{{.Param}}, srvr {{.Pkg}}.{{.Svc}}_{{.Name}}Server) error {
sslobodr13182842019-02-08 14:40:30 -0500115 log.Debug("Serving server streaming {{$.Name}}")
sslobodrd6e07e72019-01-31 16:07:20 -0500116 return nil
117}
118{{else if .Cs}}
119func (ts {{$.Name}}TestServer) {{.Name}}({{.Pkg}}.{{.Svc}}_{{.Name}}Server) error {
sslobodr13182842019-02-08 14:40:30 -0500120 log.Debug("Serving client streaming {{$.Name}}")
sslobodrd6e07e72019-01-31 16:07:20 -0500121 return nil
122}
123{{else}}
124func (ts {{$.Name}}TestServer) {{.Name}}(ctx context.Context, in *{{.Param}}) (*{{.Rtrn}}, error) {
125 var r * incoming = &incoming{}
sslobodr1d1e50b2019-03-14 09:17:40 -0400126 //log.Debug("Serving {{$.Name}}")
sslobodrd6e07e72019-01-31 16:07:20 -0500127 // Read the metadata
128 if md,ok := metadata.FromIncomingContext(ctx); ok == false {
129 log.Error("Getting matadata during call to {{.Name}}")
130 } else {
131 r.meta = md.Copy()
132 }
133 // Read the data sent to the function
134 if parm,err := json.Marshal(in); err != nil {
135 log.Error("Marshalling the {{.Param}} for {{.Name}}")
136 } else {
137 r.payload = string(parm)
138 }
139 // Send the server information back to the test framework
sslobodr13182842019-02-08 14:40:30 -0500140 go func(ctx context.Context) {
141 select {
142 case ts.control.incmg <- r:
143 return
144 case <-ctx.Done():
145 return
146
147 }
148 }(glCtx)
sslobodrd6e07e72019-01-31 16:07:20 -0500149 // Read the value that needs to be returned from the channel
sslobodrd9daabf2019-02-05 13:14:21 -0500150 select {
151 case d := <-ts.control.replyData:
152 switch r := d.repl.(type) {
153 case *{{.Rtrn}}:
154 return r, nil
155 default:
156 return nil, errors.New("Mismatched type in call to {{.Name}}")
157 }
sslobodr13182842019-02-08 14:40:30 -0500158 case <-glCtx.Done():
159 return nil, errors.New("Timeout: nothing in the reply data channel in call to {{.Name}}, sending nil")
sslobodrd6e07e72019-01-31 16:07:20 -0500160 }
161 return &{{.Rtrn}}{},nil
162}
163{{end}}
164{{end}}
165