blob: 72641de52ec78ceeae8a121975b673cb0e880460 [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
19// This file implements the ArouterPoxy struct and its
20// functions. The ArouterProxy is the top level object
21// for the affinity router.
22
23import (
24 "github.com/opencord/voltha-go/common/log"
25)
26
sslobodr392ebd52019-01-18 12:41:49 -050027// String names for display in error messages.
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040028var arProxy *ArouterProxy = nil
sslobodr392ebd52019-01-18 12:41:49 -050029
30type ArouterProxy struct {
31 servers map[string]*server // Defined in handler.go
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040032 api *ArouterApi
sslobodr392ebd52019-01-18 12:41:49 -050033}
34
sslobodr392ebd52019-01-18 12:41:49 -050035// Create the routing proxy
36func NewArouterProxy(conf *Configuration) (*ArouterProxy, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040037 arProxy = &ArouterProxy{servers: make(map[string]*server)}
sslobodr392ebd52019-01-18 12:41:49 -050038 // Create all the servers listed in the configuration
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040039 for _, s := range conf.Servers {
40 if ns, err := newServer(&s); err != nil {
41 log.Error("Configuration failed")
42 return nil, err
43 } else {
sslobodr392ebd52019-01-18 12:41:49 -050044 arProxy.servers[ns.Name()] = ns
45 }
46 }
47
48 // TODO: The API is not mandatory, check if it's even in the config before
49 // trying to create it. If it isn't then don't bother but log a warning.
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040050 if api, err := newApi(&conf.Api, arProxy); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050051 return nil, err
52 } else {
53 arProxy.api = api
54 }
55
56 return arProxy, nil
57}
58
59// Start serving
60func (ap *ArouterProxy) ListenAndServe() error {
61
62 for _, srvr := range ap.servers {
63 ap.serve(srvr)
64 }
65 ap.api.serve()
66
67 // Just wait until we're done which only happens
68 // on a signal or an error.
69 err := <-doneChan
70
71 return err
72}
73
74func (ap *ArouterProxy) serve(srvr *server) {
75
76 // Start a serving thread
77 go func() {
78 srvr.running = true
79 if err := srvr.proxyServer.Serve(srvr.proxyListener); err != nil {
80 srvr.running = false
81 log.Error(err)
82 errChan <- err
83 }
84 }()
85}