<commit_before>package agent

import (
	"github.com/golang/glog"
	"github.com/romana/core/common"
	"github.com/romana/core/pkg/util/firewall"
	"sync"
)

// agentStore is a backing storage. Agent will likely use
// sqlite which is not very reliable in concurrent access scenario,
// so we are going to guard access with mutex.
type agentStore struct {
	common.DbStore
	mu *sync.Mutex
}

// GetDb implements firewall.FirewallStore
func (agentStore agentStore) GetDb() common.DbStore {
	return agentStore.DbStore
}

// GetMutex implements firewall.FirewallStore
func (agentStore agentStore) GetMutex() *sync.Mutex {
	return agentStore.mu
}

// Entities implements Entities method of
// Service interface.
func (agentStore *agentStore) Entities() []interface{} {
	retval := make([]interface{}, 3)
	retval[0] = new(Route)
	retval[1] = new(NetworkInterface)
	retval[2] = new(firewall.IPtablesRule)
	return retval
}

// NewStore returns initialized agentStore.
func NewStore(config common.ServiceConfig) *agentStore {
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	store := agentStore{
		mu: &sync.Mutex{},
	}
	store.ServiceStore = &store
	store.SetConfig(storeConfig)

	return &store
}

// Route is a model to store managed routes
type Route struct {
	ID     uint64 `sql:"AUTO_INCREMENT"`
	IP     string
	Mask   string
	Kind   targetKind
	Spec   string
	Status string
}

// targetKind is a an IP route destination type.
type targetKind string

const (
	device  targetKind = "dev"
	gateway targetKind = "gw"
)

// NetworkInterface is a model to store managed network interfaces.
type NetworkInterface struct {
	ID     uint64 `sql:"AUTO_INCREMENT"`
	Name   string
	Status string
}

// CreateSchemaPostProcess implements CreateSchemaPostProcess method of
// Service interface.
func (agentStore *agentStore) CreateSchemaPostProcess() error {
	return nil
}

func (agentStore *agentStore) deleteNetworkInterface(iface *NetworkInterface) error {
	glog.V(1).Info("Acquiring store mutex for deleteNetworkInterface")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for deleteNetworkInterface")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for deleteNetworkInterface")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Delete(iface)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}

	return nil
}

func (agentStore *agentStore) findNetworkInterface(ifaceName string) (*NetworkInterface, error) {
	glog.V(1).Info("Acquiring store mutex for findNetworkInterface")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for findNetworkInterface")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for findNetworkInterface")

	var iface NetworkInterface
	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Where("name = ?", ifaceName).First(&iface)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return nil, err
	}
	if db.Error != nil {
		return nil, db.Error
	}
	return &iface, nil
}

func (agentStore *agentStore) addNetworkInterface(iface *NetworkInterface) error {
	glog.V(1).Info("Acquiring store mutex for addNetworkInterface")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for addNetworkInterface")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for addNetworkInterface")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Create(iface)
	if db.Error != nil {
		return db.Error
	}
	agentStore.DbStore.Db.NewRecord(*iface)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}
	return nil
}

func (agentStore *agentStore) listNetworkInterfaces() ([]NetworkInterface, error) {
	glog.V(1).Info("Acquiring store mutex for listNetworkInterfaces")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for listNetworkInterfaces")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for listNetworkInterfaces")

	var networkInterfaces []NetworkInterface
	agentStore.DbStore.Db.Find(&networkInterfaces)
	err := common.MakeMultiError(agentStore.DbStore.Db.GetErrors())
	if err != nil {
		return nil, err
	}
	return networkInterfaces, nil
}<commit_msg>Remove networkInterfaces model, add basic methods for routes model management<commit_after>package agent

import (
	"github.com/golang/glog"
	"github.com/romana/core/common"
	"github.com/romana/core/pkg/util/firewall"
	"sync"
)

// agentStore is a backing storage. Agent will likely use
// sqlite which is not very reliable in concurrent access scenario,
// so we are going to guard access with mutex.
type agentStore struct {
	common.DbStore
	mu *sync.Mutex
}

// GetDb implements firewall.FirewallStore
func (agentStore agentStore) GetDb() common.DbStore {
	return agentStore.DbStore
}

// GetMutex implements firewall.FirewallStore
func (agentStore agentStore) GetMutex() *sync.Mutex {
	return agentStore.mu
}

// Entities implements Entities method of
// Service interface.
func (agentStore *agentStore) Entities() []interface{} {
	retval := make([]interface{}, 2)
	retval[0] = new(Route)
	retval[1] = new(firewall.IPtablesRule)
	return retval
}

// NewStore returns initialized agentStore.
func NewStore(config common.ServiceConfig) *agentStore {
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	store := agentStore{
		mu: &sync.Mutex{},
	}
	store.ServiceStore = &store
	store.SetConfig(storeConfig)

	return &store
}

// Route is a model to store managed routes
type Route struct {
	ID     uint64 `sql:"AUTO_INCREMENT"`
	IP     string
	Mask   string
	Kind   targetKind
	Spec   string
	Status string
}

// targetKind is a an IP route destination type.
type targetKind string

const (
	device  targetKind = "dev"
	gateway targetKind = "gw"
)

// CreateSchemaPostProcess implements CreateSchemaPostProcess method of
// Service interface.
func (agentStore *agentStore) CreateSchemaPostProcess() error {
	return nil
}

func (agentStore *agentStore) deleteRoute(route *Route) error {
	glog.V(1).Info("Acquiring store mutex for deleteRoute")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for deleteRoute")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for deleteRoute")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Delete(route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}

	return nil
}

func (agentStore *agentStore) findRouteByIface(routeIface string) (*Route, error) {
	glog.V(1).Info("Acquiring store mutex for findRoute")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for findRoute")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for findRoute")

	var route Route
	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Where("ip = ?", routeIface).First(&route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return nil, err
	}
	if db.Error != nil {
		return nil, db.Error
	}
	return &route, nil
}

func (agentStore *agentStore) addRoute(route *Route) error {
	glog.V(1).Info("Acquiring store mutex for addRoute")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for addRoute")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for addRoute")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Create(route)
	if db.Error != nil {
		return db.Error
	}
	agentStore.DbStore.Db.NewRecord(*route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}
	return nil
}

func (agentStore *agentStore) listRoutes() ([]Route, error) {
	glog.V(1).Info("Acquiring store mutex for listRoutes")
	agentStore.mu.Lock()
	defer func() {
		glog.V(1).Info("Releasing store mutex for listRoutes")
		agentStore.mu.Unlock()
	}()
	glog.V(1).Info("Acquired store mutex for listRoutes")

	var networkInterfaces []Route
	agentStore.DbStore.Db.Find(&networkInterfaces)
	err := common.MakeMultiError(agentStore.DbStore.Db.GetErrors())
	if err != nil {
		return nil, err
	}
	return networkInterfaces, nil
}
{'fraction_non_alphanumeric': 0.12144765606023804, 'fraction_numerical': 0.004493563274228807, 'mean_word_length': 4.590631364562118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5849809', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2735, 'n_words': 704}