sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 20 | "errors" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 21 | "fmt" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/common/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 23 | pb "github.com/opencord/voltha-protos/go/afrouter" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 24 | "golang.org/x/net/context" |
| 25 | "google.golang.org/grpc" |
| 26 | "net" |
| 27 | "runtime" |
| 28 | "strconv" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 31 | type ArouterApi struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 32 | addr string |
| 33 | port int |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 34 | apiListener net.Listener |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 35 | apiServer *grpc.Server |
| 36 | running bool |
| 37 | ar *ArouterProxy |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 38 | } |
| 39 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 40 | func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 41 | 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 Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 48 | if rtrn_err { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 49 | return nil, errors.New("Errors in API configuration") |
| 50 | } else { |
| 51 | var err error = nil |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 52 | aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 53 | // Create the listener for the API server |
| 54 | if aa.apiListener, err = |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 55 | net.Listen("tcp", config.Addr+":"+ |
| 56 | strconv.Itoa(int(config.Port))); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 57 | 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 63 | return aa, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | func (aa *ArouterApi) getServer(srvr string) (*server, error) { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 68 | if s, ok := aa.ar.servers[srvr]; !ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 69 | err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", srvr)) |
| 70 | return nil, err |
| 71 | } else { |
| 72 | return s, nil |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 77 | for _, pkg := range s.routers { |
| 78 | for _, r := range pkg { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 79 | 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 Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 88 | func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 89 | for _, pkg := range s.routers { |
| 90 | for _, r := range pkg { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 91 | 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 Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 100 | func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 101 | for _, b := range c.backends { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 102 | if b.name == bknd { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 103 | return b, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 104 | } |
| 105 | } |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 106 | err := errors.New(fmt.Sprintf("Backend '%s' doesn't exist in cluster %s", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 107 | bknd, c.name)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 108 | return nil, err |
| 109 | } |
| 110 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 111 | func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) { |
| 112 | if c, ok := b.connections[con]; !ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 120 | func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 121 | sPort := strconv.FormatUint(in.Port, 10) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 127 | cn.close() |
| 128 | cn.addr = in.Addr |
| 129 | cn.port = sPort |
| 130 | cn.connect() |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func (aa ArouterApi) SetAffinity(ctx context.Context, in *pb.Affinity) (*pb.Result, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 135 | log.Debugf("SetAffinity called! %v", in) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 136 | //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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 144 | _ = aap |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 145 | |
| 146 | log.Debugf("Getting router %s and route %s", in.Router, in.Route) |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 147 | if r, ok := allRouters[in.Router+in.Route]; ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 148 | switch rr := r.(type) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 149 | 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 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 164 | } |
| 165 | } else { |
| 166 | log.Debugf("Couldn't get router type") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 167 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 170 | return &pb.Result{Success: true, Error: ""}, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 174 | // 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 Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 178 | var c *cluster |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 179 | var b *backend |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 180 | var cn *connection |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 181 | var err error |
| 182 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 183 | log.Debugf("SetConnection called! %v", in) |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 184 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 185 | aap := &aa |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 186 | if s, err = (aap).getServer(in.Server); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 187 | err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server)) |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 188 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 189 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 190 | } |
| 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 195 | if c, err = aap.getCluster(s, in.Cluster); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 196 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 197 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 200 | if b, err = aap.getBackend(c, in.Backend); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 201 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 202 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 205 | if cn, err = aap.getConnection(b, in.Connection); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 206 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 207 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if err = aap.updateConnection(in, cn, b); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 211 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 212 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 215 | return &pb.Result{Success: true, Error: ""}, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 216 | } |
| 217 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 218 | func (aa ArouterApi) GetGoroutineCount(ctx context.Context, in *pb.Empty) (*pb.Count, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 219 | return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 220 | } |
| 221 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 222 | func (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 | } |