Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
goSDN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
goSDN
Merge requests
!264
WIP: Develop
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
WIP: Develop
develop
into
master
Overview
0
Commits
57
Pipelines
100
Changes
2
Merged
Ghost User
requested to merge
develop
into
master
3 years ago
Overview
0
Commits
57
Pipelines
100
Changes
2
Expand
Description
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
My code follows the code style of this project.
My change requires a change to the documentation.
I have updated the documentation accordingly.
I have read the
CONTRIBUTING
document.
I have added tests to cover my changes.
All new and existing tests passed.
0
0
Merge request reports
Viewing commit
4016da44
Prev
Next
Show latest version
2 files
+
4
−
4
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
4016da44
Fix lint errors
· 4016da44
Andre Sterba
authored
3 years ago
See merge request
!285
controller/nucleus/databaseDeviceStore.go
0 → 100644
+
185
−
0
Options
package
nucleus
import
(
"fmt"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/device"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/google/uuid"
log
"github.com/sirupsen/logrus"
)
// DatabaseDeviceStore is used to store Devices
type
DatabaseDeviceStore
struct
{
storeName
string
}
// NewDatabaseDeviceStore returns a DeviceStore
func
NewDatabaseDeviceStore
(
pndUUID
uuid
.
UUID
)
device
.
Store
{
return
&
DatabaseDeviceStore
{
storeName
:
fmt
.
Sprintf
(
"device-store-%s.json"
,
pndUUID
.
String
()),
}
}
// Get takes a Device's UUID or name and returns the Device.
func
(
s
*
DatabaseDeviceStore
)
Get
(
query
store
.
Query
)
(
device
.
LoadedDevice
,
error
)
{
var
loadedDevice
device
.
LoadedDevice
if
query
.
ID
.
String
()
!=
""
{
loadedDevice
,
err
:=
s
.
getByID
(
query
.
ID
)
if
err
!=
nil
{
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
return
loadedDevice
,
nil
}
loadedDevice
,
err
:=
s
.
getByName
(
query
.
Name
)
if
err
!=
nil
{
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
return
loadedDevice
,
nil
}
func
(
s
*
DatabaseDeviceStore
)
getByID
(
idOfDevice
uuid
.
UUID
)
(
device
.
LoadedDevice
,
error
)
{
var
loadedDevice
device
.
LoadedDevice
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
db
:=
client
.
Database
(
database
.
DatabaseName
)
collection
:=
db
.
Collection
(
s
.
storeName
)
result
:=
collection
.
FindOne
(
ctx
,
bson
.
D
{
primitive
.
E
{
Key
:
"_id"
,
Value
:
idOfDevice
.
String
()}})
if
result
==
nil
{
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
err
:=
result
.
Decode
(
&
loadedDevice
)
if
err
!=
nil
{
log
.
Printf
(
"Failed marshalling %v"
,
err
)
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
return
loadedDevice
,
nil
}
func
(
s
*
DatabaseDeviceStore
)
getByName
(
nameOfDevice
string
)
(
device
.
LoadedDevice
,
error
)
{
var
loadedDevice
device
.
LoadedDevice
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
db
:=
client
.
Database
(
database
.
DatabaseName
)
collection
:=
db
.
Collection
(
s
.
storeName
)
result
:=
collection
.
FindOne
(
ctx
,
bson
.
D
{
primitive
.
E
{
Key
:
"name"
,
Value
:
nameOfDevice
}})
if
result
==
nil
{
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
err
:=
result
.
Decode
(
&
loadedDevice
)
if
err
!=
nil
{
log
.
Printf
(
"Failed marshalling %v"
,
err
)
return
loadedDevice
,
errors
.
ErrCouldNotFind
{
StoreName
:
deviceStoreName
}
}
return
loadedDevice
,
nil
}
// GetAll returns all stored devices.
func
(
s
*
DatabaseDeviceStore
)
GetAll
()
([]
device
.
LoadedDevice
,
error
)
{
var
loadedDevices
[]
device
.
LoadedDevice
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
db
:=
client
.
Database
(
database
.
DatabaseName
)
collection
:=
db
.
Collection
(
s
.
storeName
)
cursor
,
err
:=
collection
.
Find
(
ctx
,
bson
.
D
{})
if
err
!=
nil
{
return
nil
,
err
}
defer
cursor
.
Close
(
ctx
)
err
=
cursor
.
All
(
ctx
,
&
loadedDevices
)
if
err
!=
nil
{
log
.
Printf
(
"Failed marshalling %v"
,
err
)
return
nil
,
errors
.
ErrCouldNotMarshall
{
StoreName
:
pndStoreName
}
}
return
loadedDevices
,
nil
}
// Add adds a device to the device store.
func
(
s
*
DatabaseDeviceStore
)
Add
(
deviceToAdd
device
.
Device
)
error
{
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
_
,
err
:=
client
.
Database
(
database
.
DatabaseName
)
.
Collection
(
s
.
storeName
)
.
InsertOne
(
ctx
,
deviceToAdd
)
if
err
!=
nil
{
log
.
Printf
(
"Could not create Device: %v"
,
err
)
return
errors
.
ErrCouldNotCreate
{
StoreName
:
pndStoreName
}
}
return
nil
}
// Update updates a existing device.
func
(
s
*
DatabaseDeviceStore
)
Update
(
deviceToUpdate
device
.
Device
)
error
{
var
updatedLoadedDevice
device
.
LoadedDevice
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
update
:=
bson
.
D
{
primitive
.
E
{
Key
:
"$set"
,
Value
:
deviceToUpdate
}}
upsert
:=
false
after
:=
options
.
After
opt
:=
options
.
FindOneAndUpdateOptions
{
Upsert
:
&
upsert
,
ReturnDocument
:
&
after
,
}
err
:=
client
.
Database
(
database
.
DatabaseName
)
.
Collection
(
s
.
storeName
)
.
FindOneAndUpdate
(
ctx
,
bson
.
M
{
"_id"
:
deviceToUpdate
.
ID
()
.
String
()},
update
,
&
opt
)
.
Decode
(
&
updatedLoadedDevice
)
if
err
!=
nil
{
log
.
Printf
(
"Could not update Device: %v"
,
err
)
return
errors
.
ErrCouldNotUpdate
{
StoreName
:
pndStoreName
}
}
return
nil
}
// Delete deletes a device from the device store.
func
(
s
*
DatabaseDeviceStore
)
Delete
(
deviceToDelete
device
.
Device
)
error
{
client
,
ctx
,
cancel
:=
database
.
GetMongoConnection
()
defer
cancel
()
defer
client
.
Disconnect
(
ctx
)
db
:=
client
.
Database
(
database
.
DatabaseName
)
collection
:=
db
.
Collection
(
s
.
storeName
)
_
,
err
:=
collection
.
DeleteOne
(
ctx
,
bson
.
D
{
primitive
.
E
{
Key
:
deviceToDelete
.
ID
()
.
String
()}})
if
err
!=
nil
{
return
err
}
return
nil
}
Loading