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