diff --git a/controller/nucleus/networkElementWatcher.go b/controller/nucleus/networkElementWatcher.go index dd7a8cd685cd86825c10892d918333283df2345b..dd8713aa834ce8156ce423f98a7a9e40e328fb8e 100644 --- a/controller/nucleus/networkElementWatcher.go +++ b/controller/nucleus/networkElementWatcher.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "strconv" + "sync" "code.fbi.h-da.de/danet/gosdn/controller/config" "code.fbi.h-da.de/danet/gosdn/controller/customerrs" @@ -33,9 +34,10 @@ const ( // NetworkElementWatcher is a component that subscribes to network elements via gNMI from within the controller and handles // responses by triggering the internal event process. type NetworkElementWatcher struct { - mneService networkelement.Service - networkelementSubcriptions map[uuid.UUID]*networkelementSubscriptionHelper // TODO: Mutex stuff here! - eventService eventInterfaces.Service + mneService networkelement.Service + networkelementSubscriptionsMutex sync.Mutex + networkelementSubcriptions map[uuid.UUID]*networkelementSubscriptionHelper + eventService eventInterfaces.Service } // networkelementSubscriptionHelper is used to store information to stop a running subscribe go routine. @@ -150,6 +152,9 @@ func (n *NetworkElementWatcher) callSubscribe(stopContext context.Context, mne n } func (n *NetworkElementWatcher) addToNetworkElementSubscriptions(subID uuid.UUID, devSub *networkelementSubscriptionHelper) { + n.networkelementSubscriptionsMutex.Lock() + defer n.networkelementSubscriptionsMutex.Unlock() + n.networkelementSubcriptions[subID] = devSub } @@ -163,6 +168,9 @@ func (n *NetworkElementWatcher) StopAndRemoveAllNetworkElementSubscriptions() { // StopAndRemoveNetworkElementSubscription passes a subscription uuid to stop the running subscription go routing and removes the entry from the map // of network element subscriptions. func (n *NetworkElementWatcher) StopAndRemoveNetworkElementSubscription(subID uuid.UUID) { + n.networkelementSubscriptionsMutex.Lock() + defer n.networkelementSubscriptionsMutex.Unlock() + n.networkelementSubcriptions[subID].stopFunc() delete(n.networkelementSubcriptions, subID) } @@ -253,6 +261,8 @@ func (n *NetworkElementWatcher) mergeGnmiSubscriptions(gNMISusbcriptionPathsFrom // GetAllSubscriptionInformations returns the information of all running sunscriptions. func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkelementSubscriptionHelper { var information []*networkelementSubscriptionHelper + n.networkelementSubscriptionsMutex.Lock() + defer n.networkelementSubscriptionsMutex.Unlock() for _, info := range n.networkelementSubcriptions { information = append(information, info) @@ -263,6 +273,9 @@ func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkeleme // GetSubscriptionInformations returns the information for one specific subscription referenced by its ID. func (n *NetworkElementWatcher) GetSubscriptionInformations(subID uuid.UUID) (*networkelementSubscriptionHelper, error) { + n.networkelementSubscriptionsMutex.Lock() + defer n.networkelementSubscriptionsMutex.Unlock() + information, ok := n.networkelementSubcriptions[subID] if !ok { return nil, errors.New(fmt.Sprintf("Couldn't retrieve information for subscription with ID: %s", subID.String()))