From fbabfc81184a4b08d0939e0c4f83a5bf49353609 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Fri, 8 Sep 2023 20:22:21 +0300 Subject: [PATCH] feat(db): add repository package for persistence This is not full implementation yet, just the start Signed-off-by: Boris Glimcher --- cmd/main.go | 7 +++++ go.mod | 1 + go.sum | 2 ++ pkg/repository/errors.go | 37 ++++++++++++++++++++++ pkg/repository/memory.go | 47 +++++++++++++++++++++++++++ pkg/repository/models.go | 61 ++++++++++++++++++++++++++++++++++++ pkg/repository/redis.go | 57 +++++++++++++++++++++++++++++++++ pkg/repository/repository.go | 26 +++++++++++++++ 8 files changed, 238 insertions(+) create mode 100644 pkg/repository/errors.go create mode 100644 pkg/repository/memory.go create mode 100644 pkg/repository/models.go create mode 100644 pkg/repository/redis.go create mode 100644 pkg/repository/repository.go diff --git a/cmd/main.go b/cmd/main.go index 1297e8d6..65a3ae93 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,6 +13,7 @@ import ( pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" "github.com/opiproject/opi-evpn-bridge/pkg/evpn" + "github.com/opiproject/opi-evpn-bridge/pkg/repository" "google.golang.org/grpc" "google.golang.org/grpc/reflection" @@ -28,6 +29,12 @@ func main() { if err != nil { log.Fatalf("failed to listen: %v", err) } + db, err := repository.Factory("memory") + if err != nil { + log.Fatalf("failed to create database: %v", err) + } + log.Printf("todo: use db in opi server (%v)", db) + s := grpc.NewServer() opi := evpn.NewServer() diff --git a/go.mod b/go.mod index a838da9f..f69842f1 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-redis/redis v6.15.9+incompatible // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index b5a7f873..f4ff3b25 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= diff --git a/pkg/repository/errors.go b/pkg/repository/errors.go new file mode 100644 index 00000000..449acc64 --- /dev/null +++ b/pkg/repository/errors.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. + +// Package repository is the database abstraction implementing repository design pattern +package repository + +// OperationError when cannot perform a given operation on database (SET, GET or DELETE) +type OperationError struct { + operation string +} + +func (err *OperationError) Error() string { + return "Could not perform the " + err.operation + " operation." +} + +// DownError when its not a redis.Nil response, in this case the database is down +type DownError struct{} + +func (dbe *DownError) Error() string { + return "Database is down" +} + +// CreateDatabaseError when cannot perform set on database +type CreateDatabaseError struct{} + +func (err *CreateDatabaseError) Error() string { + return "Could not create Databse" +} + +// NotImplementedDatabaseError when user tries to create a not implemented database +type NotImplementedDatabaseError struct { + database string +} + +func (err *NotImplementedDatabaseError) Error() string { + return err.database + " not implemented" +} diff --git a/pkg/repository/memory.go b/pkg/repository/memory.go new file mode 100644 index 00000000..d2585e80 --- /dev/null +++ b/pkg/repository/memory.go @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. + +// Package repository is the database abstraction implementing repository design pattern +package repository + +import ( + "fmt" + "sync" +) + +type memoryDatabase struct { + data map[string]string + lock sync.RWMutex +} + +func newMemoryDatabase() *memoryDatabase { + return &memoryDatabase{ + data: make(map[string]string), + // lock: &sync.RWMutex{}, + } +} + +func (repo *memoryDatabase) Set(key string, value string) (string, error) { + repo.lock.RLock() + defer repo.lock.RUnlock() + repo.data[key] = value + return key, nil +} + +func (repo *memoryDatabase) Get(key string) (string, error) { + repo.lock.RLock() + defer repo.lock.RUnlock() + value, ok := repo.data[key] + if !ok { + // TODO: use our own errors, maybe OperationError ? + return "", fmt.Errorf("value does not exist for key: %s", key) + } + return value, nil +} + +func (repo *memoryDatabase) Delete(key string) (string, error) { + repo.lock.RLock() + defer repo.lock.RUnlock() + delete(repo.data, key) + return key, nil +} diff --git a/pkg/repository/models.go b/pkg/repository/models.go new file mode 100644 index 00000000..8ec22a0e --- /dev/null +++ b/pkg/repository/models.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. + +// Package repository is the database abstraction implementing repository design pattern +package repository + +import ( + "encoding/binary" + "net" + + pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" +) + +// Vrf object, separate from protobuf for decoupling +type Vrf struct { + Vni uint32 + LoopbackIP net.IPNet + VtepIP net.IPNet +} + +// NewVrf creates new VRF object from protobuf message +func NewVrf(in *pb.Vrf) *Vrf { + loopip := make(net.IP, 4) + binary.BigEndian.PutUint32(loopip, in.Spec.LoopbackIpPrefix.Addr.GetV4Addr()) + lip := net.IPNet{IP: loopip, Mask: net.CIDRMask(int(in.Spec.LoopbackIpPrefix.Len), 32)} + vtepip := make(net.IP, 4) + binary.BigEndian.PutUint32(vtepip, in.Spec.VtepIpPrefix.Addr.GetV4Addr()) + vip := net.IPNet{IP: vtepip, Mask: net.CIDRMask(int(in.Spec.VtepIpPrefix.Len), 32)} + return &Vrf{Vni: *in.Spec.Vni, LoopbackIP: lip, VtepIP: vip} +} + +// ToPb transforms VRF object to protobuf message +func (in *Vrf) ToPb() (*pb.Vrf, error) { + return &pb.Vrf{Spec: nil, Status: nil}, nil +} + +// Svi object, separate from protobuf for decoupling +type Svi struct { + VrfRefKey string + LogicalBridgeRefKey string + MacAddress net.HardwareAddr + GwIP []net.IPNet +} + +// NewSvi creates new SVI object from protobuf message +func NewSvi(in *pb.Svi) *Svi { + mac := net.HardwareAddr(in.Spec.MacAddress) + gwIPList := []net.IPNet{} + for _, item := range in.Spec.GwIpPrefix { + myip := make(net.IP, 4) + binary.BigEndian.PutUint32(myip, item.Addr.GetV4Addr()) + gip := net.IPNet{IP: myip, Mask: net.CIDRMask(int(item.Len), 32)} + gwIPList = append(gwIPList, gip) + } + return &Svi{VrfRefKey: in.Spec.Vrf, LogicalBridgeRefKey: in.Spec.LogicalBridge, MacAddress: mac, GwIP: gwIPList} +} + +// ToPb transforms SVI object to protobuf message +func (in *Svi) ToPb() (*pb.Svi, error) { + return &pb.Svi{Spec: nil, Status: nil}, nil +} diff --git a/pkg/repository/redis.go b/pkg/repository/redis.go new file mode 100644 index 00000000..23708607 --- /dev/null +++ b/pkg/repository/redis.go @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. + +// Package repository is the database abstraction implementing repository design pattern +package repository + +import "github.com/go-redis/redis" + +type redisDatabase struct { + client *redis.Client +} + +func newRedisDatabase() (*redisDatabase, error) { + // TODO: pass address + url := "redis://redis:6379?password=&protocol=3/vrf" + opts, err := redis.ParseURL(url) + if err != nil { + return nil, &CreateDatabaseError{} + } + client := redis.NewClient(opts) + _, err = client.Ping().Result() // makes sure database is connected + if err != nil { + return nil, &CreateDatabaseError{} + } + return &redisDatabase{client: client}, nil +} + +func (r *redisDatabase) Set(key string, value string) (string, error) { + _, err := r.client.Set(key, value, 0).Result() + if err != nil { + return generateError("set", err) + } + return key, nil +} + +func (r *redisDatabase) Get(key string) (string, error) { + value, err := r.client.Get(key).Result() + if err != nil { + return generateError("get", err) + } + return value, nil +} + +func (r *redisDatabase) Delete(key string) (string, error) { + _, err := r.client.Del(key).Result() + if err != nil { + return generateError("delete", err) + } + return key, nil +} + +func generateError(operation string, err error) (string, error) { + if err == redis.Nil { + return "", &OperationError{operation} + } + return "", &DownError{} +} diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go new file mode 100644 index 00000000..899f4328 --- /dev/null +++ b/pkg/repository/repository.go @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. + +// Package repository is the database abstraction implementing repository design pattern +package repository + +// TODO: split this to IVrfRepository, ISviRepository, IPortRepository, IBridgeRepository + +// Database abstraction +type Database interface { + Set(key string, value string) (string, error) + Get(key string) (string, error) + Delete(key string) (string, error) +} + +// Factory pattern to create new Database +func Factory(databaseImplementation string) (Database, error) { + switch databaseImplementation { + case "redis": + return newRedisDatabase() + case "memory": + return newMemoryDatabase(), nil + default: + return nil, &NotImplementedDatabaseError{databaseImplementation} + } +}