Skip to content
Snippets Groups Projects

Add basic application framework and example application to show interaction between events an NBI

Merged Ghost User requested to merge istaester/init-application-framework into develop
4 files
+ 326
0
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 75
0
package app
import (
"fmt"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
)
// IService is the app service.
type IService interface {
Register(string, string) (*App, error)
Deregister(string) error
}
// Service manages apps and their access to the event system.
type Service struct {
store IStore
}
// NewAppService creates a new app service.
func NewAppService(store IStore) IService {
return &Service{
store: store,
}
}
// Register checks if the app already exists and if not creates a new one.
func (a *Service) Register(appName, token string) (*App, error) {
if token != "SecurePresharedToken" {
return nil, fmt.Errorf("token not valid")
}
exisitingApp, err := a.store.Get(store.Query{ID: uuid.Nil, Name: appName})
if err != nil {
if exisitingApp.ID == uuid.Nil {
return a.createNewApp(appName)
}
return nil, err
}
return &exisitingApp, nil
}
// Deregister deregisters an app.
func (a *Service) Deregister(appName string) error {
app, err := a.store.Get(store.Query{Name: appName})
if err != nil {
return err
}
err = a.store.Delete(app)
if err != nil {
return err
}
return nil
}
func (a *Service) createNewApp(appName string) (*App, error) {
app := App{
ID: uuid.New(),
Name: appName,
EventSystemCredentials: "amqp://guest:guest@127.0.0.1:5672",
}
// generate app credentials
err := a.store.Add(app)
if err != nil {
return nil, err
}
return &app, nil
}
Loading