blob: 11e852d78d3b9ca62f12547e8fd81d99fa44ffa6 [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -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// gRPC affinity router with active/active backends
17
18package afrouter
19
20import (
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
28type 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
44func (br BindingRouter) BackendCluster(s string, metaKey string) (*backendCluster,error) {
45 return br.bkndClstr, nil
46 //return nil,errors.New("Not implemented yet")
47}
48func (br BindingRouter) Name() (string) {
49 return br.name
50}
51func (br BindingRouter) Service() (string) {
52 return br.grpcService
53}
54func (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}
sslobodr360c8d72019-02-05 12:47:56 -050072func (br BindingRouter) FindBackendCluster(becName string) (*backendCluster) {
73 if becName == br.bkndClstr.name {
74 return br.bkndClstr
75 }
sslobodr392ebd52019-01-18 12:41:49 -050076 return nil
77}
78func (br BindingRouter) ReplyHandler(v interface{}) error {
79 return nil
80}
81func (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
sslobodrcd37bc52019-01-24 11:47:16 -0500127func newBindingRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500128 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.
sslobodr5f0b5a32019-01-24 07:45:19 -0500209 for idx := range rTypeNames {
sslobodr392ebd52019-01-18 12:41:49 -0500210 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 {
sslobodrcd37bc52019-01-24 11:47:16 -0500219 if br.bkndClstr, err = newBackendCluster(config.backendCluster); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500220 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}