[VOL-5464] - Skip ONU device existence check on ONU discovery

Change-Id: I651ed51523c69257e46331c627257487324d9516
Signed-off-by: Sridhar Ravindra <sridhar.ravindra@radisys.com>
diff --git a/VERSION b/VERSION
index 2975971..29c87b1 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-4.5.15
+4.5.16
diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go
index 0bd4fde..a7dc890 100644
--- a/internal/pkg/config/config.go
+++ b/internal/pkg/config/config.go
@@ -61,6 +61,7 @@
 	defaultMaxBackoffRetryDelay               = 10 * time.Second
 	defaultAdapterEndpoint                    = "adapter-open-olt"
 	defaultCheckOnuDevExistenceAtOnuDiscovery = false
+	defaultForceOnuDiscIndProcessing          = false
 	defaultMaxRetries                         = 10
 )
 
@@ -101,6 +102,7 @@
 	EnableONUStats                     bool
 	EnableGEMStats                     bool
 	CheckOnuDevExistenceAtOnuDiscovery bool
+	ForceOnuDiscIndProcessing          bool
 }
 
 // NewAdapterFlags returns a new RWCore config
@@ -135,6 +137,7 @@
 		MinBackoffRetryDelay:               defaultMinBackoffRetryDelay,
 		MaxBackoffRetryDelay:               defaultMaxBackoffRetryDelay,
 		CheckOnuDevExistenceAtOnuDiscovery: defaultCheckOnuDevExistenceAtOnuDiscovery,
+		ForceOnuDiscIndProcessing:          defaultForceOnuDiscIndProcessing,
 		MaxRetries:                         defaultMaxRetries,
 	}
 	return &adapterFlags
@@ -301,6 +304,10 @@
 		"check_onu_exist_on_discovery",
 		defaultCheckOnuDevExistenceAtOnuDiscovery,
 		"Whether to check for flows only or child device before honoring discovery?")
+	flag.BoolVar(&(so.ForceOnuDiscIndProcessing),
+		"force_onu_disc_processing",
+		defaultForceOnuDiscIndProcessing,
+		"Skip the check for onu device existence on onu discovery")
 
 	flag.Parse()
 	containerName := getContainerInfo()
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index 9c82a8c..bc971a6 100755
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -1710,14 +1710,28 @@
 	return onuDev
 }
 
+// checkForResourceExistance checks for either device in rw-core or active techprofile in olt-adapter, based on the flag CheckOnuDevExistenceAtOnuDiscovery.
+// If the ONU device/resource exists in either of these, the ONU discovery will be ignored.
 func (dh *DeviceHandler) checkForResourceExistance(ctx context.Context, onuDiscInd *oop.OnuDiscIndication, sn string) (bool, error) {
-	channelID := onuDiscInd.GetIntfId()
 	parentPortNo := plt.IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
-	tpInstExists := false
-
 	// CheckOnuDevExistenceAtOnuDiscovery if true , a check will be made for the existence of the onu device. If the onu device
 	// still exists , the onu discovery will be ignored, else a check for active techprofiles for ONU is checked.
-	if !dh.openOLT.CheckOnuDevExistenceAtOnuDiscovery {
+	if dh.openOLT.CheckOnuDevExistenceAtOnuDiscovery {
+		onuDevice, _ := dh.getChildDeviceFromCore(ctx, &ca.ChildDeviceFilter{
+			ParentId:     dh.device.Id,
+			SerialNumber: sn,
+			ParentPortNo: parentPortNo,
+		})
+		if onuDevice != nil {
+			logger.Infow(ctx, "Child device still present ignoring discovery indication", log.Fields{"sn": sn})
+			return true, nil
+		}
+		logger.Infow(ctx, "No device present in core , continuing with discovery", log.Fields{"sn": sn})
+
+		return false, nil
+	} else {
+		tpInstExists := false
+		channelID := onuDiscInd.GetIntfId()
 		onuDev := dh.getChildDevice(ctx, sn, parentPortNo)
 		if onuDev != nil {
 			var onuGemInfo *rsrcMgr.OnuGemInfo
@@ -1740,19 +1754,6 @@
 		}
 		return tpInstExists, nil
 	}
-
-	onuDevice, _ := dh.getChildDeviceFromCore(ctx, &ca.ChildDeviceFilter{
-		ParentId:     dh.device.Id,
-		SerialNumber: sn,
-		ParentPortNo: parentPortNo,
-	})
-	if onuDevice != nil {
-		logger.Infow(ctx, "Child device still present ignoring discovery indication", log.Fields{"sn": sn})
-		return true, nil
-	}
-	logger.Infow(ctx, "No device present in core , continuing with discovery", log.Fields{"sn": sn})
-
-	return false, nil
 }
 
 // processDiscONULOSClear clears the LOS Alarm if it's needed
@@ -1815,14 +1816,17 @@
 
 	logger.Infow(ctx, "new-discovery-indication", log.Fields{"sn": sn})
 
-	tpInstExists, error = dh.checkForResourceExistance(ctx, onuDiscInd, sn)
-	if error != nil {
-		return error
-	}
-	if tpInstExists {
-		// ignore the discovery if tpinstance is present.
-		logger.Debugw(ctx, "ignoring-onu-indication-as-tp-already-exists", log.Fields{"sn": sn})
-		return nil
+	// Check for resource existence only if the ForceOnuDiscIndProcessing is disabled.
+	if !dh.cfg.ForceOnuDiscIndProcessing {
+		tpInstExists, error = dh.checkForResourceExistance(ctx, onuDiscInd, sn)
+		if error != nil {
+			return error
+		}
+		if tpInstExists {
+			// ignore the discovery if tpinstance is present.
+			logger.Debugw(ctx, "ignoring-onu-indication-as-tp-already-exists", log.Fields{"sn": sn})
+			return nil
+		}
 	}
 	inProcess, existing := dh.discOnus.LoadOrStore(sn, true)
 
diff --git a/internal/pkg/core/openolt.go b/internal/pkg/core/openolt.go
index c1372eb..0df5140 100644
--- a/internal/pkg/core/openolt.go
+++ b/internal/pkg/core/openolt.go
@@ -61,6 +61,7 @@
 	enableONUStats                     bool
 	enableGemStats                     bool
 	CheckOnuDevExistenceAtOnuDiscovery bool
+	ForceOnuDiscIndProcessing          bool
 }
 
 // NewOpenOLT returns a new instance of OpenOLT
@@ -85,6 +86,7 @@
 	openOLT.enableGemStats = cfg.EnableGEMStats
 	openOLT.rpcTimeout = cfg.RPCTimeout
 	openOLT.CheckOnuDevExistenceAtOnuDiscovery = cfg.CheckOnuDevExistenceAtOnuDiscovery
+	openOLT.ForceOnuDiscIndProcessing = cfg.ForceOnuDiscIndProcessing
 	return &openOLT
 }