Skip to content
Snippets Groups Projects
databasePndStore.go 6.07 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    	"context"
    
    	"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"
    
    Andre Sterba's avatar
    Andre Sterba 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"
    
    	"go.mongodb.org/mongo-driver/mongo"
    
    const pndStoreName = "pnd-store.json"
    
    
    // DatabasePndStore is used to store PrincipalNetworkDomains.
    
    type DatabasePndStore struct {
    
    	pendingChannels map[uuid.UUID]chan networkelement.Details
    
    	collection      *mongo.Collection
    
    	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 {
    
    		loadedPND, err := s.getByID(ctx, query.ID)
    
    		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()}})
    
    	if result == nil {
    
    		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}
    
    Andre Sterba's avatar
    Andre Sterba committed
    
    
    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)
    
    	if err != nil {
    
    		log.Printf("Failed marshalling %v", err)
    		return loadedPnd, customerrs.CouldNotMarshallError{Identifier: nameOfPnd, Type: loadedPnd, Err: err}
    
    }
    
    // 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)
    
    
    		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)
    
    	if err != nil {
    
    		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()}})
    
    	if err != nil {
    
    		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
    
    func (s *DatabasePndStore) PendingChannels(id uuid.UUID, parseErrors ...error) (chan networkelement.Details, error) {
    
    	ch, ok := s.pendingChannels[id]
    	if !ok {
    
    // 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
    // }