VOL-2437 Leftover state in etcd after OLT device deleted

Some of the kv store values were not cleared when device is deleted.

 Added APis to clear the kv values once device is deleted.

Change-Id: Id3978206dd4375dd6cb17df2f140d3a9c25cb91b
diff --git a/adaptercore/device_handler.go b/adaptercore/device_handler.go
index ee53736..3ec4d0e 100644
--- a/adaptercore/device_handler.go
+++ b/adaptercore/device_handler.go
@@ -1288,6 +1288,7 @@
 		for _, flowID := range flowIDs {
 			dh.resourceMgr.FreeFlowID(uint32(nniIntfID), -1, -1, uint32(flowID))
 		}
+		dh.resourceMgr.RemoveResourceMap(nniIntfID, int32(nniOnuID), int32(nniUniID))
 	}
 	if err = dh.resourceMgr.DelNNiFromKVStore(); err != nil {
 		log.Error("Failed to clear nni from kv store")
@@ -1321,11 +1322,19 @@
 				return err
 			}
 			for _, onu := range onuGemData {
+				onuID := make([]uint32, 1)
 				log.Debugw("onu data ", log.Fields{"onu": onu})
 				if err = dh.clearUNIData(&onu); err != nil {
 					log.Errorw("Failed to clear data for onu", log.Fields{"onu-device": onu})
 				}
+				// Clear flowids for gem cache.
+				for _, gem := range onu.GemPorts {
+					dh.resourceMgr.DeleteFlowIDsForGem(ponPort, gem)
+				}
+				onuID[0] = onu.OnuID
+				dh.resourceMgr.FreeonuID(ponPort, onuID)
 			}
+			dh.resourceMgr.DeleteIntfIDGempMapPath(ponPort)
 			onuGemData = nil
 			err = dh.resourceMgr.DelOnuGemInfoForIntf(ponPort)
 			if err != nil {
diff --git a/adaptercore/openolt_flowmgr.go b/adaptercore/openolt_flowmgr.go
index ef6dad5..3376809 100644
--- a/adaptercore/openolt_flowmgr.go
+++ b/adaptercore/openolt_flowmgr.go
@@ -1173,7 +1173,10 @@
 		f.resourceMgr.FreeFlowID(intfID, deviceFlow.OnuId, deviceFlow.UniId, deviceFlow.FlowId)
 		return false
 	}
-	f.registerFlow(logicalFlow, deviceFlow)
+	if deviceFlow.GemportId != -1 {
+		// No need to register the flow if it is a trap on nni flow.
+		f.registerFlow(logicalFlow, deviceFlow)
+	}
 	log.Debugw("Flow added to device successfully ", log.Fields{"flow": *deviceFlow})
 	return true
 }
diff --git a/adaptercore/resourcemanager/resourcemanager.go b/adaptercore/resourcemanager/resourcemanager.go
index fda1171..21f21c9 100755
--- a/adaptercore/resourcemanager/resourcemanager.go
+++ b/adaptercore/resourcemanager/resourcemanager.go
@@ -739,8 +739,6 @@
 // resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
 func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
 
-	var onuIDs []uint32
-	onuIDs = append(onuIDs, onuID)
 	IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
 
 	AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
@@ -758,14 +756,8 @@
 	RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
 		ponrmgr.FLOW_ID,
 		FlowIDs)
-	if int32(onuID) >= 0 {
-		RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
-			ponrmgr.ONU_ID,
-			onuIDs)
-	}
 	// Clear resource map associated with (pon_intf_id, gemport_id) tuple.
 	RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
-
 	// Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
 	for _, GEM := range GEMPortIDs {
 		_ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
@@ -1289,3 +1281,20 @@
 	}
 	return flowsForGem, nil
 }
+
+//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
+func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(intf uint32) {
+
+	path := fmt.Sprintf(FlowIDsForGem, intf)
+	if err := RsrcMgr.KVStore.Delete(path); err != nil {
+		log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
+		return
+	}
+	return
+}
+
+// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
+func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(intfID uint32, onuID int32, uniID int32) {
+	IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+	RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
+}