[VOL-5493] DB calls optimizations and reductions

Change-Id: I1b9053aabd9f170df365a236f6c3c9678735ad2e
Signed-off-by: bseeniva <balaji.seenivasan@radisys.com>
diff --git a/VERSION b/VERSION
index a76ccff..0b2eb36 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.7.1
+3.7.2
diff --git a/rw_core/core/device/agent.go b/rw_core/core/device/agent.go
index 2f5e278..47e7ff3 100755
--- a/rw_core/core/device/agent.go
+++ b/rw_core/core/device/agent.go
@@ -1138,23 +1138,21 @@
 	defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }()
 
 	// Remove the associated peer ports on the parent device
-	for portID := range agent.portLoader.ListIDs() {
-		if portHandle, have := agent.portLoader.Lock(portID); have {
-			oldPort := portHandle.GetReadOnly()
-			updatedPeers := make([]*voltha.Port_PeerPort, 0)
-			for _, peerPort := range oldPort.Peers {
-				if peerPort.DeviceId != device.Id {
-					updatedPeers = append(updatedPeers, peerPort)
-				}
+	if portHandle, have := agent.portLoader.Lock(device.ParentPortNo); have {
+		oldPort := portHandle.GetReadOnly()
+		updatedPeers := make([]*voltha.Port_PeerPort, 0)
+		for _, peerPort := range oldPort.Peers {
+			if peerPort.DeviceId != device.Id {
+				updatedPeers = append(updatedPeers, peerPort)
 			}
-			newPort := *oldPort
-			newPort.Peers = updatedPeers
-			if err = portHandle.Update(ctx, &newPort); err != nil {
-				portHandle.Unlock()
-				return nil
-			}
-			portHandle.Unlock()
 		}
+		newPort := *oldPort
+		newPort.Peers = updatedPeers
+		if err = portHandle.Update(ctx, &newPort); err != nil {
+			portHandle.Unlock()
+			return nil
+		}
+		portHandle.Unlock()
 	}
 	if err = agent.deviceMgr.canAdapterRequestProceed(ctx, agent.deviceID); err != nil {
 		logger.Errorw(ctx, "adapter-request-cannot-proceed", log.Fields{"device-id": agent.deviceID, "error": err})
diff --git a/rw_core/core/device/logical_agent.go b/rw_core/core/device/logical_agent.go
index 406b869..904fa91 100644
--- a/rw_core/core/device/logical_agent.go
+++ b/rw_core/core/device/logical_agent.go
@@ -196,8 +196,11 @@
 		// load the meters from KV to cache
 		agent.meterLoader.Load(ctx)
 
-		// load the logical ports from KV to cache
-		agent.portLoader.Load(ctx)
+		// Setup the logical ports from loaded physical ports
+		if err := agent.setupLogicalPorts(ctx); err != nil {
+			logger.Errorf(ctx, "setupLogicalPorts failed: %v", err)
+			return
+		}
 	}
 
 	// Setup the device routes. Building routes may fail if the pre-conditions are not satisfied (e.g. no PON ports present)
diff --git a/rw_core/core/device/logical_agent_meter_helpers.go b/rw_core/core/device/logical_agent_meter_helpers.go
index 22ff7ca..f0a8ad6 100644
--- a/rw_core/core/device/logical_agent_meter_helpers.go
+++ b/rw_core/core/device/logical_agent_meter_helpers.go
@@ -90,15 +90,6 @@
 		}
 	}
 
-	newMeter := &ofp.OfpMeterEntry{
-		Config: oldMeter.Config,
-		Stats:  &newStats,
-	}
-	if err := meterHandle.Update(ctx, newMeter); err != nil {
-		logger.Debugw(ctx, "unable-to-update-meter-in-db", log.Fields{"logical-device-id": agent.logicalDeviceID, "meterID": meterID})
-		return false
-	}
-
 	logger.Debugw(ctx, "updated-meter-flow-stats", log.Fields{"meterId": meterID})
 	return true
 }
