blob: 323ffb2424d8b1bfd09c5ef482307a2ce3e91441 [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 */
sslobodr392ebd52019-01-18 12:41:49 -050016
17package afrouter
18
19import (
sslobodr392ebd52019-01-18 12:41:49 -050020 "errors"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040021 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050022 "google.golang.org/grpc"
23)
24
Kent Hagerman1e9061e2019-05-21 16:01:21 -040025var allRouters = make(map[string]Router)
sslobodr392ebd52019-01-18 12:41:49 -050026
27// The router interface
28type Router interface {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040029 Name() string
sslobodr392ebd52019-01-18 12:41:49 -050030 Route(interface{}) *backend
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040031 Service() string
Kent Hagerman1e9061e2019-05-21 16:01:21 -040032 BackendCluster(string, string) (*cluster, error)
33 FindBackendCluster(string) *cluster
sslobodr392ebd52019-01-18 12:41:49 -050034 ReplyHandler(interface{}) error
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040035 GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error)
sslobodr392ebd52019-01-18 12:41:49 -050036}
37
sslobodrcd37bc52019-01-24 11:47:16 -050038func newRouter(config *RouterConfig) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040039 r, err := newMethodRouter(config)
40 if err == nil {
sslobodr392ebd52019-01-18 12:41:49 -050041 allRouters[r.Name()] = r
42 }
43 return r, err
44}
45
sslobodrcd37bc52019-01-24 11:47:16 -050046func newSubRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040047 switch config.Type {
48 case RouteTypeRpcAffinityMessage:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040049 r, err := newAffinityRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050050 if err == nil {
51 allRouters[rconf.Name+config.Name] = r
52 }
53 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040054 case RouteTypeBinding:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040055 r, err := newBindingRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050056 if err == nil {
57 allRouters[rconf.Name+config.Name] = r
58 }
59 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040060 case RouteTypeRoundRobin:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040061 r, err := newRoundRobinRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050062 if err == nil {
63 allRouters[rconf.Name+config.Name] = r
64 }
65 return r, err
66 default:
67 return nil, errors.New(fmt.Sprintf("Internal error, undefined router type: %s", config.Type))
68 }
sslobodr392ebd52019-01-18 12:41:49 -050069}