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