diff --git a/rw_core/core/device/logical_port/common.go b/rw_core/core/device/logical_port/common.go
index 5e73770..e2933f0 100644
--- a/rw_core/core/device/logical_port/common.go
+++ b/rw_core/core/device/logical_port/common.go
@@ -21,6 +21,7 @@
 	"github.com/opencord/voltha-lib-go/v7/pkg/log"
 )
 
+//nolint:unused
 var logger log.CLogger
 
 func init() {
diff --git a/rw_core/core/device/logical_port/loader.go b/rw_core/core/device/logical_port/loader.go
index ffaad12..3a76b25 100644
--- a/rw_core/core/device/logical_port/loader.go
+++ b/rw_core/core/device/logical_port/loader.go
@@ -18,14 +18,10 @@
 
 import (
 	"context"
-	"fmt"
 	"sync"
 
 	"github.com/opencord/voltha-go/db/model"
-	"github.com/opencord/voltha-lib-go/v7/pkg/log"
 	"github.com/opencord/voltha-protos/v5/go/voltha"
-	"google.golang.org/grpc/codes"
-	"google.golang.org/grpc/status"
 )
 
 // Loader hides all low-level locking & synchronization related to port state updates
@@ -53,23 +49,6 @@
 	}
 }
 
-// Load queries existing ports from the kv,
-// and should only be called once when first created.
-func (loader *Loader) Load(ctx context.Context) {
-	loader.lock.Lock()
-	defer loader.lock.Unlock()
-
-	var ports []*voltha.LogicalPort
-	if err := loader.dbProxy.List(ctx, &ports); err != nil {
-		logger.Errorw(ctx, "failed-to-list-ports-from-cluster-data-proxy", log.Fields{"error": err})
-		return
-	}
-	for _, port := range ports {
-		loader.ports[port.OfpPort.PortNo] = &chunk{port: port}
-		loader.addLookup(port.DeviceId, port.OfpPort.PortNo)
-	}
-}
-
 // LockOrCreate locks this port if it exists, or creates a new port if it does not.
 // In the case of port creation, the provided "port" must not be modified afterwards.
 func (loader *Loader) LockOrCreate(ctx context.Context, port *voltha.LogicalPort) (*Handle, bool, error) {
@@ -87,17 +66,6 @@
 		entry.lock.Lock()
 		loader.lock.Unlock()
 
-		if err := loader.dbProxy.Set(ctx, fmt.Sprint(port.OfpPort.PortNo), port); err != nil {
-			// revert the map
-			loader.lock.Lock()
-			delete(loader.ports, port.OfpPort.PortNo)
-			loader.removeLookup(port.DeviceId, port.OfpPort.PortNo)
-			loader.lock.Unlock()
-
-			entry.deleted = true
-			entry.lock.Unlock()
-			return nil, false, err
-		}
 		return &Handle{loader: loader, chunk: entry}, true, nil
 	}
 	loader.lock.Unlock()
@@ -143,21 +111,15 @@
 	return h.chunk.port
 }
 
-// Update updates an existing port in the kv.
+// Update updates an existing port in the cache.
 // The provided "port" must not be modified afterwards.
 func (h *Handle) Update(ctx context.Context, port *voltha.LogicalPort) error {
-	if err := h.loader.dbProxy.Set(ctx, fmt.Sprint(port.OfpPort.PortNo), port); err != nil {
-		return status.Errorf(codes.Internal, "failed-update-port-%v: %s", port.OfpPort.PortNo, err)
-	}
 	h.chunk.port = port
 	return nil
 }
 
-// Delete removes the device from the kv
+// Delete removes the device from the cache and marks it as deleted.
 func (h *Handle) Delete(ctx context.Context) error {
-	if err := h.loader.dbProxy.Remove(ctx, fmt.Sprint(h.chunk.port.OfpPort.PortNo)); err != nil {
-		return fmt.Errorf("couldnt-delete-port-from-store-%v", h.chunk.port.OfpPort.PortNo)
-	}
 	h.chunk.deleted = true
 
 	h.loader.lock.Lock()