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 | // The template for the tester. |
| 17 | // This template is filled in by the |
| 18 | // test driver based on the configuration. |
| 19 | |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 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" |
| 30 | "github.com/opencord/voltha-go/common/log" |
| 31 | // 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}} |
| 46 | type reply struct { |
| 47 | repl interface{} |
| 48 | } |
| 49 | type incoming struct { |
| 50 | meta metadata.MD |
| 51 | payload string |
| 52 | } |
| 53 | type serverCtl struct { |
| 54 | replyData chan * reply |
| 55 | incmg chan * incoming |
| 56 | } |
| 57 | {{end}} |
| 58 | |
| 59 | type {{.Name}}TestServer struct { |
| 60 | control *serverCtl |
| 61 | srv *grpc.Server |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | func 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 | |
| 76 | func {{.Name}}ListenAndServe() error { |
| 77 | var s {{.Name}}TestServer |
| 78 | |
| 79 | s.control = &serverCtl{replyData:make(chan *reply,1), incmg:make(chan *incoming,1)} |
| 80 | 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}} |
| 114 | func (ts {{$.Name}}TestServer) {{.Name}}(in *{{.Param}}, srvr {{.Pkg}}.{{.Svc}}_{{.Name}}Server) error { |
| 115 | return nil |
| 116 | } |
| 117 | {{else if .Cs}} |
| 118 | func (ts {{$.Name}}TestServer) {{.Name}}({{.Pkg}}.{{.Svc}}_{{.Name}}Server) error { |
| 119 | return nil |
| 120 | } |
| 121 | {{else}} |
| 122 | func (ts {{$.Name}}TestServer) {{.Name}}(ctx context.Context, in *{{.Param}}) (*{{.Rtrn}}, error) { |
| 123 | var r * incoming = &incoming{} |
| 124 | // Read the metadata |
| 125 | if md,ok := metadata.FromIncomingContext(ctx); ok == false { |
| 126 | log.Error("Getting matadata during call to {{.Name}}") |
| 127 | } else { |
| 128 | r.meta = md.Copy() |
| 129 | } |
| 130 | // Read the data sent to the function |
| 131 | if parm,err := json.Marshal(in); err != nil { |
| 132 | log.Error("Marshalling the {{.Param}} for {{.Name}}") |
| 133 | } else { |
| 134 | r.payload = string(parm) |
| 135 | } |
| 136 | // Send the server information back to the test framework |
| 137 | ts.control.incmg <- r |
| 138 | // Read the value that needs to be returned from the channel |
| 139 | d := <-ts.control.replyData |
| 140 | switch r := d.repl.(type) { |
| 141 | case *{{.Rtrn}}: |
| 142 | return r, nil |
| 143 | default: |
| 144 | return nil, errors.New("Mismatched type in call to {{.Name}}") |
| 145 | } |
| 146 | return &{{.Rtrn}}{},nil |
| 147 | } |
| 148 | {{end}} |
| 149 | {{end}} |
| 150 | |