blob: 8b728970a3df53fa5faa0dc552cf41a9684cf743 [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 {
53 var err error = nil
54 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 {
71 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) {
79 for _, pkg := range s.routers {
80 for _, r := range pkg {
81 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
90func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) {
91 for _, pkg := range s.routers {
92 for _, r := range pkg {
93 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
102func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) {
103 for _, b := range c.backends {
104 if b.name == bknd {
105 return b, nil
106 }
107 }
108 err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist in cluster %s",
109 bknd, c.name))
110 return nil, err
111}
112
113func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) {
114 if c, ok := b.connections[con]; !ok {
115 err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con))
116 return nil, err
117 } else {
118 return c, nil
119 }
120}
121
122func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error {
123 return errors.New("updateConnection not implemented")
124}
125
126func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
127 log.Debugf("SetAffinity called! %v", in)
128 //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
136 _ = aap
137
138 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
139 if r, ok := allRouters[in.Router+in.Route]; ok {
140 switch rr := r.(type) {
141 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
156 }
157 } else {
158 log.Debugf("Couldn't get router type")
159 return &pb.Result{Success: false, Error: err.Error()}, err
160 }
161
162 return &pb.Result{Success: true, Error: ""}, nil
163}
164
165func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
166 // 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
170 var c *cluster
171 var b *backend
172 var cn *connection
173 var err error
174
175 log.Debugf("SetConnection called! %v", in)
176
177 aap := &aa
178 if s, err = (aap).getServer(in.Server); err != nil {
179 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
180 log.Error(err)
181 return &pb.Result{Success: false, Error: err.Error()}, err
182 }
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
187 if c, err = aap.getCluster(s, in.Cluster); err != nil {
188 log.Error(err)
189 return &pb.Result{Success: false, Error: err.Error()}, err
190 }
191
192 if b, err = aap.getBackend(c, in.Backend); err != nil {
193 log.Error(err)
194 return &pb.Result{Success: false, Error: err.Error()}, err
195 }
196
197 if cn, err = aap.getConnection(b, in.Connection); err != nil {
198 log.Error(err)
199 return &pb.Result{Success: false, Error: err.Error()}, err
200 }
201
202 if err = aap.updateConnection(in, cn, b); err != nil {
203 log.Error(err)
204 return &pb.Result{Success: false, Error: err.Error()}, err
205 }
206
207 return &pb.Result{Success: true, Error: ""}, nil
208}
209
210func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
211 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
212}
213
214func (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
255func (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}