Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [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 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 20 | "fmt" |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 22 | "google.golang.org/grpc" |
| 23 | ) |
| 24 | |
| 25 | type RoundRobinRouter struct { |
| 26 | name string |
| 27 | grpcService string |
| 28 | cluster *cluster |
| 29 | currentBackend **backend |
| 30 | } |
| 31 | |
| 32 | func newRoundRobinRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) { |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 33 | var err error |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 34 | var rtrn_err = false |
| 35 | // Validate the configuration |
| 36 | |
| 37 | log.Debug("Creating a new round robin router") |
| 38 | // A name must exist |
| 39 | if config.Name == "" { |
| 40 | log.Error("A router 'name' must be specified") |
| 41 | rtrn_err = true |
| 42 | } |
| 43 | |
| 44 | if rconf.ProtoPackage == "" { |
| 45 | log.Error("A 'package' must be specified") |
| 46 | rtrn_err = true |
| 47 | } |
| 48 | |
| 49 | if rconf.ProtoService == "" { |
| 50 | log.Error("A 'service' must be specified") |
| 51 | rtrn_err = true |
| 52 | } |
| 53 | |
| 54 | var bptr *backend |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 55 | rr := RoundRobinRouter{ |
| 56 | name: config.Name, |
| 57 | grpcService: rconf.ProtoService, |
| 58 | currentBackend: &bptr, |
| 59 | } |
| 60 | |
| 61 | // Create the backend cluster or link to an existing one |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 62 | var ok bool |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 63 | if rr.cluster, ok = clusters[config.backendCluster.Name]; !ok { |
| 64 | if rr.cluster, err = newBackendCluster(config.backendCluster); err != nil { |
| 65 | log.Errorf("Could not create a backend for router %s", config.Name) |
| 66 | rtrn_err = true |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if rtrn_err { |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 71 | return rr, fmt.Errorf("Failed to create a new router '%s'", rr.name) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return rr, nil |
| 75 | } |
| 76 | |
| 77 | func (rr RoundRobinRouter) GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error) { |
| 78 | return "", "", nil |
| 79 | } |
| 80 | |
| 81 | func (rr RoundRobinRouter) IsStreaming(_ string) (bool, bool) { |
| 82 | panic("not implemented") |
| 83 | } |
| 84 | |
| 85 | func (rr RoundRobinRouter) BackendCluster(s string, mk string) (*cluster, error) { |
| 86 | return rr.cluster, nil |
| 87 | } |
| 88 | |
| 89 | func (rr RoundRobinRouter) Name() string { |
| 90 | return rr.name |
| 91 | } |
| 92 | |
| 93 | func (rr RoundRobinRouter) Route(sel interface{}) (*backend, *connection) { |
| 94 | var err error |
| 95 | switch sl := sel.(type) { |
| 96 | case *requestFrame: |
| 97 | // Since this is a round robin router just get the next backend |
| 98 | if *rr.currentBackend, err = rr.cluster.nextBackend(*rr.currentBackend, BackendSequenceRoundRobin); err == nil { |
| 99 | return *rr.currentBackend, nil |
| 100 | } else { |
| 101 | sl.err = err |
| 102 | return nil, nil |
| 103 | } |
| 104 | default: |
| 105 | log.Errorf("Internal: invalid data type in Route call %v", sel) |
| 106 | return nil, nil |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func (rr RoundRobinRouter) Service() string { |
| 111 | return rr.grpcService |
| 112 | } |
| 113 | |
| 114 | func (rr RoundRobinRouter) FindBackendCluster(becName string) *cluster { |
| 115 | if becName == rr.cluster.name { |
| 116 | return rr.cluster |
| 117 | } |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | func (rr RoundRobinRouter) ReplyHandler(sel interface{}) error { // This is a no-op |
| 122 | return nil |
| 123 | } |