VOL-4154: Changes to techprofile module for etcd storage improvements.
- using protobuf definitions of techprofile template and instance
- store smaller footprint resource instance on the kv store
- store techprofile instance in cache
- reconcile techprofile instance from resource instance on adapter restart
- retry etcd get/put/delete on failure
- remove dealing of onu-gem-info data from PONResourceManager module
  as adapter has to deal with this.

Change-Id: I741181e3f0dc5c4a419ffbed577eb4d21b73c4d6
diff --git a/pkg/ponresourcemanager/ponresourcemanager.go b/pkg/ponresourcemanager/ponresourcemanager.go
index 70ed8e6..804a6f3 100755
--- a/pkg/ponresourcemanager/ponresourcemanager.go
+++ b/pkg/ponresourcemanager/ponresourcemanager.go
@@ -25,10 +25,10 @@
 	"time"
 
 	bitmap "github.com/boljen/go-bitmap"
-	"github.com/opencord/voltha-lib-go/v4/pkg/db"
-	"github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
-	"github.com/opencord/voltha-lib-go/v4/pkg/log"
-	tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
+	"github.com/opencord/voltha-lib-go/v5/pkg/db"
+	"github.com/opencord/voltha-lib-go/v5/pkg/db/kvstore"
+	"github.com/opencord/voltha-lib-go/v5/pkg/log"
+	tp "github.com/opencord/voltha-lib-go/v5/pkg/techprofile"
 )
 
 const (
@@ -118,10 +118,6 @@
 	//Format: <device_id>/flow_id_info/<(pon_intf_id, onu_id)><flow_id>
 	FLOW_ID_INFO_PATH = FLOW_ID_INFO_PATH_PREFIX + "/{%s}/{%d}"
 
-	//path on the kvstore to store onugem info map
-	//format: <device-id>/onu_gem_info/<intfid>
-	ONU_GEM_INFO_PATH = "{%s}/onu_gem_info/{%d}" // onu_gem/<(intfid)>
-
 	//Constants for internal usage.
 	PON_INTF_ID     = "pon_intf_id"
 	START_IDX       = "start_idx"
@@ -428,6 +424,43 @@
 	return err
 }
 
+func (PONRMgr *PONResourceManager) InitDeviceResourcePoolForIntf(ctx context.Context, intfID uint32) error {
+
+	logger.Debug(ctx, "Init resource ranges for intf %d", intfID)
+
+	var err error
+
+	if err = PONRMgr.InitResourceIDPool(ctx, intfID, ONU_ID,
+		PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32),
+		PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil {
+		logger.Error(ctx, "Failed to init ONU ID resource pool")
+		return err
+	}
+
+	if err = PONRMgr.InitResourceIDPool(ctx, intfID, ALLOC_ID,
+		PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32),
+		PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil {
+		logger.Error(ctx, "Failed to init ALLOC ID resource pool ")
+		return err
+	}
+
+	if err = PONRMgr.InitResourceIDPool(ctx, intfID, GEMPORT_ID,
+		PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32),
+		PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil {
+		logger.Error(ctx, "Failed to init GEMPORT ID resource pool")
+		return err
+	}
+
+	if err = PONRMgr.InitResourceIDPool(ctx, intfID, FLOW_ID,
+		PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32),
+		PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil {
+		logger.Error(ctx, "Failed to init FLOW ID resource pool")
+		return err
+	}
+
+	return nil
+}
+
 func (PONRMgr *PONResourceManager) ClearDeviceResourcePool(ctx context.Context) error {
 
 	//Clear resource pool for all PON ports.
@@ -491,6 +524,33 @@
 	return nil
 }
 
+func (PONRMgr *PONResourceManager) ClearDeviceResourcePoolForIntf(ctx context.Context, intfID uint32) error {
+
+	logger.Debugf(ctx, "Clear resource ranges for intf %d", intfID)
+
+	if status := PONRMgr.ClearResourceIDPool(ctx, intfID, ONU_ID); !status {
+		logger.Error(ctx, "Failed to clear ONU ID resource pool")
+		return errors.New("Failed to clear ONU ID resource pool")
+	}
+
+	if status := PONRMgr.ClearResourceIDPool(ctx, intfID, ALLOC_ID); !status {
+		logger.Error(ctx, "Failed to clear ALLOC ID resource pool ")
+		return errors.New("Failed to clear ALLOC ID resource pool")
+	}
+
+	if status := PONRMgr.ClearResourceIDPool(ctx, intfID, GEMPORT_ID); !status {
+		logger.Error(ctx, "Failed to clear GEMPORT ID resource pool")
+		return errors.New("Failed to clear GEMPORT ID resource pool")
+	}
+
+	if status := PONRMgr.ClearResourceIDPool(ctx, intfID, FLOW_ID); !status {
+		logger.Error(ctx, "Failed to clear FLOW ID resource pool")
+		return errors.New("Failed to clear FLOW ID resource pool")
+	}
+
+	return nil
+}
+
 func (PONRMgr *PONResourceManager) InitResourceIDPool(ctx context.Context, Intf uint32, ResourceType string, StartID uint32, EndID uint32) error {
 
 	/*Initialize Resource ID pool for a given Resource Type on a given PON Port
@@ -1305,69 +1365,3 @@
 		return "", fmt.Errorf("unexpected-type-%T", t)
 	}
 }
-
-func (PONRMgr *PONResourceManager) AddOnuGemInfo(ctx context.Context, intfID uint32, onuGemData interface{}) error {
-	/*
-	   Update onugem info map,
-	   :param pon_intf_id: reference of PON interface id
-	   :param onuegmdata: onugem info map
-	*/
-	var Value []byte
-	var err error
-	Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID)
-	Value, err = json.Marshal(onuGemData)
-	if err != nil {
-		logger.Error(ctx, "failed to Marshal")
-		return err
-	}
-
-	if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
-		logger.Errorf(ctx, "Failed to update resource %s", Path)
-		return err
-	}
-	return err
-}
-
-func (PONRMgr *PONResourceManager) GetOnuGemInfo(ctx context.Context, IntfId uint32, onuGemInfo interface{}) error {
-	/*
-	  Get onugeminfo map from kvstore
-	  :param intfid: refremce pon intfid
-	  :param onuGemInfo: onugem info to return from kv strore.
-	*/
-	var Val []byte
-
-	path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId)
-	value, err := PONRMgr.KVStore.Get(ctx, path)
-	if err != nil {
-		logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
-		return err
-	} else if value == nil {
-		logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
-		return nil // returning nil as this could happen if there are no onus for the interface yet
-	}
-	if Val, err = kvstore.ToByte(value.Value); err != nil {
-		logger.Error(ctx, "Failed to convert to byte array")
-		return err
-	}
-
-	if err = json.Unmarshal(Val, &onuGemInfo); err != nil {
-		logger.Error(ctx, "Failed to unmarshall")
-		return err
-	}
-	logger.Debugw(ctx, "found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo})
-	return err
-}
-
-func (PONRMgr *PONResourceManager) DelOnuGemInfoForIntf(ctx context.Context, intfId uint32) error {
-	/*
-	   delete onugem info for an interface from kvstore
-	   :param intfid: refremce pon intfid
-	*/
-
-	path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId)
-	if err := PONRMgr.KVStore.Delete(ctx, path); err != nil {
-		logger.Errorf(ctx, "Falied to remove resource %s", path)
-		return err
-	}
-	return nil
-}