de4fa585f83505f01af97af9637b24b29fa7d177
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / FlowRemovedTranslator.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributor: deepthi.v.v@ericsson.com
9  */
10 package org.opendaylight.openflowplugin.openflow.md.core.translator;
11
12 import java.math.BigInteger;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.CopyOnWriteArrayList;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
23 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
24 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
25 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
26 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
28 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
29 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemovedBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedReasonFlags;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.MatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntry;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Dst;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Src;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
138 import org.opendaylight.yangtools.yang.binding.DataObject;
139 import org.slf4j.Logger;
140 import org.slf4j.LoggerFactory;
141
142 public class FlowRemovedTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
143
144     protected static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
145     private static final String PREFIX_SEPARATOR = "/";
146
147     @Override
148     public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
149         if (msg instanceof FlowRemovedMessage) {
150             FlowRemovedMessage ofFlow = (FlowRemovedMessage) msg;
151             List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
152             LOG.debug("Flow Removed Message received: Table Id={}, Flow removed reason={} ", ofFlow.getTableId(),
153                     ofFlow.getReason());
154
155             SwitchFlowRemovedBuilder salFlowRemoved = new SwitchFlowRemovedBuilder();
156
157             if (ofFlow.getCookie() != null) {
158                 salFlowRemoved.setCookie(new FlowCookie(ofFlow.getCookie()));
159             }
160             salFlowRemoved.setPriority(ofFlow.getPriority());
161
162             if (ofFlow.getTableId() != null) {
163                 salFlowRemoved.setTableId(ofFlow.getTableId().getValue().shortValue());
164             }
165
166             salFlowRemoved.setDurationSec(ofFlow.getDurationSec());
167             salFlowRemoved.setDurationNsec(ofFlow.getDurationNsec());
168             salFlowRemoved.setIdleTimeout(ofFlow.getIdleTimeout());
169             salFlowRemoved.setHardTimeout(ofFlow.getHardTimeout());
170             salFlowRemoved.setPacketCount(ofFlow.getPacketCount());
171             salFlowRemoved.setByteCount(ofFlow.getByteCount());
172             RemovedReasonFlags removeReasonFlag = new RemovedReasonFlags(
173                     FlowRemovedReason.OFPRRDELETE.equals(ofFlow.getReason()),
174                     FlowRemovedReason.OFPRRGROUPDELETE.equals(ofFlow.getReason()),
175                     FlowRemovedReason.OFPRRHARDTIMEOUT.equals(ofFlow.getReason()),
176                     FlowRemovedReason.OFPRRIDLETIMEOUT.equals(ofFlow.getReason())
177             );
178
179             salFlowRemoved.setRemovedReason(removeReasonFlag);
180
181             OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());
182             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch = ofFlow
183                     .getMatch();
184             if (ofMatch != null) {
185                 salFlowRemoved.setMatch(fromMatch(ofMatch, sc.getFeatures().getDatapathId(), ofVersion));
186             } else if (ofFlow.getMatchV10() != null) {
187                 MatchBuilder matchBuilder = new MatchBuilder(MatchConvertorImpl.fromOFMatchV10ToSALMatch(ofFlow.getMatchV10(), sc.getFeatures().getDatapathId(), ofVersion));
188                 salFlowRemoved.setMatch(matchBuilder.build());
189             }
190             salFlowRemoved.setNode(new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
191                     .getDatapathId())));
192             list.add(salFlowRemoved.build());
193             return list;
194         } else {
195             LOG.error("Message is not a flow removed message ");
196             return Collections.emptyList();
197         }
198     }
199
200
201     public Match fromMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match ofMatch,
202                            BigInteger datapathid, OpenflowVersion ofVersion) {
203         MatchBuilder matchBuilder = new MatchBuilder();
204         EthernetMatchBuilder ethernetMatch = null;
205         VlanMatchBuilder vlanMatch = null;
206         IpMatchBuilder ipMatch = null;
207         TcpMatchBuilder tcpMatch = null;
208         UdpMatchBuilder udpMatch = null;
209         SctpMatchBuilder sctpMatch = null;
210         Icmpv4MatchBuilder icmpv4Match = null;
211         Icmpv6MatchBuilder icmpv6Match = null;
212         Ipv4MatchBuilder ipv4Match = null;
213         ArpMatchBuilder arpMatch = null;
214         Ipv6MatchBuilder ipv6Match = null;
215         ProtocolMatchFieldsBuilder protocolMatchFields = null;
216
217         for (MatchEntries entry : ofMatch.getMatchEntries()) {
218             Class<? extends MatchField> field = entry.getOxmMatchField();
219             if (field.equals(InPort.class)) {
220                 matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
221                         entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
222             } else if (field.equals(InPhyPort.class)) {
223                 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
224                         entry.getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().longValue(), ofVersion));
225             } else if (field.equals(Metadata.class)) {
226                 MetadataBuilder metadata = new MetadataBuilder();
227                 metadata.setMetadata(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
228                 if (entry.isHasMask()) {
229                     metadata.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
230                 }
231                 matchBuilder.setMetadata(metadata.build());
232             } else if (field.equals(EthDst.class) || field.equals(EthSrc.class) || field.equals(EthType.class)) {
233                 if (ethernetMatch == null) {
234                     ethernetMatch = new EthernetMatchBuilder();
235                 }
236                 if (field.equals(EthDst.class)) {
237                     EthernetDestinationBuilder ethDst = new EthernetDestinationBuilder();
238                     ethDst.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
239                     if (entry.isHasMask()) {
240                         ethDst.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(), ":")));
241                     }
242                     ethernetMatch.setEthernetDestination(ethDst.build());
243                 } else if (field.equals(EthSrc.class)) {
244                     EthernetSourceBuilder ethSrc = new EthernetSourceBuilder();
245                     ethSrc.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
246                     if (entry.isHasMask()) {
247                         ethSrc.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(), ":")));
248                     }
249                     ethernetMatch.setEthernetSource(ethSrc.build());
250                 } else if (field.equals(EthType.class)) {
251                     EthernetTypeBuilder ethType = new EthernetTypeBuilder();
252                     ethType.setType(new EtherType(entry.getAugmentation(EthTypeMatchEntry.class).getEthType()
253                             .getValue().longValue()));
254                     ethernetMatch.setEthernetType(ethType.build());
255                 }
256             } else if (field.equals(VlanVid.class) || field.equals(VlanPcp.class)) {
257                 if (vlanMatch == null) {
258                     vlanMatch = new VlanMatchBuilder();
259                 }
260                 if (field.equals(VlanVid.class)) {
261                     boolean vlanIdPresent = false;
262                     VlanIdBuilder vlanId = new VlanIdBuilder();
263                     VlanVidMatchEntry vlanVid = entry.getAugmentation(VlanVidMatchEntry.class);
264                     Integer vlanVidValue = vlanVid.getVlanVid();
265                     if (vlanVid.isCfiBit()) {
266                         vlanIdPresent = true;
267                     }
268                     vlanId.setVlanIdPresent(vlanIdPresent);
269                     if (vlanVidValue != null) {
270                         vlanId.setVlanId(new VlanId(vlanVidValue));
271                     }
272                     vlanMatch.setVlanId(vlanId.build());
273                 } else if (field.equals(VlanPcp.class)) {
274                     vlanMatch.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
275                             entry.getAugmentation(VlanPcpMatchEntry.class).getVlanPcp().shortValue()));
276                 }
277             } else if (field.equals(IpDscp.class) || field.equals(IpEcn.class) || field.equals(IpProto.class)) {
278                 if (ipMatch == null) {
279                     ipMatch = new IpMatchBuilder();
280                 }
281                 if (field.equals(IpDscp.class)) {
282                     ipMatch.setIpDscp(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp(
283                             entry.getAugmentation(DscpMatchEntry.class).getDscp().getValue()));
284                 } else if (field.equals(IpEcn.class)) {
285                     ipMatch.setIpEcn(entry.getAugmentation(EcnMatchEntry.class).getEcn());
286                 } else if (field.equals(IpProto.class)) {
287                     ipMatch.setIpProtocol(entry.getAugmentation(ProtocolNumberMatchEntry.class).getProtocolNumber());
288                 }
289             } else if (field.equals(TcpSrc.class) || field.equals(TcpDst.class)) {
290                 if (tcpMatch == null) {
291                     tcpMatch = new TcpMatchBuilder();
292                 }
293                 if (field.equals(TcpSrc.class)) {
294                     tcpMatch.setTcpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
295                             .getValue()));
296                 } else if (field.equals(TcpDst.class)) {
297                     tcpMatch.setTcpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
298                             .getValue()));
299                 }
300             } else if (field.equals(UdpSrc.class) || field.equals(UdpDst.class)) {
301                 if (udpMatch == null) {
302                     udpMatch = new UdpMatchBuilder();
303                 }
304                 if (field.equals(UdpSrc.class)) {
305                     udpMatch.setUdpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
306                             .getValue()));
307                 } else if (field.equals(UdpDst.class)) {
308                     udpMatch.setUdpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
309                             .getValue()));
310                 }
311             } else if (field.equals(SctpSrc.class) || field.equals(SctpDst.class)) {
312                 if (sctpMatch == null) {
313                     sctpMatch = new SctpMatchBuilder();
314                 }
315                 if (field.equals(SctpSrc.class)) {
316                     sctpMatch.setSctpSourcePort(new PortNumber(entry.getAugmentation(PortMatchEntry.class).getPort()
317                             .getValue()));
318                 } else if (field.equals(SctpDst.class)) {
319                     sctpMatch.setSctpDestinationPort(new PortNumber(entry.getAugmentation(PortMatchEntry.class)
320                             .getPort().getValue()));
321                 }
322             } else if (field.equals(Icmpv4Type.class) || field.equals(Icmpv4Code.class)) {
323                 if (icmpv4Match == null) {
324                     icmpv4Match = new Icmpv4MatchBuilder();
325                 }
326                 if (field.equals(Icmpv4Type.class)) {
327                     icmpv4Match.setIcmpv4Type(entry.getAugmentation(Icmpv4TypeMatchEntry.class).getIcmpv4Type());
328                 } else if (field.equals(Icmpv4Code.class)) {
329                     icmpv4Match.setIcmpv4Code(entry.getAugmentation(Icmpv4CodeMatchEntry.class).getIcmpv4Code());
330                 }
331             } else if (field.equals(Icmpv6Type.class) || field.equals(Icmpv6Code.class)) {
332                 if (icmpv6Match == null) {
333                     icmpv6Match = new Icmpv6MatchBuilder();
334                 }
335                 if (field.equals(Icmpv6Type.class)) {
336                     icmpv6Match.setIcmpv6Type(entry.getAugmentation(Icmpv6TypeMatchEntry.class).getIcmpv6Type());
337                 } else if (field.equals(Icmpv6Code.class)) {
338                     icmpv6Match.setIcmpv6Code(entry.getAugmentation(Icmpv6CodeMatchEntry.class).getIcmpv6Code());
339                 }
340             } else if (field.equals(Ipv4Src.class) || field.equals(Ipv4Dst.class)) {
341                 if (ipv4Match == null) {
342                     ipv4Match = new Ipv4MatchBuilder();
343                 }
344                 if (field.equals(Ipv4Src.class)) {
345                     ipv4Match.setIpv4Source(toIpv4Prefix(entry));
346                 } else if (field.equals(Ipv4Dst.class)) {
347                     ipv4Match.setIpv4Destination(toIpv4Prefix(entry));
348                 }
349             } else if (field.equals(ArpOp.class) || field.equals(ArpSpa.class) || field.equals(ArpTpa.class)
350                     || field.equals(ArpSha.class) || field.equals(ArpTha.class)) {
351                 if (arpMatch == null) {
352                     arpMatch = new ArpMatchBuilder();
353                 }
354                 if (field.equals(ArpOp.class)) {
355                     arpMatch.setArpOp(entry.getAugmentation(OpCodeMatchEntry.class).getOpCode());
356                 } else if (field.equals(ArpSpa.class)) {
357                     arpMatch.setArpSourceTransportAddress(toIpv4Prefix(entry));
358                 } else if (field.equals(ArpTpa.class)) {
359                     arpMatch.setArpTargetTransportAddress(toIpv4Prefix(entry));
360                 } else if (field.equals(ArpSha.class)) {
361                     ArpSourceHardwareAddressBuilder arpSha = new ArpSourceHardwareAddressBuilder();
362                     arpSha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
363                     if (entry.isHasMask()) {
364                         arpSha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(), ":")));
365                     }
366                     arpMatch.setArpSourceHardwareAddress(arpSha.build());
367                 } else if (field.equals(ArpTha.class)) {
368                     ArpTargetHardwareAddressBuilder arpTha = new ArpTargetHardwareAddressBuilder();
369                     arpTha.setAddress(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
370                     if (entry.isHasMask()) {
371                         arpTha.setMask(new MacAddress(ByteUtil.bytesToHexstring(entry.getAugmentation(MaskMatchEntry.class).getMask(), ":")));
372                     }
373                     arpMatch.setArpTargetHardwareAddress(arpTha.build());
374                 }
375             } else if (field.equals(TunnelIpv4Src.class) || field.equals(TunnelIpv4Dst.class)) {
376                 if (ipv4Match == null) {
377                     ipv4Match = new Ipv4MatchBuilder();
378                 }
379                 if (field.equals(TunnelIpv4Src.class)) {
380                     ipv4Match.setIpv4Source(toIpv4Prefix(entry));
381                 } else if (field.equals(TunnelIpv4Dst.class)) {
382                     ipv4Match.setIpv4Destination(toIpv4Prefix(entry));
383                 }
384             } else if (field.equals(Ipv6Src.class) || field.equals(Ipv6Dst.class) || field.equals(Ipv6Flabel.class)
385                     || field.equals(Ipv6NdTarget.class) || field.equals(Ipv6NdSll.class)
386                     || field.equals(Ipv6NdTll.class) || field.equals(Ipv6Exthdr.class)) {
387                 if (ipv6Match == null) {
388                     ipv6Match = new Ipv6MatchBuilder();
389                 }
390                 if (field.equals(Ipv6Src.class)) {
391                     ipv6Match.setIpv6Source(toIpv6Prefix(entry));
392                 } else if (field.equals(Ipv6Dst.class)) {
393                     ipv6Match.setIpv6Destination(toIpv6Prefix(entry));
394                 } else if (field.equals(Ipv6Flabel.class)) {
395                     Ipv6LabelBuilder ipv6Label = new Ipv6LabelBuilder();
396                     ipv6Label.setIpv6Flabel(entry.getAugmentation(Ipv6FlabelMatchEntry.class).getIpv6Flabel());
397                     if (entry.isHasMask()) {
398                         ipv6Label.setFlabelMask(new Ipv6FlowLabel(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask())));
399                     }
400                     ipv6Match.setIpv6Label(ipv6Label.build());
401                 } else if (field.equals(Ipv6NdTarget.class)) {
402                     ipv6Match.setIpv6NdTarget(entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address());
403                 } else if (field.equals(Ipv6NdSll.class)) {
404                     ipv6Match.setIpv6NdSll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
405                 } else if (field.equals(Ipv6NdTll.class)) {
406                     ipv6Match.setIpv6NdTll(entry.getAugmentation(MacAddressMatchEntry.class).getMacAddress());
407                 } else if (field.equals(Ipv6Exthdr.class)) {
408                     // verify
409                     Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
410                     Ipv6ExthdrFlags pseudoField = entry.getAugmentation(PseudoFieldMatchEntry.class).getPseudoField();
411                     Map<Integer, Boolean> map = new HashMap<>();
412                     map.put(0, pseudoField.isNonext());
413                     map.put(1, pseudoField.isEsp());
414                     map.put(2, pseudoField.isAuth());
415                     map.put(3, pseudoField.isDest());
416                     map.put(4, pseudoField.isFrag());
417                     map.put(5, pseudoField.isRouter());
418                     map.put(6, pseudoField.isHop());
419                     map.put(7, pseudoField.isUnrep());
420                     map.put(8, pseudoField.isUnseq());
421                     int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
422                     ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
423                     if (entry.isHasMask()) {
424                         ipv6ExtHeaderBuilder.setIpv6ExthdrMask(
425                                 ByteUtil.bytesToUnsignedShort(entry.getAugmentation(MaskMatchEntry.class).getMask()));
426                     }
427                     ipv6Match.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
428                 }
429             } else if (field.equals(MplsLabel.class) || field.equals(MplsTc.class) || field.equals(MplsBos.class)
430                     || field.equals(PbbIsid.class)) {
431                 if (protocolMatchFields == null) {
432                     protocolMatchFields = new ProtocolMatchFieldsBuilder();
433                 }
434                 if (field.equals(MplsLabel.class)) {
435                     protocolMatchFields.setMplsLabel(entry.getAugmentation(MplsLabelMatchEntry.class).getMplsLabel());
436                 } else if (field.equals(MplsTc.class)) {
437                     protocolMatchFields.setMplsTc(entry.getAugmentation(TcMatchEntry.class).getTc());
438                 } else if (field.equals(MplsBos.class)) {
439                     protocolMatchFields
440                             .setMplsBos((short) (entry.getAugmentation(BosMatchEntry.class).isBos() ? 1 : 0));
441                 } else if (field.equals(PbbIsid.class)) {
442                     PbbBuilder pbb = new PbbBuilder();
443                     pbb.setPbbIsid(entry.getAugmentation(IsidMatchEntry.class).getIsid());
444                     if (entry.isHasMask()) {
445                         pbb.setPbbMask(ByteUtil.bytesToUnsignedInt(entry.getAugmentation(MaskMatchEntry.class).getMask()));
446                     }
447                     protocolMatchFields.setPbb(pbb.build());
448                 }
449             } else if (field.equals(TunnelId.class)) {
450                 TunnelBuilder tunnel = new TunnelBuilder();
451                 tunnel.setTunnelId(new BigInteger(1, entry.getAugmentation(MetadataMatchEntry.class).getMetadata()));
452                 if (entry.isHasMask()) {
453                     tunnel.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, entry.getAugmentation(MaskMatchEntry.class).getMask()));
454                 }
455                 matchBuilder.setTunnel(tunnel.build());
456             }
457         }
458
459         AugmentTuple<Match> matchExtensionWrap =
460                 MatchExtensionHelper.processAllExtensions(
461                         ofMatch.getMatchEntries(), ofVersion, MatchPath.SWITCHFLOWREMOVED_MATCH);
462         if (matchExtensionWrap != null) {
463             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
464         }
465
466
467         if (ethernetMatch != null) {
468             matchBuilder.setEthernetMatch(ethernetMatch.build());
469         }
470         if (vlanMatch != null) {
471             matchBuilder.setVlanMatch(vlanMatch.build());
472         }
473         if (ipMatch != null) {
474             matchBuilder.setIpMatch(ipMatch.build());
475         }
476
477         if (tcpMatch != null) {
478             matchBuilder.setLayer4Match(tcpMatch.build());
479         } else if (udpMatch != null) {
480             matchBuilder.setLayer4Match(udpMatch.build());
481         } else if (sctpMatch != null) {
482             matchBuilder.setLayer4Match(sctpMatch.build());
483         }
484
485         if (icmpv4Match != null) {
486             matchBuilder.setIcmpv4Match(icmpv4Match.build());
487         } else if (icmpv6Match != null) {
488             matchBuilder.setIcmpv6Match(icmpv6Match.build());
489         }
490
491         if (ipv4Match != null) {
492             matchBuilder.setLayer3Match(ipv4Match.build());
493         } else if (arpMatch != null) {
494             matchBuilder.setLayer3Match(arpMatch.build());
495         } else if (ipv6Match != null) {
496             matchBuilder.setLayer3Match(ipv6Match.build());
497         }
498         if (protocolMatchFields != null) {
499             matchBuilder.setProtocolMatchFields(protocolMatchFields.build());
500         }
501         return matchBuilder.build();
502     }
503
504     protected Ipv4Prefix toIpv4Prefix(MatchEntries entry) {
505         String ipv4Prefix = entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().getValue();
506         if (entry.isHasMask()) {
507             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
508             ipv4Prefix = ipv4Prefix + PREFIX_SEPARATOR + countBits(mask);
509         } else {
510             ipv4Prefix += PREFIX_SEPARATOR + "32";
511         }
512         return new Ipv4Prefix(ipv4Prefix);
513     }
514
515     protected Ipv6Prefix toIpv6Prefix(MatchEntries entry) {
516         String ipv6Prefix = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();
517         if (entry.isHasMask()) {
518             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
519             ipv6Prefix = ipv6Prefix + PREFIX_SEPARATOR + countBits(mask);
520         }
521         return new Ipv6Prefix(ipv6Prefix);
522     }
523
524     private int toInt(byte b) {
525         return b < 0 ? b + 256 : b;
526     }
527
528     private int countBits(byte[] mask) {
529         int netmask = 0;
530         for (byte b : mask) {
531             netmask += Integer.bitCount(toInt(b));
532         }
533         return netmask;
534     }
535 }