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 c261df2
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/repository/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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

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
}

func (repo *memoryDatabase) Get(key string) (string, error) {
repo.lock.RLock()
defer repo.lock.RUnlock()
value, err := repo.data[key]
if err != nil {
return nil, status.Errorf(codes.NotFound, "unable to find key %s", vxlanName)
}
return value, nil
}

func (repo *memoryDatabase) Delete(key string) (string, error) {
repo.lock.RLock()
defer repo.lock.RUnlock()
delete(repo.data, key)
}
48 changes: 48 additions & 0 deletions pkg/repository/models.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 (
"encoding/binary"
"net"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
)

type Vrf struct {
Vni int
LoopbackIp net.IPNet
VtepIp net.IPNet
}

func NewVrf(in pb.Vrf) *Vrf {
myip := make(net.IP, 4)
binary.BigEndian.PutUint32(myip, in.Vrf.Spec.LoopbackIpPrefix.Addr.GetV4Addr())
lip := net.IPNet{IP: myip, Mask: net.CIDRMask(int(in.Vrf.Spec.LoopbackIpPrefix.Len), 32)}
return &Vrf{Vni: in.Spec.Vni, LoopbackIp: lip}
}

func (s *Vrf) ToPb() (*pb.Vrf, error) {
return &pb.Vrf{Spec: nil, Status: nil}
}

type Svi struct {
VrfRefKey string
LogicalBridgeRefKey string
MacAddress net.HardwareAddr
GwIp net.IPNet
}

func NewSvi(in pb.Svi) *Svi {
mac := net.HardwareAddr(in.Spec.MacAddress[:])
myip := make(net.IP, 4)
binary.BigEndian.PutUint32(myip, in.Svi.Spec.GwIpPrefix.Addr.GetV4Addr())
gip := net.IPNet{IP: myip, Mask: net.CIDRMask(int(in.Svi.Spec.GwIpPrefix.Len), 32)}
return &Svi{VrfRefKey: in.Spec.Vrf, LogicalBridgeRefKey: in.Spec.LogicalBridge, MacAddress: mac, GwIp: gip}
}

func (s *Svi) ToPb() (*pb.Svi, error) {
return &pb.Svi{Spec: nil, Status: nil}
}
51 changes: 51 additions & 0 deletions pkg/repository/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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"

Check failure on line 7 in pkg/repository/redis.go

View workflow job for this annotation

GitHub Actions / call / build (1.18)

no required module provides package github.com/go-redis/redis; to add it:

Check failure on line 7 in pkg/repository/redis.go

View workflow job for this annotation

GitHub Actions / call / build (1.19)

no required module provides package github.com/go-redis/redis; to add it:

Check failure on line 7 in pkg/repository/redis.go

View workflow job for this annotation

GitHub Actions / call / build (1.20)

no required module provides package github.com/go-redis/redis; to add it:

Check failure on line 7 in pkg/repository/redis.go

View workflow job for this annotation

GitHub Actions / call / build (1.21)

no required module provides package github.com/go-redis/redis; to add it:

type redisDatabase struct {
client *redis.Client
}

func NewRedisDatabase() (Database, error) {
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 {
// TODO: use our own errors
return err
}
return key, nil
}

func (r *redisDatabase) Get(key string) (string, error) {
value, err := r.client.Get(key).Result()
if err != nil {
// TODO: use our own errors
return err
}
return value, nil
}

func (r *redisDatabase) Delete(key string) (string, error) {
_, err := r.client.Del(key).Result()
if err != nil {
// TODO: use our own errors
return err
}
return key, nil
}
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()
default:
return nil, &NotImplementedDatabaseError{databaseImplementation}
}
}

0 comments on commit c261df2

Please sign in to comment.