Bug 1764 - moved Session related interfaces to openflowplugin-api
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / FlowRemovedTranslator.java
index 3bf76a1773c3d77f42b443f4d247a7b9a7fff893..8f3a29d585b5cc7f357187325087cf490ad89807 100644 (file)
@@ -9,27 +9,25 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.translator;
 
-import java.math.BigInteger;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-import org.opendaylight.openflowplugin.openflow.md.OFConstants;
-import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
+import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
+import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemovedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedReasonFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.MatchBuilder;
@@ -122,6 +120,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Sctp
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Dst;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Src;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
@@ -133,6 +133,13 @@ import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.math.BigInteger;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+
 public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
 
     protected static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
@@ -148,7 +155,9 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
 
             SwitchFlowRemovedBuilder salFlowRemoved = new SwitchFlowRemovedBuilder();
 
-            salFlowRemoved.setCookie(ofFlow.getCookie());
+            if (ofFlow.getCookie() != null) {
+                salFlowRemoved.setCookie(new FlowCookie(ofFlow.getCookie()));
+            }
             salFlowRemoved.setPriority(ofFlow.getPriority());
 
             if (ofFlow.getTableId() != null) {
@@ -170,13 +179,14 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
 
             salFlowRemoved.setRemovedReason(removeReasonFlag);
 
+            OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());
             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch = ofFlow
                     .getMatch();
             if (ofMatch != null) {
-                salFlowRemoved.setMatch(fromMatch(ofMatch,sc.getFeatures().getDatapathId()));
+                salFlowRemoved.setMatch(fromMatch(ofMatch, sc.getFeatures().getDatapathId(), ofVersion));
             }
             else if(ofFlow.getMatchV10() != null){
-                MatchBuilder matchBuilder = new MatchBuilder(MatchConvertorImpl.fromOFMatchV10ToSALMatch(ofFlow.getMatchV10(),sc.getFeatures().getDatapathId()));
+                MatchBuilder matchBuilder = new MatchBuilder(MatchConvertorImpl.fromOFMatchV10ToSALMatch(ofFlow.getMatchV10(), sc.getFeatures().getDatapathId(), ofVersion));
                 salFlowRemoved.setMatch(matchBuilder.build());
             }
             salFlowRemoved.setNode(new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
@@ -190,8 +200,8 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
     }
 
 
-    public Match fromMatch(
-            org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch,BigInteger datapathid) {
+    public Match fromMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch,
+            BigInteger datapathid, OpenflowVersion ofVersion) {
         MatchBuilder matchBuilder = new MatchBuilder();
         EthernetMatchBuilder ethernetMatch = null;
         VlanMatchBuilder vlanMatch = null;
@@ -209,11 +219,11 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
         for (MatchEntries entry : ofMatch.getMatchEntries()) {
             Class<? extends MatchField> field = entry.getOxmMatchField();
             if (field.equals(InPort.class)) {
-                matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue()
-                        .longValue()));
+                matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
+                        entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
             } else if (field.equals(InPhyPort.class)) {
-                matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue()
-                        .longValue()));
+                matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
+                        entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
             } else if (field.equals(Metadata.class)) {
                 MetadataBuilder metadata = new MetadataBuilder();
                 metadata.setMetadata(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
@@ -364,6 +374,15 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     }
                     arpMatch.setArpTargetHardwareAddress(arpTha.build());
                 }
+            } else if (field.equals(TunnelIpv4Src.class) || field.equals(TunnelIpv4Dst.class)) {
+                if (ipv4Match == null) {
+                    ipv4Match = new Ipv4MatchBuilder();
+                }
+                if (field.equals(TunnelIpv4Src.class)) {
+                    ipv4Match.setIpv4Source(toIpv4Prefix(entry));
+                } else if (field.equals(TunnelIpv4Dst.class)) {
+                    ipv4Match.setIpv4Destination(toIpv4Prefix(entry));
+                }
             } else if (field.equals(Ipv6Src.class) || field.equals(Ipv6Dst.class) || field.equals(Ipv6Flabel.class)
                     || field.equals(Ipv6NdTarget.class) || field.equals(Ipv6NdSll.class)
                     || field.equals(Ipv6NdTll.class) || field.equals(Ipv6Exthdr.class)) {
@@ -401,7 +420,7 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     map.put(6, pseudoField.isHop());
                     map.put(7, pseudoField.isUnrep());
                     map.put(8, pseudoField.isUnseq());
-                    int bitmap = fillBitMaskFromMap(map);
+                    int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
                     ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
                     if (entry.isHasMask()) {
                         ipv6ExtHeaderBuilder.setIpv6ExthdrMask(
@@ -436,9 +455,17 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     tunnel.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
                 }
                 matchBuilder.setTunnel(tunnel.build());
-            }
+            } 
         }
-
+        
+        AugmentTuple<Match> matchExtensionWrap = 
+                MatchExtensionHelper.processAllExtensions(
+                        ofMatch.getMatchEntries(), ofVersion, MatchPath.SWITCHFLOWREMOVED_MATCH);
+        if (matchExtensionWrap != null) {
+            matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
+        }
+        
+        
         if (ethernetMatch != null) {
             matchBuilder.setEthernetMatch(ethernetMatch.build());
         }
@@ -476,26 +503,8 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
         return matchBuilder.build();
     }
 
-    /**
-     * Fills the bitmask from boolean map where key is bit position
-     *
-     * @param booleanMap
-     *            bit to boolean mapping
-     * @return bit mask
-     */
-    private int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
-        int bitmask = 0;
-
-        for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
-            if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
-                bitmask |= 1 << iterator.getKey();
-            }
-        }
-        return bitmask;
-    }
-
-    private Ipv4Prefix toIpv4Prefix(MatchEntries entry) {
-        String ipv4Prefix = entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().toString();
+    protected Ipv4Prefix toIpv4Prefix(MatchEntries entry) {
+        String ipv4Prefix = entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().getValue();
         if (entry.isHasMask()) {
             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
             ipv4Prefix = ipv4Prefix + PREFIX_SEPARATOR + countBits(mask);
@@ -503,8 +512,8 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
         return new Ipv4Prefix(ipv4Prefix);
     }
 
-    private Ipv6Prefix toIpv6Prefix(MatchEntries entry) {
-        String ipv6Prefix = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().toString();
+    protected Ipv6Prefix toIpv6Prefix(MatchEntries entry) {
+        String ipv6Prefix = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();
         if (entry.isHasMask()) {
             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
             ipv6Prefix = ipv6Prefix + PREFIX_SEPARATOR + countBits(mask);