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" |
Kent Hagerman | 70a49bb | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 27 | "net/url" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 28 | "runtime" |
| 29 | "strconv" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 32 | type ArouterApi struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 33 | addr string |
| 34 | port int |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 35 | apiListener net.Listener |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 36 | apiServer *grpc.Server |
| 37 | running bool |
| 38 | ar *ArouterProxy |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 39 | } |
| 40 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 41 | func newApi(config *ApiConfig, ar *ArouterProxy) (*ArouterApi, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 42 | var rtrn_err bool |
| 43 | // Create a seperate server and listener for the API |
| 44 | // Validate the ip address if one is provided |
Kent Hagerman | 70a49bb | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 45 | if _, err := url.Parse(config.Addr); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 46 | log.Errorf("Invalid address '%s' provided for API server", config.Addr) |
| 47 | rtrn_err = true |
| 48 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 49 | if rtrn_err { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 50 | return nil, errors.New("Errors in API configuration") |
| 51 | } else { |
| 52 | var err error = nil |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 53 | aa := &ArouterApi{addr: config.Addr, port: int(config.Port), ar: ar} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 54 | // Create the listener for the API server |
| 55 | if aa.apiListener, err = |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 56 | net.Listen("tcp", config.Addr+":"+ |
| 57 | strconv.Itoa(int(config.Port))); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 64 | return aa, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | func (aa *ArouterApi) getServer(srvr string) (*server, error) { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 69 | if s, ok := aa.ar.servers[srvr]; !ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 | |
| 77 | func (aa *ArouterApi) getRouter(s *server, clstr string) (Router, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 78 | for _, pkg := range s.routers { |
| 79 | for _, r := range pkg { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 89 | func (aa *ArouterApi) getCluster(s *server, clstr string) (*cluster, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 90 | for _, pkg := range s.routers { |
| 91 | for _, r := range pkg { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 101 | func (aa *ArouterApi) getBackend(c *cluster, bknd string) (*backend, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 102 | for _, b := range c.backends { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 103 | if b.name == bknd { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 104 | return b, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 107 | 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] | 108 | bknd, c.name)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 109 | return nil, err |
| 110 | } |
| 111 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 112 | func (aa *ArouterApi) getConnection(b *backend, con string) (*connection, error) { |
| 113 | if c, ok := b.connections[con]; !ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 114 | err := errors.New(fmt.Sprintf("Connection '%s' doesn't exist", con)) |
| 115 | return nil, err |
| 116 | } else { |
| 117 | return c, nil |
| 118 | } |
| 119 | } |
| 120 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 121 | func (aa *ArouterApi) updateConnection(in *pb.Conn, cn *connection, b *backend) error { |
Kent Hagerman | 70a49bb | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 122 | return errors.New("updateConnection not implemented") |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | 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] | 126 | log.Debugf("SetAffinity called! %v", in) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 127 | //return &pb.Result{Success:true,Error:""},nil |
| 128 | // Navigate down tot he connection and compare IP addresses and ports if they're |
| 129 | // not the same then close the existing connection. If they are bothe the same |
| 130 | // then return an error describing the situation. |
| 131 | var err error |
| 132 | |
| 133 | aap := &aa |
| 134 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 135 | _ = aap |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 136 | |
| 137 | log.Debugf("Getting router %s and route %s", in.Router, in.Route) |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 138 | if r, ok := allRouters[in.Router+in.Route]; ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 139 | switch rr := r.(type) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 140 | case AffinityRouter: |
| 141 | log.Debug("Affinity router found") |
| 142 | b := rr.FindBackendCluster(in.Cluster).getBackend(in.Backend) |
| 143 | if b != nil { |
| 144 | rr.setAffinity(in.Id, b) |
| 145 | } else { |
| 146 | log.Errorf("Requested backend '%s' not found", in.Backend) |
| 147 | } |
| 148 | _ = rr |
| 149 | case MethodRouter: |
| 150 | log.Debug("Method router found") |
| 151 | _ = rr |
| 152 | default: |
| 153 | log.Debug("Some other router found") |
| 154 | _ = rr |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 155 | } |
| 156 | } else { |
| 157 | log.Debugf("Couldn't get router type") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 158 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 161 | return &pb.Result{Success: true, Error: ""}, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func (aa ArouterApi) SetConnection(ctx context.Context, in *pb.Conn) (*pb.Result, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 165 | // Navigate down tot he connection and compare IP addresses and ports if they're |
| 166 | // not the same then close the existing connection. If they are bothe the same |
| 167 | // then return an error describing the situation. |
| 168 | var s *server |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 169 | var c *cluster |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 170 | var b *backend |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 171 | var cn *connection |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 172 | var err error |
| 173 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 174 | log.Debugf("SetConnection called! %v", in) |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 175 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 176 | aap := &aa |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 177 | if s, err = (aap).getServer(in.Server); err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 178 | err := errors.New(fmt.Sprintf("Server '%s' doesn't exist", in.Server)) |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 179 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 180 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 181 | } |
| 182 | // The cluster is usually accessed via tha router but since each |
| 183 | // cluster is unique it's good enough to find the router that |
| 184 | // has the cluster we're looking for rather than fully keying |
| 185 | // the path |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 186 | if c, err = aap.getCluster(s, in.Cluster); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 187 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 188 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 191 | if b, err = aap.getBackend(c, in.Backend); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 192 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 193 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 196 | if cn, err = aap.getConnection(b, in.Connection); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 197 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 198 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if err = aap.updateConnection(in, cn, b); err != nil { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 202 | log.Error(err) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 203 | return &pb.Result{Success: false, Error: err.Error()}, err |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 206 | return &pb.Result{Success: true, Error: ""}, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 207 | } |
| 208 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 209 | 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] | 210 | return &pb.Count{Count: uint32(runtime.NumGoroutine())}, nil |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 211 | } |
| 212 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 213 | func (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 | } |