Bug-835 - Reserve Ports should be logical ports
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / FlowRemovedTranslator.java
index df32c8a45024bb172018887c2138e65733638c94..2b628768093821334fe9e5d9a8414a2b2c222209 100644 (file)
@@ -1,20 +1,30 @@
+/**
+ * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor: deepthi.v.v@ericsson.com
+ */
 package org.opendaylight.openflowplugin.openflow.md.core.translator;
 
-import java.math.BigInteger;
-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.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
 import org.opendaylight.openflowplugin.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.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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
@@ -25,6 +35,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
@@ -63,10 +74,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry.PseudoField;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
@@ -108,13 +120,21 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpD
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 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.Map.Entry;
+import java.util.concurrent.CopyOnWriteArrayList;
+
 public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
 
     protected static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
@@ -130,7 +150,7 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
 
             SwitchFlowRemovedBuilder salFlowRemoved = new SwitchFlowRemovedBuilder();
 
-            salFlowRemoved.setCookie(ofFlow.getCookie());
+            salFlowRemoved.setCookie(new FlowCookie(ofFlow.getCookie()));
             salFlowRemoved.setPriority(ofFlow.getPriority());
 
             if (ofFlow.getTableId() != null) {
@@ -143,10 +163,24 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
             salFlowRemoved.setHardTimeout(ofFlow.getHardTimeout());
             salFlowRemoved.setPacketCount(ofFlow.getPacketCount());
             salFlowRemoved.setByteCount(ofFlow.getByteCount());
-            org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.Match ofMatch = ofFlow
+            RemovedReasonFlags removeReasonFlag = new RemovedReasonFlags(
+                    FlowRemovedReason.OFPRRDELETE.equals(ofFlow.getReason()),
+                    FlowRemovedReason.OFPRRGROUPDELETE.equals(ofFlow.getReason()),
+                    FlowRemovedReason.OFPRRHARDTIMEOUT.equals(ofFlow.getReason()),
+                    FlowRemovedReason.OFPRRIDLETIMEOUT.equals(ofFlow.getReason())
+                    );
+
+            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));
+                salFlowRemoved.setMatch(fromMatch(ofMatch, sc.getFeatures().getDatapathId(), ofVersion));
+            }
+            else if(ofFlow.getMatchV10() != null){
+                MatchBuilder matchBuilder = new MatchBuilder(MatchConvertorImpl.fromOFMatchV10ToSALMatch(ofFlow.getMatchV10(), sc.getFeatures().getDatapathId(), ofVersion));
+                salFlowRemoved.setMatch(matchBuilder.build());
             }
             salFlowRemoved.setNode(new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
                     .getDatapathId())));
@@ -154,12 +188,13 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
             return list;
         } else {
             LOG.error("Message is not a flow removed message ");
-            return null;
+            return Collections.emptyList();
         }
     }
 
