blob: 36e79a38f09bb226b34f17181e748a33897bd72d [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 */
16// gRPC affinity router with active/active backends
17
18package afrouter
19
20import (
sslobodr392ebd52019-01-18 12:41:49 -050021 "errors"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040022 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050023 "github.com/opencord/voltha-go/common/log"
William Kurkiandaa6bb22019-03-07 12:26:28 -050024 pb "github.com/opencord/voltha-protos/go/afrouter"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040025 "golang.org/x/net/context"
26 "google.golang.org/grpc"
27 "net"
28 "runtime"
29 "strconv"
sslobodr392ebd52019-01-18 12:41:49 -050030)
31
sslobodr392ebd52019-01-18 12:41:49 -050032type ArouterApi struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040033 addr string
34 port int
sslobodr392ebd52019-01-18 12:41:49 -050035 apiListener net.Listener
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040036 apiServer *grpc.Server
37 running bool
38 ar *ArouterProxy
sslobodr392ebd52019-01-18 12:41:49 -050039}
40
sslobodrcd37bc52019-01-24 11:47:16 -050041func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) {
sslobodr392ebd52019-01-18 12:41:49 -050042 var rtrn_err bool
43 // Create a seperate server and listener for the API
44 // Validate the ip address if one is provided
45 if ip := net.ParseIP(config.Addr); config.Addr != "" && ip == nil {
46 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
47 rtrn_err = true
48 }
49 if rtrn_err == true {
50 return nil, errors.New("Errors in API configuration")
51 } else {
52 var err error = nil
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040053 aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar}
sslobodr392ebd52019-01-18 12:41:49 -050054 // Create the listener for the API server
55 if aa.apiListener, err =
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040056 net.Listen("tcp", config.Addr+":"+
57 strconv.Itoa(int(config.Port))); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050058 log.Error(err)
59 return nil, err
60 }
61 // Create the API server
62 aa.apiServer = grpc.NewServer()
63 pb.RegisterConfigurationServer(aa.apiServer, *aa)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040064 return aa, err
sslobodr392ebd52019-01-18 12:41:49 -050065 }
66}
67
68func (aa *ArouterApi) getServer(srvr string) (*server, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040069 if s, ok := aa.ar.servers[srvr]; ok == false {
sslobodr392ebd52019-01-18 12:41:49 -050070 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", srvr))
71 return nil, err
72 } else {
73 return s, nil
74 }
75}
76
77func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040078 for _, pkg := range s.routers {
79 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050080 if c := r.FindBackendCluster(clstr); c != nil {
81 return r, nil
82 }
83 }
84 }
85 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
86 return nil, err
87}
88
89func (aa *ArouterApi) getCluster(s *server, clstr string) (*backendCluster, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040090 for _, pkg := range s.routers {
91 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050092 if c := r.FindBackendCluster(clstr); c != nil {
93 return c, nil
94 }
95 }
96 }
97 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
98 return nil, err
99}
100
101func (aa *ArouterApi) getBackend(c *backendCluster, bknd string) (*backend, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400102 for _, b := range c.backends {
sslobodr392ebd52019-01-18 12:41:49 -0500103 if b.name == bknd {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400104 return b, nil
sslobodr392ebd52019-01-18 12:41:49 -0500105 }
106 }
sslobodr360c8d72019-02-05 12:47:56 -0500107 err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist in cluster %s",
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400108 bknd, c.name))
sslobodr392ebd52019-01-18 12:41:49 -0500109 return nil, err
110}
111
112func (aa *ArouterApi) getConnection(b *backend, con string) (*beConnection, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400113 if c, ok := b.connections[con]; ok == false {
sslobodr392ebd52019-01-18 12:41:49 -0500114 err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con))
115 return nil, err
116 } else {
117 return c, nil
118 }
119}
120
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400121func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *beConnection, b *backend) error {
122 sPort := strconv.FormatUint(in.Port, 10)
sslobodr392ebd52019-01-18 12:41:49 -0500123 // Check that the ip address and or port are different
124 if in.Addr == cn.addr && sPort == cn.port {
125 err := errors.New(fmt.Sprintf("Refusing to change connection '%s' to identical values", in.Connection))
126 return err
127 }
128 //log.Debugf("BEFORE: Be1: %v Be2 %v", cn.bknd, b)
129 cn.close()
130 cn.addr = in.Addr
131 cn.port = sPort
132 cn.connect()
133 return nil
134}
135
136func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400137 log.Debugf("SetAffinity called! %v", in)
sslobodr392ebd52019-01-18 12:41:49 -0500138 //return &pb.Result{Success:true,Error:""},nil
139 // Navigate down tot he connection and compare IP addresses and ports if they're
140 // not the same then close the existing connection. If they are bothe the same
141 // then return an error describing the situation.
142 var err error
143
144 aap := &aa
145
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400146 _ = aap
sslobodr392ebd52019-01-18 12:41:49 -0500147
148 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400149 if r, ok := allRouters[in.Router+in.Route]; ok == true {
sslobodr392ebd52019-01-18 12:41:49 -0500150 switch rr := r.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400151 case AffinityRouter:
152 log.Debug("Affinity router found")
153 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
154 if b != nil {
155 rr.setAffinity(in.Id, b)
156 } else {
157 log.Errorf("Requested backend '%s' not found", in.Backend)
158 }
159 _ = rr
160 case MethodRouter:
161 log.Debug("Method router found")
162 _ = rr
163 default:
164 log.Debug("Some other router found")
165 _ = rr
sslobodr392ebd52019-01-18 12:41:49 -0500166 }
167 } else {
168 log.Debugf("Couldn't get router type")
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400169 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500170 }
171
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400172 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500173}
174
175func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500176 // Navigate down tot he connection and compare IP addresses and ports if they're
177 // not the same then close the existing connection. If they are bothe the same
178 // then return an error describing the situation.
179 var s *server
180 var c *backendCluster
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400181 var b *backend
182 var cn *beConnection
sslobodr392ebd52019-01-18 12:41:49 -0500183 var err error
184
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400185 log.Debugf("SetConnection called! %v", in)
sslobodr360c8d72019-02-05 12:47:56 -0500186
sslobodr392ebd52019-01-18 12:41:49 -0500187 aap := &aa
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400188 if s, err = (aap).getServer(in.Server); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500189 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
sslobodr360c8d72019-02-05 12:47:56 -0500190 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400191 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500192 }
193 // The cluster is usually accessed via tha router but since each
194 // cluster is unique it's good enough to find the router that
195 // has the cluster we're looking for rather than fully keying
196 // the path
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400197 if c, err = aap.getCluster(s, in.Cluster); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500198 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400199 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500200 }
201
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400202 if b, err = aap.getBackend(c, in.Backend); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500203 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400204 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500205 }
206
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400207 if cn, err = aap.getConnection(b, in.Connection); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500208 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400209 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500210 }
211
212 if err = aap.updateConnection(in, cn, b); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500213 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400214 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500215 }
216
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400217 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500218}
219
sslobodr1d1e50b2019-03-14 09:17:40 -0400220func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400221 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
sslobodr1d1e50b2019-03-14 09:17:40 -0400222}
223
sslobodr392ebd52019-01-18 12:41:49 -0500224func (aa *ArouterApi) serve() {
225 // Start a serving thread
226 go func() {
227 aa.running = true
228 if err := aa.apiServer.Serve(aa.apiListener); err != nil {
229 aa.running = false
230 log.Error(err)
231 errChan <- err
232 }
233 }()
234}