blob: 1d2360b6b4bf1e64fbbff3117ad969f0f5de9a76 [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
19import (
20 "errors"
21 "fmt"
22 "github.com/opencord/voltha-go/common/log"
23 pb "github.com/opencord/voltha-protos/go/afrouter"
24 common_pb "github.com/opencord/voltha-protos/go/common"
25 "golang.org/x/net/context"
26 "google.golang.org/grpc"
27 "net"
28 "net/url"
29 "runtime"
30 "strconv"
31)
32
33type ArouterApi struct {
34 addr string
35 port int
36 apiListener net.Listener
37 apiServer *grpc.Server
38 running bool
39 ar *ArouterProxy
40}
41
42func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) {
43 var rtrn_err bool
44 // Create a seperate server and listener for the API
45 // Validate the ip address if one is provided
46 if _, err := url.Parse(config.Addr); err != nil {
47 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
48 rtrn_err = true
49 }
50 if rtrn_err {
51 return nil, errors.New("Errors in API configuration")
52 } else {
Scott Baker4989fe92019-10-09 17:03:06 -070053 var err error
Scott Bakere7144bc2019-10-01 14:16:47 -070054 aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar}
55 // Create the listener for the API server
56 if aa.apiListener, err =
57 net.Listen("tcp", config.Addr+":"+
58 strconv.Itoa(int(config.Port))); err != nil {
59 log.Error(err)
60 return nil, err
61 }
62 // Create the API server
63 aa.apiServer = grpc.NewServer()
64 pb.RegisterConfigurationServer(aa.apiServer, *aa)
65 return aa, err
66 }
67}
68
69func (aa *ArouterApi) getServer(srvr string) (*server, error) {
70 if s, ok := aa.ar.servers[srvr]; !ok {
Scott Baker4989fe92019-10-09 17:03:06 -070071 err := fmt.Errorf("Server '%s' doesn't exist", srvr)
Scott Bakere7144bc2019-10-01 14:16:47 -070072 return nil, err
73 } else {
74 return s, nil
75 }
76}
77
Scott Baker4989fe92019-10-09 17:03:06 -070078// nolint: unused
Scott Bakere7144bc2019-10-01 14:16:47 -070079func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) {
80 for _, pkg := range s.routers {
81 for _, r := range pkg {
82 if c := r.FindBackendCluster(clstr); c != nil {
83 return r, nil
84 }
85 }
86 }
Scott Baker4989fe92019-10-09 17:03:06 -070087 err := fmt.Errorf("Cluster '%s' doesn't exist", clstr)
Scott Bakere7144bc2019-10-01 14:16:47 -070088 return nil, err
89}
90
91func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) {
92 for _, pkg := range s.routers {
93 for _, r := range pkg {
94 if c := r.FindBackendCluster(clstr); c != nil {
95 return c, nil
96 }
97 }
98 }
Scott Baker4989fe92019-10-09 17:03:06 -070099 err := fmt.Errorf("Cluster '%s' doesn't exist", clstr)
Scott Bakere7144bc2019-10-01 14:16:47 -0700100 return nil, err
101}
102
103func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) {
104 for _, b := range c.backends {
105 if b.name == bknd {
106 return b, nil
107 }
108 }
Scott Baker4989fe92019-10-09 17:03:06 -0700109 err := fmt.Errorf("Backend '%s' doesn't exist in cluster %s",
110 bknd, c.name)
Scott Bakere7144bc2019-10-01 14:16:47 -0700111 return nil, err
112}
113
114func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) {
115 if c, ok := b.connections[con]; !ok {
Scott Baker4989fe92019-10-09 17:03:06 -0700116 err := fmt.Errorf("Connection '%s' doesn't exist", con)
Scott Bakere7144bc2019-10-01 14:16:47 -0700117 return nil, err
118 } else {
119 return c, nil
120 }
121}
122
123func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error {
124 return errors.New("updateConnection not implemented")
125}
126
127func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
128 log.Debugf("SetAffinity called! %v", in)
129 //return &pb.Result{Success:true,Error:""},nil
130 // Navigate down tot he connection and compare IP addresses and ports if they're
131 // not the same then close the existing connection. If they are bothe the same
132 // then return an error describing the situation.
133 var err error
134
135 aap := &aa
136
137 _ = aap
138
139 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
140 if r, ok := allRouters[in.Router+in.Route]; ok {
141 switch rr := r.(type) {
142 case AffinityRouter:
143 log.Debug("Affinity router found")
144 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
145 if b != nil {
Scott Baker4989fe92019-10-09 17:03:06 -0700146 err := rr.setAffinity(in.Id, b)
147 if err != nil {
148 log.Debugf("Couldn't set affinity: %s", err.Error())
149 return &pb.Result{Success: false, Error: err.Error()}, err
150 }
Scott Bakere7144bc2019-10-01 14:16:47 -0700151 } else {
152 log.Errorf("Requested backend '%s' not found", in.Backend)
153 }
154 _ = rr
155 case MethodRouter:
156 log.Debug("Method router found")
157 _ = rr
158 default:
159 log.Debug("Some other router found")
160 _ = rr
161 }
162 } else {
Scott Baker4989fe92019-10-09 17:03:06 -0700163 err = errors.New("Couldn't get router type")
164 log.Debugf("%v", err)
Scott Bakere7144bc2019-10-01 14:16:47 -0700165 return &pb.Result{Success: false, Error: err.Error()}, err
166 }
167
168 return &pb.Result{Success: true, Error: ""}, nil
169}
170
171func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
172 // Navigate down tot he connection and compare IP addresses and ports if they're
173 // not the same then close the existing connection. If they are bothe the same
174 // then return an error describing the situation.
175 var s *server
176 var c *cluster
177 var b *backend
178 var cn *connection
179 var err error
180
181 log.Debugf("SetConnection called! %v", in)
182
183 aap := &aa
184 if s, err = (aap).getServer(in.Server); err != nil {
Scott Baker4989fe92019-10-09 17:03:06 -0700185 err := fmt.Errorf("Server '%s' doesn't exist", in.Server)
Scott Bakere7144bc2019-10-01 14:16:47 -0700186 log.Error(err)
187 return &pb.Result{Success: false, Error: err.Error()}, err
188 }
189 // The cluster is usually accessed via tha router but since each
190 // cluster is unique it's good enough to find the router that
191 // has the cluster we're looking for rather than fully keying
192 // the path
193 if c, err = aap.getCluster(s, in.Cluster); err != nil {
194 log.Error(err)
195 return &pb.Result{Success: false, Error: err.Error()}, err
196 }
197
198 if b, err = aap.getBackend(c, in.Backend); err != nil {
199 log.Error(err)
200 return &pb.Result{Success: false, Error: err.Error()}, err
201 }
202
203 if cn, err = aap.getConnection(b, in.Connection); err != nil {
204 log.Error(err)
205 return &pb.Result{Success: false, Error: err.Error()}, err
206 }
207
208 if err = aap.updateConnection(in, cn, b); err != nil {
209 log.Error(err)
210 return &pb.Result{Success: false, Error: err.Error()}, err
211 }
212
213 return &pb.Result{Success: true, Error: ""}, nil
214}
215
216func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
217 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
218}
219
220func (aa ArouterApi) UpdateLogLevel(ctx context.Context, in *common_pb.Logging) (*pb.Empty, error) {
221 intLevel := int(in.Level)
222
223 if in.PackageName == "" {
224 log.SetAllLogLevel(intLevel)
225 log.SetDefaultLogLevel(intLevel)
226 } else if in.PackageName == "default" {
227 log.SetDefaultLogLevel(intLevel)
228 } else {
229 log.SetPackageLogLevel(in.PackageName, intLevel)
230 }
231
232 return &pb.Empty{}, nil
233}
234
235func (aa ArouterApi) GetLogLevels(ctx context.Context, in *common_pb.LoggingComponent) (*common_pb.Loggings, error) {
236 logLevels := &common_pb.Loggings{}
237
238 // do the per-package log levels
239 for _, packageName := range log.GetPackageNames() {
240 level, err := log.GetPackageLogLevel(packageName)
241 if err != nil {
242 return nil, err
243 }
244 logLevel := &common_pb.Logging{
245 ComponentName: in.ComponentName,
246 PackageName: packageName,
247 Level: common_pb.LogLevel_LogLevel(level)}
248 logLevels.Items = append(logLevels.Items, logLevel)
249 }
250
251 // now do the default log level
252 logLevel := &common_pb.Logging{
253 ComponentName: in.ComponentName,
254 PackageName: "default",
255 Level: common_pb.LogLevel_LogLevel(log.GetDefaultLogLevel())}
256 logLevels.Items = append(logLevels.Items, logLevel)
257
258 return logLevels, nil
259}
260
261func (aa *ArouterApi) serve() {
262 // Start a serving thread
263 go func() {
264 aa.running = true
265 if err := aa.apiServer.Serve(aa.apiListener); err != nil {
266 aa.running = false
267 log.Error(err)
268 errChan <- err
269 }
270 }()
271}