-    public Match fromMatch(
-            org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.Match ofMatch) {
+
+    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;
@@ -177,16 +212,16 @@ 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(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(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(entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
+                metadata.setMetadata(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
                 if (entry.isHasMask()) {
-                    metadata.setMetadataMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                    metadata.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
                 }
                 matchBuilder.setMetadata(metadata.build());
             } else if (field.equals(EthDst.class) || field.equals(EthSrc.class) || field.equals(EthType.class)) {
@@ -197,14 +232,14 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     EthernetDestinationBuilder ethDst = new EthernetDestinationBuilder();
                     ethDst.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
                     if (entry.isHasMask()) {
-                        ethDst.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        ethDst.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
                     }
                     ethernetMatch.setEthernetDestination(ethDst.build());
                 } else if (field.equals(EthSrc.class)) {
                     EthernetSourceBuilder ethSrc = new EthernetSourceBuilder();
                     ethSrc.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
                     if (entry.isHasMask()) {
-                        ethSrc.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        ethSrc.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
                     }
                     ethernetMatch.setEthernetSource(ethSrc.build());
                 } else if (field.equals(EthType.class)) {
@@ -218,15 +253,16 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     vlanMatch = new VlanMatchBuilder();
                 }
                 if (field.equals(VlanVid.class)) {
+                    boolean vlanIdPresent = false;
                     VlanIdBuilder vlanId = new VlanIdBuilder();
                     VlanVidMatchEntry vlanVid = entry.getAugmentation(VlanVidMatchEntry.class);
-                    int vlanVidValue = vlanVid.getVlanVid() << 1;
+                    Integer vlanVidValue = vlanVid.getVlanVid();
                     if (vlanVid.isCfiBit()) {
-                        vlanVidValue = vlanVidValue | 1;
+                        vlanIdPresent = true;
                     }
-                    vlanId.setVlanId(new VlanId(vlanVidValue));
-                    if (entry.isHasMask()) {
-                        vlanId.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                    vlanId.setVlanIdPresent(vlanIdPresent);
+                    if (vlanVidValue != null) {
+                        vlanId.setVlanId(new VlanId(vlanVidValue));
                     }
                     vlanMatch.setVlanId(vlanId.build());
                 } else if (field.equals(VlanPcp.class)) {
@@ -243,9 +279,7 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                 } else if (field.equals(IpEcn.class)) {
                     ipMatch.setIpEcn(entry.getAugmentation(EcnMatchEntry.class).getEcn());
                 } else if (field.equals(IpProto.class)) {
-                    ipMatch.setIpProto(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion
-                            .forValue(entry.getAugmentation(ProtocolNumberMatchEntry.class).getProtocolNumber()
-                                    .intValue()));
+                    ipMatch.setIpProtocol(entry.getAugmentation(ProtocolNumberMatchEntry.class).getProtocolNumber());
                 }
             } else if (field.equals(TcpSrc.class) || field.equals(TcpDst.class)) {
                 if (tcpMatch == null) {
@@ -322,14 +356,14 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     ArpSourceHardwareAddressBuilder arpSha = new ArpSourceHardwareAddressBuilder();
                     arpSha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
                     if (entry.isHasMask()) {
-                        arpSha.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        arpSha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
                     }
                     arpMatch.setArpSourceHardwareAddress(arpSha.build());
                 } else if (field.equals(ArpTha.class)) {
                     ArpTargetHardwareAddressBuilder arpTha = new ArpTargetHardwareAddressBuilder();
                     arpTha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
                     if (entry.isHasMask()) {
-                        arpTha.setMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        arpTha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(),":")));
                     }
                     arpMatch.setArpTargetHardwareAddress(arpTha.build());
                 }
@@ -347,7 +381,7 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     Ipv6LabelBuilder ipv6Label = new Ipv6LabelBuilder();
                     ipv6Label.setIpv6Flabel(entry.getAugmentation(Ipv6FlabelMatchEntry.class).getIpv6Flabel());
                     if (entry.isHasMask()) {
-                        ipv6Label.setFlabelMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        ipv6Label.setFlabelMask(new Ipv6FlowLabel(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask())));
                     }
                     ipv6Match.setIpv6Label(ipv6Label.build());
                 } else if (field.equals(Ipv6NdTarget.class)) {
@@ -358,7 +392,8 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     ipv6Match.setIpv6NdTll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
                 } else if (field.equals(Ipv6Exthdr.class)) {
                     // verify
-                    PseudoField pseudoField = entry.getAugmentation(PseudoFieldMatchEntry.class).getPseudoField();
+                    Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
+                    Ipv6ExthdrFlags pseudoField = entry.getAugmentation(PseudoFieldMatchEntry.class).getPseudoField();
                     Map<Integer, Boolean> map = new HashMap<>();
                     map.put(0, pseudoField.isNonext());
                     map.put(1, pseudoField.isEsp());
@@ -370,7 +405,12 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     map.put(7, pseudoField.isUnrep());
                     map.put(8, pseudoField.isUnseq());
                     int bitmap = fillBitMaskFromMap(map);
-                    ipv6Match.setIpv6Exthdr(bitmap);
+                    ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
+                    if (entry.isHasMask()) {
+                        ipv6ExtHeaderBuilder.setIpv6ExthdrMask(
+                            ByteUtil.bytesToUnsignedShort(entry.getAugmentation(MaskMatchEntry.class).getMask()));
+                    }
+                    ipv6Match.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
                 }
             } else if (field.equals(MplsLabel.class) || field.equals(MplsTc.class) || field.equals(MplsBos.class)
                     || field.equals(PbbIsid.class)) {
@@ -388,15 +428,15 @@ public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, Lis
                     PbbBuilder pbb = new PbbBuilder();
                     pbb.setPbbIsid(entry.getAugmentation(IsidMatchEntry.class).getIsid());
                     if (entry.isHasMask()) {
-                        pbb.setPbbMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                        pbb.setPbbMask(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask()));
                     }
                     protocolMatchFields.setPbb(pbb.build());
                 }
             } else if (field.equals(TunnelId.class)) {
                 TunnelBuilder tunnel = new TunnelBuilder();
-                tunnel.setTunnelId(new BigInteger(entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
+                tunnel.setTunnelId(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
                 if (entry.isHasMask()) {
-                    tunnel.setTunnelMask(entry.getAugmentation(MaskMatchEntry.class).getMask());
+                    tunnel.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
                 }
                 matchBuilder.setTunnel(tunnel.build());
             }