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