blob: 3e47ef812ea1fb1a1ee5bc02ee21d7f32919a5a0 [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
19import (
sslobodr392ebd52019-01-18 12:41:49 -050020 "errors"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040021 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050022 "github.com/opencord/voltha-go/common/log"
William Kurkiandaa6bb22019-03-07 12:26:28 -050023 pb "github.com/opencord/voltha-protos/go/afrouter"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040024 "golang.org/x/net/context"
25 "google.golang.org/grpc"
26 "net"
Kent Hagerman03b58992019-08-29 17:21:03 -040027 "net/url"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040028 "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
Kent Hagerman03b58992019-08-29 17:21:03 -040045 if _, err := url.Parse(config.Addr); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050046 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
47 rtrn_err = true
48 }
Kent Hagerman1e9061e2019-05-21 16:01:21 -040049 if rtrn_err {
sslobodr392ebd52019-01-18 12:41:49 -050050 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 Hagerman1e9061e2019-05-21 16:01:21 -040069 if s, ok := aa.ar.servers[srvr]; !ok {
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
Kent Hagerman1e9061e2019-05-21 16:01:21 -040089func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, 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
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400101func (aa *ArouterApi) getBackend(c *cluster, 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
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400112func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) {
113 if c, ok := b.connections[con]; !ok {
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 Hagerman1e9061e2019-05-21 16:01:21 -0400121func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error {
Kent Hagerman03b58992019-08-29 17:21:03 -0400122 return errors.New("updateConnection not implemented")
sslobodr392ebd52019-01-18 12:41:49 -0500123}
124
125func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400126 log.Debugf("SetAffinity called! %v", in)
sslobodr392ebd52019-01-18 12:41:49 -0500127 //return &pb.Result{Success:true,Error:""},nil
128 // Navigate down tot he connection and compare IP addresses and ports if they're
129 // not the same then close the existing connection. If they are bothe the same
130 // then return an error describing the situation.
131 var err error
132
133 aap := &aa
134
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400135 _ = aap
sslobodr392ebd52019-01-18 12:41:49 -0500136
137 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400138 if r, ok := allRouters[in.Router+in.Route]; ok {
sslobodr392ebd52019-01-18 12:41:49 -0500139 switch rr := r.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400140 case AffinityRouter:
141 log.Debug("Affinity router found")
142 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
143 if b != nil {
144 rr.setAffinity(in.Id, b)
145 } else {
146 log.Errorf("Requested backend '%s' not found", in.Backend)
147 }
148 _ = rr
149 case MethodRouter:
150 log.Debug("Method router found")
151 _ = rr
152 default:
153 log.Debug("Some other router found")
154 _ = rr
sslobodr392ebd52019-01-18 12:41:49 -0500155 }
156 } else {
157 log.Debugf("Couldn't get router type")
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400158 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500159 }
160
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400161 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500162}
163
164func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500165 // Navigate down tot he connection and compare IP addresses and ports if they're
166 // not the same then close the existing connection. If they are bothe the same
167 // then return an error describing the situation.
168 var s *server
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400169 var c *cluster
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400170 var b *backend
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400171 var cn *connection
sslobodr392ebd52019-01-18 12:41:49 -0500172 var err error
173
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400174 log.Debugf("SetConnection called! %v", in)
sslobodr360c8d72019-02-05 12:47:56 -0500175
sslobodr392ebd52019-01-18 12:41:49 -0500176 aap := &aa
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400177 if s, err = (aap).getServer(in.Server); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500178 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
sslobodr360c8d72019-02-05 12:47:56 -0500179 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400180 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500181 }
182 // The cluster is usually accessed via tha router but since each
183 // cluster is unique it's good enough to find the router that
184 // has the cluster we're looking for rather than fully keying
185 // the path
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400186 if c, err = aap.getCluster(s, in.Cluster); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500187 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400188 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500189 }
190
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400191 if b, err = aap.getBackend(c, in.Backend); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500192 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400193 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500194 }
195
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400196 if cn, err = aap.getConnection(b, in.Connection); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500197 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400198 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500199 }
200
201 if err = aap.updateConnection(in, cn, b); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500202 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400203 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500204 }
205
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400206 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500207}
208
sslobodr1d1e50b2019-03-14 09:17:40 -0400209func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400210 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
sslobodr1d1e50b2019-03-14 09:17:40 -0400211}
212
sslobodr392ebd52019-01-18 12:41:49 -0500213func (aa *ArouterApi) serve() {
214 // Start a serving thread
215 go func() {
216 aa.running = true
217 if err := aa.apiServer.Serve(aa.apiListener); err != nil {
218 aa.running = false
219 log.Error(err)
220 errChan <- err
221 }
222 }()
223}