Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/device"
"code.fbi.h-da.de/danet/gosdn/application-framework/event"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// Application is an example for a sdn application.
type Application struct {
eventServiceDevices event.ServiceInterface
eventServiceRoutes event.ServiceInterface
stopChannel chan os.Signal
grpcClientConn *grpc.ClientConn
}
// Run runs the application.
func (a *Application) Run(controllerAddress string) {
statusMap = make(map[string]map[string]*InterfaceStatus)
signal.Notify(a.stopChannel, os.Interrupt, syscall.SIGTERM)
a.eventServiceDevices.SubscribeToEventType([]event.TypeToCallbackTuple{
{Type: event.Update, Callback: a.DeviceCallback},
})
a.eventServiceDevices.SetupEventReciever(a.stopChannel)
conn, err := grpc.Dial(controllerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
a.grpcClientConn = conn
var forever chan struct{}
go func() {
for {
select {
case <-a.stopChannel:
close(forever)
_ = a.grpcClientConn.Close()
return
}
}
}()
if err := StartHTTPServer(); err != nil {
panic(err)
}
<-forever
}
func (a *Application) DeviceCallback(event *event.Event) {
deviceServer := device.NewDeviceServiceClient(a.grpcClientConn)
changedInterfaces, err := checkIfOperationStateHasChanged(deviceServer, event.EntityID)
if err != nil {
fmt.Printf("Error %+v\n ", err)
}
if changedInterfaces != nil {
for _, changedInterface := range changedInterfaces {
fmt.Printf("Change on %s: status of interface %s has changed to %s", changedInterface.NetworkElementName, changedInterface.Name, changedInterface.Status)