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 | */ |
| 16 | // gRPC affinity router with active/active backends |
| 17 | |
| 18 | package afrouter |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "errors" |
| 23 | "google.golang.org/grpc" |
| 24 | "google.golang.org/grpc/metadata" |
| 25 | "github.com/opencord/voltha-go/common/log" |
| 26 | ) |
| 27 | |
| 28 | type BindingRouter struct { |
| 29 | name string |
| 30 | routerType int // TODO: This is probably not needed |
| 31 | association int |
| 32 | //routingField string |
| 33 | grpcService string |
| 34 | //protoDescriptor *pb.FileDescriptorSet |
| 35 | //methodMap map[string]byte |
| 36 | bkndClstr *backendCluster |
| 37 | bindings map[string]*backend |
| 38 | bindingType string |
| 39 | bindingField string |
| 40 | bindingMethod string |
| 41 | curBknd **backend |
| 42 | } |
| 43 | |
| 44 | func (br BindingRouter) BackendCluster(s string, metaKey string) (*backendCluster,error) { |
| 45 | return br.bkndClstr, nil |
| 46 | //return nil,errors.New("Not implemented yet") |
| 47 | } |
| 48 | func (br BindingRouter) Name() (string) { |
| 49 | return br.name |
| 50 | } |
| 51 | func (br BindingRouter) Service() (string) { |
| 52 | return br.grpcService |
| 53 | } |
| 54 | func (br BindingRouter) GetMetaKeyVal(serverStream grpc.ServerStream) (string,string,error) { |
| 55 | var rtrnK string = "" |
| 56 | var rtrnV string = "" |
| 57 | |
| 58 | // Get the metadata from the server stream |
| 59 | md, ok := metadata.FromIncomingContext(serverStream.Context()) |
| 60 | if !ok { |
| 61 | return rtrnK, rtrnV, errors.New("Could not get a server stream metadata") |
| 62 | } |
| 63 | |
| 64 | // Determine if one of the method routing keys exists in the metadata |
| 65 | if _,ok := md[br.bindingField]; ok == true { |
| 66 | rtrnV = md[br.bindingField][0] |
| 67 | rtrnK = br.bindingField |
| 68 | } |
| 69 | |
| 70 | return rtrnK,rtrnV,nil |
| 71 | } |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 72 | func (br BindingRouter) FindBackendCluster(becName string) (*backendCluster) { |
| 73 | if becName == br.bkndClstr.name { |
| 74 | return br.bkndClstr |
| 75 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 76 | return nil |
| 77 | } |
| 78 | func (br BindingRouter) ReplyHandler(v interface{}) error { |
| 79 | return nil |
| 80 | } |
| 81 | func (br BindingRouter) Route(sel interface{}) (*backend) { |
| 82 | var err error |
| 83 | switch sl := sel.(type) { |
| 84 | case *nbFrame: |
| 85 | if b, ok := br.bindings[sl.metaVal]; ok == true { // binding exists, just return it |
| 86 | return b |
| 87 | } else { // establish a new binding or error. |
| 88 | if sl.metaVal != "" { |
| 89 | err = errors.New(fmt.Sprintf("Attempt to route on non-existent metadata value '%s' in key '%s'", |
| 90 | sl.metaVal, sl.metaKey)) |
| 91 | log.Error(err) |
| 92 | sl.err = err |
| 93 | return nil |
| 94 | } |
| 95 | if sl.mthdSlice[REQ_METHOD] != br.bindingMethod { |
| 96 | err = errors.New(fmt.Sprintf("Binding must occur with method %s but attempted with method %s", |
| 97 | br.bindingMethod, sl.mthdSlice[REQ_METHOD])) |
| 98 | log.Error(err) |
| 99 | sl.err = err |
| 100 | return nil |
| 101 | } |
| 102 | log.Debugf("MUST CREATE A NEW BINDING MAP ENTRY!!") |
| 103 | if len(br.bindings) < len(br.bkndClstr.backends) { |
| 104 | if *br.curBknd, err = br.bkndClstr.nextBackend(*br.curBknd,BE_SEQ_RR); err == nil { |
| 105 | // Use the name of the backend as the metaVal for this new binding |
| 106 | br.bindings[(*br.curBknd).name] = *br.curBknd |
| 107 | return *br.curBknd |
| 108 | } else { |
| 109 | log.Error(err) |
| 110 | sl.err = err |
| 111 | return nil |
| 112 | } |
| 113 | } else { |
| 114 | err = errors.New(fmt.Sprintf("Backends exhausted in attempt to bind for metakey '%s' with value '%s'", |
| 115 | sl.metaKey, sl.metaVal)) |
| 116 | log.Error(err) |
| 117 | sl.err = err |
| 118 | } |
| 119 | } |
| 120 | return nil |
| 121 | default: |
| 122 | return nil |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 127 | func newBindingRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 128 | var rtrn_err bool = false |
| 129 | var err error = nil |
| 130 | log.Debugf("Creating binding router %s",config.Name) |
| 131 | // A name must exist |
| 132 | if config.Name == "" { |
| 133 | log.Error("A router 'name' must be specified") |
| 134 | rtrn_err = true |
| 135 | } |
| 136 | |
| 137 | if rconf.ProtoPackage == "" { |
| 138 | log.Error("A 'package' must be specified") |
| 139 | rtrn_err = true |
| 140 | } |
| 141 | |
| 142 | if rconf.ProtoService == "" { |
| 143 | log.Error("A 'service' must be specified") |
| 144 | rtrn_err = true |
| 145 | } |
| 146 | |
| 147 | //if config.RouteField == "" { |
| 148 | // log.Error("A 'routing_field' must be specified") |
| 149 | // rtrn_err = true |
| 150 | //} |
| 151 | |
| 152 | // TODO: Using the specified service, the imported proto |
| 153 | // descriptor file should be scanned for all methods provided |
| 154 | // for this router to ensure that this field exists in |
| 155 | // the message(s) passed to the method. This will avoid run |
| 156 | // time failures that might not be detected for long periods |
| 157 | // of time. |
| 158 | |
| 159 | // TODO The routes section is currently not being used |
| 160 | // so the router will route all methods based on the |
| 161 | // routing_field. This needs to be done. |
| 162 | var bptr *backend |
| 163 | bptr = nil |
| 164 | br := BindingRouter{ |
| 165 | name:config.Name, |
| 166 | grpcService:rconf.ProtoService, |
| 167 | bindings:make(map[string]*backend), |
| 168 | //methodMap:make(map[string]byte), |
| 169 | curBknd:&bptr, |
| 170 | //serialNo:0, |
| 171 | } |
| 172 | |
| 173 | // A binding association must exist |
| 174 | br.association = strIndex(rAssnNames, config.Binding.Association) |
| 175 | if br.association == 0 { |
| 176 | if config.Binding.Association == "" { |
| 177 | log.Error("An binding association must be specified") |
| 178 | } else { |
| 179 | log.Errorf("The binding association '%s' is not valid", config.Binding.Association) |
| 180 | } |
| 181 | rtrn_err = true |
| 182 | } |
| 183 | // A binding type must exist |
| 184 | // TODO: This is parsed but ignored and a header based type is used. |
| 185 | if config.Binding.Type != "header" { |
| 186 | log.Error("The binding type must be set to header") |
| 187 | rtrn_err = true |
| 188 | } else { |
| 189 | br.bindingType = config.Binding.Type |
| 190 | } |
| 191 | // A binding method must exist |
| 192 | if config.Binding.Method == "" { |
| 193 | log.Error("The binding method must be specified") |
| 194 | rtrn_err = true |
| 195 | } else { |
| 196 | br.bindingMethod = config.Binding.Method |
| 197 | } |
| 198 | // A binding field must exxist |
| 199 | if config.Binding.Field == "" { |
| 200 | log.Error("The binding field must be specified") |
| 201 | rtrn_err = true |
| 202 | } else { |
| 203 | br.bindingField = config.Binding.Field |
| 204 | } |
| 205 | |
| 206 | |
| 207 | // This has already been validated bfore this function |
| 208 | // is called so just use it. |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 209 | for idx := range rTypeNames { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 210 | if config.Type == rTypeNames[idx] { |
| 211 | br.routerType = idx |
| 212 | break |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Create the backend cluster or link to an existing one |
| 217 | ok := true |
| 218 | if br.bkndClstr, ok = bClusters[config.backendCluster.Name]; ok == false { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 219 | if br.bkndClstr, err = newBackendCluster(config.backendCluster); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 220 | log.Errorf("Could not create a backend for router %s", config.Name) |
| 221 | rtrn_err = true |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // HERE HERE HERE |
| 226 | |
| 227 | if rtrn_err { |
| 228 | return br,errors.New(fmt.Sprintf("Failed to create a new router '%s'",br.name)) |
| 229 | } |
| 230 | |
| 231 | |
| 232 | return br,nil |
| 233 | } |