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