blob: dd1506ffa02d7203b6734bef8ae634cf89920b6d [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 */
16// gRPC affinity router with active/active backends
17
18package afrouter
19
20import (
21 "net"
22 "fmt"
23 "strconv"
24 "errors"
25 "google.golang.org/grpc"
26 "golang.org/x/net/context"
27 "github.com/opencord/voltha-go/common/log"
28 pb "github.com/opencord/voltha-go/protos/afrouter"
29)
30
31
32type ArouterApi struct {
33 addr string
34 port int
35 apiListener net.Listener
36 apiServer * grpc.Server
37 running bool
38 ar *ArouterProxy
39}
40
41func NewApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) {
42 var rtrn_err bool
43 // Create a seperate server and listener for the API
44 // Validate the ip address if one is provided
45 if ip := net.ParseIP(config.Addr); config.Addr != "" && ip == nil {
46 log.Errorf("Invalid address '%s' provided for API server", config.Addr)
47 rtrn_err = true
48 }
49 if rtrn_err == true {
50 return nil, errors.New("Errors in API configuration")
51 } else {
52 var err error = nil
53 aa := &ArouterApi{addr:config.Addr,port:int(config.Port),ar:ar}
54 // Create the listener for the API server
55 if aa.apiListener, err =
56 net.Listen("tcp", config.Addr + ":"+
57 strconv.Itoa(int(config.Port))); err != nil {
58 log.Error(err)
59 return nil, err
60 }
61 // Create the API server
62 aa.apiServer = grpc.NewServer()
63 pb.RegisterConfigurationServer(aa.apiServer, *aa)
64 return aa,err
65 }
66}
67
68func (aa *ArouterApi) getServer(srvr string) (*server, error) {
69 if s,ok := aa.ar.servers[srvr]; ok == false {
70 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) {
78 for _,pkg := range s.routers {
79 for _,r := range pkg {
80 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
89func (aa *ArouterApi) getCluster(s *server, clstr string) (*backendCluster, error) {
90 for _,pkg := range s.routers {
91 for _,r := range pkg {
92 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
101func (aa *ArouterApi) getBackend(c *backendCluster, bknd string) (*backend, error) {
102 for _,b := range c.backends {
103 if b.name == bknd {
104 return b,nil
105 }
106 }
107 err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist", bknd))
108 return nil, err
109}
110
111func (aa *ArouterApi) getConnection(b *backend, con string) (*beConnection, error) {
112 if c,ok := b.connections[con]; ok == false {
113 err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con))
114 return nil, err
115 } else {
116 return c, nil
117 }
118}
119
120func (aa * ArouterApi) updateConnection(in *pb.Conn, cn *beConnection, b *backend) error {
121 sPort := strconv.FormatUint(in.Port,10)
122 // 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 }
127 //log.Debugf("BEFORE: Be1: %v Be2 %v", cn.bknd, b)
128 cn.close()
129 cn.addr = in.Addr
130 cn.port = sPort
131 cn.connect()
132 return nil
133}
134
135func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) {
136 log.Debugf("SetAffinity called! %v",in);
137 //return &pb.Result{Success:true,Error:""},nil
138 // Navigate down tot he connection and compare IP addresses and ports if they're
139 // not the same then close the existing connection. If they are bothe the same
140 // then return an error describing the situation.
141 var err error
142
143 aap := &aa
144
145 _=aap
146
147 log.Debugf("Getting router %s and route %s", in.Router, in.Route)
148 if r,ok := allRouters[in.Router+in.Route]; ok == true {
149 switch rr := r.(type) {
150 case AffinityRouter:
151 log.Debug("Affinity router found")
152 b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend)
153 if b != nil {
154 rr.setAffinity(in.Id, b)
155 } else {
156 log.Errorf("Requested backend '%s' not found", in.Backend)
157 }
158 _ = rr
159 case MethodRouter:
160 log.Debug("Method router found")
161 _ = rr
162 default:
163 log.Debug("Some other router found")
164 _ = rr
165 }
166 } else {
167 log.Debugf("Couldn't get router type")
168 return &pb.Result{Success:false,Error:err.Error()}, err
169 }
170
171 return &pb.Result{Success:true,Error:""},nil
172}
173
174func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) {
175 log.Debugf("SetConnection called! %v",in);
176 // Navigate down tot he connection and compare IP addresses and ports if they're
177 // not the same then close the existing connection. If they are bothe the same
178 // then return an error describing the situation.
179 var s *server
180 var c *backendCluster
181 var b * backend
182 var cn * beConnection
183 var err error
184
185 aap := &aa
186 if s,err = (aap).getServer(in.Server); err != nil {
187 err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server))
188 return &pb.Result{Success:false,Error:err.Error()}, err
189 }
190 // The cluster is usually accessed via tha router but since each
191 // cluster is unique it's good enough to find the router that
192 // has the cluster we're looking for rather than fully keying
193 // the path
194 if c,err = aap.getCluster(s, in.Cluster); err != nil {
195 return &pb.Result{Success:false,Error:err.Error()}, err
196 }
197
198 if b,err = aap.getBackend(c, in.Backend); err != nil {
199 return &pb.Result{Success:false,Error:err.Error()}, err
200 }
201
202 if cn,err = aap.getConnection(b, in.Connection); err != nil {
203 return &pb.Result{Success:false,Error:err.Error()}, err
204 }
205
206 if err = aap.updateConnection(in, cn, b); err != nil {
207 return &pb.Result{Success:false,Error:err.Error()}, err
208 }
209
210 return &pb.Result{Success:true,Error:""},nil
211}
212
213func (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}
224