Skip to content
Snippets Groups Projects
Commit c42757e8 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

add mutex to map

parent 42396a3f
No related branches found
No related tags found
1 merge request!1037Resolve "Implement gNMI subscription management"
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"sync"
"code.fbi.h-da.de/danet/gosdn/controller/config" "code.fbi.h-da.de/danet/gosdn/controller/config"
"code.fbi.h-da.de/danet/gosdn/controller/customerrs" "code.fbi.h-da.de/danet/gosdn/controller/customerrs"
...@@ -33,9 +34,10 @@ const ( ...@@ -33,9 +34,10 @@ const (
// NetworkElementWatcher is a component that subscribes to network elements via gNMI from within the controller and handles // NetworkElementWatcher is a component that subscribes to network elements via gNMI from within the controller and handles
// responses by triggering the internal event process. // responses by triggering the internal event process.
type NetworkElementWatcher struct { type NetworkElementWatcher struct {
mneService networkelement.Service mneService networkelement.Service
networkelementSubcriptions map[uuid.UUID]*networkelementSubscriptionHelper // TODO: Mutex stuff here! networkelementSubscriptionsMutex sync.Mutex
eventService eventInterfaces.Service networkelementSubcriptions map[uuid.UUID]*networkelementSubscriptionHelper
eventService eventInterfaces.Service
} }
// networkelementSubscriptionHelper is used to store information to stop a running subscribe go routine. // 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 ...@@ -150,6 +152,9 @@ func (n *NetworkElementWatcher) callSubscribe(stopContext context.Context, mne n
} }
func (n *NetworkElementWatcher) addToNetworkElementSubscriptions(subID uuid.UUID, devSub *networkelementSubscriptionHelper) { func (n *NetworkElementWatcher) addToNetworkElementSubscriptions(subID uuid.UUID, devSub *networkelementSubscriptionHelper) {
n.networkelementSubscriptionsMutex.Lock()
defer n.networkelementSubscriptionsMutex.Unlock()
n.networkelementSubcriptions[subID] = devSub n.networkelementSubcriptions[subID] = devSub
} }
...@@ -163,6 +168,9 @@ func (n *NetworkElementWatcher) StopAndRemoveAllNetworkElementSubscriptions() { ...@@ -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 // StopAndRemoveNetworkElementSubscription passes a subscription uuid to stop the running subscription go routing and removes the entry from the map
// of network element subscriptions. // of network element subscriptions.
func (n *NetworkElementWatcher) StopAndRemoveNetworkElementSubscription(subID uuid.UUID) { func (n *NetworkElementWatcher) StopAndRemoveNetworkElementSubscription(subID uuid.UUID) {
n.networkelementSubscriptionsMutex.Lock()
defer n.networkelementSubscriptionsMutex.Unlock()
n.networkelementSubcriptions[subID].stopFunc() n.networkelementSubcriptions[subID].stopFunc()
delete(n.networkelementSubcriptions, subID) delete(n.networkelementSubcriptions, subID)
} }
...@@ -253,6 +261,8 @@ func (n *NetworkElementWatcher) mergeGnmiSubscriptions(gNMISusbcriptionPathsFrom ...@@ -253,6 +261,8 @@ func (n *NetworkElementWatcher) mergeGnmiSubscriptions(gNMISusbcriptionPathsFrom
// GetAllSubscriptionInformations returns the information of all running sunscriptions. // GetAllSubscriptionInformations returns the information of all running sunscriptions.
func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkelementSubscriptionHelper { func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkelementSubscriptionHelper {
var information []*networkelementSubscriptionHelper var information []*networkelementSubscriptionHelper
n.networkelementSubscriptionsMutex.Lock()
defer n.networkelementSubscriptionsMutex.Unlock()
for _, info := range n.networkelementSubcriptions { for _, info := range n.networkelementSubcriptions {
information = append(information, info) information = append(information, info)
...@@ -263,6 +273,9 @@ func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkeleme ...@@ -263,6 +273,9 @@ func (n *NetworkElementWatcher) GetAllSubscriptionInformations() []*networkeleme
// GetSubscriptionInformations returns the information for one specific subscription referenced by its ID. // GetSubscriptionInformations returns the information for one specific subscription referenced by its ID.
func (n *NetworkElementWatcher) GetSubscriptionInformations(subID uuid.UUID) (*networkelementSubscriptionHelper, error) { func (n *NetworkElementWatcher) GetSubscriptionInformations(subID uuid.UUID) (*networkelementSubscriptionHelper, error) {
n.networkelementSubscriptionsMutex.Lock()
defer n.networkelementSubscriptionsMutex.Unlock()
information, ok := n.networkelementSubcriptions[subID] information, ok := n.networkelementSubcriptions[subID]
if !ok { if !ok {
return nil, errors.New(fmt.Sprintf("Couldn't retrieve information for subscription with ID: %s", subID.String())) return nil, errors.New(fmt.Sprintf("Couldn't retrieve information for subscription with ID: %s", subID.String()))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment