Skip to content
Snippets Groups Projects
Commit ed74dcfa authored by André Sterba's avatar André Sterba
Browse files

Add event interactions to node service

parent 81d7d8ce
Branches
No related tags found
5 merge requests!376Add additional example application hostname-checker,!349Northbound refactoring to implement NIB concept for devices,!343Add basic application framework and example application to show interaction between events an NBI,!339Create basic venv-manager for use with arista,!324Provide prototype implementation for topology handling
Pipeline #107154 failed
...@@ -77,7 +77,7 @@ func initialize() error { ...@@ -77,7 +77,7 @@ func initialize() error {
return err return err
} }
nodeService := nodes.NewNodeService(nodes.NewDatabaseNodeStore()) nodeService := nodes.NewNodeService(nodes.NewDatabaseNodeStore(), eventService)
portService := ports.NewPortService(ports.NewDatabasePortStore()) portService := ports.NewPortService(ports.NewDatabasePortStore())
c = &Core{ c = &Core{
......
package nodes package nodes
import ( import (
"code.fbi.h-da.de/danet/gosdn/controller/event"
eventInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/event"
"code.fbi.h-da.de/danet/gosdn/controller/topology/store" "code.fbi.h-da.de/danet/gosdn/controller/topology/store"
"github.com/google/uuid" "github.com/google/uuid"
) )
const (
// NodeEventTopic is the used topic for node related entity changes
NodeEventTopic = "node"
)
// Service defines a interface for a NodeService // Service defines a interface for a NodeService
type Service interface { type Service interface {
EnsureExists(Node) (Node, error) EnsureExists(Node) (Node, error)
...@@ -16,29 +23,33 @@ type Service interface { ...@@ -16,29 +23,33 @@ type Service interface {
// NodeService is a NodeService // NodeService is a NodeService
type NodeService struct { type NodeService struct {
store Store store Store
eventService eventInterfaces.Service
} }
// NewNodeService creates a NodeService // NewNodeService creates a NodeService
func NewNodeService(store Store) Service { func NewNodeService(store Store, eventService eventInterfaces.Service) Service {
return &NodeService{ return &NodeService{
store: store, store: store,
eventService: eventService,
} }
} }
// EnsureExists either creates a new node or returns an already existing node // EnsureExists either creates a new node or returns an already existing node
func (p *NodeService) EnsureExists(node Node) (Node, error) { func (n *NodeService) EnsureExists(node Node) (Node, error) {
if node.ID == uuid.Nil { if node.ID == uuid.Nil {
node.ID = uuid.New() node.ID = uuid.New()
err := p.store.Add(node) err := n.store.Add(node)
if err != nil { if err != nil {
return node, err return node, err
} }
n.eventService.PublishEvent(NodeEventTopic, event.NewAddEvent(node.ID))
return node, nil return node, nil
} }
node, err := p.Get(store.Query{ID: node.ID}) node, err := n.Get(store.Query{ID: node.ID})
if err != nil { if err != nil {
return node, err return node, err
} }
...@@ -47,28 +58,32 @@ func (p *NodeService) EnsureExists(node Node) (Node, error) { ...@@ -47,28 +58,32 @@ func (p *NodeService) EnsureExists(node Node) (Node, error) {
} }
// Update updates an existing node // Update updates an existing node
func (p *NodeService) Update(node Node) error { func (n *NodeService) Update(node Node) error {
err := p.store.Update(node) err := n.store.Update(node)
if err != nil { if err != nil {
return err return err
} }
n.eventService.PublishEvent(NodeEventTopic, event.NewUpdateEvent(node.ID))
return nil return nil
} }
// Delete deletes a node // Delete deletes a node
func (p *NodeService) Delete(node Node) error { func (n *NodeService) Delete(node Node) error {
err := p.store.Delete(node) err := n.store.Delete(node)
if err != nil { if err != nil {
return err return err
} }
n.eventService.PublishEvent(NodeEventTopic, event.NewDeleteEvent(node.ID))
return nil return nil
} }
// Get gets a node // Get gets a node
func (p *NodeService) Get(query store.Query) (Node, error) { func (n *NodeService) Get(query store.Query) (Node, error) {
node, err := p.store.Get(query) node, err := n.store.Get(query)
if err != nil { if err != nil {
return node, err return node, err
} }
...@@ -77,8 +92,8 @@ func (p *NodeService) Get(query store.Query) (Node, error) { ...@@ -77,8 +92,8 @@ func (p *NodeService) Get(query store.Query) (Node, error) {
} }
// GetAll gets all existing nodes // GetAll gets all existing nodes
func (p *NodeService) GetAll() ([]Node, error) { func (n *NodeService) GetAll() ([]Node, error) {
nodes, err := p.store.GetAll() nodes, err := n.store.GetAll()
if err != nil { if err != nil {
return nodes, err return nodes, err
} }
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"reflect" "reflect"
"testing" "testing"
eventservice "code.fbi.h-da.de/danet/gosdn/controller/eventService"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/event"
"code.fbi.h-da.de/danet/gosdn/controller/topology/store" "code.fbi.h-da.de/danet/gosdn/controller/topology/store"
"github.com/google/uuid" "github.com/google/uuid"
) )
...@@ -37,7 +39,8 @@ func getTestStoreWithNodes(t *testing.T, nodes []Node) Store { ...@@ -37,7 +39,8 @@ func getTestStoreWithNodes(t *testing.T, nodes []Node) Store {
func TestNewNodeService(t *testing.T) { func TestNewNodeService(t *testing.T) {
type args struct { type args struct {
store Store store Store
eventService event.Service
} }
tests := []struct { tests := []struct {
name string name string
...@@ -47,14 +50,15 @@ func TestNewNodeService(t *testing.T) { ...@@ -47,14 +50,15 @@ func TestNewNodeService(t *testing.T) {
{ {
name: "should create a new node service", name: "should create a new node service",
args: args{ args: args{
store: getTestStoreWithNodes(t, []Node{}), store: getTestStoreWithNodes(t, []Node{}),
eventService: eventservice.NewMockEventService(),
}, },
want: NewNodeService(getTestStoreWithNodes(t, []Node{})), want: NewNodeService(getTestStoreWithNodes(t, []Node{}), eventservice.NewMockEventService()),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := NewNodeService(tt.args.store); !reflect.DeepEqual(got, tt.want) { if got := NewNodeService(tt.args.store, tt.args.eventService); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNodeService() = %v, want %v", got, tt.want) t.Errorf("NewNodeService() = %v, want %v", got, tt.want)
} }
}) })
...@@ -63,7 +67,8 @@ func TestNewNodeService(t *testing.T) { ...@@ -63,7 +67,8 @@ func TestNewNodeService(t *testing.T) {
func TestNodeService_EnsureExists(t *testing.T) { func TestNodeService_EnsureExists(t *testing.T) {
type fields struct { type fields struct {
store Store store Store
eventService event.Service
} }
type args struct { type args struct {
node Node node Node
...@@ -94,7 +99,8 @@ func TestNodeService_EnsureExists(t *testing.T) { ...@@ -94,7 +99,8 @@ func TestNodeService_EnsureExists(t *testing.T) {
{ {
name: "should error if node with uuid is not in store", name: "should error if node with uuid is not in store",
fields: fields{ fields: fields{
store: store.NewGenericStore[Node](), store: store.NewGenericStore[Node](),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
node: getTestNode(), node: getTestNode(),
...@@ -105,7 +111,8 @@ func TestNodeService_EnsureExists(t *testing.T) { ...@@ -105,7 +111,8 @@ func TestNodeService_EnsureExists(t *testing.T) {
{ {
name: "should return node that is in the store", name: "should return node that is in the store",
fields: fields{ fields: fields{
store: getTestStoreWithNodes(t, []Node{getTestNode()}), store: getTestStoreWithNodes(t, []Node{getTestNode()}),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
node: getTestNode(), node: getTestNode(),
...@@ -117,7 +124,8 @@ func TestNodeService_EnsureExists(t *testing.T) { ...@@ -117,7 +124,8 @@ func TestNodeService_EnsureExists(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &NodeService{ p := &NodeService{
store: tt.fields.store, store: tt.fields.store,
eventService: tt.fields.eventService,
} }
got, err := p.EnsureExists(tt.args.node) got, err := p.EnsureExists(tt.args.node)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
...@@ -133,7 +141,8 @@ func TestNodeService_EnsureExists(t *testing.T) { ...@@ -133,7 +141,8 @@ func TestNodeService_EnsureExists(t *testing.T) {
func TestNodeService_Update(t *testing.T) { func TestNodeService_Update(t *testing.T) {
type fields struct { type fields struct {
store Store store Store
eventService event.Service
} }
type args struct { type args struct {
node Node node Node
...@@ -148,7 +157,8 @@ func TestNodeService_Update(t *testing.T) { ...@@ -148,7 +157,8 @@ func TestNodeService_Update(t *testing.T) {
{ {
name: "should update an existing node", name: "should update an existing node",
fields: fields{ fields: fields{
store: getTestStoreWithNodes(t, []Node{getTestNode()}), store: getTestStoreWithNodes(t, []Node{getTestNode()}),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
node: getTestNode(), node: getTestNode(),
...@@ -160,7 +170,8 @@ func TestNodeService_Update(t *testing.T) { ...@@ -160,7 +170,8 @@ func TestNodeService_Update(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &NodeService{ p := &NodeService{
store: tt.fields.store, store: tt.fields.store,
eventService: tt.fields.eventService,
} }
if err := p.Update(tt.args.node); (err != nil) != tt.wantErr { if err := p.Update(tt.args.node); (err != nil) != tt.wantErr {
t.Errorf("NodeService.Update() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("NodeService.Update() error = %v, wantErr %v", err, tt.wantErr)
...@@ -180,7 +191,8 @@ func TestNodeService_Update(t *testing.T) { ...@@ -180,7 +191,8 @@ func TestNodeService_Update(t *testing.T) {
func TestNodeService_Delete(t *testing.T) { func TestNodeService_Delete(t *testing.T) {
type fields struct { type fields struct {
store Store store Store
eventService event.Service
} }
type args struct { type args struct {
node Node node Node
...@@ -194,7 +206,8 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -194,7 +206,8 @@ func TestNodeService_Delete(t *testing.T) {
{ {
name: "should delete an existing node", name: "should delete an existing node",
fields: fields{ fields: fields{
store: getTestStoreWithNodes(t, []Node{getTestNode()}), store: getTestStoreWithNodes(t, []Node{getTestNode()}),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
node: getTestNode(), node: getTestNode(),
...@@ -204,7 +217,8 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -204,7 +217,8 @@ func TestNodeService_Delete(t *testing.T) {
{ {
name: "should fail if a node does not exists", name: "should fail if a node does not exists",
fields: fields{ fields: fields{
store: store.NewGenericStore[Node](), store: store.NewGenericStore[Node](),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
node: getTestNode(), node: getTestNode(),
...@@ -215,7 +229,8 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -215,7 +229,8 @@ func TestNodeService_Delete(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &NodeService{ p := &NodeService{
store: tt.fields.store, store: tt.fields.store,
eventService: tt.fields.eventService,
} }
if err := p.Delete(tt.args.node); (err != nil) != tt.wantErr { if err := p.Delete(tt.args.node); (err != nil) != tt.wantErr {
t.Errorf("NodeService.Delete() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("NodeService.Delete() error = %v, wantErr %v", err, tt.wantErr)
...@@ -226,7 +241,8 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -226,7 +241,8 @@ func TestNodeService_Delete(t *testing.T) {
func TestNodeService_Get(t *testing.T) { func TestNodeService_Get(t *testing.T) {
type fields struct { type fields struct {
store Store store Store
eventService event.Service
} }
type args struct { type args struct {
query store.Query query store.Query
...@@ -241,7 +257,8 @@ func TestNodeService_Get(t *testing.T) { ...@@ -241,7 +257,8 @@ func TestNodeService_Get(t *testing.T) {
{ {
name: "should error if node with uuid is not in store", name: "should error if node with uuid is not in store",
fields: fields{ fields: fields{
store: store.NewGenericStore[Node](), store: store.NewGenericStore[Node](),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
query: store.Query{ query: store.Query{
...@@ -255,7 +272,8 @@ func TestNodeService_Get(t *testing.T) { ...@@ -255,7 +272,8 @@ func TestNodeService_Get(t *testing.T) {
{ {
name: "should return node that is in the store", name: "should return node that is in the store",
fields: fields{ fields: fields{
store: getTestStoreWithNodes(t, []Node{getTestNode()}), store: getTestStoreWithNodes(t, []Node{getTestNode()}),
eventService: eventservice.NewMockEventService(),
}, },
args: args{ args: args{
query: store.Query{ query: store.Query{
...@@ -270,7 +288,8 @@ func TestNodeService_Get(t *testing.T) { ...@@ -270,7 +288,8 @@ func TestNodeService_Get(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &NodeService{ p := &NodeService{
store: tt.fields.store, store: tt.fields.store,
eventService: tt.fields.eventService,
} }
got, err := p.Get(tt.args.query) got, err := p.Get(tt.args.query)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
...@@ -286,7 +305,8 @@ func TestNodeService_Get(t *testing.T) { ...@@ -286,7 +305,8 @@ func TestNodeService_Get(t *testing.T) {
func TestNodeService_GetAll(t *testing.T) { func TestNodeService_GetAll(t *testing.T) {
type fields struct { type fields struct {
store Store store Store
eventService event.Service
} }
tests := []struct { tests := []struct {
name string name string
...@@ -297,7 +317,8 @@ func TestNodeService_GetAll(t *testing.T) { ...@@ -297,7 +317,8 @@ func TestNodeService_GetAll(t *testing.T) {
{ {
name: "should get all stored nodes", name: "should get all stored nodes",
fields: fields{ fields: fields{
store: getTestStoreWithNodes(t, []Node{getTestNode()}), store: getTestStoreWithNodes(t, []Node{getTestNode()}),
eventService: eventservice.NewMockEventService(),
}, },
want: []Node{getTestNode()}, want: []Node{getTestNode()},
wantErr: false, wantErr: false,
...@@ -306,7 +327,8 @@ func TestNodeService_GetAll(t *testing.T) { ...@@ -306,7 +327,8 @@ func TestNodeService_GetAll(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &NodeService{ p := &NodeService{
store: tt.fields.store, store: tt.fields.store,
eventService: tt.fields.eventService,
} }
got, err := p.GetAll() got, err := p.GetAll()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
......
...@@ -4,10 +4,12 @@ import ( ...@@ -4,10 +4,12 @@ import (
"reflect" "reflect"
"testing" "testing"
eventservice "code.fbi.h-da.de/danet/gosdn/controller/eventService"
"code.fbi.h-da.de/danet/gosdn/controller/topology/links" "code.fbi.h-da.de/danet/gosdn/controller/topology/links"
"code.fbi.h-da.de/danet/gosdn/controller/topology/nodes" "code.fbi.h-da.de/danet/gosdn/controller/topology/nodes"
"code.fbi.h-da.de/danet/gosdn/controller/topology/ports" "code.fbi.h-da.de/danet/gosdn/controller/topology/ports"
"code.fbi.h-da.de/danet/gosdn/controller/topology/store" "code.fbi.h-da.de/danet/gosdn/controller/topology/store"
"github.com/google/uuid" "github.com/google/uuid"
) )
...@@ -108,12 +110,12 @@ func TestNewTopologyService(t *testing.T) { ...@@ -108,12 +110,12 @@ func TestNewTopologyService(t *testing.T) {
name: "should create a new topology service", name: "should create a new topology service",
args: args{ args: args{
store: getTestStoreWithLinks(t, []links.Link{}), store: getTestStoreWithLinks(t, []links.Link{}),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
want: NewTopologyService( want: NewTopologyService(
getTestStoreWithLinks(t, []links.Link{}), getTestStoreWithLinks(t, []links.Link{}),
nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
), ),
}, },
...@@ -150,7 +152,7 @@ func TestTopologyService_AddLink(t *testing.T) { ...@@ -150,7 +152,7 @@ func TestTopologyService_AddLink(t *testing.T) {
nodeService: nodes.NewNodeService(getTestStoreWithNodes( nodeService: nodes.NewNodeService(getTestStoreWithNodes(
t, t,
[]nodes.Node{getTestSourceNode(), getTestTargetNode()}, []nodes.Node{getTestSourceNode(), getTestTargetNode()},
)), ), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts( portService: ports.NewPortService(getTestStoreWithPorts(
t, t,
[]ports.Port{getTestSourcePort(), getTestTargetPort()}, []ports.Port{getTestSourcePort(), getTestTargetPort()},
...@@ -208,7 +210,7 @@ func TestTopologyService_Update(t *testing.T) { ...@@ -208,7 +210,7 @@ func TestTopologyService_Update(t *testing.T) {
name: "should update an existing link", name: "should update an existing link",
fields: fields{ fields: fields{
store: getTestStoreWithLinks(t, []links.Link{getTestLink()}), store: getTestStoreWithLinks(t, []links.Link{getTestLink()}),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
args: args{ args: args{
...@@ -263,7 +265,7 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -263,7 +265,7 @@ func TestNodeService_Delete(t *testing.T) {
name: "should delete an existing link", name: "should delete an existing link",
fields: fields{ fields: fields{
store: getTestStoreWithLinks(t, []links.Link{getTestLink()}), store: getTestStoreWithLinks(t, []links.Link{getTestLink()}),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
args: args{ args: args{
...@@ -275,7 +277,7 @@ func TestNodeService_Delete(t *testing.T) { ...@@ -275,7 +277,7 @@ func TestNodeService_Delete(t *testing.T) {
name: "should fail if a node does not exists", name: "should fail if a node does not exists",
fields: fields{ fields: fields{
store: store.NewGenericStore[links.Link](), store: store.NewGenericStore[links.Link](),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
args: args{ args: args{
...@@ -318,7 +320,7 @@ func TestTopologyService_Get(t *testing.T) { ...@@ -318,7 +320,7 @@ func TestTopologyService_Get(t *testing.T) {
name: "should error if link with uuid is not in store", name: "should error if link with uuid is not in store",
fields: fields{ fields: fields{
store: getTestStoreWithLinks(t, []links.Link{}), store: getTestStoreWithLinks(t, []links.Link{}),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
args: args{ args: args{
...@@ -380,7 +382,7 @@ func TestTopologyService_GetAll(t *testing.T) { ...@@ -380,7 +382,7 @@ func TestTopologyService_GetAll(t *testing.T) {
name: "should get all stored links", name: "should get all stored links",
fields: fields{ fields: fields{
store: getTestStoreWithLinks(t, []links.Link{getTestLink()}), store: getTestStoreWithLinks(t, []links.Link{getTestLink()}),
nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{})), nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()),
portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})), portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{})),
}, },
want: []links.Link{getTestLink()}, want: []links.Link{getTestLink()},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment