[VOL-3930] Using a shared lock for Resource acquisition and release

Change-Id: Ibf6346135380ab14d0c89f0e2945ebefdee1b4f9
diff --git a/internal/pkg/core/device_handler_test.go b/internal/pkg/core/device_handler_test.go
index 012ce51..1cae8b6 100644
--- a/internal/pkg/core/device_handler_test.go
+++ b/internal/pkg/core/device_handler_test.go
@@ -20,6 +20,7 @@
 import (
 	"context"
 	conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
+	tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
 	"net"
 	"reflect"
 	"sync"
@@ -193,15 +194,23 @@
 	ranges["gemport_id_shared"] = uint32(0)
 	ranges["flow_id_shared"] = uint32(0)
 
-	ponmgr := &ponrmgr.PONResourceManager{
-		DeviceID: "onu-1",
-		IntfIDs:  []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
-		KVStore: &db.Backend{
-			Client: &mocks.MockKVClient{},
-		},
-		PonResourceRanges: ranges,
-		SharedIdxByType:   sharedIdxByType,
+	ponmgr := &ponrmgr.PONResourceManager{}
+
+	ctx := context.TODO()
+	tpMgr, err := tp.NewTechProfile(ctx, ponmgr, "etcd", "127.0.0.1", "/")
+	if err != nil {
+		logger.Fatal(ctx, err.Error())
 	}
+
+	ponmgr.DeviceID = "onu-1"
+	ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
+	ponmgr.KVStore = &db.Backend{
+		Client: &mocks.MockKVClient{},
+	}
+	ponmgr.PonResourceRanges = ranges
+	ponmgr.SharedIdxByType = sharedIdxByType
+	ponmgr.TechProfileMgr = tpMgr
+
 	for i := 0; i < NumPonPorts; i++ {
 		dh.resourceMgr.ResourceMgrs[uint32(i)] = ponmgr
 	}
diff --git a/internal/pkg/core/openolt_flowmgr.go b/internal/pkg/core/openolt_flowmgr.go
index 680df66..31ca07d 100644
--- a/internal/pkg/core/openolt_flowmgr.go
+++ b/internal/pkg/core/openolt_flowmgr.go
@@ -840,6 +840,9 @@
 		}
 		logger.Infow(ctx, "allocated-tcont-and-gem-ports",
 			log.Fields{
+				"intf-id":   intfID,
+				"onu-id":    onuID,
+				"uni-id":    uniID,
 				"alloc-ids": allocIDs,
 				"gemports":  allgemPortIDs,
 				"device-id": f.deviceHandler.device.Id})
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
index 140bbc8..7a3f8ec 100755
--- a/internal/pkg/resourcemanager/resourcemanager.go
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -446,7 +446,7 @@
 		return 0, err
 	}
 	// Get ONU id for a provided pon interface ID.
-	onuID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
+	onuID, err := RsrcMgr.ResourceMgrs[ponIntfID].TechProfileMgr.GetResourceID(ctx, ponIntfID,
 		ponrmgr.ONU_ID, 1)
 	if err != nil {
 		logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
@@ -841,7 +841,13 @@
 	RsrcMgr.OnuIDMgmtLock[intfID].Lock()
 	defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
 
-	RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
+	if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
+		logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
+			"intf-id": intfID,
+			"onu-id":  onuID,
+			"err":     err.Error(),
+		})
+	}
 
 	/* Free onu id for a particular interface.*/
 	var IntfonuID string
@@ -861,7 +867,13 @@
 	RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
 	allocIDs := make([]uint32, 0)
 	allocIDs = append(allocIDs, allocID)
-	RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
+	if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
+		logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
+			"intf-id": IntfID,
+			"onu-id":  onuID,
+			"err":     err.Error(),
+		})
+	}
 }
 
 // FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
@@ -874,7 +886,13 @@
 	RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
 	gemPortIDs := make([]uint32, 0)
 	gemPortIDs = append(gemPortIDs, gemPortID)
-	RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
+	if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
+		logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
+			"intf-id": IntfID,
+			"onu-id":  onuID,
+			"err":     err.Error(),
+		})
+	}
 }
 
 // FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
@@ -886,16 +904,28 @@
 	RsrcMgr.AllocIDMgmtLock[intfID].Lock()
 	AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
 
-	RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
+	if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
 		ponrmgr.ALLOC_ID,
-		AllocIDs)
+		AllocIDs); err != nil {
+		logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
+			"intf-id": intfID,
+			"onu-id":  onuID,
+			"err":     err.Error(),
+		})
+	}
 	RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
 
 	RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
 	GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
-	RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
+	if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
 		ponrmgr.GEMPORT_ID,
-		GEMPortIDs)
+		GEMPortIDs); err != nil {
+		logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
+			"intf-id": intfID,
+			"onu-id":  onuID,
+			"err":     err.Error(),
+		})
+	}
 	RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
 
 	// Clear resource map associated with (pon_intf_id, gemport_id) tuple.
diff --git a/internal/pkg/resourcemanager/resourcemanager_test.go b/internal/pkg/resourcemanager/resourcemanager_test.go
index bef6709..e71d07e 100644
--- a/internal/pkg/resourcemanager/resourcemanager_test.go
+++ b/internal/pkg/resourcemanager/resourcemanager_test.go
@@ -27,6 +27,7 @@
 	"context"
 	"encoding/json"
 	"errors"
+	tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
 	"reflect"
 	"strconv"
 	"strings"
@@ -86,6 +87,7 @@
 
 // getResMgr mocks OpenOltResourceMgr struct.
 func getResMgr() *fields {
+	ctx := context.TODO()
 	var resMgr fields
 	resMgr.KVStore = &db.Backend{
 		Client: &MockResKVClient{},
@@ -106,15 +108,21 @@
 	ranges["gemport_id_shared"] = uint32(0)
 	ranges["flow_id_shared"] = uint32(0)
 	resMgr.NumOfPonPorts = 16
-	ponMgr := &ponrmgr.PONResourceManager{
-		DeviceID: "onu-1",
-		IntfIDs:  []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
-		KVStore: &db.Backend{
-			Client: &MockResKVClient{},
-		},
-		PonResourceRanges: ranges,
-		SharedIdxByType:   sharedIdxByType,
+	ponMgr := &ponrmgr.PONResourceManager{}
+	tpMgr, err := tp.NewTechProfile(ctx, ponMgr, "etcd", "127.0.0.1", "/")
+	if err != nil {
+		logger.Fatal(ctx, err.Error())
 	}
+
+	ponMgr.DeviceID = "onu-1"
+	ponMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
+	ponMgr.KVStore = &db.Backend{
+		Client: &MockResKVClient{},
+	}
+	ponMgr.PonResourceRanges = ranges
+	ponMgr.SharedIdxByType = sharedIdxByType
+	ponMgr.TechProfileMgr = tpMgr
+
 	var ponIntf uint32
 	for ponIntf = 0; ponIntf < resMgr.NumOfPonPorts; ponIntf++ {
 		resMgr.ResourceMgrs[ponIntf] = ponMgr