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 <[email protected]>
  • Loading branch information
glimchb committed Sep 8, 2023
1 parent 6b042bb commit a510fe3
Show file tree
Hide file tree
Showing 8 changed files with 226 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"
}
47 changes: 47 additions & 0 deletions pkg/repository/memory.go
Original file line number Diff line number Diff line change
@@ -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 {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported function NewMemoryDatabase should have comment or be unexported (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
}
52 changes: 52 additions & 0 deletions pkg/repository/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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"
)

type Vrf struct {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type Vrf should have comment or be unexported (revive)
Vni uint32
LoopbackIp net.IPNet

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

View workflow job for this annotation

GitHub Actions / call / golangci

var-naming: struct field LoopbackIp should be LoopbackIP (revive)
VtepIp net.IPNet
}

func NewVrf(in pb.Vrf) *Vrf {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported function NewVrf should have comment or be unexported (revive)
myip := make(net.IP, 4)
binary.BigEndian.PutUint32(myip, in.Spec.LoopbackIpPrefix.Addr.GetV4Addr())
lip := net.IPNet{IP: myip, Mask: net.CIDRMask(int(in.Spec.LoopbackIpPrefix.Len), 32)}
return &Vrf{Vni: *in.Spec.Vni, LoopbackIp: lip}
}

func (s *Vrf) ToPb() (*pb.Vrf, error) {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported method Vrf.ToPb should have comment or be unexported (revive)
return &pb.Vrf{Spec: nil, Status: nil}, nil
}

type Svi struct {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type Svi should have comment or be unexported (revive)
VrfRefKey string
LogicalBridgeRefKey string
MacAddress net.HardwareAddr
GwIp []net.IPNet
}

func NewSvi(in pb.Svi) *Svi {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported function NewSvi should have comment or be unexported (revive)
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}
}

func (s *Svi) ToPb() (*pb.Svi, error) {

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

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported method Svi.ToPb should have comment or be unexported (revive)
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 a510fe3

Please sign in to comment.