blob: 6d1c747b39861f7bcd2fd537ea0986cdadbae895 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
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
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 (
Scott Bakerf579f132019-10-24 14:31:41 -070024 "github.com/opencord/voltha-lib-go/v2/pkg/log"
divyadesaif117fc22019-11-04 06:32:01 +000025 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Bakere7144bc2019-10-01 14:16:47 -070026)
27
28// String names for display in error messages.
Scott Baker4989fe92019-10-09 17:03:06 -070029var arProxy *ArouterProxy
Scott Bakere7144bc2019-10-01 14:16:47 -070030
31type ArouterProxy struct {
32 servers map[string]*server // Defined in handler.go
33 api *ArouterApi
34}
35
36// Create the routing proxy
divyadesaif117fc22019-11-04 06:32:01 +000037func NewArouterProxy(conf *Configuration, p *probe.Probe) (*ArouterProxy, error) {
Scott Bakere7144bc2019-10-01 14:16:47 -070038 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
divyadesaif117fc22019-11-04 06:32:01 +000057 p.UpdateStatus("affinity-router-proxy", probe.ServiceStatusRunning)
Scott Bakere7144bc2019-10-01 14:16:47 -070058 return arProxy, nil
59}
60
61// Start serving
62func (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
76func (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}