From 17211eb27a0f4782aad5a4fbf4225cae07b90eca Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.seidl@h-da.de>
Date: Tue, 17 Oct 2023 08:48:41 +0000
Subject: [PATCH] Resolve "Improve the event system by adding an extra Event
 for gNMI-Subscribe to replace the currently used Update Event"

See merge request danet/gosdn!568
---
 application-framework/event/type.go         | 11 ++++++++---
 controller/event/event.go                   |  9 ++++++---
 controller/event/event_test.go              | 14 +++++++-------
 controller/nucleus/networkElementService.go |  4 ++--
 controller/nucleus/networkElementWatcher.go |  2 +-
 5 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/application-framework/event/type.go b/application-framework/event/type.go
index a26631660..787076e56 100644
--- a/application-framework/event/type.go
+++ b/application-framework/event/type.go
@@ -12,6 +12,8 @@ const (
 	Update
 	// Delete is a delete event.
 	Delete
+	// Subscribe is a gNMI subscribe event.
+	Subscribe
 )
 
 // String implements the stringer interface for types.
@@ -23,6 +25,8 @@ func (t Type) String() string {
 		return "update"
 	case Delete:
 		return "delete"
+	case Subscribe:
+		return "subscribe"
 	}
 
 	return "unknown"
@@ -30,9 +34,10 @@ func (t Type) String() string {
 
 var (
 	typeLookup = map[string]Type{
-		"add":    Add,
-		"update": Update,
-		"delete": Delete,
+		"add":       Add,
+		"update":    Update,
+		"delete":    Delete,
+		"subscribe": Subscribe,
 	}
 )
 
diff --git a/controller/event/event.go b/controller/event/event.go
index df39d2b9a..bf74817d5 100644
--- a/controller/event/event.go
+++ b/controller/event/event.go
@@ -19,6 +19,9 @@ const (
 
 	// TypeDelete is a delete event.
 	TypeDelete = "delete"
+
+	// TypeSubscribe is a gNMI subscribe event.
+	TypeSubscribe = "subscribe"
 )
 
 // NewAddEvent creates a new add event.
@@ -48,12 +51,12 @@ func NewUpdateEvent(entityID uuid.UUID) Event {
 	}
 }
 
-// NewMneUpdateEvent creates a new update event for managed network elements.
-func NewMneUpdateEvent(entityID uuid.UUID, pathsAndValues map[string]string) Event {
+// NewGnmiSubscribeEvent creates a new gNMI subscribe event for managed network elements.
+func NewGnmiSubscribeEvent(entityID uuid.UUID, pathsAndValues map[string]string) Event {
 	return Event{
 		ID:                uuid.New(),
 		EntityID:          entityID,
-		Type:              TypeUpdate,
+		Type:              TypeSubscribe,
 		PathsAndValuesMap: pathsAndValues,
 	}
 }
diff --git a/controller/event/event_test.go b/controller/event/event_test.go
index 011c6a8ed..8acfb3c5c 100644
--- a/controller/event/event_test.go
+++ b/controller/event/event_test.go
@@ -121,7 +121,7 @@ func TestNewUpdateEvent(t *testing.T) {
 	}
 }
 
-func TestNewMneUpdateEvent(t *testing.T) {
+func TestNewGnmiSubscribeEvent(t *testing.T) {
 	type args struct {
 		entityID          uuid.UUID
 		pathsAndValuesMap map[string]string
@@ -132,7 +132,7 @@ func TestNewMneUpdateEvent(t *testing.T) {
 		want Event
 	}{
 		{
-			name: "should create a new update event",
+			name: "should create a new subscribe event",
 			args: args{
 				entityID:          getTestEntityUUID(),
 				pathsAndValuesMap: map[string]string{"some/random/path": "val"},
@@ -140,25 +140,25 @@ func TestNewMneUpdateEvent(t *testing.T) {
 			want: Event{
 				ID:                uuid.New(),
 				EntityID:          getTestEntityUUID(),
-				Type:              TypeUpdate,
+				Type:              TypeSubscribe,
 				PathsAndValuesMap: map[string]string{"some/random/path": "val"},
 			},
 		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			got := NewMneUpdateEvent(tt.args.entityID, tt.args.pathsAndValuesMap)
+			got := NewGnmiSubscribeEvent(tt.args.entityID, tt.args.pathsAndValuesMap)
 
 			if !reflect.DeepEqual(got.EntityID, tt.want.EntityID) {
-				t.Errorf("NewMneUpdateEvent().EntityID = %v, want %v", got, tt.want)
+				t.Errorf("NewGnmiSubscribeEvent().EntityID = %v, want %v", got, tt.want)
 			}
 
 			if !reflect.DeepEqual(got.Type, tt.want.Type) {
-				t.Errorf("NewMneUpdateEvent().Type = %v, want %v", got, tt.want)
+				t.Errorf("NewGnmiSubscribeEvent().Type = %v, want %v", got, tt.want)
 			}
 
 			if !reflect.DeepEqual(got.PathsAndValuesMap, tt.want.PathsAndValuesMap) {
-				t.Errorf("NewMneUpdateEvent().PathsAndValuesMap = %v, want %v", got, tt.want)
+				t.Errorf("NewGnmiSubscribeEvent().PathsAndValuesMap = %v, want %v", got, tt.want)
 			}
 		})
 	}
diff --git a/controller/nucleus/networkElementService.go b/controller/nucleus/networkElementService.go
index 32736bdbb..a5fe2cac1 100644
--- a/controller/nucleus/networkElementService.go
+++ b/controller/nucleus/networkElementService.go
@@ -146,7 +146,7 @@ func (s *NetworkElementService) UpdateModel(networkElementID uuid.UUID, modelAsS
 	}
 
 	// TODO (faseid): check if we want to add the paths with values here instead of empty map!
-	pubEvent := event.NewMneUpdateEvent(networkElementID, map[string]string{})
+	pubEvent := event.NewUpdateEvent(networkElementID)
 	if err := s.eventService.PublishEvent(NetworkElementEventTopic, pubEvent); err != nil {
 		go func() {
 			s.eventService.Reconnect()
@@ -169,7 +169,7 @@ func (s *NetworkElementService) Update(networkElementToUpdate networkelement.Net
 	}
 
 	// TODO (faseid): check if we want to add the paths with values here instead of empty map!
-	pubEvent := event.NewMneUpdateEvent(networkElementToUpdate.ID(), map[string]string{})
+	pubEvent := event.NewUpdateEvent(networkElementToUpdate.ID())
 	if err := s.eventService.PublishEvent(NetworkElementEventTopic, pubEvent); err != nil {
 		go func() {
 			s.eventService.Reconnect()
diff --git a/controller/nucleus/networkElementWatcher.go b/controller/nucleus/networkElementWatcher.go
index 2102a969b..672574d90 100644
--- a/controller/nucleus/networkElementWatcher.go
+++ b/controller/nucleus/networkElementWatcher.go
@@ -201,7 +201,7 @@ func (n *NetworkElementWatcher) handleSubscribeResponseUpdate(resp *gpb.Subscrib
 		log.Errorf("Error trying to parse uuid, could not handle subscription response: %v", err)
 	}
 
-	pubEvent := event.NewMneUpdateEvent(mneID, pathsAndValues)
+	pubEvent := event.NewGnmiSubscribeEvent(mneID, pathsAndValues)
 	if err := n.eventService.PublishEvent(NetworkElementEventTopic, pubEvent); err != nil {
 		go func() {
 			n.eventService.Reconnect()
-- 
GitLab