VOL-2092 VOL-2116 : Ensure the latest data is updated

This fix resolves the issue when kv/memory does not
reflect the content of what was updated.

- Cleanup data on secondary core when removed from owner core

Amendments:

- Removed Config.Data from hash generation logic (redundant)

Change-Id: I08237e25d565ab0d875a81ad5b25b93cfd89914d
diff --git a/pkg/db/model/non_persisted_revision.go b/pkg/db/model/non_persisted_revision.go
index 88320cb..514b3b3 100644
--- a/pkg/db/model/non_persisted_revision.go
+++ b/pkg/db/model/non_persisted_revision.go
@@ -25,6 +25,7 @@
 	"github.com/opencord/voltha-lib-go/pkg/log"
 	"reflect"
 	"sort"
+	"strings"
 	"sync"
 	"time"
 )
@@ -284,12 +285,7 @@
 	npr.mutex.Lock()
 	defer npr.mutex.Unlock()
 
-	if ctx != nil {
-		if ctxTS, ok := ctx.Value(RequestTimestamp).(int64); ok && npr.lastUpdate.UnixNano() > ctxTS {
-			log.Warnw("data-is-older-than-current", log.Fields{"ctx-ts": ctxTS, "rev-ts": npr.lastUpdate.UnixNano()})
-			return npr
-		}
-	}
+	log.Debugw("update-data", log.Fields{"hash": npr.GetHash(), "current": npr.Config.Data, "provided": data})
 
 	// Do not update the revision if data is the same
 	if npr.Config.Data != nil && npr.Config.hashData(npr.Root, data) == npr.Config.Hash {
@@ -313,6 +309,8 @@
 
 	newRev.Finalize(false)
 
+	log.Debugw("update-data-complete", log.Fields{"updated": newRev.Config.Data, "provided": data})
+
 	return &newRev
 }
 
@@ -461,6 +459,25 @@
 	}
 }
 
+/// ChildDropByName will remove a child entry matching the type and name
+func (npr *NonPersistedRevision) ChildDropByName(childName string) {
+	// Extract device type
+	parts := strings.SplitN(childName, "/", 2)
+	childType := parts[0]
+
+	if childType != "" {
+		children := make([]Revision, len(npr.GetChildren(childType)))
+		copy(children, npr.GetChildren(childType))
+		for i, child := range children {
+			if child.GetName() == childName {
+				children = append(children[:i], children[i+1:]...)
+				npr.SetChildren(childType, children)
+				break
+			}
+		}
+	}
+}
+
 func (npr *NonPersistedRevision) SetLastUpdate(ts ...time.Time) {
 	npr.mutex.Lock()
 	defer npr.mutex.Unlock()
diff --git a/pkg/db/model/persisted_revision.go b/pkg/db/model/persisted_revision.go
index c644e14..8ab182b 100644
--- a/pkg/db/model/persisted_revision.go
+++ b/pkg/db/model/persisted_revision.go
@@ -162,7 +162,14 @@
 			switch event.EventType {
 			case kvstore.DELETE:
 				log.Debugw("delete-from-memory", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()})
-				pr.Revision.Drop("", true)
+
+				// Remove reference from cache
+				GetRevCache().Delete(latestRev.GetName())
+
+				// Remove reference from parent
+				parent := pr.GetBranch().Node.GetRoot()
+				parent.GetBranch(NONE).Latest.ChildDropByName(latestRev.GetName())
+
 				break StopWatchLoop
 
 			case kvstore.PUT:
@@ -382,10 +389,11 @@
 		// Also check if we are treating a newer revision of the data or not
 		if childRev.GetData().(proto.Message).String() != data.(proto.Message).String() && childRev.getVersion() < version {
 			log.Debugw("revision-data-is-different", log.Fields{
-				"key":     childRev.GetHash(),
-				"name":    childRev.GetName(),
-				"data":    childRev.GetData(),
-				"version": childRev.getVersion(),
+				"key":               childRev.GetHash(),
+				"name":              childRev.GetName(),
+				"data":              childRev.GetData(),
+				"in-memory-version": childRev.getVersion(),
+				"persisted-version": version,
 			})
 
 			//
@@ -436,9 +444,11 @@
 		} else {
 			if childRev != nil {
 				log.Debugw("keeping-revision-data", log.Fields{
-					"key":  childRev.GetHash(),
-					"name": childRev.GetName(),
-					"data": childRev.GetData(),
+					"key":                 childRev.GetHash(),
+					"name":                childRev.GetName(),
+					"data":                childRev.GetData(),
+					"in-memory-version":   childRev.getVersion(),
+					"persistence-version": version,
 				})
 
 				// Update timestamp to reflect when it was last read and to reset tracked timeout
@@ -455,9 +465,10 @@
 		// There is no available child with that key value.
 		// Create a new child and update the parent revision.
 		log.Debugw("no-such-revision-entry", log.Fields{
-			"key":  keyValue,
-			"name": typeName,
-			"data": data,
+			"key":     keyValue,
+			"name":    typeName,
+			"data":    data,
+			"version": version,
 		})
 
 		// BEGIN child lock
diff --git a/pkg/db/model/revision.go b/pkg/db/model/revision.go
index bb61355..71c5d0c 100644
--- a/pkg/db/model/revision.go
+++ b/pkg/db/model/revision.go
@@ -28,6 +28,7 @@
 	Drop(txid string, includeConfig bool)
 	StorageDrop(txid string, includeConfig bool)
 	ChildDrop(childType string, childHash string)
+	ChildDropByName(childName string)
 	SetChildren(name string, children []Revision)
 	GetChildren(name string) []Revision
 	SetAllChildren(children map[string][]Revision)