sslobodr | 392ebd5 | 2019-01-18 12:41:49 -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 | */ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 20 | "errors" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "github.com/opencord/voltha-go/common/log" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 23 | "google.golang.org/grpc" |
| 24 | "google.golang.org/grpc/codes" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 25 | "net" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 26 | "strconv" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | clientStreamDescForProxying = &grpc.StreamDesc{ |
| 31 | ServerStreams: true, |
| 32 | ClientStreams: true, |
| 33 | } |
| 34 | ) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 35 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 36 | type server struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 37 | running bool |
| 38 | name string |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 39 | proxyListener net.Listener |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 40 | routers map[string]map[string]Router |
| 41 | proxyServer *grpc.Server |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 44 | func newServer(config *ServerConfig) (*server, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 45 | var err error = nil |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 46 | var rtrn_err = false |
| 47 | var s *server |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 48 | // Change over to the new configuration format |
| 49 | // Validate the configuration |
| 50 | // There should be a name |
| 51 | if config.Name == "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 52 | log.Error("A server has been defined with no name") |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 53 | rtrn_err = true |
| 54 | } |
| 55 | // Validate that there's a port specified |
| 56 | if config.Port == 0 { |
| 57 | log.Errorf("Server %s does not have a valid port assigned", config.Name) |
| 58 | rtrn_err = true |
| 59 | } |
| 60 | // Validate the ip address if one is provided |
| 61 | if ip := net.ParseIP(config.Addr); config.Addr != "" && ip == nil { |
| 62 | log.Errorf("Invalid address '%s' provided for server '%s'", config.Addr, config.Name) |
| 63 | rtrn_err = true |
| 64 | } |
| 65 | if config.Type != "grpc" && config.Type != "streaming_grpc" { |
| 66 | if config.Type == "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 67 | log.Errorf("A server 'type' must be defined for server %s", config.Name) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 68 | } else { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 69 | log.Errorf("The server type must be either 'grpc' or 'streaming_grpc' "+ |
| 70 | "but '%s' was found for server '%s'", config.Type, config.Name) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 71 | } |
| 72 | rtrn_err = true |
| 73 | } |
| 74 | if len(config.Routers) == 0 { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 75 | log.Errorf("At least one router must be specified for server '%s'", config.Name) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 76 | rtrn_err = true |
| 77 | } |
| 78 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 79 | if rtrn_err { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 80 | return nil, errors.New("Server configuration failed") |
| 81 | } else { |
| 82 | // The configuration is valid, create a server and configure it. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 83 | s = &server{name: config.Name, routers: make(map[string]map[string]Router)} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 84 | // The listener |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 85 | if s.proxyListener, err = |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 86 | net.Listen("tcp", config.Addr+":"+ |
| 87 | strconv.Itoa(int(config.Port))); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 88 | log.Error(err) |
| 89 | return nil, err |
| 90 | } |
| 91 | // Create the routers |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 92 | log.Debugf("Configuring the routers for server %s", s.name) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 93 | for p, r := range config.routers { |
| 94 | log.Debugf("Processing router %s for package %s", r.Name, p) |
| 95 | if dr, err := newRouter(r); err != nil { |
| 96 | log.Error(err) |
| 97 | return nil, err |
| 98 | } else { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 99 | log.Debugf("Adding router %s to the server %s for package %s and service %s", |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 100 | dr.Name(), s.name, p, dr.Service()) |
| 101 | if _, ok := s.routers[p]; ok { |
| 102 | s.routers[p][dr.Service()] = dr |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 103 | } else { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 104 | s.routers[p] = make(map[string]Router) |
| 105 | s.routers[p][dr.Service()] = dr |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | // Configure the grpc handler |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 110 | s.proxyServer = grpc.NewServer( |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 111 | grpc.CustomCodec(Codec()), |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 112 | grpc.UnknownServiceHandler(s.TransparentHandler()), |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 113 | ) |
| 114 | |
| 115 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 116 | return s, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 119 | func (s *server) Name() string { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 120 | return s.name |
| 121 | } |
| 122 | |
| 123 | func (s *server) TransparentHandler() grpc.StreamHandler { |
| 124 | return s.handler |
| 125 | } |
| 126 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 127 | func (s *server) getRouter(pkg string, service string) (Router, bool) { |
| 128 | if fn, ok := s.routers[pkg][service]; ok { // Both specified |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 129 | return fn, ok |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 130 | } else if fn, ok = s.routers["*"][service]; ok { // Package wild card |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 131 | return fn, ok |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 132 | } else if fn, ok = s.routers[pkg]["*"]; ok { // Service wild card |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 133 | return fn, ok |
| 134 | } else if fn, ok = s.routers["*"]["*"]; ok { // Both Wildcarded |
| 135 | return fn, ok |
| 136 | } else { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 137 | return nil, false |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 141 | func (s *server) handler(srv interface{}, serverStream grpc.ServerStream) error { |
| 142 | // Determine what router is intended to handle this request |
| 143 | fullMethodName, ok := grpc.MethodFromServerStream(serverStream) |
| 144 | if !ok { |
| 145 | return grpc.Errorf(codes.Internal, "lowLevelServerStream doesn't exist in context") |
| 146 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 147 | log.Debugf("Processing grpc request %s on server %s", fullMethodName, s.name) |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 148 | methodInfo := newMethodDetails(fullMethodName) |
| 149 | r, ok := s.getRouter(methodInfo.pkg, methodInfo.service) |
| 150 | //fn, ok := s.routers[methodInfo.pkg][methodInfo.service] |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 151 | if !ok { |
| 152 | // TODO: Should this be punted to a default transparent router?? |
| 153 | // Probably not, if one is defined yes otherwise just crap out. |
| 154 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 155 | err := fmt.Errorf("Unable to dispatch! Service '%s' for package '%s' not found.", methodInfo.service, methodInfo.pkg) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 156 | log.Error(err) |
| 157 | return err |
| 158 | } |
| 159 | log.Debugf("Selected router %s\n", r.Name()) |
| 160 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 161 | mk, mv, err := r.GetMetaKeyVal(serverStream) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 162 | if err != nil { |
| 163 | log.Error(err) |
| 164 | return err |
| 165 | } |
| 166 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 167 | //nbR := &nbRequest(srv:srv,serverStream:serverStream,r:r,methodInfo:methodInfo,metaKey:mk,metaVal:mv) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 168 | |
| 169 | // Extract the cluster from the selected router and use it to manage the transfer |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 170 | if cluster, err := r.BackendCluster(methodInfo.method, mk); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 171 | return err |
| 172 | } else { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 173 | //return beCluster.handler(nbR) |
| 174 | return cluster.handler(srv, serverStream, r, methodInfo, mk, mv) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 175 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 176 | } |