Skip to content
Snippets Groups Projects
memorySbiStore.go 2.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    	"encoding/json"
    
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
    	"code.fbi.h-da.de/danet/gosdn/controller/store"
    )
    
    // MemorySbiStore provides a in-memory implementation for sbis.
    type MemorySbiStore struct {
    	Store           map[string]southbound.LoadedSbi
    	nameLookupTable map[string]string
    }
    
    // NewMemorySbiStore returns a specific in-memory store for a type T.
    func NewMemorySbiStore() southbound.Store {
    	return &MemorySbiStore{
    		Store:           make(map[string]southbound.LoadedSbi),
    		nameLookupTable: make(map[string]string),
    	}
    }
    
    // Add adds a item to the store.
    func (t *MemorySbiStore) Add(item southbound.SouthboundInterface) error {
    	var sbi southbound.LoadedSbi
    
    	b, err := json.Marshal(item)
    	if err != nil {
    		return err
    	}
    	err = json.Unmarshal(b, &sbi)
    	if err != nil {
    		return err
    	}
    
    	_, ok := t.Store[sbi.ID]
    	if ok {
    		return nil
    	}
    
    	t.Store[sbi.ID] = sbi
    	t.nameLookupTable[item.Name()] = sbi.ID
    
    	return nil
    }
    
    // Update updates a existing sbi.
    func (t *MemorySbiStore) Update(item southbound.SouthboundInterface) error {
    	_, ok := t.Store[item.ID().String()]
    	if ok {
    		return nil
    	}
    
    	var sbi southbound.LoadedSbi
    
    	b, err := json.Marshal(item)
    	if err != nil {
    		return err
    	}
    	err = json.Unmarshal(b, &sbi)
    	if err != nil {
    		return err
    	}
    
    	t.Store[item.ID().String()] = sbi
    	t.nameLookupTable[item.Name()] = item.ID().String()
    
    	return nil
    }
    
    // Delete deletes a sbi from the store.
    func (t *MemorySbiStore) Delete(item southbound.SouthboundInterface) error {
    	delete(t.Store, item.ID().String())
    
    	return nil
    }
    
    // Get takes a sbi's UUID or name and returns the sbi.
    func (t *MemorySbiStore) Get(query store.Query) (southbound.LoadedSbi, error) {
    	// First search for direct hit on UUID.
    	item, ok := t.Store[query.ID.String()]
    	if !ok {
    		// Second search for name
    		id, ok := t.nameLookupTable[query.Name]
    		if !ok {
    
    			return item, errors.ErrCouldNotFind{ID: query.ID, Name: query.Name}
    
    		}
    
    		item, ok := t.Store[id]
    		if !ok {
    
    			return item, errors.ErrCouldNotFind{ID: query.ID, Name: query.Name}
    
    		}
    
    		return item, nil
    	}
    
    	return item, nil
    }
    
    // GetAll returns all stored sbis.
    func (t *MemorySbiStore) GetAll() ([]southbound.LoadedSbi, error) {
    	var allItems []southbound.LoadedSbi
    
    	for _, item := range t.Store {
    		allItems = append(allItems, item)
    	}
    
    	return allItems, nil
    }