Newer
Older
Fabian Seidl
committed
package integration_test_application
import (
"context"
"os"
"os/signal"
Fabian Seidl
committed
"syscall"
"code.fbi.h-da.de/danet/gosdn/application-framework/event"
"code.fbi.h-da.de/danet/gosdn/application-framework/registration"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
Fabian Seidl
committed
// Application is an example for a sdn application.
type Application struct {
eventService event.ServiceInterface
stopChannel chan os.Signal
grpcClientConn *grpc.ClientConn
eventChannel chan event.Event
}
func NewApplication(ctx context.Context, grpcClientConn *grpc.ClientConn, controllerAddress string, topics []event.Topic, rabbitMQAddress string) *Application {
Fabian Seidl
committed
queueCredentials, err := registration.Register(ctx, controllerAddress, "integration-test-application", "SecurePresharedToken")
if err != nil {
logrus.Errorf("failed to register application on control plane. %v", err)
os.Exit(1)
}
if rabbitMQAddress != "" {
queueCredentials = strings.ReplaceAll(queueCredentials, localhost, rabbitMQAddress)
}
Fabian Seidl
committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
eventService, err := event.NewEventService(
queueCredentials,
topics,
)
if err != nil {
logrus.Errorf("failed to create event service. %v", err)
os.Exit(1)
}
return &Application{
eventService: eventService,
stopChannel: make(chan os.Signal, 1),
grpcClientConn: grpcClientConn,
eventChannel: make(chan event.Event, 1),
}
}
// Run runs the application.
func (a *Application) Run(eventTypeCallbackTuples []event.TypeToCallbackTuple) {
signal.Notify(a.stopChannel, os.Interrupt, syscall.SIGTERM)
a.eventService.SubscribeToEventType(eventTypeCallbackTuples)
a.eventService.SetupEventReciever(a.stopChannel)
var forever chan struct{}
go func() {
for {
select {
case <-a.stopChannel:
close(forever)
_ = a.grpcClientConn.Close()
return
}
}
}()
<-forever
}
func (a *Application) callback(event *event.Event) {
logrus.Infof("Incoming Event: EntityID: %v, ID: %v, PathsAndValues: %v, Type: %v", event.EntityID, event.ID, event.PathsAndValuesMap, event.Type)
Fabian Seidl
committed
a.eventChannel <- *event
}