Skip to content

Commit

Permalink
feat(db): add repository package for persistence
Browse files Browse the repository at this point in the history
This is not full implementation yet, just the start

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Sep 8, 2023
1 parent 6b042bb commit c29493a
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
37 changes: 37 additions & 0 deletions pkg/repository/errors.go
Original file line number Diff line number Diff line change
@@ -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"
}
48 changes: 48 additions & 0 deletions pkg/repository/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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
}

// Creates new in-memory database based on golang map

Check warning on line 17 in pkg/repository/memory.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported function NewMemoryDatabase should be of the form "NewMemoryDatabase ..." (revive)
func NewMemoryDatabase() *memoryDatabase {

Check warning on line 18 in pkg/repository/memory.go

View workflow job for this annotation

GitHub Actions / call / golangci

unexported-return: exported func NewMemoryDatabase returns unexported type *repository.memoryDatabase, which can be annoying to use (revive)
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
}
61 changes: 61 additions & 0 deletions pkg/repository/models.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 14 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported type Vrf should be of the form "Vrf ..." (with optional leading article) (revive)
type Vrf struct {
Vni uint32
LoopbackIP net.IPNet
VtepIP net.IPNet
}

// Create new VRF object from protobuf

Check warning on line 21 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported function NewVrf should be of the form "NewVrf ..." (revive)
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}
}

// Transform VRF object to protobuf

Check warning on line 32 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported method Vrf.ToPb should be of the form "ToPb ..." (revive)
func (in *Vrf) ToPb() (*pb.Vrf, error) {
return &pb.Vrf{Spec: nil, Status: nil}, nil
}

// SVI object, separate from protobuf for decoupling

Check warning on line 37 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported type Svi should be of the form "Svi ..." (with optional leading article) (revive)
type Svi struct {
VrfRefKey string
LogicalBridgeRefKey string
MacAddress net.HardwareAddr
GwIP []net.IPNet
}

// Create new SVI object from protobuf

Check warning on line 45 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported function NewSvi should be of the form "NewSvi ..." (revive)
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}
}

// Transform SVI object to protobuf

Check warning on line 58 in pkg/repository/models.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: comment on exported method Svi.ToPb should be of the form "ToPb ..." (revive)
func (in *Svi) ToPb() (*pb.Svi, error) {
return &pb.Svi{Spec: nil, Status: nil}, nil
}
56 changes: 56 additions & 0 deletions pkg/repository/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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() (Database, error) {

Check warning on line 13 in pkg/repository/redis.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported function NewRedisDatabase should have comment or be unexported (revive)
// TODO: pass address
client := redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "", // no password set
DB: 0, // use default DB
})
_, 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{}
}
24 changes: 24 additions & 0 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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

// 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}
}
}

0 comments on commit c29493a

Please sign in to comment.