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