Refactor vpp.go to have the connection mgmt and vpp_*.go to have one Manager each

This commit is contained in:
Pim van Pelt
2025-06-24 07:00:52 +02:00
parent 96b9dd501d
commit bdaa2e366b
4 changed files with 364 additions and 206 deletions

View File

@@ -26,8 +26,68 @@ type InterfaceDetails struct {
// InterfaceEventCallback is called when interface events occur
type InterfaceEventCallback func(details []InterfaceDetails)
// InterfaceManager handles interface-related VPP operations
type InterfaceManager struct {
client *VPPClient
eventCallback InterfaceEventCallback
}
// NewInterfaceManager creates a new interface manager
func NewInterfaceManager(client *VPPClient) *InterfaceManager {
return &InterfaceManager{
client: client,
}
}
// SetEventCallback sets the callback for interface events
func (im *InterfaceManager) SetEventCallback(callback InterfaceEventCallback) {
im.eventCallback = callback
}
// GetAllInterfaceDetails retrieves detailed information for all interfaces
func GetAllInterfaceDetails(ch api.Channel) ([]InterfaceDetails, error) {
func (im *InterfaceManager) GetAllInterfaceDetails() ([]InterfaceDetails, error) {
if !im.client.IsConnected() {
return nil, &VPPError{Message: "VPP client not connected"}
}
ch, err := im.client.NewAPIChannel()
if err != nil {
return nil, err
}
defer ch.Close()
return getAllInterfaceDetails(ch)
}
// StartEventWatcher starts watching for interface events
func (im *InterfaceManager) StartEventWatcher() error {
if !im.client.IsConnected() {
return &VPPError{Message: "VPP client not connected"}
}
ch, err := im.client.NewAPIChannel()
if err != nil {
return err
}
return watchInterfaceEvents(ch, im.handleInterfaceEvent)
}
// handleInterfaceEvent handles interface events and calls the callback
func (im *InterfaceManager) handleInterfaceEvent() {
if im.eventCallback != nil {
details, err := im.GetAllInterfaceDetails()
if err != nil {
logger.Debugf("Failed to retrieve interface details after event: %v", err)
} else {
logger.Debugf("Calling interface event callback with %d interfaces", len(details))
im.eventCallback(details)
}
}
}
// getAllInterfaceDetails retrieves detailed information for all interfaces (internal function)
func getAllInterfaceDetails(ch api.Channel) ([]InterfaceDetails, error) {
logger.Debugf("Retrieving all interface details from VPP")
// Get all interfaces
@@ -73,7 +133,8 @@ func GetAllInterfaceDetails(ch api.Channel) ([]InterfaceDetails, error) {
return details, nil
}
func WatchInterfaceEvents(ch api.Channel, callback InterfaceEventCallback) error {
// watchInterfaceEvents watches for VPP interface events (internal function)
func watchInterfaceEvents(ch api.Channel, callback func()) error {
logger.Debugf("WatchInterfaceEvents() called - starting interface event monitoring")
notifChan := make(chan api.Message, 100)
@@ -127,15 +188,9 @@ func WatchInterfaceEvents(ch api.Channel, callback InterfaceEventCallback) error
logger.Debugf("interface event: SwIfIndex=%d, Flags=%d, Deleted=%t",
e.SwIfIndex, e.Flags, e.Deleted)
// When an interface event occurs, retrieve all interface details and call callback
// When an interface event occurs, call the callback
if callback != nil {
details, err := GetAllInterfaceDetails(ch)
if err != nil {
logger.Debugf("Failed to retrieve interface details after event: %v", err)
} else {
logger.Debugf("Calling interface event callback with %d interfaces", len(details))
callback(details)
}
callback()
}
}
logger.Debugf("Interface event listener goroutine ended")