-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): add repository package for persistence
This is not full implementation yet, just the start Signed-off-by: Boris Glimcher <[email protected]>
- Loading branch information
Showing
8 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
} | ||
} |