blob: 87f5573cac0496ca01ad3ba8743c6558306d854c [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"
27 "runtime"
28 "strconv"
sslobodr392ebd52019-01-18 12:41:49 -050029)
30
sslobodr392ebd52019-01-18 12:41:49 -050031type ArouterApi struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040032 addr string
33 port int
sslobodr392ebd52019-01-18 12:41:49 -050034 apiListener net.Listener
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040035 apiServer *grpc.Server
36 running bool
37 ar *ArouterProxy
sslobodr392ebd52019-01-18 12:41:49 -050038}
39
sslobodrcd37bc52019-01-24 11:47:16 -050040func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) {
sslobodr392ebd52019-01-18 12:41:49 -050041 var rtrn_err bool
42 // Create a seperate server and listener for the API
43 // Validate the ip address if one is provided
44 if ip := net.ParseIP(config.Addr); config.Addr != "" && ip == nil {
45 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
46 rtrn_err = true
47 }
Kent Hagerman1e9061e2019-05-21 16:01:21 -040048 if rtrn_err {
sslobodr392ebd52019-01-18 12:41:49 -050049 return nil, errors.New("Errors in API configuration")
50 } else {
51 var err error = nil
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040052 aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar}
sslobodr392ebd52019-01-18 12:41:49 -050053 // Create the listener for the API server
54 if aa.apiListener, err =
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040055 net.Listen("tcp", config.Addr+":"+
56 strconv.Itoa(int(config.Port))); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050057 log.Error(err)
58 return nil, err
59 }
60 // Create the API server
61 aa.apiServer = grpc.NewServer()
62 pb.RegisterConfigurationServer(aa.apiServer, *aa)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040063 return aa, err
sslobodr392ebd52019-01-18 12:41:49 -050064 }
65}
66
67func (aa *ArouterApi) getServer(srvr string) (*server, error) {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040068 if s, ok := aa.ar.servers[srvr]; !ok {
sslobodr392ebd52019-01-18 12:41:49 -050069 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", srvr))
70 return nil, err
71 } else {
72 return s, nil
73 }
74}
75
76func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040077 for _, pkg := range s.routers {
78 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050079 if c := r.FindBackendCluster(clstr); c != nil {
80 return r, nil
81 }
82 }
83 }
84 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
85 return nil, err
86}
87
Kent Hagerman1e9061e2019-05-21 16:01:21 -040088func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040089 for _, pkg := range s.routers {
90 for _, r := range pkg {
sslobodr392ebd52019-01-18 12:41:49 -050091 if c := r.FindBackendCluster(clstr); c != nil {
92 return c, nil
93 }
94 }
95 }
96 err := errors.New(fmt.Sprintf("Cluster '%s' doesn't exist", clstr))
97 return nil, err
98}
99
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400100func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400101 for _, b := range c.backends {
sslobodr392ebd52019-01-18 12:41:49 -0500102 if b.name == bknd {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400103 return b, nil
sslobodr392ebd52019-01-18 12:41:49 -0500104 }
105 }
sslobodr360c8d72019-02-05 12:47:56 -0500106 err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist in cluster %s",
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400107 bknd, c.name))
sslobodr392ebd52019-01-18 12:41:49 -0500108 return nil, err
109}
110
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400111func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) {
112 if c, ok := b.connections[con]; !ok {
sslobodr392ebd52019-01-18 12:41:49 -0500113 err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con))
114 return nil, err
115 } else {
116 return c, nil
117 }
118}
119
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400120func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400121 sPort := strconv.FormatUint(in.Port, 10)
sslobodr392ebd52019-01-18 12:41:49 -0500122 // Check that the ip address and or port are different
123 if in.Addr == cn.addr && sPort == cn.port {
124 err := errors.New(fmt.Sprintf("Refusing to change connection '%s' to identical values", in.Connection))
125 return err
126 }
sslobodr392ebd52019-01-18 12:41:49 -0500127 cn.close()
128 cn.addr = in.Addr
129 cn.port = sPort
130 cn.connect()
131 return nil
132}
133
134func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400135 log.Debugf("SetAffinity called! %v", in)
sslobodr392ebd52019-01-18 12:41:49 -0500136 //return &pb.Result{Success:true,Error:""},nil
137 // Navigate down tot he connection and compare IP addresses and ports if they're
138 // not the same then close the existing connection. If they are bothe the same
139 // then return an error describing the situation.
140 var err error
141
142 aap := &aa
143
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400144 _ = aap
sslobodr392ebd52019-01-18 12:41:49 -0500145
146 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400147 if r, ok := allRouters[in.Router+in.Route]; ok {
sslobodr392ebd52019-01-18 12:41:49 -0500148 switch rr := r.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400149 case AffinityRouter:
150 log.Debug("Affinity router found")
151 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
152 if b != nil {
153 rr.setAffinity(in.Id, b)
154 } else {
155 log.Errorf("Requested backend '%s' not found", in.Backend)
156 }
157 _ = rr
158 case MethodRouter:
159 log.Debug("Method router found")
160 _ = rr
161 default:
162 log.Debug("Some other router found")
163 _ = rr
sslobodr392ebd52019-01-18 12:41:49 -0500164 }
165 } else {
166 log.Debugf("Couldn't get router type")
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400167 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500168 }
169
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400170 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500171}
172
173func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500174 // Navigate down tot he connection and compare IP addresses and ports if they're
175 // not the same then close the existing connection. If they are bothe the same
176 // then return an error describing the situation.
177 var s *server
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400178 var c *cluster
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400179 var b *backend
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400180 var cn *connection
sslobodr392ebd52019-01-18 12:41:49 -0500181 var err error
182
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400183 log.Debugf("SetConnection called! %v", in)
sslobodr360c8d72019-02-05 12:47:56 -0500184
sslobodr392ebd52019-01-18 12:41:49 -0500185 aap := &aa
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400186 if s, err = (aap).getServer(in.Server); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500187 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
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 // The cluster is usually accessed via tha router but since each
192 // cluster is unique it's good enough to find the router that
193 // has the cluster we're looking for rather than fully keying
194 // the path
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400195 if c, err = aap.getCluster(s, in.Cluster); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500196 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400197 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500198 }
199
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400200 if b, err = aap.getBackend(c, in.Backend); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500201 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400202 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500203 }
204
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400205 if cn, err = aap.getConnection(b, in.Connection); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500206 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400207 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500208 }
209
210 if err = aap.updateConnection(in, cn, b); err != nil {
sslobodr360c8d72019-02-05 12:47:56 -0500211 log.Error(err)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400212 return &pb.Result{Success: false, Error: err.Error()}, err
sslobodr392ebd52019-01-18 12:41:49 -0500213 }
214
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400215 return &pb.Result{Success: true, Error: ""}, nil
sslobodr392ebd52019-01-18 12:41:49 -0500216}
217
sslobodr1d1e50b2019-03-14 09:17:40 -0400218func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400219 return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil
sslobodr1d1e50b2019-03-14 09:17:40 -0400220}
221
sslobodr392ebd52019-01-18 12:41:49 -0500222func (aa *ArouterApi) serve() {
223 // Start a serving thread
224 go func() {
225 aa.running = true
226 if err := aa.apiServer.Serve(aa.apiListener); err != nil {
227 aa.running = false
228 log.Error(err)
229 errChan <- err
230 }
231 }()
232}