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