1ad95b3285cb6bb4ed097ddaa04abc3b3fd5e4e2
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImpl.java
1 /**
2  * Copyright (c) 2013 Ericsson. 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
10
11 import java.math.BigInteger;
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.openflowplugin.extension.api.ConverterExtensionKey;
19 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
20 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
21 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionResolvers;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
23 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
24 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
25 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
26 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowVersion;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
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.ArpTargetHardwareAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestination;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSource;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeader;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeaderBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6Label;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFields;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatch;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4Match;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.Pbb;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntry;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntryBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntryBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntryBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntryBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntryBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntryBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntryBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntryBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntryBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntryBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntryBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntryBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntryBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntryBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntryBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntryBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntryBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntryBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcpFlagMatchEntry;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntryBuilder;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmClassBase;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpFlag;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Dst;
184 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Src;
185 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
186 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
187 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
188 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
189 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
190 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
191 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
192 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
193 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralExtensionListGrouping;
194 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
195 import org.slf4j.Logger;
196 import org.slf4j.LoggerFactory;
197
198 import com.google.common.base.Optional;
199 import com.google.common.base.Splitter;
200
201 /**
202  * Utility class for converting a MD-SAL Flow into the OF flow mod
203  */
204 public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
205     private static final Logger logger = LoggerFactory.getLogger(MatchConvertorImpl.class);
206     static final String PREFIX_SEPARATOR = "/";
207     static final Splitter PREFIX_SPLITTER = Splitter.on('/');
208     private static final byte[] VLAN_VID_MASK = new byte[] { 16, 0 };
209     private static final short PROTO_TCP = 6;
210     private static final short PROTO_UDP = 17;
211     private static final String noIp = "0.0.0.0/0";
212
213     @Override
214     public List<MatchEntries> convert(
215             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
216         List<MatchEntries> matchEntriesList = new ArrayList<>();
217         if (match == null) return matchEntriesList;
218         if (match.getInPort() != null) {
219             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
220             matchEntriesList.add(toOfPort(InPort.class,
221                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPort())));
222         }
223
224         if (match.getInPhyPort() != null) {
225             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
226             matchEntriesList.add(toOfPort(InPhyPort.class,
227                     InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPhyPort())));
228         }
229
230         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata metadata = match
231                 .getMetadata();
232         if (metadata != null) {
233             matchEntriesList.add(toOfMetadata(Metadata.class, metadata.getMetadata(), metadata.getMetadataMask()));
234         }
235
236         EthernetMatch ethernetMatch = match.getEthernetMatch();
237         if (ethernetMatch != null) {
238             EthernetDestination ethernetDestination = ethernetMatch.getEthernetDestination();
239             if (ethernetDestination != null) {
240                 matchEntriesList.add(toOfMacAddress(EthDst.class, ethernetDestination.getAddress(),
241                         ethernetDestination.getMask()));
242             }
243
244             EthernetSource ethernetSource = ethernetMatch.getEthernetSource();
245             if (ethernetSource != null) {
246                 matchEntriesList
247                         .add(toOfMacAddress(EthSrc.class, ethernetSource.getAddress(), ethernetSource.getMask()));
248             }
249
250             if (ethernetMatch.getEthernetType() != null) {
251                 matchEntriesList.add(toOfEthernetType(ethernetMatch.getEthernetType()));
252             }
253         }
254
255         VlanMatch vlanMatch = match.getVlanMatch();
256         if (vlanMatch != null) {
257             if (vlanMatch.getVlanId() != null) {
258                 matchEntriesList.add(toOfVlanVid(vlanMatch.getVlanId()));
259             }
260
261             if (vlanMatch.getVlanPcp() != null) {
262                 matchEntriesList.add(toOfVlanPcp(vlanMatch.getVlanPcp()));
263             }
264         }
265
266         IpMatch ipMatch = match.getIpMatch();
267         if (ipMatch != null) {
268             if (ipMatch.getIpDscp() != null) {
269                 matchEntriesList.add(toOfIpDscp(ipMatch.getIpDscp()));
270             }
271
272             if (ipMatch.getIpEcn() != null) {
273                 matchEntriesList.add(toOfIpEcn(ipMatch.getIpEcn()));
274             }
275
276             if (ipMatch.getIpProtocol() != null) {
277                 matchEntriesList.add(toOfIpProto(ipMatch.getIpProtocol()));
278             }
279
280         }
281
282         Layer4Match layer4Match = match.getLayer4Match();
283         if (layer4Match != null) {
284             if (layer4Match instanceof TcpMatch) {
285                 TcpMatch tcpMatch = (TcpMatch) layer4Match;
286                 if (tcpMatch.getTcpSourcePort() != null) {
287                     matchEntriesList.add(toOfLayer3Port(TcpSrc.class, tcpMatch.getTcpSourcePort()));
288                 }
289
290                 if (tcpMatch.getTcpDestinationPort() != null) {
291                     matchEntriesList.add(toOfLayer3Port(TcpDst.class, tcpMatch.getTcpDestinationPort()));
292                 }
293             } else if (layer4Match instanceof UdpMatch) {
294                 UdpMatch udpMatch = (UdpMatch) layer4Match;
295                 if (udpMatch.getUdpSourcePort() != null) {
296                     matchEntriesList.add(toOfLayer3Port(UdpSrc.class, udpMatch.getUdpSourcePort()));
297                 }
298
299                 if (udpMatch.getUdpDestinationPort() != null) {
300                     matchEntriesList.add(toOfLayer3Port(UdpDst.class, udpMatch.getUdpDestinationPort()));
301                 }
302             } else if (layer4Match instanceof SctpMatch) {
303                 SctpMatch sctpMatch = (SctpMatch) layer4Match;
304                 if (sctpMatch.getSctpSourcePort() != null) {
305                     matchEntriesList.add(toOfLayer3Port(SctpSrc.class, sctpMatch.getSctpSourcePort()));
306                 }
307
308                 if (sctpMatch.getSctpDestinationPort() != null) {
309                     matchEntriesList.add(toOfLayer3Port(SctpDst.class, sctpMatch.getSctpDestinationPort()));
310                 }
311             }
312         }
313
314         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
315         if (icmpv4Match != null) {
316             if (icmpv4Match.getIcmpv4Type() != null) {
317                 matchEntriesList.add(toOfIcmpv4Type(icmpv4Match.getIcmpv4Type()));
318             }
319
320             if (icmpv4Match.getIcmpv4Code() != null) {
321                 matchEntriesList.add(toOfIcmpv4Code(icmpv4Match.getIcmpv4Code()));
322             }
323         }
324
325         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
326         if (icmpv6Match != null) {
327             if (icmpv6Match.getIcmpv6Type() != null) {
328                 matchEntriesList.add(toOfIcmpv6Type(icmpv6Match.getIcmpv6Type()));
329             }
330
331             if (icmpv6Match.getIcmpv6Code() != null) {
332                 matchEntriesList.add(toOfIcmpv6Code(icmpv6Match.getIcmpv6Code()));
333             }
334         }
335
336         Layer3Match layer3Match = match.getLayer3Match();
337         if (layer3Match != null) {
338             if (layer3Match instanceof Ipv4Match) {
339                 Ipv4Match ipv4Match = (Ipv4Match) layer3Match;
340                 if (ipv4Match.getIpv4Source() != null) {
341                     matchEntriesList.add(toOfIpv4Prefix(Ipv4Src.class, ipv4Match.getIpv4Source()));
342                 }
343                 if (ipv4Match.getIpv4Destination() != null) {
344                     matchEntriesList.add(toOfIpv4Prefix(Ipv4Dst.class, ipv4Match.getIpv4Destination()));
345                 }
346             }
347             if (layer3Match instanceof TunnelIpv4Match) {
348                 TunnelIpv4Match tunnelIpv4Src = (TunnelIpv4Match) layer3Match;
349                 if (tunnelIpv4Src.getTunnelIpv4Source() != null) {
350                     matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Src.class, tunnelIpv4Src.getTunnelIpv4Source()));
351                 }
352                 if (tunnelIpv4Src.getTunnelIpv4Destination() != null) {
353                     matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Dst.class, tunnelIpv4Src.getTunnelIpv4Destination()));
354                 }
355             } else if (layer3Match instanceof ArpMatch) {
356                 ArpMatch arpMatch = (ArpMatch) layer3Match;
357                 if (arpMatch.getArpOp() != null) {
358                     matchEntriesList.add(toOfArpOpCode(arpMatch.getArpOp()));
359                 }
360
361                 if (arpMatch.getArpSourceTransportAddress() != null) {
362                     matchEntriesList.add(toOfIpv4Prefix(ArpSpa.class, arpMatch.getArpSourceTransportAddress()));
363                 }
364
365                 if (arpMatch.getArpTargetTransportAddress() != null) {
366                     matchEntriesList.add(toOfIpv4Prefix(ArpTpa.class, arpMatch.getArpTargetTransportAddress()));
367                 }
368
369                 ArpSourceHardwareAddress arpSourceHardwareAddress = arpMatch.getArpSourceHardwareAddress();
370                 if (arpSourceHardwareAddress != null) {
371                     matchEntriesList.add(toOfMacAddress(ArpSha.class, arpSourceHardwareAddress.getAddress(),
372                             arpSourceHardwareAddress.getMask()));
373                 }
374
375                 ArpTargetHardwareAddress arpTargetHardwareAddress = arpMatch.getArpTargetHardwareAddress();
376                 if (arpTargetHardwareAddress != null) {
377                     matchEntriesList.add(toOfMacAddress(ArpTha.class, arpTargetHardwareAddress.getAddress(),
378                             arpTargetHardwareAddress.getMask()));
379                 }
380             }
381
382             else if (layer3Match instanceof Ipv6Match) {
383                 Ipv6Match ipv6Match = (Ipv6Match) layer3Match;
384                 if (ipv6Match.getIpv6Source() != null) {
385                     matchEntriesList.add(toOfIpv6Prefix(Ipv6Src.class, ipv6Match.getIpv6Source()));
386                 }
387
388                 if (ipv6Match.getIpv6Destination() != null) {
389                     matchEntriesList.add(toOfIpv6Prefix(Ipv6Dst.class, ipv6Match.getIpv6Destination()));
390                 }
391
392                 if (ipv6Match.getIpv6Label() != null) {
393                     matchEntriesList.add(toOfIpv6FlowLabel(ipv6Match.getIpv6Label()));
394                 }
395
396                 if (ipv6Match.getIpv6NdTarget() != null) {
397                     matchEntriesList.add(toOfIpv6Address(ipv6Match.getIpv6NdTarget()));
398                 }
399
400                 if (ipv6Match.getIpv6NdSll() != null) {
401                     matchEntriesList.add(toOfMacAddress(Ipv6NdSll.class, ipv6Match.getIpv6NdSll(), null));
402                 }
403
404                 if (ipv6Match.getIpv6NdTll() != null) {
405                     matchEntriesList.add(toOfMacAddress(Ipv6NdTll.class, ipv6Match.getIpv6NdTll(), null));
406                 }
407
408                 if (ipv6Match.getIpv6ExtHeader() != null) {
409                     matchEntriesList.add(toOfIpv6ExtHeader(ipv6Match.getIpv6ExtHeader()));
410                 }
411             }
412         }
413
414         ProtocolMatchFields protocolMatchFields = match.getProtocolMatchFields();
415         if (protocolMatchFields != null) {
416             if (protocolMatchFields.getMplsLabel() != null) {
417                 matchEntriesList.add(toOfMplsLabel(protocolMatchFields.getMplsLabel()));
418             }
419
420             if (protocolMatchFields.getMplsBos() != null) {
421                 matchEntriesList.add(toOfMplsBos(protocolMatchFields.getMplsBos()));
422             }
423
424             if (protocolMatchFields.getMplsTc() != null) {
425                 matchEntriesList.add(toOfMplsTc(protocolMatchFields.getMplsTc()));
426             }
427
428             if (protocolMatchFields.getPbb() != null) {
429                 matchEntriesList.add(toOfMplsPbb(protocolMatchFields.getPbb()));
430             }
431         }
432
433         //FIXME: move to extensible support
434         // TODO: Move to seperate bundle as soon as OF extensibility is supported by ofplugin/java
435         TcpFlagMatch tcpFlagMatch = match.getTcpFlagMatch();
436         if (tcpFlagMatch != null) {
437             if (tcpFlagMatch.getTcpFlag() != null) {
438                 matchEntriesList.add(NxmExtensionsConvertor.toNxmTcpFlag(tcpFlagMatch.getTcpFlag()));
439             }
440         }
441
442         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Tunnel tunnel = match
443                 .getTunnel();
444         if (tunnel != null) {
445             matchEntriesList.add(toOfMetadata(TunnelId.class, tunnel.getTunnelId(), tunnel.getTunnelMask()));
446         }
447         
448         
449         /**
450          * TODO: EXTENSION PROPOSAL (match, MD-SAL to OFJava)
451          * - we might need version for conversion and for key
452          * - sanitize NPE
453          */
454         Optional<GeneralExtensionListGrouping> extensionListOpt = ExtensionResolvers.getMatchExtensionResolver().getExtension(match);
455         if (extensionListOpt.isPresent()) {
456             for (ExtensionList extensionItem : extensionListOpt.get().getExtensionList()) {
457                 // TODO: get real version
458                 ConverterExtensionKey<? extends ExtensionKey> key = new ConverterExtensionKey<>(extensionItem.getExtensionKey(), OFConstants.OFP_VERSION_1_3);
459                 ConvertorToOFJava<MatchEntries> convertor = 
460                         OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
461                 MatchEntries ofMatch = convertor.convert(extensionItem.getExtension());
462                 matchEntriesList.add(ofMatch);
463             }
464         }
465
466         return matchEntriesList;
467     }
468
469     /**
470      * Method convert Openflow 1.0 specific flow match to MD-SAL format flow
471      * match
472      *
473      * @param match
474      * @return
475      * @author avishnoi@in.ibm.com
476      */
477     public static Match fromOFMatchV10ToSALMatch(final MatchV10 swMatch, final BigInteger datapathid,  final OpenflowVersion ofVersion) {
478         MatchBuilder matchBuilder = new MatchBuilder();
479         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
480         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
481         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
482         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
483         if (!swMatch.getWildcards().isINPORT().booleanValue() && swMatch.getInPort() != null) {
484             matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
485                     (long) swMatch.getInPort(), ofVersion));
486         }
487
488         if (!swMatch.getWildcards().isDLSRC().booleanValue() && swMatch.getDlSrc() != null) {
489             EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
490             ethSrcBuilder.setAddress(swMatch.getDlSrc());
491             ethMatchBuilder.setEthernetSource(ethSrcBuilder.build());
492             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
493         }
494         if (!swMatch.getWildcards().isDLDST().booleanValue() && swMatch.getDlDst() != null) {
495             EthernetDestinationBuilder ethDstBuilder = new EthernetDestinationBuilder();
496             ethDstBuilder.setAddress(swMatch.getDlDst());
497             ethMatchBuilder.setEthernetDestination(ethDstBuilder.build());
498             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
499         }
500         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getDlType() != null) {
501             EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
502             ethTypeBuilder.setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
503                     (long) swMatch.getDlType()));
504             ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
505             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
506         }
507         if (!swMatch.getWildcards().isDLVLAN().booleanValue() && swMatch.getDlVlan() != null) {
508             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
509             int vlanId = (swMatch.getDlVlan() == ((int) 0xffff)) ? 0 : swMatch.getDlVlan();
510             vlanIdBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
511             vlanIdBuilder.setVlanIdPresent(vlanId == 0 ? false: true);
512             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
513             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
514         }
515         if (!swMatch.getWildcards().isDLVLANPCP().booleanValue() && swMatch.getDlVlanPcp() != null) {
516             vlanMatchBuilder.setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
517                     swMatch.getDlVlanPcp()));
518             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
519         }
520         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwSrc() != null) {
521             String ipv4PrefixStr = swMatch.getNwSrc().getValue();
522             if (swMatch.getNwSrcMask() != null) {
523                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwSrcMask();
524             }
525             if (!ipv4PrefixStr.equals(noIp)) {
526                 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
527                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
528             }
529         }
530         if (!swMatch.getWildcards().isDLTYPE().booleanValue() && swMatch.getNwDst() != null) {
531             String ipv4PrefixStr = swMatch.getNwDst().getValue();
532             if (swMatch.getNwDstMask() != null) {
533                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwDstMask();
534             }
535             if (!ipv4PrefixStr.equals(noIp)) {
536                 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
537                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
538             }
539         }
540         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() != null) {
541             ipMatchBuilder.setIpProtocol(swMatch.getNwProto());
542             matchBuilder.setIpMatch(ipMatchBuilder.build());
543         }
544         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_TCP) {
545             TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
546             if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
547                 tcpMatchBuilder
548                         .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
549                                 swMatch.getTpSrc()));
550             }
551             if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
552                 tcpMatchBuilder
553                         .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
554                                 swMatch.getTpDst()));
555             }
556
557             if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
558                 matchBuilder.setLayer4Match(tcpMatchBuilder.build());
559             }
560         }
561         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_UDP) {
562             UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
563             if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
564                 udpMatchBuilder
565                         .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
566                                 swMatch.getTpSrc()));
567             }
568             if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
569                 udpMatchBuilder
570                         .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
571                                 swMatch.getTpDst()));
572             }
573
574             if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
575                 matchBuilder.setLayer4Match(udpMatchBuilder.build());
576             }
577         }
578         if (!swMatch.getWildcards().isNWTOS().booleanValue() && swMatch.getNwTos() != null) {
579             // DSCP default value is 0 from the library but controller side it
580             // is null.
581             // look if there better solution
582             if (0 != swMatch.getNwTos()) {
583                 ipMatchBuilder.setIpDscp(new Dscp(swMatch.getNwTos()));
584             }
585             matchBuilder.setIpMatch(ipMatchBuilder.build());
586         }
587
588         return matchBuilder.build();
589     }
590
591     /**
592      * Method converts Openflow 1.3+ specific flow match to MD-SAL format flow
593      * match
594      *
595      * @param match
596      * @param swMatch 
597      * @param datapathid 
598      * @param ofVersion 
599      * @return md-sal match instance
600      * @author avishnoi@in.ibm.com
601      */
602     public static MatchBuilder fromOFMatchToSALMatch(
603             final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match swMatch,
604             final BigInteger datapathid, final OpenflowVersion ofVersion) {
605         return OfMatchToSALMatchConvertor(swMatch.getMatchEntries(), datapathid, ofVersion);
606     }
607
608     private static MatchBuilder OfMatchToSALMatchConvertor(List<MatchEntries> swMatchList, final BigInteger datapathid, 
609             OpenflowVersion ofVersion){
610         
611         MatchBuilder matchBuilder = new MatchBuilder();
612         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
613         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
614         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
615         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
616         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
617         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
618         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
619         Icmpv6MatchBuilder icmpv6MatchBuilder = new Icmpv6MatchBuilder();
620         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
621         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
622         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
623         ProtocolMatchFieldsBuilder protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
624         TcpFlagMatchBuilder tcpFlagMatchBuilder = new TcpFlagMatchBuilder();
625         TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
626
627         for (MatchEntries ofMatch : swMatchList) {
628
629             if (ofMatch.getOxmMatchField().equals(InPort.class)) {
630                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
631                 if(portNumber != null){
632                     Long portNo = portNumber.getPortNumber().getValue();
633                     String logicalName = OpenflowPortsUtil.getPortLogicalName(ofVersion, portNo);
634                     matchBuilder.setInPort(new NodeConnectorId(logicalName == null? portNo.toString() : logicalName));
635                 }
636             } else if (ofMatch.getOxmMatchField().equals(InPhyPort.class)) {
637                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
638                 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
639                         portNumber.getPortNumber().getValue(), ofVersion));
640             } else if (ofMatch.getOxmMatchField().equals(Metadata.class)) {
641                 MetadataBuilder metadataBuilder = new MetadataBuilder();
642                 MetadataMatchEntry metadataMatchEntry = ofMatch.getAugmentation(MetadataMatchEntry.class);
643                 if (metadataMatchEntry != null) {
644                     metadataBuilder.setMetadata(new BigInteger(1, metadataMatchEntry.getMetadata()));
645                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
646                     if (maskMatchEntry != null) {
647                         metadataBuilder.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, maskMatchEntry
648                                 .getMask()));
649                     }
650                     matchBuilder.setMetadata(metadataBuilder.build());
651                 }
652             } else if (ofMatch.getOxmMatchField().equals(EthSrc.class)) {
653                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
654                 if (macAddressMatchEntry != null) {
655                     EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
656                     ethSourceBuilder.setAddress(macAddressMatchEntry.getMacAddress());
657                     ethMatchBuilder.setEthernetSource(ethSourceBuilder.build());
658                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
659                 }
660             } else if (ofMatch.getOxmMatchField().equals(EthDst.class)) {
661                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
662                 if (macAddressMatchEntry != null) {
663                     EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
664                     ethDestinationBuilder.setAddress(macAddressMatchEntry.getMacAddress());
665                     ethMatchBuilder.setEthernetDestination(ethDestinationBuilder.build());
666                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
667                 }
668             } else if (ofMatch.getOxmMatchField().equals(EthType.class)) {
669                 EthTypeMatchEntry ethTypeMatchEntry = ofMatch.getAugmentation(EthTypeMatchEntry.class);
670                 if (ethTypeMatchEntry != null) {
671                     EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
672                     ethTypeBuilder
673                             .setType(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType(
674                                     (long) ethTypeMatchEntry.getEthType().getValue()));
675                     ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
676                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
677                 }
678             } else if (ofMatch.getOxmMatchField().equals(VlanVid.class)) {
679                 VlanVidMatchEntry vlanVidMatchEntry = ofMatch.getAugmentation(VlanVidMatchEntry.class);
680                 if (vlanVidMatchEntry != null) {
681                     VlanIdBuilder vlanBuilder = new VlanIdBuilder();
682                     vlanBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(
683                             vlanVidMatchEntry.getVlanVid()));
684                     vlanMatchBuilder.setVlanId(vlanBuilder.build());
685                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
686                 }
687             } else if (ofMatch.getOxmMatchField().equals(VlanPcp.class)) {
688                 VlanPcpMatchEntry vlanPcpMatchEntry = ofMatch.getAugmentation(VlanPcpMatchEntry.class);
689                 if (vlanPcpMatchEntry != null) {
690                     vlanMatchBuilder
691                             .setVlanPcp(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(
692                                     vlanPcpMatchEntry.getVlanPcp()));
693                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
694                 }
695             } else if (ofMatch.getOxmMatchField().equals(IpDscp.class)) {
696                 DscpMatchEntry dscpMatchEntry = ofMatch.getAugmentation(DscpMatchEntry.class);
697                 if (dscpMatchEntry != null) {
698                     ipMatchBuilder.setIpDscp(new Dscp(dscpMatchEntry.getDscp().getValue()));
699                     matchBuilder.setIpMatch(ipMatchBuilder.build());
700                 }
701             } else if (ofMatch.getOxmMatchField().equals(IpEcn.class)) {
702                 EcnMatchEntry ecnMatchEntry = ofMatch.getAugmentation(EcnMatchEntry.class);
703                 if (ecnMatchEntry != null) {
704                     ipMatchBuilder.setIpEcn(ecnMatchEntry.getEcn());
705                     matchBuilder.setIpMatch(ipMatchBuilder.build());
706                 }
707             } else if (ofMatch.getOxmMatchField().equals(IpProto.class)) {
708                 ProtocolNumberMatchEntry protocolNumberMatchEntry = ofMatch
709                         .getAugmentation(ProtocolNumberMatchEntry.class);
710                 if (protocolNumberMatchEntry != null) {
711                     ipMatchBuilder.setIpProtocol(protocolNumberMatchEntry.getProtocolNumber());
712                     matchBuilder.setIpMatch(ipMatchBuilder.build());
713                 }
714             } else if (ofMatch.getOxmMatchField().equals(TcpSrc.class)) {
715                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
716                 if (portMatchEntry != null) {
717                     tcpMatchBuilder.setTcpSourcePort(portMatchEntry.getPort());
718                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
719                 }
720             } else if (ofMatch.getOxmMatchField().equals(TcpDst.class)) {
721                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
722                 if (portMatchEntry != null) {
723                     tcpMatchBuilder.setTcpDestinationPort(portMatchEntry.getPort());
724                     matchBuilder.setLayer4Match(tcpMatchBuilder.build());
725                 }
726             } else if (ofMatch.getOxmMatchField().equals(UdpSrc.class)) {
727                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
728                 if (portMatchEntry != null) {
729                     udpMatchBuilder.setUdpSourcePort(portMatchEntry.getPort());
730                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
731                 }
732             } else if (ofMatch.getOxmMatchField().equals(UdpDst.class)) {
733                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
734                 if (portMatchEntry != null) {
735                     udpMatchBuilder.setUdpDestinationPort(portMatchEntry.getPort());
736                     matchBuilder.setLayer4Match(udpMatchBuilder.build());
737                 }
738             } else if (ofMatch.getOxmMatchField().equals(SctpSrc.class)) {
739                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
740                 if (portMatchEntry != null) {
741                     sctpMatchBuilder.setSctpSourcePort(portMatchEntry.getPort());
742                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
743                 }
744             } else if (ofMatch.getOxmMatchField().equals(SctpDst.class)) {
745                 PortMatchEntry portMatchEntry = ofMatch.getAugmentation(PortMatchEntry.class);
746                 if (portMatchEntry != null) {
747                     sctpMatchBuilder.setSctpDestinationPort(portMatchEntry.getPort());
748                     matchBuilder.setLayer4Match(sctpMatchBuilder.build());
749                 }
750             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Type.class)) {
751                 Icmpv4TypeMatchEntry icmpv4TypeMatchEntry = ofMatch.getAugmentation(Icmpv4TypeMatchEntry.class);
752                 if (icmpv4TypeMatchEntry != null) {
753                     icmpv4MatchBuilder.setIcmpv4Type(icmpv4TypeMatchEntry.getIcmpv4Type());
754                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
755                 }
756             } else if (ofMatch.getOxmMatchField().equals(Icmpv4Code.class)) {
757                 Icmpv4CodeMatchEntry icmpv4CodeMatchEntry = ofMatch.getAugmentation(Icmpv4CodeMatchEntry.class);
758                 if (icmpv4CodeMatchEntry != null) {
759                     icmpv4MatchBuilder.setIcmpv4Code(icmpv4CodeMatchEntry.getIcmpv4Code());
760                     matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
761                 }
762             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Type.class)) {
763                 Icmpv6TypeMatchEntry icmpv6TypeMatchEntry = ofMatch.getAugmentation(Icmpv6TypeMatchEntry.class);
764                 if (icmpv6TypeMatchEntry != null) {
765                     icmpv6MatchBuilder.setIcmpv6Type(icmpv6TypeMatchEntry.getIcmpv6Type());
766                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
767                 }
768             } else if (ofMatch.getOxmMatchField().equals(Icmpv6Code.class)) {
769                 Icmpv6CodeMatchEntry icmpv6CodeMatchEntry = ofMatch.getAugmentation(Icmpv6CodeMatchEntry.class);
770                 if (icmpv6CodeMatchEntry != null) {
771                     icmpv6MatchBuilder.setIcmpv6Code(icmpv6CodeMatchEntry.getIcmpv6Code());
772                     matchBuilder.setIcmpv6Match(icmpv6MatchBuilder.build());
773                 }
774             } else if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)
775                     || ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
776                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
777                 if (ipv4AddressMatchEntry != null) {
778                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
779                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
780                     if (maskMatchEntry != null) {
781                         int receivedMask = ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
782                         int shiftCount=0;
783                         while(receivedMask != 0xffffffff){
784                             receivedMask = receivedMask >> 1;
785                             shiftCount++;
786                         }
787                         ipv4PrefixStr += PREFIX_SEPARATOR + (32-shiftCount);
788                     }else{
789                         //Openflow Spec : 1.3.2
790                         //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
791                         // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
792                         // statistics response.
793
794                         ipv4PrefixStr+=PREFIX_SEPARATOR + "32";
795                     }
796                     if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)) {
797                         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
798                     }
799                     if (ofMatch.getOxmMatchField().equals(Ipv4Dst.class)) {
800                         ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
801                     }
802                     matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
803                 }
804             }
805
806             else if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)
807                     || ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
808                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
809                 if (ipv4AddressMatchEntry != null) {
810                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
811                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
812                     if (maskMatchEntry != null) {
813                         ipv4PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
814                     }
815                     if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)) {
816                         tunnelIpv4MatchBuilder.setTunnelIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
817                     }
818                     if (ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
819                         tunnelIpv4MatchBuilder.setTunnelIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
820                     }
821                     matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
822                 }
823             }
824             else if (ofMatch.getOxmMatchField().equals(ArpOp.class)) {
825                 OpCodeMatchEntry opCodeMatchEntry = ofMatch.getAugmentation(OpCodeMatchEntry.class);
826                 if (opCodeMatchEntry != null) {
827                     arpMatchBuilder.setArpOp(opCodeMatchEntry.getOpCode());
828                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
829                 }
830             } else if (ofMatch.getOxmMatchField().equals(ArpSpa.class)
831                     || ofMatch.getOxmMatchField().equals(ArpTpa.class)) {
832                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
833                 if (ipv4AddressMatchEntry != null) {
834                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
835                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
836                     if (maskMatchEntry != null) {
837                         ipv4PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
838                     }
839                     if (ofMatch.getOxmMatchField().equals(ArpSpa.class)) {
840                         arpMatchBuilder.setArpSourceTransportAddress(new Ipv4Prefix(ipv4PrefixStr));
841                     }
842                     if (ofMatch.getOxmMatchField().equals(ArpTpa.class)) {
843                         arpMatchBuilder.setArpTargetTransportAddress(new Ipv4Prefix(ipv4PrefixStr));
844                     }
845                     matchBuilder.setLayer3Match(arpMatchBuilder.build());
846                 }
847             } else if (ofMatch.getOxmMatchField().equals(ArpSha.class)
848                     || ofMatch.getOxmMatchField().equals(ArpTha.class)) {
849                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
850                 if (macAddressMatchEntry != null) {
851                     if (ofMatch.getOxmMatchField().equals(ArpSha.class)) {
852                         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
853                         arpSourceHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
854                         MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
855                         if (maskMatchEntry != null) {
856                             arpSourceHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
857                                     .macAddressToString(maskMatchEntry.getMask())));
858                         }
859                         arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
860                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
861                     }
862                     if (ofMatch.getOxmMatchField().equals(ArpTha.class)) {
863                         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
864                         arpTargetHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
865                         MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
866                         if (maskMatchEntry != null) {
867                             arpTargetHardwareAddressBuilder.setMask(new MacAddress(ByteBufUtils
868                                     .macAddressToString(maskMatchEntry.getMask())));
869                         }
870                         arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
871                         matchBuilder.setLayer3Match(arpMatchBuilder.build());
872                     }
873                 }
874             } else if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)
875                     || ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
876                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = ofMatch.getAugmentation(Ipv6AddressMatchEntry.class);
877                 if (ipv6AddressMatchEntry != null) {
878                     String ipv6PrefixStr = ipv6AddressMatchEntry.getIpv6Address().getValue();
879                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
880                     if(maskMatchEntry != null){
881                         ipv6PrefixStr += PREFIX_SEPARATOR
882                                 + MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskMatchEntry.getMask());
883                     }
884                         
885                     if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
886                         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(ipv6PrefixStr));
887                     }
888                     if (ofMatch.getOxmMatchField().equals(Ipv6Dst.class)) {
889                         ipv6MatchBuilder.setIpv6Destination(new Ipv6Prefix(ipv6PrefixStr));
890                     }
891                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
892                 }
893             } else if (ofMatch.getOxmMatchField().equals(Ipv6Flabel.class)) {
894                 Ipv6FlabelMatchEntry ipv6FlabelMatchEntry = ofMatch.getAugmentation(Ipv6FlabelMatchEntry.class);
895                 if (ipv6FlabelMatchEntry != null) {
896                     Ipv6LabelBuilder ipv6LabelBuilder = new Ipv6LabelBuilder();
897                     ipv6LabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(ipv6FlabelMatchEntry.getIpv6Flabel()));
898                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
899                     if (maskMatchEntry != null) {
900                         ipv6LabelBuilder.setFlabelMask(new Ipv6FlowLabel(Long.valueOf(ByteUtil
901                                 .bytesToUnsignedInt(maskMatchEntry.getMask()))));
902                     }
903                     ipv6MatchBuilder.setIpv6Label(ipv6LabelBuilder.build());
904                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
905                 }
906             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTarget.class)) {
907                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = ofMatch.getAugmentation(Ipv6AddressMatchEntry.class);
908                 if (ipv6AddressMatchEntry != null) {
909                     ipv6MatchBuilder.setIpv6NdTarget(ipv6AddressMatchEntry.getIpv6Address());
910                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
911                 }
912             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdSll.class)) {
913                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
914                 if (macAddressMatchEntry != null) {
915                     ipv6MatchBuilder.setIpv6NdSll(macAddressMatchEntry.getMacAddress());
916                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
917                 }
918             } else if (ofMatch.getOxmMatchField().equals(Ipv6NdTll.class)) {
919                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
920                 if (macAddressMatchEntry != null) {
921                     ipv6MatchBuilder.setIpv6NdTll(macAddressMatchEntry.getMacAddress());
922                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
923                 }
924             } else if (ofMatch.getOxmMatchField().equals(Ipv6Exthdr.class)) {
925                 PseudoFieldMatchEntry pseudoFieldMatchEntry = ofMatch.getAugmentation(PseudoFieldMatchEntry.class);
926                 if (pseudoFieldMatchEntry != null) {
927                     Ipv6ExtHeaderBuilder ipv6ExtHeaderBuilder = new Ipv6ExtHeaderBuilder();
928
929                     Ipv6ExthdrFlags pField = pseudoFieldMatchEntry.getPseudoField();
930                     Integer bitmap = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
931
932                     ipv6ExtHeaderBuilder.setIpv6Exthdr(bitmap);
933                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
934                     if (maskMatchEntry != null) {
935                         ipv6ExtHeaderBuilder.setIpv6ExthdrMask(ByteUtil.bytesToUnsignedShort(maskMatchEntry.getMask()));
936                     }
937                     ipv6MatchBuilder.setIpv6ExtHeader(ipv6ExtHeaderBuilder.build());
938                     matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
939                 }
940             } else if (ofMatch.getOxmMatchField().equals(MplsLabel.class)) {
941                 MplsLabelMatchEntry mplsLabelMatchEntry = ofMatch.getAugmentation(MplsLabelMatchEntry.class);
942                 if (mplsLabelMatchEntry != null) {
943                     protocolMatchFieldsBuilder.setMplsLabel(mplsLabelMatchEntry.getMplsLabel());
944                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
945                 }
946             } else if (ofMatch.getOxmMatchField().equals(MplsBos.class)) {
947                 BosMatchEntry bosMatchEntry = ofMatch.getAugmentation(BosMatchEntry.class);
948                 if (bosMatchEntry != null) {
949                     protocolMatchFieldsBuilder.setMplsBos(bosMatchEntry.isBos() ? (short) 1 : (short) 0);
950                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
951                 }
952             } else if (ofMatch.getOxmMatchField().equals(MplsTc.class)) {
953                 TcMatchEntry tcMatchEntry = ofMatch.getAugmentation(TcMatchEntry.class);
954                 if (tcMatchEntry != null) {
955                     protocolMatchFieldsBuilder.setMplsTc(tcMatchEntry.getTc());
956                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
957                 }
958             } else if (ofMatch.getOxmMatchField().equals(PbbIsid.class)) {
959                 IsidMatchEntry isidMatchEntry = ofMatch.getAugmentation(IsidMatchEntry.class);
960                 if (isidMatchEntry != null) {
961                     PbbBuilder pbbBuilder = new PbbBuilder();
962                     pbbBuilder.setPbbIsid(isidMatchEntry.getIsid());
963                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
964                     if (maskMatchEntry != null) {
965                         pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedInt(maskMatchEntry.getMask()));
966                     }
967                     protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
968                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
969                 }
970             } else if (ofMatch.getOxmMatchField().equals(TunnelId.class)) {
971                 TunnelBuilder tunnelBuilder = new TunnelBuilder();
972                 MetadataMatchEntry metadataMatchEntry = ofMatch.getAugmentation(MetadataMatchEntry.class);
973                 if (metadataMatchEntry != null) {
974                     tunnelBuilder.setTunnelId(new BigInteger(1, metadataMatchEntry.getMetadata()));
975                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
976                     if (maskMatchEntry != null) {
977                         tunnelBuilder.setTunnelMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, maskMatchEntry
978                                 .getMask()));
979                     }
980                     matchBuilder.setTunnel(tunnelBuilder.build());
981                 }
982             } else if (ofMatch.getOxmMatchField().equals(TcpFlag.class)) {
983                 //FIXME: move to extensible support
984                 TcpFlagMatchEntry tcpFlagMatch = ofMatch.getAugmentation(TcpFlagMatchEntry.class);
985                 if (tcpFlagMatch != null) {
986                     tcpFlagMatchBuilder.setTcpFlag(tcpFlagMatch.getTcpFlag());
987                     matchBuilder.setTcpFlagMatch(tcpFlagMatchBuilder.build());
988                 }
989             } 
990         }
991         return matchBuilder;
992     }
993     private static MatchEntries toOfMplsPbb(final Pbb pbb) {
994         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
995         boolean hasmask = false;
996         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
997         matchEntriesBuilder.setOxmMatchField(PbbIsid.class);
998         IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();
999         isidBuilder.setIsid(pbb.getPbbIsid());
1000         matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
1001         if (pbb.getPbbMask() != null) {
1002             hasmask = true;
1003             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(pbb.getPbbMask()));
1004         }
1005         matchEntriesBuilder.setHasMask(hasmask);
1006         return matchEntriesBuilder.build();
1007     }
1008
1009     private static MatchEntries toOfMplsTc(final Short mplsTc) {
1010         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1011         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1012         matchEntriesBuilder.setHasMask(false);
1013         matchEntriesBuilder.setOxmMatchField(MplsTc.class);
1014         TcMatchEntryBuilder tcBuilder = new TcMatchEntryBuilder();
1015         tcBuilder.setTc(mplsTc);
1016         matchEntriesBuilder.addAugmentation(TcMatchEntry.class, tcBuilder.build());
1017         return matchEntriesBuilder.build();
1018     }
1019
1020     private static MatchEntries toOfMplsBos(final Short mplsBos) {
1021         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1022         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1023         matchEntriesBuilder.setHasMask(false);
1024         matchEntriesBuilder.setOxmMatchField(MplsBos.class);
1025         BosMatchEntryBuilder bosBuilder = new BosMatchEntryBuilder();
1026         if (mplsBos != 0) {
1027             bosBuilder.setBos(true);
1028         } else {
1029             bosBuilder.setBos(false);
1030         }
1031         matchEntriesBuilder.addAugmentation(BosMatchEntry.class, bosBuilder.build());
1032         return matchEntriesBuilder.build();
1033     }
1034
1035     private static MatchEntries toOfMplsLabel(final Long mplsLabel) {
1036         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1037         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1038         matchEntriesBuilder.setHasMask(false);
1039         matchEntriesBuilder.setOxmMatchField(MplsLabel.class);
1040         MplsLabelMatchEntryBuilder mplsLabelBuilder = new MplsLabelMatchEntryBuilder();
1041         mplsLabelBuilder.setMplsLabel(mplsLabel);
1042         matchEntriesBuilder.addAugmentation(MplsLabelMatchEntry.class, mplsLabelBuilder.build());
1043         return matchEntriesBuilder.build();
1044     }
1045
1046     private static MatchEntries toOfIpv6ExtHeader(final Ipv6ExtHeader ipv6ExtHeader) {
1047         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1048         boolean hasmask = false;
1049         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1050         matchEntriesBuilder.setOxmMatchField(Ipv6Exthdr.class);
1051         PseudoFieldMatchEntryBuilder pseudoBuilder = new PseudoFieldMatchEntryBuilder();
1052         Integer bitmap = ipv6ExtHeader.getIpv6Exthdr();
1053         final Boolean NONEXT = ((bitmap) & (1 << 0)) != 0;
1054         final Boolean ESP = ((bitmap) & (1 << 1)) != 0;
1055         final Boolean AUTH = ((bitmap) & (1 << 2)) != 0;
1056         final Boolean DEST = ((bitmap) & (1 << 3)) != 0;
1057         final Boolean FRAG = ((bitmap) & (1 << 4)) != 0;
1058         final Boolean ROUTER = ((bitmap) & (1 << 5)) != 0;
1059         final Boolean HOP = ((bitmap) & (1 << 6)) != 0;
1060         final Boolean UNREP = ((bitmap) & (1 << 7)) != 0;
1061         final Boolean UNSEQ = ((bitmap) & (1 << 8)) != 0;
1062         pseudoBuilder.setPseudoField(new Ipv6ExthdrFlags(AUTH, DEST, ESP, FRAG, HOP, NONEXT, ROUTER, UNREP, UNSEQ));
1063         matchEntriesBuilder.addAugmentation(PseudoFieldMatchEntry.class, pseudoBuilder.build());
1064         if (ipv6ExtHeader.getIpv6ExthdrMask() != null) {
1065             hasmask = true;
1066             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedShortToBytes(ipv6ExtHeader.getIpv6ExthdrMask()));
1067         }
1068         matchEntriesBuilder.setHasMask(hasmask);
1069         return matchEntriesBuilder.build();
1070     }
1071
1072     private static MatchEntries toOfIpv6FlowLabel(final Ipv6Label ipv6Label) {
1073         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1074         boolean hasmask = false;
1075         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1076         matchEntriesBuilder.setOxmMatchField(Ipv6Flabel.class);
1077         Ipv6FlabelMatchEntryBuilder ipv6FlabelBuilder = new Ipv6FlabelMatchEntryBuilder();
1078         ipv6FlabelBuilder.setIpv6Flabel(ipv6Label.getIpv6Flabel());
1079         matchEntriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ipv6FlabelBuilder.build());
1080         if (ipv6Label.getFlabelMask() != null) {
1081             hasmask = true;
1082             addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(ipv6Label.getFlabelMask().getValue()));
1083         }
1084         matchEntriesBuilder.setHasMask(hasmask);
1085         return matchEntriesBuilder.build();
1086     }
1087
1088     private static MatchEntries toOfPort(final Class<? extends MatchField> field, final Long portNumber) {
1089         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1090         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1091         matchEntriesBuilder.setHasMask(false);
1092         matchEntriesBuilder.setOxmMatchField(field);
1093         PortNumberMatchEntryBuilder port = new PortNumberMatchEntryBuilder();
1094         port.setPortNumber(new PortNumber(portNumber));
1095         matchEntriesBuilder.addAugmentation(PortNumberMatchEntry.class, port.build());
1096         return matchEntriesBuilder.build();
1097     }
1098
1099     private static MatchEntries toOfMetadata(final Class<? extends MatchField> field, final BigInteger metadata,
1100             final BigInteger metadataMask) {
1101         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1102         boolean hasmask = false;
1103         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1104         matchEntriesBuilder.setOxmMatchField(field);
1105         addMetadataAugmentation(matchEntriesBuilder, metadata);
1106         if (metadataMask != null) {
1107             hasmask = true;
1108             addMaskAugmentation(matchEntriesBuilder,
1109                     ByteUtil.convertBigIntegerToNBytes(metadataMask, OFConstants.SIZE_OF_LONG_IN_BYTES));
1110         }
1111         matchEntriesBuilder.setHasMask(hasmask);
1112         return matchEntriesBuilder.build();
1113     }
1114
1115     public static MatchEntries toOfMacAddress(final Class<? extends MatchField> field,
1116             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress macAddress,
1117             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress mask) {
1118         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1119         boolean hasmask = false;
1120         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1121         matchEntriesBuilder.setOxmMatchField(field);
1122         addMacAddressAugmentation(matchEntriesBuilder, macAddress);
1123         if (mask != null) {
1124             hasmask = true;
1125             addMaskAugmentation(matchEntriesBuilder, ByteBufUtils.macAddressToBytes(mask.getValue()));
1126         }
1127         matchEntriesBuilder.setHasMask(hasmask);
1128         return matchEntriesBuilder.build();
1129     }
1130
1131     private static MatchEntries toOfEthernetType(final EthernetType ethernetType) {
1132         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1133         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1134         matchEntriesBuilder.setHasMask(false);
1135         matchEntriesBuilder.setOxmMatchField(EthType.class);
1136         EthTypeMatchEntryBuilder ethertypeBuilder = new EthTypeMatchEntryBuilder();
1137         ethertypeBuilder.setEthType(new EtherType(ethernetType.getType().getValue().intValue()));
1138         matchEntriesBuilder.addAugmentation(EthTypeMatchEntry.class, ethertypeBuilder.build());
1139         return matchEntriesBuilder.build();
1140     }
1141
1142     private static MatchEntries toOfLayer3Port(final Class<? extends MatchField> field,
1143             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber) {
1144         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1145         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1146         matchEntriesBuilder.setHasMask(false);
1147         matchEntriesBuilder.setOxmMatchField(field);
1148         PortMatchEntryBuilder portBuilder = new PortMatchEntryBuilder();
1149         portBuilder.setPort(portNumber);
1150         matchEntriesBuilder.addAugmentation(PortMatchEntry.class, portBuilder.build());
1151         return matchEntriesBuilder.build();
1152     }
1153
1154     private static MatchEntries toOfIcmpv4Type(final Short icmpv4Type) {
1155         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1156         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1157         matchEntriesBuilder.setHasMask(false);
1158         matchEntriesBuilder.setOxmMatchField(Icmpv4Type.class);
1159         Icmpv4TypeMatchEntryBuilder icmpv4TypeBuilder = new Icmpv4TypeMatchEntryBuilder();
1160         icmpv4TypeBuilder.setIcmpv4Type(icmpv4Type);
1161         matchEntriesBuilder.addAugmentation(Icmpv4TypeMatchEntry.class, icmpv4TypeBuilder.build());
1162         return matchEntriesBuilder.build();
1163     }
1164
1165     private static MatchEntries toOfIcmpv4Code(final Short icmpv4Code) {
1166         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1167         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1168         matchEntriesBuilder.setHasMask(false);
1169         matchEntriesBuilder.setOxmMatchField(Icmpv4Code.class);
1170         Icmpv4CodeMatchEntryBuilder icmpv4CodeBuilder = new Icmpv4CodeMatchEntryBuilder();
1171         icmpv4CodeBuilder.setIcmpv4Code(icmpv4Code);
1172         matchEntriesBuilder.addAugmentation(Icmpv4CodeMatchEntry.class, icmpv4CodeBuilder.build());
1173         return matchEntriesBuilder.build();
1174     }
1175
1176     private static MatchEntries toOfIcmpv6Type(final Short icmpv6Type) {
1177         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1178         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1179         matchEntriesBuilder.setHasMask(false);
1180         matchEntriesBuilder.setOxmMatchField(Icmpv6Type.class);
1181         Icmpv6TypeMatchEntryBuilder icmpv6TypeBuilder = new Icmpv6TypeMatchEntryBuilder();
1182         icmpv6TypeBuilder.setIcmpv6Type(icmpv6Type);
1183         matchEntriesBuilder.addAugmentation(Icmpv6TypeMatchEntry.class, icmpv6TypeBuilder.build());
1184         return matchEntriesBuilder.build();
1185     }
1186
1187     private static MatchEntries toOfIcmpv6Code(final Short icmpv6Code) {
1188         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1189         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1190         matchEntriesBuilder.setHasMask(false);
1191         matchEntriesBuilder.setOxmMatchField(Icmpv6Code.class);
1192         Icmpv6CodeMatchEntryBuilder icmpv6CodeBuilder = new Icmpv6CodeMatchEntryBuilder();
1193         icmpv6CodeBuilder.setIcmpv6Code(icmpv6Code);
1194         matchEntriesBuilder.addAugmentation(Icmpv6CodeMatchEntry.class, icmpv6CodeBuilder.build());
1195         return matchEntriesBuilder.build();
1196     }
1197
1198     public static MatchEntries toOfIpv4Prefix(final Class<? extends MatchField> field, final Ipv4Prefix ipv4Prefix) {
1199         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1200         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1201         matchEntriesBuilder.setOxmMatchField(field);
1202         boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, ipv4Prefix);
1203         matchEntriesBuilder.setHasMask(hasMask);
1204         return matchEntriesBuilder.build();
1205     }
1206
1207     private static MatchEntries toOfIpv6Prefix(final Class<? extends MatchField> field, final Ipv6Prefix ipv6Prefix) {
1208         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1209         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1210         matchEntriesBuilder.setOxmMatchField(field);
1211         boolean hasmask = addIpv6PrefixAugmentation(matchEntriesBuilder, ipv6Prefix);
1212         matchEntriesBuilder.setHasMask(hasmask);
1213         return matchEntriesBuilder.build();
1214     }
1215
1216     public static MatchEntries toOfIpDscp(final Dscp ipDscp) {
1217         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1218         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1219         matchEntriesBuilder.setHasMask(false);
1220         matchEntriesBuilder.setOxmMatchField(IpDscp.class);
1221         DscpMatchEntryBuilder dscpBuilder = new DscpMatchEntryBuilder();
1222         dscpBuilder.setDscp(ipDscp);
1223         matchEntriesBuilder.addAugmentation(DscpMatchEntry.class, dscpBuilder.build());
1224         return matchEntriesBuilder.build();
1225     }
1226
1227     public static MatchEntries toOfVlanPcp(
1228             final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp vlanPcp) {
1229         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1230         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1231         matchEntriesBuilder.setHasMask(false);
1232         matchEntriesBuilder.setOxmMatchField(VlanPcp.class);
1233         VlanPcpMatchEntryBuilder vlanPcpBuilder = new VlanPcpMatchEntryBuilder();
1234         vlanPcpBuilder.setVlanPcp(vlanPcp.getValue());
1235         matchEntriesBuilder.addAugmentation(VlanPcpMatchEntry.class, vlanPcpBuilder.build());
1236         return matchEntriesBuilder.build();
1237     }
1238
1239     private static MatchEntries toOfVlanVid(final VlanId vlanId) {
1240         // TODO: verify
1241         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1242         boolean hasmask = false;
1243         boolean setCfiBit = false;
1244         Integer vidEntryValue = 0;
1245         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1246         matchEntriesBuilder.setOxmMatchField(VlanVid.class);
1247         VlanVidMatchEntryBuilder vlanVidBuilder = new VlanVidMatchEntryBuilder();
1248         if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
1249             setCfiBit = true;
1250             if (vlanId.getVlanId() != null) {
1251                 vidEntryValue = vlanId.getVlanId().getValue();
1252             }
1253             hasmask = (vidEntryValue == 0);
1254         }
1255         vlanVidBuilder.setCfiBit(setCfiBit);
1256         vlanVidBuilder.setVlanVid(vidEntryValue);
1257         matchEntriesBuilder.addAugmentation(VlanVidMatchEntry.class, vlanVidBuilder.build());
1258         if (hasmask) {
1259             addMaskAugmentation(matchEntriesBuilder, VLAN_VID_MASK);
1260         }
1261         matchEntriesBuilder.setHasMask(hasmask);
1262         return matchEntriesBuilder.build();
1263     }
1264
1265     private static MatchEntries toOfIpProto(final Short ipProtocol) {
1266         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1267         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1268         matchEntriesBuilder.setHasMask(false);
1269         matchEntriesBuilder.setOxmMatchField(IpProto.class);
1270         ProtocolNumberMatchEntryBuilder protoNumberBuilder = new ProtocolNumberMatchEntryBuilder();
1271         protoNumberBuilder.setProtocolNumber(ipProtocol);
1272         matchEntriesBuilder.addAugmentation(ProtocolNumberMatchEntry.class, protoNumberBuilder.build());
1273         return matchEntriesBuilder.build();
1274     }
1275
1276     private static MatchEntries toOfIpEcn(final Short ipEcn) {
1277         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1278         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1279         matchEntriesBuilder.setHasMask(false);
1280         matchEntriesBuilder.setOxmMatchField(IpEcn.class);
1281         EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder();
1282         ecnBuilder.setEcn(ipEcn);
1283         matchEntriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build());
1284         return matchEntriesBuilder.build();
1285     }
1286
1287     private static MatchEntries toOfArpOpCode(final Integer arpOp) {
1288         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1289         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1290         matchEntriesBuilder.setHasMask(false);
1291         matchEntriesBuilder.setOxmMatchField(ArpOp.class);
1292         OpCodeMatchEntryBuilder opcodeBuilder = new OpCodeMatchEntryBuilder();
1293         opcodeBuilder.setOpCode(arpOp);
1294         matchEntriesBuilder.addAugmentation(OpCodeMatchEntry.class, opcodeBuilder.build());
1295         return matchEntriesBuilder.build();
1296     }
1297
1298     private static MatchEntries toOfIpv6Address(final Ipv6Address address) {
1299         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
1300         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
1301         matchEntriesBuilder.setHasMask(false);
1302         matchEntriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
1303         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
1304         ipv6AddressBuilder.setIpv6Address(address);
1305         matchEntriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
1306         return matchEntriesBuilder.build();
1307     }
1308
1309     private static void addMaskAugmentation(final MatchEntriesBuilder builder, final byte[] mask) {
1310         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
1311         maskBuilder.setMask(mask);
1312         builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
1313     }
1314
1315     private static boolean addIpv6PrefixAugmentation(final MatchEntriesBuilder builder, final Ipv6Prefix address) {
1316         boolean hasMask = false;
1317         Iterator<String> addressParts = PREFIX_SPLITTER.split(address.getValue()).iterator();
1318         Ipv6Address ipv6Address = new Ipv6Address(addressParts.next());
1319
1320         Integer prefix = null;
1321         if (addressParts.hasNext()) {
1322             prefix = Integer.parseInt(addressParts.next());
1323         }
1324
1325         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
1326         ipv6AddressBuilder.setIpv6Address(ipv6Address);
1327         builder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
1328         if (prefix != null) {
1329             hasMask = true;
1330             addMaskAugmentation(builder, convertIpv6PrefixToByteArray(prefix));
1331         }
1332         return hasMask;
1333     }
1334
1335     private static byte[] convertIpv6PrefixToByteArray(final int prefix) {
1336         // TODO: Temporary fix. Has performance impacts.
1337         byte[] mask = new byte[16];
1338         int oneCount = prefix;
1339         for (int count = 0; count < 16; count++) {
1340             int byteBits = 0;
1341             if (oneCount >= 8) {
1342                 byteBits = 8;
1343                 oneCount = oneCount - 8;
1344             } else {
1345                 byteBits = oneCount;
1346                 oneCount = 0;
1347             }
1348
1349             mask[count] = (byte) (256 - Math.pow(2, 8 - byteBits));
1350         }
1351         return mask;
1352     }
1353
1354     private static void addMetadataAugmentation(final MatchEntriesBuilder builder, final BigInteger metadata) {
1355         MetadataMatchEntryBuilder metadataMatchEntry = new MetadataMatchEntryBuilder();
1356         metadataMatchEntry.setMetadata(ByteUtil.convertBigIntegerToNBytes(metadata, OFConstants.SIZE_OF_LONG_IN_BYTES));
1357         builder.addAugmentation(MetadataMatchEntry.class, metadataMatchEntry.build());
1358     }
1359
1360     /**
1361      * @return true if Ipv4Prefix contains prefix (and it is used in mask),
1362      *         false otherwise
1363      */
1364     private static boolean addIpv4PrefixAugmentation(final MatchEntriesBuilder builder, final Ipv4Prefix address) {
1365         boolean hasMask = false;
1366         Iterator<String> addressParts = PREFIX_SPLITTER.split(address.getValue()).iterator();
1367         Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
1368
1369         final int prefix;
1370         if (addressParts.hasNext()) {
1371             prefix = Integer.parseInt(addressParts.next());
1372         } else {
1373             prefix = 0;
1374         }
1375
1376         Ipv4AddressMatchEntryBuilder ipv4AddressBuilder = new Ipv4AddressMatchEntryBuilder();
1377         ipv4AddressBuilder.setIpv4Address(ipv4Address);
1378         builder.addAugmentation(Ipv4AddressMatchEntry.class, ipv4AddressBuilder.build());
1379         if (prefix != 0) {
1380             int mask = 0xffffffff << (32 - prefix);
1381             byte[] maskBytes = new byte[] { (byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
1382                     (byte) mask };
1383             addMaskAugmentation(builder, maskBytes);
1384             hasMask = true;
1385         }
1386         return hasMask;
1387     }
1388
1389     private static void addMacAddressAugmentation(final MatchEntriesBuilder builder, final MacAddress address) {
1390         MacAddressMatchEntryBuilder macAddress = new MacAddressMatchEntryBuilder();
1391         macAddress.setMacAddress(address);
1392         builder.addAugmentation(MacAddressMatchEntry.class, macAddress.build());
1393     }
1394
1395     /**
1396      * Method converts OF SetField action to SAL SetFiled action.
1397      *
1398      * @param action
1399      * @param ofVersion current ofp version
1400      * @return
1401      */
1402     public static SetField fromOFSetFieldToSALSetFieldAction(
1403             final Action action, OpenflowVersion ofVersion) {
1404         logger.debug("Converting OF SetField action to SAL SetField action");
1405         SetFieldBuilder setField = new SetFieldBuilder();
1406         OxmFieldsAction oxmFields = action.getAugmentation(OxmFieldsAction.class);
1407         MatchBuilder match = OfMatchToSALMatchConvertor(oxmFields.getMatchEntries(), null, ofVersion);
1408         setField.fieldsFrom(match.build());
1409         return setField.build();
1410     }
1411 }