VOL-3856 Fix for TagType is hardcoded as DoubleTag in handleFlowWithGroup function

Support transparent VLAN at the OLT.

Change-Id: I4ecc9c2b7e01556f6b8df8a596a7aed13fa0dc26
diff --git a/VERSION b/VERSION
index 6128b08..d736b11 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.5.11
+3.5.12
diff --git a/internal/pkg/core/openolt_flowmgr.go b/internal/pkg/core/openolt_flowmgr.go
index 26973ba..119ec95 100644
--- a/internal/pkg/core/openolt_flowmgr.go
+++ b/internal/pkg/core/openolt_flowmgr.go
@@ -972,7 +972,7 @@
 }
 
 func (f *OpenOltFlowMgr) addUpstreamDataPathFlow(ctx context.Context, flowContext *flowContext) error {
-	flowContext.classifier[PacketTagType] = SingleTag
+	flowContext.classifier[PacketTagType] = SingleTag // FIXME: This hardcoding needs to be removed.
 	logger.Debugw(ctx, "adding-upstream-data-flow",
 		log.Fields{
 			"uplinkClassifier": flowContext.classifier,
@@ -985,7 +985,16 @@
 	downlinkClassifier := flowContext.classifier
 	downlinkAction := flowContext.action
 
-	downlinkClassifier[PacketTagType] = DoubleTag
+	// TODO: For now mark the PacketTagType as SingleTag when OLT is transparent to VLAN
+	// Per some deployment models, it is also possible that ONU operates on double tagged packets,
+	// so this hardcoding of SingeTag packet-tag-type may be problem for such deployment models.
+	// Need a better way for detection of packet tag type from OpenFlow message.
+	if _, ok := downlinkClassifier[VlanVid]; !ok {
+		downlinkClassifier[PacketTagType] = SingleTag
+	} else {
+		downlinkClassifier[PacketTagType] = DoubleTag
+		downlinkAction[PopVlan] = true
+	}
 	logger.Debugw(ctx, "adding-downstream-data-flow",
 		log.Fields{
 			"downlinkClassifier": downlinkClassifier,
@@ -1007,17 +1016,10 @@
 		}
 	}
 
-	/* Already this info available classifier? */
-	downlinkAction[PopVlan] = true
 	// vlan_vid is a uint32.  must be type asserted as such or conversion fails
 	dlClVid, ok := downlinkClassifier[VlanVid].(uint32)
 	if ok {
 		downlinkAction[VlanVid] = dlClVid & 0xfff
-	} else {
-		return olterrors.NewErrInvalidValue(log.Fields{
-			"reason":    "failed-to-convert-vlanid-classifier",
-			"vlan-id":   VlanVid,
-			"device-id": f.deviceHandler.device.Id}, nil).Log()
 	}
 
 	return f.addSymmetricDataPathFlow(ctx, flowContext, Downstream)
@@ -1407,10 +1409,21 @@
 			classifier.OVid = vid
 		}
 	}
+	// The classifierInfo[Metadata] carries the vlan that the OLT see when it receives packet from the ONU
 	if metadata, ok := classifierInfo[Metadata].(uint64); ok {
 		vid := uint32(metadata)
-		if vid != ReservedVlan {
-			classifier.IVid = vid
+		// Set the OVid or IVid classifier based on the whether OLT is using a transparent tag or not
+		// If OLT is using transparent tag mechanism, then it classifies whatever tag it sees to/from ONU which
+		//is OVid from the perspective of the OLT. When OLT also places or pops the outer tag, then classifierInfo[Metadata]
+		// becomes the IVid.
+		if classifier.OVid != 0 && classifier.OVid != ReservedVlan { // This is case when classifier.OVid is not set
+			if vid != ReservedVlan {
+				classifier.IVid = vid
+			}
+		} else {
+			if vid != ReservedVlan {
+				classifier.OVid = vid
+			}
 		}
 	}
 	// Use VlanPCPMask (0xff) to signify NO PCP. Else use valid PCP (0 to 7)
@@ -1465,9 +1478,13 @@
 		}
 	} else if _, ok := actionInfo[TrapToHost]; ok {
 		action.Cmd.TrapToHost = actionInfo[TrapToHost].(bool)
-	} else {
-		return nil, olterrors.NewErrInvalidValue(log.Fields{"action-command": actionInfo}, nil)
 	}
+	// When OLT is transparent to vlans no-action is valid.
+	/*
+		else {
+			return nil, olterrors.NewErrInvalidValue(log.Fields{"action-command": actionInfo}, nil)
+		}
+	*/
 	return &action, nil
 }
 
@@ -2355,7 +2372,7 @@
 
 // handleFlowWithGroup adds multicast flow to the device.
 func (f *OpenOltFlowMgr) handleFlowWithGroup(ctx context.Context, actionInfo, classifierInfo map[string]interface{}, flow *ofp.OfpFlowStats) error {
-	classifierInfo[PacketTagType] = DoubleTag
+	classifierInfo[PacketTagType] = getPacketTypeFromClassifiers(classifierInfo)
 	logger.Debugw(ctx, "add-multicast-flow", log.Fields{
 		"classifier-info": classifierInfo,
 		"actionInfo":      actionInfo})
@@ -3036,8 +3053,11 @@
 			classifierInfo[InPort] = field.GetPort()
 			logger.Debug(ctx, "field-type-in-port", log.Fields{"classifierInfo[IN_PORT]": classifierInfo[InPort].(uint32)})
 		} else if field.Type == flows.VLAN_VID {
-			classifierInfo[VlanVid] = field.GetVlanVid() & 0xfff
-			logger.Debug(ctx, "field-type-vlan-vid", log.Fields{"classifierInfo[VLAN_VID]": classifierInfo[VlanVid].(uint32)})
+			// The ReservedVlan is used to signify transparent vlan. Do not do any classification when we see ReservedVlan
+			if field.GetVlanVid() != ReservedVlan {
+				classifierInfo[VlanVid] = field.GetVlanVid() & 0xfff
+				logger.Debug(ctx, "field-type-vlan-vid", log.Fields{"classifierInfo[VLAN_VID]": classifierInfo[VlanVid].(uint32)})
+			}
 		} else if field.Type == flows.VLAN_PCP {
 			classifierInfo[VlanPcp] = field.GetVlanPcp()
 			logger.Debug(ctx, "field-type-vlan-pcp", log.Fields{"classifierInfo[VLAN_PCP]": classifierInfo[VlanPcp].(uint32)})