diff --git a/controller/topology/routing-tables/routingTableService.go b/controller/topology/routing-tables/routingTableService.go index b5823217301eb5d92697c0154940611e2423950a..c190dc7ca2e6f191977b7d48fbcf1573ef25234f 100644 --- a/controller/topology/routing-tables/routingTableService.go +++ b/controller/topology/routing-tables/routingTableService.go @@ -1,10 +1,18 @@ package routingtables import ( + "code.fbi.h-da.de/danet/gosdn/controller/event" + eventInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/event" query "code.fbi.h-da.de/danet/gosdn/controller/store" "code.fbi.h-da.de/danet/gosdn/controller/topology/nodes" "code.fbi.h-da.de/danet/gosdn/controller/topology/ports" "github.com/google/uuid" + log "github.com/sirupsen/logrus" +) + +const ( + // RoutingTableEventTopic is the used topic for routing table related entity changes + RoutingTableEventTopic = "routingTable" ) // Service defines a interface for a RoutingTableService @@ -18,9 +26,10 @@ type Service interface { // RoutingTableService is a RoutingTableService type RoutingTableService struct { - store Store - nodeService nodes.Service - portService ports.Service + store Store + nodeService nodes.Service + portService ports.Service + eventService eventInterfaces.Service } // NewRoutingTableService creates a RoutingTableService @@ -28,11 +37,13 @@ func NewRoutingTableService( store Store, nodeService nodes.Service, portService ports.Service, + eventService eventInterfaces.Service, ) Service { return &RoutingTableService{ - store: store, - nodeService: nodeService, - portService: portService, + store: store, + nodeService: nodeService, + portService: portService, + eventService: eventService, } } @@ -57,7 +68,12 @@ func (r *RoutingTableService) createRoutingTable(routingTable RoutingTable) (Rou return routingTable, err } - return routingTable, err + if err := r.eventService.PublishEvent(RoutingTableEventTopic, event.NewAddEvent(routingTable.ID)); err != nil { + //TODO: improve error handling, maybe to sth like reconnect + log.Error(err) + } + + return routingTable, nil } // Update updates an existing routingTable @@ -67,6 +83,11 @@ func (r *RoutingTableService) Update(routingTable RoutingTable) error { return err } + if err := r.eventService.PublishEvent(RoutingTableEventTopic, event.NewUpdateEvent(routingTable.ID)); err != nil { + //TODO: improve error handling, maybe to sth like reconnect + log.Error(err) + } + return nil } @@ -77,6 +98,11 @@ func (r *RoutingTableService) Delete(routingTable RoutingTable) error { return err } + if err := r.eventService.PublishEvent(RoutingTableEventTopic, event.NewDeleteEvent(routingTable.ID)); err != nil { + //TODO: improve error handling, maybe to sth like reconnect + log.Error(err) + } + return nil } diff --git a/controller/topology/routing-tables/routingTableService_test.go b/controller/topology/routing-tables/routingTableService_test.go index e2fc764524c012a12496c727f43c1c5415014cf9..2330208c6549de43a501683541805a650ab2c446 100644 --- a/controller/topology/routing-tables/routingTableService_test.go +++ b/controller/topology/routing-tables/routingTableService_test.go @@ -5,6 +5,7 @@ import ( "testing" eventservice "code.fbi.h-da.de/danet/gosdn/controller/eventService" + eventInterfaces "code.fbi.h-da.de/danet/gosdn/controller/interfaces/event" query "code.fbi.h-da.de/danet/gosdn/controller/store" "code.fbi.h-da.de/danet/gosdn/controller/topology/nodes" "code.fbi.h-da.de/danet/gosdn/controller/topology/ports" @@ -85,9 +86,10 @@ func getTestStoreWithPorts(t *testing.T, portsToAdd []ports.Port) ports.Store { func TestNewRoutingTableService(t *testing.T) { type args struct { - store Store - nodeService nodes.Service - portService ports.Service + store Store + nodeService nodes.Service + portService ports.Service + eventService eventInterfaces.Service } tests := []struct { name string @@ -97,20 +99,22 @@ func TestNewRoutingTableService(t *testing.T) { { name: "should create a new topology service", args: args{ - store: getTestStoreWithRoutingTables(t, []RoutingTable{}), - nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()), - portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{}), eventservice.NewMockEventService()), + store: getTestStoreWithRoutingTables(t, []RoutingTable{}), + nodeService: nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()), + portService: ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{}), eventservice.NewMockEventService()), + eventService: eventservice.NewMockEventService(), }, want: NewRoutingTableService( getTestStoreWithRoutingTables(t, []RoutingTable{}), nodes.NewNodeService(getTestStoreWithNodes(t, []nodes.Node{}), eventservice.NewMockEventService()), ports.NewPortService(getTestStoreWithPorts(t, []ports.Port{}), eventservice.NewMockEventService()), + eventservice.NewMockEventService(), ), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewRoutingTableService(tt.args.store, tt.args.nodeService, tt.args.portService); !reflect.DeepEqual(got, tt.want) { + if got := NewRoutingTableService(tt.args.store, tt.args.nodeService, tt.args.portService, tt.args.eventService); !reflect.DeepEqual(got, tt.want) { t.Errorf("NewNodeService() = %v, want %v", got, tt.want) } })