Skip to content
Snippets Groups Projects
databaseSbiStore.go 4.15 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    	spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
    	"code.fbi.h-da.de/danet/gosdn/controller/store"
    	"go.mongodb.org/mongo-driver/bson"
    	"go.mongodb.org/mongo-driver/bson/primitive"
    	"go.mongodb.org/mongo-driver/mongo"
    
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    )
    
    // DatabaseSbiStore is used to store SouthboundInterfaces
    type DatabaseSbiStore struct {
    	sbiStoreName string
    }
    
    // Add adds a SBI.
    func (s *DatabaseSbiStore) Add(item southbound.SouthboundInterface) error {
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	_, err := client.Database(database.DatabaseName).
    		Collection(s.sbiStoreName).
    		InsertOne(ctx, item)
    	if err != nil {
    		if mongo.IsDuplicateKeyError(err) {
    			return nil
    		}
    
    		return errors.ErrCouldNotCreate{StoreName: sbiStoreName}
    	}
    
    	return nil
    }
    
    // Delete deletes an SBI.
    func (s *DatabaseSbiStore) Delete(item southbound.SouthboundInterface) error {
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	_, err := client.Database(database.DatabaseName).
    		Collection(s.sbiStoreName).
    		DeleteOne(ctx, bson.D{primitive.E{Key: "_id", Value: item.ID().String()}})
    	if err != nil {
    		return errors.ErrCouldNotCreate{StoreName: sbiStoreName}
    	}
    
    	return nil
    }
    
    // Get takes a SouthboundInterface's UUID or name and returns the SouthboundInterface. If the requested
    // SouthboundInterface does not exist an error is returned.
    func (s *DatabaseSbiStore) Get(query store.Query) (southbound.SouthboundInterface, error) {
    	var loadedSbi *LoadedSbi
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	log.Debugf("SBI-Search-ID: %+v\n", query.ID.String())
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.sbiStoreName)
    	result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: query.ID.String()}})
    	if result == nil {
    		return nil, nil
    	}
    
    	err := result.Decode(&loadedSbi)
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
    	}
    
    	newSbi, _ := NewSBI(loadedSbi.Type, uuid.MustParse(loadedSbi.ID))
    
    	return newSbi, nil
    }
    
    // GetAll returns all SBIs
    func (s *DatabaseSbiStore) GetAll() ([]southbound.SouthboundInterface, error) {
    	var loadedSbis []LoadedSbi
    	var sbis []southbound.SouthboundInterface
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.sbiStoreName)
    
    	cursor, err := collection.Find(ctx, bson.D{})
    	if err != nil {
    		return nil, err
    	}
    	defer cursor.Close(ctx)
    
    	err = cursor.All(ctx, &loadedSbis)
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
    	}
    
    	for _, sbi := range loadedSbis {
    		newSbi, _ := NewSBI(sbi.Type, uuid.MustParse(sbi.ID))
    		sbis = append(sbis, newSbi)
    	}
    
    	return sbis, nil
    }
    
    // GetSBI takes a SouthboundInterface's UUID or name and returns the SouthboundInterface. If the requested
    // SouthboundInterface does not exist an error is returned.
    func (s *DatabaseSbiStore) GetSBI(id uuid.UUID) (southbound.SouthboundInterface, error) {
    	var loadedSbi LoadedSbi
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.sbiStoreName)
    	result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: id.String()}})
    	if result == nil {
    		return nil, nil
    	}
    
    	err := result.Decode(&loadedSbi)
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
    	}
    
    	newSbi, err := NewSBI(loadedSbi.Type, uuid.MustParse(loadedSbi.ID))
    
    	return newSbi, nil
    }
    
    func getSbiTypeFromString(sbiTypeAsString string) spb.Type {
    	sbiTypeInt := spb.Type_value[sbiTypeAsString]
    
    	return spb.Type(sbiTypeInt)
    }