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