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