blob: 8b728970a3df53fa5faa0dc552cf41a9684cf743 [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"
Scott Baker112b0d42019-08-22 08:32:26 -070024 common_pb "github.com/opencord/voltha-protos/go/common"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040025 "golang.org/x/net/context"
26 "google.golang.org/grpc"
27 "net"
Kent Hagerman03b58992019-08-29 17:21:03 -040028 "net/url"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040029 "runtime"
30 "strconv"
sslobodr392ebd52019-01-18 12:41:49 -050031)
32
sslobodr392ebd52019-01-18 12:41:49 -050033type ArouterApi struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040034 addr string
35 port int
sslobodr392ebd52019-01-18 12:41:49 -050036 apiListener net.Listener
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040037 apiServer *grpc.Server
38 running bool
39 ar *ArouterProxy
sslobodr392ebd52019-01-18 12:41:49 -050040}
41
sslobodrcd37bc52019-01-24 11:47:16 -050042func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) {
sslobodr392ebd52019-01-18 12:41:49 -050043 var rtrn_err bool
44 // Create a seperate server and listener for the API
45 // Validate the ip address if one is provided
Kent Hagerman03b58992019-08-29 17:21:03 -040046 if _, err := url.Parse(config.Addr); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050047 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
48 rtrn_err = true
49 }
Kent Hagerman1e9061e2019-05-21 16:01:21 -040050 if rtrn_err {
sslobodr392ebd52019-01-18 12:41:49 -050051 return nil, errors.New("Errors in API configuration")
52 } else {
53 var err error = nil
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040054 aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar}
sslobodr392ebd52019-01-18 12:41:49 -050055 // Create the listener for the API server
56 if aa.apiListener, err =
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040057 net.Listen("tcp", config.Addr+":"+
58 strconv.Itoa(int(config.Port))); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050059 log.Error(err)
60 return nil, err
61 }
62 // Create the API server
63 aa.apiServer = grpc.NewServer()
64 pb.RegisterConfigurationServer(aa.apiServer, *aa)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040065 return aa, err
sslobodr392ebd52019-01-18 12:41:49 -050066 }
67}
68
69func (aa *ArouterApi) getServer(srvr string) (*server, error) {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040070 if s, ok := aa.ar.servers[srvr]; !ok {
sslobodr392ebd52019-01-18 12:41:49 -050071 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", srvr))
72 return nil, err
73 } else {
74 return s, nil
75 }
76}
77
78func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040079 for _, pkg := range s.routers {
80 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050081 if c := r.FindBackendCluster(clstr); c != nil {
82 return r, nil
83 }
84 }
85 }
86 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
87 return nil, err
88}
89
Kent Hagerman1e9061e2019-05-21 16:01:21 -040090func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040091 for _, pkg := range s.routers {
92 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050093 if c := r.FindBackendCluster(clstr); c != nil {
94 return c, nil
95 }
96 }
97 }
98 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
99 return nil, err
100}
101
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400102func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400103 for _, b := range c.backends {
sslobodr392ebd52019-01-18 12:41:49 -0500104 if b.name == bknd {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400105 return b, nil
sslobodr392ebd52019-01-18 12:41:49 -0500106 }
107 }
sslobodr360c8d72019-02-05 12:47:56 -0500108 err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist in cluster %s",
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400109 bknd, c.name))
sslobodr392ebd52019-01-18 12:41:49 -0500110 return nil, err
111}
112
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400113func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) {
114 if c, ok := b.connections[con]; !ok {
sslobodr392ebd52019-01-18 12:41:49 -0500115 err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con))
116 return nil, err
117 } else {
118 return c, nil
119 }
120}
121
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400122func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error {
Kent Hagerman03b58992019-08-29 17:21:03 -0400123 return errors.New("updateConnection not implemented")
sslobodr392ebd52019-01-18 12:41:49 -0500124}
125
126func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400127 log.Debugf("SetAffinity called! %v", in)
sslobodr392ebd52019-01-18 12:41:49 -0500128 //return &pb.Result{Success:true,Error:""},nil
129 // Navigate down tot he connection and compare IP addresses and ports if they're
130 // not the same then close the existing connection. If they are bothe the same
131 // then return an error describing the situation.
132 var err error
133
134 aap := &aa
135
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400136 _ = aap
sslobodr392ebd52019-01-18 12:41:49 -0500137
138 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400139 if r, ok := allRouters[in.Router+in.Route]; ok {
sslobodr392ebd52019-01-18 12:41:49 -0500140 switch rr := r.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400141 case AffinityRouter:
142 log.Debug("Affinity router found")
143 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
144 if b != nil {
145 rr.setAffinity(in.Id, b)
146 } else {
147 log.Errorf("Requested backend '%s' not found", in.Backend)
148 }
149 _ = rr
150 case MethodRouter:
151 log.Debug("Method router found")
152 _ = rr
153 default:
154 log.Debug("Some other router found")
155 _ = rr
sslobodr392ebd52019-01-18 12:41:49 -0500156 }
157 } else {
158 log.Debugf("Couldn't get router type")
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400159 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500160 }
161
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400162 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500163}
164
165func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500166 // Navigate down tot he connection and compare IP addresses and ports if they're
167 // not the same then close the existing connection. If they are bothe the same
168 // then return an error describing the situation.
169 var s *server
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400170 var c *cluster
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400171 var b *backend
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400172 var cn *connection
sslobodr392ebd52019-01-18 12:41:49 -0500173 var err error
174
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400175 log.Debugf("SetConnection called! %v", in)
sslobodr360c8d72019-02-05 12:47:56 -0500176
sslobodr392ebd52019-01-18 12:41:49 -0500177 aap := &aa
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400178 if s, err = (aap).getServer(in.Server); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500179 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
sslobodr360c8d72019-02-05 12:47:56 -0500180 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400181 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500182 }
183 // The cluster is usually accessed via tha router but since each
184 // cluster is unique it's good enough to find the router that
185 // has the cluster we're looking for rather than fully keying
186 // the path
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400187 if c, err = aap.getCluster(s, in.Cluster); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500188 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400189 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500190 }
191
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400192 if b, err = aap.getBackend(c, in.Backend); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500193 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400194 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500195 }
196
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400197 if cn, err = aap.getConnection(b, in.Connection); 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
202 if err = aap.updateConnection(in, cn, b); 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 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500208}
209
sslobodr1d1e50b2019-03-14 09:17:40 -0400210func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400211 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
sslobodr1d1e50b2019-03-14 09:17:40 -0400212}
213
Scott Baker112b0d42019-08-22 08:32:26 -0700214func (aa ArouterApi) UpdateLogLevel(ctx context.Context, in *common_pb.Logging) (*pb.Empty, error) {
215 intLevel := int(in.Level)
216
217 if in.PackageName == "" {
218 log.SetAllLogLevel(intLevel)
219 log.SetDefaultLogLevel(intLevel)
220 } else if in.PackageName == "default" {
221 log.SetDefaultLogLevel(intLevel)
222 } else {
223 log.SetPackageLogLevel(in.PackageName, intLevel)
224 }
225
226 return &pb.Empty{}, nil
227}
228
229func (aa ArouterApi) GetLogLevels(ctx context.Context, in *common_pb.LoggingComponent) (*common_pb.Loggings, error) {
230 logLevels := &common_pb.Loggings{}
231
232 // do the per-package log levels
233 for _, packageName := range log.GetPackageNames() {
234 level, err := log.GetPackageLogLevel(packageName)
235 if err != nil {
236 return nil, err
237 }
238 logLevel := &common_pb.Logging{
239 ComponentName: in.ComponentName,
240 PackageName: packageName,
241 Level: common_pb.LogLevel_LogLevel(level)}
242 logLevels.Items = append(logLevels.Items, logLevel)
243 }
244
245 // now do the default log level
246 logLevel := &common_pb.Logging{
247 ComponentName: in.ComponentName,
248 PackageName: "default",
249 Level: common_pb.LogLevel_LogLevel(log.GetDefaultLogLevel())}
250 logLevels.Items = append(logLevels.Items, logLevel)
251
252 return logLevels, nil
253}
254
sslobodr392ebd52019-01-18 12:41:49 -0500255func (aa *ArouterApi) serve() {
256 // Start a serving thread
257 go func() {
258 aa.running = true
259 if err := aa.apiServer.Serve(aa.apiListener); err != nil {
260 aa.running = false
261 log.Error(err)
262 errChan <- err
263 }
264 }()
265}