Skip to content
Snippets Groups Projects
app.go 1.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • André Sterba's avatar
    André Sterba committed
    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"os/signal"
    	"syscall"
    	"time"
    
    	"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
    
    	"code.fbi.h-da.de/danet/gosdn/application-framework/event"
    	"google.golang.org/grpc"
    )
    
    // Application is an example for a sdn application
    type Application struct {
    	eventService   event.ServiceInterface
    	stopChannel    chan os.Signal
    	grpcClientConn *grpc.ClientConn
    }
    
    // Run runs the application
    func (a *Application) Run() {
    	signal.Notify(a.stopChannel, os.Interrupt, syscall.SIGTERM)
    
    	a.eventService.SubscribeToTopic("add", a.callback)
    	a.eventService.SetupEventReciever(a.stopChannel)
    
    	conn, err := grpc.Dial("localhost:55055", grpc.WithInsecure())
    	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
    			}
    		}
    	}()
    
    	<-forever
    }
    
    func (a *Application) callback(event *event.Event) {
    
    	fmt.Printf("Event Callback: %+v \n", event)
    
    André Sterba's avatar
    André Sterba committed
    
    	ctx := context.Background()
    	userService := rbac.NewUserServiceClient(a.grpcClientConn)
    
    	request := &rbac.GetUserRequest{
    		Timestamp: time.Now().UnixNano(),
    		Name:      event.ID.String(),
    
    André Sterba's avatar
    André Sterba committed
    		Id:        event.EntityID.String(),
    
    André Sterba's avatar
    André Sterba committed
    	}
    
    	response, err := userService.GetUser(ctx, request)
    	if err != nil {
    		fmt.Printf("Error %+v\n ", err)
    		return
    	}
    
    	fmt.Printf("ID: %v, Name: %v \n", response.User.Id, response.User.Name)
    
    	for key, elem := range response.User.Roles {
    		fmt.Printf("Role on PND: %v %v \n", key, elem)
    	}