Newer
Older
"fmt"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkelement"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
Fabian Seidl
committed
"code.fbi.h-da.de/danet/gosdn/controller/customerrs"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
// DatabasePndStore is used to store PrincipalNetworkDomains.
pendingChannels map[uuid.UUID]chan networkelement.Details
pluginService plugin.Service
func NewDatabasePndStore(db *mongo.Database, pluginService plugin.Service) *DatabasePndStore {
collection := db.Collection(pndStoreName)
return &DatabasePndStore{
pendingChannels: make(map[uuid.UUID]chan networkelement.Details),
pluginService: pluginService,
collection: collection,
}
}
// Get takes a PrincipalNetworkDomain's UUID or name and returns the PrincipalNetworkDomain. If the requested
// PrincipalNetworkDomain does not exist an error is returned.
func (s *DatabasePndStore) Get(ctx context.Context, query store.Query) (newPnd networkdomain.LoadedPnd, err error) {
var loadedPND networkdomain.LoadedPnd
if query.ID != uuid.Nil {
if err != nil {
return loadedPND, err
}
return loadedPND, nil
}
loadedPND, err = s.getByName(ctx, query.Name)
if err != nil {
return loadedPND, err
}
// Note: add this if using cSBI again
// csbiClient, err := s.getCsbiClient()
// if err != nil {
// return loadedPND, err
// }
// newPnd, err := NewPND(
// loadedPND.Name,
// loadedPND.Description,
// uuid.MustParse(loadedPND.ID),
// )
return loadedPND, nil
}
func (s *DatabasePndStore) getByID(ctx context.Context, idOfPnd uuid.UUID) (loadedPnd networkdomain.LoadedPnd, err error) {
result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: idOfPnd.String()}})
return loadedPnd, customerrs.CouldNotFindError{ID: idOfPnd}
err = result.Decode(&loadedPnd)
if err != nil {
log.Printf("Failed marshalling %v", err)
return loadedPnd, customerrs.CouldNotMarshallError{Identifier: idOfPnd, Type: loadedPnd, Err: err}
return loadedPnd, nil
}
func (s *DatabasePndStore) getByName(ctx context.Context, nameOfPnd string) (loadedPnd networkdomain.LoadedPnd, err error) {
result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "name", Value: nameOfPnd}})
if result == nil {
return loadedPnd, customerrs.CouldNotFindError{ID: nameOfPnd}
}
err = result.Decode(&loadedPnd)
log.Printf("Failed marshalling %v", err)
return loadedPnd, customerrs.CouldNotMarshallError{Identifier: nameOfPnd, Type: loadedPnd, Err: err}
return loadedPnd, nil
}
// GetAll returns all stored pnds.
func (s *DatabasePndStore) GetAll(ctx context.Context) (pnds []networkdomain.LoadedPnd, err error) {
var loadedPnds []networkdomain.LoadedPnd
cursor, err := s.collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer func() {
if ferr := cursor.Close(ctx); ferr != nil {
fErrString := ferr.Error()
err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
}
}()
err = cursor.All(ctx, &loadedPnds)
if err != nil {
log.Printf("Failed marshalling %v", err)
Fabian Seidl
committed
return nil, customerrs.CouldNotMarshallError{Type: loadedPnds, Err: err}
// Note: add this if using cSBI again
// csbiClient, err := s.getCsbiClient()
// if err != nil {
// return nil, err
// }
// for _, loadedPND := range loadedPnds {
// newPnd, err := NewPND(
// loadedPND.Name,
// loadedPND.Description,
// uuid.MustParse(loadedPND.ID),
// csbiClient,
// s.callback,
// )
// if err != nil {
// return nil, err
// }
// pnds = append(pnds, newPnd)
// }
return loadedPnds, nil
}
// Add adds a pnd to the pnd store.
func (s *DatabasePndStore) Add(ctx context.Context, pndToAdd networkdomain.NetworkDomain) (err error) {
_, err = s.collection.InsertOne(ctx, pndToAdd)
Fabian Seidl
committed
return customerrs.CouldNotCreateError{Identifier: pndToAdd.ID(), Type: pndToAdd, Err: err}
}
return nil
}
// Delete deletes a pnd.
// It also deletes all assosicated devices and sbis.
func (s *DatabasePndStore) Delete(ctx context.Context, pndToDelete networkdomain.NetworkDomain) (err error) {
_, err = s.collection.DeleteOne(ctx, bson.D{primitive.E{Key: "_id", Value: pndToDelete.ID().String()}})
Fabian Seidl
committed
return customerrs.CouldNotDeleteError{Identifier: pndToDelete.ID(), Type: pndToDelete, Err: err}
}
// TODO: Delete all assosicated devices + SBIs
return nil
}
// PendingChannels holds channels used communicate with pending
// cSBI deployments.
func (s *DatabasePndStore) PendingChannels(id uuid.UUID, parseErrors ...error) (chan networkelement.Details, error) {
ch, ok := s.pendingChannels[id]
if !ok {
Fabian Seidl
committed
return nil, &customerrs.CouldNotFindError{ID: id}
// AddPendingChannel adds a pending channel to the map.
func (s *DatabasePndStore) AddPendingChannel(id uuid.UUID, ch chan networkelement.Details) {
s.pendingChannels[id] = ch
}
// RemovePendingChannel removes a pending channel from the map.
func (s *DatabasePndStore) RemovePendingChannel(id uuid.UUID) {
delete(s.pendingChannels, id)
}
// func (s *DatabasePndStore) callback(id uuid.UUID, ch chan networkelement.Details) {
// if ch != nil {
// s.AddPendingChannel(id, ch)
// log.Infof("pending channel %v added", id)
// } else {
// s.RemovePendingChannel(id)
// log.Infof("pending channel %v removed", id)
// }
// }
// func (s *DatabasePndStore) getCsbiClient() (cpb.CsbiServiceClient, error) {
// if s.csbiClient == nil {
// orchestrator := viper.GetString("csbi-orchestrator")
// conn, err := grpc.Dial(orchestrator, grpc.WithTransportCredentials(insecure.NewCredentials()))
// if err != nil {
// return nil, err
// }
// s.csbiClient = cpb.NewCsbiServiceClient(conn)
// }
// return s.csbiClient, nil
// }