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 | // This file implements the ArouterPoxy struct and its |
| 20 | // functions. The ArouterProxy is the top level object |
| 21 | // for the affinity router. |
| 22 | |
| 23 | import ( |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // String names for display in error messages. |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 29 | var arProxy *ArouterProxy |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 30 | |
| 31 | type ArouterProxy struct { |
| 32 | servers map[string]*server // Defined in handler.go |
| 33 | api *ArouterApi |
| 34 | } |
| 35 | |
| 36 | // Create the routing proxy |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 37 | func NewArouterProxy(conf *Configuration, p *probe.Probe) (*ArouterProxy, error) { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 38 | arProxy = &ArouterProxy{servers: make(map[string]*server)} |
| 39 | // Create all the servers listed in the configuration |
| 40 | for _, s := range conf.Servers { |
| 41 | if ns, err := newServer(&s); err != nil { |
| 42 | log.Error("Configuration failed") |
| 43 | return nil, err |
| 44 | } else { |
| 45 | arProxy.servers[ns.Name()] = ns |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // TODO: The API is not mandatory, check if it's even in the config before |
| 50 | // trying to create it. If it isn't then don't bother but log a warning. |
| 51 | if api, err := newApi(&conf.Api, arProxy); err != nil { |
| 52 | return nil, err |
| 53 | } else { |
| 54 | arProxy.api = api |
| 55 | } |
| 56 | |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 57 | p.UpdateStatus("affinity-router-proxy", probe.ServiceStatusRunning) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 58 | return arProxy, nil |
| 59 | } |
| 60 | |
| 61 | // Start serving |
| 62 | func (ap *ArouterProxy) ListenAndServe() error { |
| 63 | |
| 64 | for _, srvr := range ap.servers { |
| 65 | ap.serve(srvr) |
| 66 | } |
| 67 | ap.api.serve() |
| 68 | |
| 69 | // Just wait until we're done which only happens |
| 70 | // on a signal or an error. |
| 71 | err := <-doneChan |
| 72 | |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | func (ap *ArouterProxy) serve(srvr *server) { |
| 77 | |
| 78 | // Start a serving thread |
| 79 | go func() { |
| 80 | srvr.running = true |
| 81 | if err := srvr.proxyServer.Serve(srvr.proxyListener); err != nil { |
| 82 | srvr.running = false |
| 83 | log.Error(err) |
| 84 | errChan <- err |
| 85 | } |
| 86 | }() |
| 87 | } |