For OutputAction:PortNumber size is different for OF versions.
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowConvertor.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;
10
11 import java.math.BigInteger;
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetField;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActions;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTable;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.Meter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActions;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadata;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
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.EthernetSource;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFields;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntryBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntry;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DscpMatchEntryBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntryBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntryBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntry;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4CodeMatchEntryBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntry;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv4TypeMatchEntryBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntryBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6TypeMatchEntryBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntryBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntryBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntryBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntryBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntry;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsLabelMatchEntryBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntry;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OpCodeMatchEntryBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntry;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortMatchEntryBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntryBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry.PseudoField;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntryBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntryBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntryBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MatchTypeBase;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpOp;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSha;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTha;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpTpa;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthDst;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthSrc;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Code;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv4Type;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Type;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpDscp;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Dst;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdSll;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTll;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpSrc;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpDst;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntriesBuilder;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.MatchBuilder;
161 import org.slf4j.Logger;
162 import org.slf4j.LoggerFactory;
163
164 /**
165  * Utility class for converting a MD-SAL Flow into the OF flow mod
166  */
167 public class FlowConvertor {
168     private static final Logger logger = LoggerFactory.getLogger(FlowConvertor.class);
169     private static final String PREFIX_SEPARATOR = "/";
170
171     // Default values for when things are null
172     private static final  BigInteger DEFAULT_COOKIE = BigInteger.ZERO; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
173     private static final BigInteger DEFAULT_COOKIE_MASK = BigInteger.ZERO; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
174     private static final TableId DEFAULT_TABLE_ID = new TableId(new Long(0)); // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
175     private static final Integer DEFAULT_IDLE_TIMEOUT = new Integer(5*60); // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
176     private static final Integer DEFAULT_HARD_TIMEOUT = new Integer(10*60); // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
177     private static final Integer DEFAULT_PRIORITY = new Integer(100);  // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
178     private static final Long DEFAULT_BUFFER_ID = Long.parseLong("ffffffff", 16);  // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
179     private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
180     private static final Long DEFAULT_OUT_PORT = OFPP_ANY; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
181     private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
182     private static final Long DEFAULT_OUT_GROUP = OFPG_ANY; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
183     private static final boolean DEFAULT_OFPFF_FLOW_REM = true; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
184     private static final boolean DEFAULT_OFPFF_CHECK_OVERLAP = false; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
185     private static final boolean DEFAULT_OFPFF_RESET_COUNTS = false; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
186     private static final boolean DEFAULT_OFPFF_NO_PKT_COUNTS = false; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
187     private static final boolean DEFAULT_OFPFF_NO_BYT_COUNTS = false; // TODO: Someone check me, I have no idea if this is a good default - eaw@cisco.com
188     private static final Class<? extends MatchTypeBase> DEFAULT_MATCH_TYPE = OxmMatchType.class;
189
190     public static FlowModInput toFlowModInput(Flow flow, short version) {
191         FlowModInputBuilder flowMod = new FlowModInputBuilder();
192         if(flow.getCookie() != null){
193             flowMod.setCookie(flow.getCookie());
194         } else {
195             flowMod.setCookie(DEFAULT_COOKIE);
196         }
197
198         if (flow.getCookieMask() != null) {
199             flowMod.setCookieMask(new BigInteger(flow.getCookieMask().toString()));
200         } else {
201             flowMod.setCookieMask(DEFAULT_COOKIE_MASK);
202         }
203
204         if (flow.getTableId() != null) {
205             flowMod.setTableId(new TableId(flow.getTableId().longValue()));
206         } else {
207             flowMod.setTableId(DEFAULT_TABLE_ID);
208         }
209
210         if (flow instanceof AddFlowInput) {
211             flowMod.setCommand(FlowModCommand.OFPFCADD);
212         } else if (flow instanceof RemoveFlowInput) {
213             if (flow.isStrict()) {
214                 flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
215             } else {
216                 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
217             }
218         } else if (flow instanceof UpdateFlowInput) {
219             if (flow.isStrict()) {
220                 flowMod.setCommand(FlowModCommand.OFPFCMODIFYSTRICT);
221             } else {
222                 flowMod.setCommand(FlowModCommand.OFPFCMODIFY);
223             }
224         }
225         if(flow.getIdleTimeout() != null) {
226             flowMod.setIdleTimeout(flow.getIdleTimeout());
227         } else {
228             flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
229         }
230         if(flow.getHardTimeout() != null) {
231             flowMod.setHardTimeout(flow.getHardTimeout());
232         } else {
233             flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
234         }
235         if(flow.getPriority() != null) {
236             flowMod.setPriority(flow.getPriority());
237         } else {
238             flowMod.setPriority(DEFAULT_PRIORITY);
239         }
240         if(flow.getBufferId() != null ) {
241             flowMod.setBufferId(flow.getBufferId());
242         } else {
243             flowMod.setBufferId(DEFAULT_BUFFER_ID);
244         }
245
246         if (flow.getOutPort() != null) {
247             flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
248         } else {
249             flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
250         }
251         if(flow.getOutGroup() != null) {
252             flowMod.setOutGroup(flow.getOutGroup());
253         } else {
254             flowMod.setOutGroup(DEFAULT_OUT_GROUP);
255         }
256
257         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags flowModFlags = flow.getFlags();
258         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags ofFlowModFlags = null;
259         if (flowModFlags != null) {
260             ofFlowModFlags = new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags(
261                     flowModFlags.isCHECKOVERLAP(), flowModFlags.isNOBYTCOUNTS(), flowModFlags.isNOPKTCOUNTS(),
262                     flowModFlags.isRESETCOUNTS(), flowModFlags.isSENDFLOWREM());
263         } else {
264             ofFlowModFlags = new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags(
265                     DEFAULT_OFPFF_CHECK_OVERLAP,DEFAULT_OFPFF_NO_BYT_COUNTS,DEFAULT_OFPFF_NO_PKT_COUNTS,
266                     DEFAULT_OFPFF_RESET_COUNTS,DEFAULT_OFPFF_FLOW_REM);
267         }
268         flowMod.setFlags(ofFlowModFlags);
269
270         if (flow.getMatch() != null) {
271             MatchBuilder matchBuilder = new MatchBuilder();
272             matchBuilder.setMatchEntries(toMatch(flow.getMatch()));
273             matchBuilder.setType(DEFAULT_MATCH_TYPE);
274             flowMod.setMatch(matchBuilder.build());
275         }
276
277         if (flow.getInstructions() != null) {
278             flowMod.setInstructions(toInstructions(flow.getInstructions(), version));
279         }
280         flowMod.setVersion(version);
281         return flowMod.build();
282     }
283
284     public static List<MatchEntries> toMatch(
285             org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match) {
286
287         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
288         List<MatchEntries> matchEntriesList = new ArrayList<>();
289
290         if (match.getInPort() != null) {
291             matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
292             matchEntriesBuilder.setHasMask(false);
293             matchEntriesBuilder.setOxmMatchField(InPort.class);
294             PortNumberMatchEntryBuilder port = new PortNumberMatchEntryBuilder();
295             port.setPortNumber(new PortNumber(match.getInPort()));
296             matchEntriesBuilder.addAugmentation(PortNumberMatchEntry.class, port.build());
297             matchEntriesList.add(matchEntriesBuilder.build());
298         }
299
300         if (match.getInPhyPort() != null) {
301             matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
302             matchEntriesBuilder.setHasMask(false);
303             matchEntriesBuilder.setOxmMatchField(InPhyPort.class);
304             PortNumberMatchEntryBuilder phyPort = new PortNumberMatchEntryBuilder();
305             phyPort.setPortNumber(new PortNumber(match.getInPhyPort()));
306             matchEntriesBuilder.addAugmentation(PortNumberMatchEntry.class, phyPort.build());
307             matchEntriesList.add(matchEntriesBuilder.build());
308         }
309
310         if (match.getMetadata() != null) {
311             boolean hasmask = false;
312             matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
313             matchEntriesBuilder.setOxmMatchField(Metadata.class);
314             addMetadataAugmentation(matchEntriesBuilder, match.getMetadata().getMetadata());
315             if (match.getMetadata().getMetadataMask() != null) {
316                 hasmask = true;
317                 addMaskAugmentation(matchEntriesBuilder, match.getMetadata().getMetadataMask());
318             }
319             matchEntriesBuilder.setHasMask(hasmask);
320             matchEntriesList.add(matchEntriesBuilder.build());
321         }
322
323         if (match.getEthernetMatch() != null) {
324             EthernetMatch ethernetMatch = match.getEthernetMatch();
325             EthernetDestination ethernetDestination = ethernetMatch.getEthernetDestination();
326             if (ethernetDestination != null) {
327                 boolean hasmask = false;
328                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
329                 matchEntriesBuilder.setOxmMatchField(EthDst.class);
330                 addMacAddressAugmentation(matchEntriesBuilder, ethernetDestination.getAddress());
331                 if (ethernetDestination.getMask() != null) {
332                     hasmask = true;
333                     addMaskAugmentation(matchEntriesBuilder, ethernetDestination.getMask());
334                 }
335                 matchEntriesBuilder.setHasMask(hasmask);
336                 matchEntriesList.add(matchEntriesBuilder.build());
337             }
338
339             EthernetSource ethernetSource = ethernetMatch.getEthernetSource();
340             if (ethernetSource != null) {
341                 boolean hasmask = false;
342                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
343                 matchEntriesBuilder.setOxmMatchField(EthSrc.class);
344                 addMacAddressAugmentation(matchEntriesBuilder, ethernetSource.getAddress());
345                 if (ethernetSource.getMask() != null) {
346                     hasmask = true;
347                     addMaskAugmentation(matchEntriesBuilder, ethernetSource.getMask());
348                 }
349                 matchEntriesBuilder.setHasMask(hasmask);
350                 matchEntriesList.add(matchEntriesBuilder.build());
351             }
352
353             if (ethernetMatch.getEthernetType() != null) {
354                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
355                 matchEntriesBuilder.setHasMask(false);
356                 matchEntriesBuilder.setOxmMatchField(EthType.class);
357                 EthTypeMatchEntryBuilder ethertypeBuilder = new EthTypeMatchEntryBuilder();
358                 ethertypeBuilder.setEthType(new EtherType(ethernetMatch.getEthernetType().getType().getValue()
359                         .intValue()));
360                 matchEntriesBuilder.addAugmentation(EthTypeMatchEntry.class, ethertypeBuilder.build());
361                 matchEntriesList.add(matchEntriesBuilder.build());
362             }
363         }
364
365         VlanMatch vlanMatch = match.getVlanMatch();
366         if (vlanMatch != null) {
367             if (vlanMatch.getVlanId() != null) {
368                 // verify
369                 boolean hasmask = false;
370                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
371                 matchEntriesBuilder.setOxmMatchField(VlanVid.class);
372                 VlanVidMatchEntryBuilder vlanVidBuilder = new VlanVidMatchEntryBuilder();
373                 Integer vidEntryValue = vlanMatch.getVlanId().getVlanId().getValue();
374                 vlanVidBuilder.setCfiBit(vidEntryValue != 0);
375                 vlanVidBuilder.setVlanVid(vidEntryValue);
376                 matchEntriesBuilder.addAugmentation(VlanVidMatchEntry.class, vlanVidBuilder.build());
377                 if (vlanMatch.getVlanId().getMask() != null) {
378                     hasmask = true;
379                     addMaskAugmentation(matchEntriesBuilder, vlanMatch.getVlanId().getMask());
380                 }
381                 matchEntriesBuilder.setHasMask(hasmask);
382                 matchEntriesList.add(matchEntriesBuilder.build());
383             }
384
385             if (vlanMatch.getVlanPcp() != null) {
386                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
387                 matchEntriesBuilder.setHasMask(false);
388                 matchEntriesBuilder.setOxmMatchField(VlanPcp.class);
389                 VlanPcpMatchEntryBuilder vlanPcpBuilder = new VlanPcpMatchEntryBuilder();
390                 vlanPcpBuilder.setVlanPcp(vlanMatch.getVlanPcp().getValue());
391                 matchEntriesBuilder.addAugmentation(VlanPcpMatchEntry.class, vlanPcpBuilder.build());
392                 matchEntriesList.add(matchEntriesBuilder.build());
393             }
394         }
395
396         IpMatch ipMatch = match.getIpMatch();
397         if (ipMatch != null) {
398             if (ipMatch.getIpDscp() != null) {
399                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
400                 matchEntriesBuilder.setHasMask(false);
401                 matchEntriesBuilder.setOxmMatchField(IpDscp.class);
402                 DscpMatchEntryBuilder dscpBuilder = new DscpMatchEntryBuilder();
403                 dscpBuilder.setDscp(ipMatch.getIpDscp());
404                 matchEntriesBuilder.addAugmentation(DscpMatchEntry.class, dscpBuilder.build());
405                 matchEntriesList.add(matchEntriesBuilder.build());
406             }
407
408             if (ipMatch.getIpEcn() != null) {
409                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
410                 matchEntriesBuilder.setHasMask(false);
411                 matchEntriesBuilder.setOxmMatchField(IpDscp.class);
412                 EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder();
413                 ecnBuilder.setEcn(ipMatch.getIpEcn());
414                 matchEntriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build());
415                 matchEntriesList.add(matchEntriesBuilder.build());
416             }
417
418             if (ipMatch.getIpProto() != null) {
419                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
420                 matchEntriesBuilder.setHasMask(false);
421                 matchEntriesBuilder.setOxmMatchField(IpProto.class);
422                 ProtocolNumberMatchEntryBuilder protoNumberBuilder = new ProtocolNumberMatchEntryBuilder();
423                 protoNumberBuilder.setProtocolNumber((short) ipMatch.getIpProto().getIntValue());
424                 matchEntriesBuilder.addAugmentation(ProtocolNumberMatchEntry.class, protoNumberBuilder.build());
425                 matchEntriesList.add(matchEntriesBuilder.build());
426             }
427
428         }
429
430         Layer4Match layer4Match = match.getLayer4Match();
431         if (layer4Match != null) {
432             if (layer4Match instanceof TcpMatch) {
433                 TcpMatch tcpMatch = (TcpMatch) layer4Match;
434                 if (tcpMatch.getTcpSourcePort() != null) {
435                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
436                     matchEntriesBuilder.setHasMask(false);
437                     matchEntriesBuilder.setOxmMatchField(TcpSrc.class);
438                     addPortAugmentation(matchEntriesBuilder, tcpMatch.getTcpSourcePort());
439                     matchEntriesList.add(matchEntriesBuilder.build());
440                 }
441
442                 if (tcpMatch.getTcpDestinationPort() != null) {
443                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
444                     matchEntriesBuilder.setHasMask(false);
445                     matchEntriesBuilder.setOxmMatchField(TcpDst.class);
446                     addPortAugmentation(matchEntriesBuilder, tcpMatch.getTcpDestinationPort());
447                     matchEntriesList.add(matchEntriesBuilder.build());
448                 }
449             }
450
451             else if (layer4Match instanceof UdpMatch) {
452                 UdpMatch udpMatch = (UdpMatch) layer4Match;
453                 if (udpMatch.getUdpSourcePort() != null) {
454                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
455                     matchEntriesBuilder.setHasMask(false);
456                     matchEntriesBuilder.setOxmMatchField(UdpSrc.class);
457                     addPortAugmentation(matchEntriesBuilder, udpMatch.getUdpSourcePort());
458                     matchEntriesList.add(matchEntriesBuilder.build());
459                 }
460
461                 if (udpMatch.getUdpDestinationPort() != null) {
462                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
463                     matchEntriesBuilder.setHasMask(false);
464                     matchEntriesBuilder.setOxmMatchField(UdpDst.class);
465                     addPortAugmentation(matchEntriesBuilder, udpMatch.getUdpDestinationPort());
466                     matchEntriesList.add(matchEntriesBuilder.build());
467                 }
468             }
469
470             else if (layer4Match instanceof SctpMatch) {
471                 SctpMatch sctpMatch = (SctpMatch) layer4Match;
472                 if (sctpMatch.getSctpSourcePort() != null) {
473                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
474                     matchEntriesBuilder.setHasMask(false);
475                     matchEntriesBuilder.setOxmMatchField(SctpSrc.class);
476                     addPortAugmentation(matchEntriesBuilder, sctpMatch.getSctpSourcePort());
477                     matchEntriesList.add(matchEntriesBuilder.build());
478                 }
479
480                 if (sctpMatch.getSctpDestinationPort() != null) {
481                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
482                     matchEntriesBuilder.setHasMask(false);
483                     matchEntriesBuilder.setOxmMatchField(SctpDst.class);
484                     addPortAugmentation(matchEntriesBuilder, sctpMatch.getSctpDestinationPort());
485                     matchEntriesList.add(matchEntriesBuilder.build());
486                 }
487             }
488         }
489
490         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
491         if (icmpv4Match != null) {
492             if (icmpv4Match.getIcmpv4Type() != null) {
493                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
494                 matchEntriesBuilder.setHasMask(false);
495                 matchEntriesBuilder.setOxmMatchField(Icmpv4Type.class);
496                 Icmpv4TypeMatchEntryBuilder icmpv4TypeBuilder = new Icmpv4TypeMatchEntryBuilder();
497                 icmpv4TypeBuilder.setIcmpv4Type(icmpv4Match.getIcmpv4Type());
498                 matchEntriesBuilder.addAugmentation(Icmpv4TypeMatchEntry.class, icmpv4TypeBuilder.build());
499                 matchEntriesList.add(matchEntriesBuilder.build());
500             }
501
502             if (icmpv4Match.getIcmpv4Code() != null) {
503                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
504                 matchEntriesBuilder.setHasMask(false);
505                 matchEntriesBuilder.setOxmMatchField(Icmpv4Code.class);
506                 Icmpv4CodeMatchEntryBuilder icmpv4CodeBuilder = new Icmpv4CodeMatchEntryBuilder();
507                 icmpv4CodeBuilder.setIcmpv4Code(icmpv4Match.getIcmpv4Code());
508                 matchEntriesBuilder.addAugmentation(Icmpv4CodeMatchEntry.class, icmpv4CodeBuilder.build());
509                 matchEntriesList.add(matchEntriesBuilder.build());
510             }
511         }
512
513         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
514         if (icmpv6Match != null) {
515             if (icmpv6Match.getIcmpv6Type() != null) {
516                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
517                 matchEntriesBuilder.setHasMask(false);
518                 matchEntriesBuilder.setOxmMatchField(Icmpv6Type.class);
519                 Icmpv6TypeMatchEntryBuilder icmpv6TypeBuilder = new Icmpv6TypeMatchEntryBuilder();
520                 icmpv6TypeBuilder.setIcmpv6Type(icmpv6Match.getIcmpv6Type());
521                 matchEntriesBuilder.addAugmentation(Icmpv6TypeMatchEntry.class, icmpv6TypeBuilder.build());
522                 matchEntriesList.add(matchEntriesBuilder.build());
523             }
524
525             if (icmpv6Match.getIcmpv6Code() != null) {
526                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
527                 matchEntriesBuilder.setHasMask(false);
528                 matchEntriesBuilder.setOxmMatchField(Icmpv6Code.class);
529                 Icmpv6CodeMatchEntryBuilder icmpv6CodeBuilder = new Icmpv6CodeMatchEntryBuilder();
530                 icmpv6CodeBuilder.setIcmpv6Code(icmpv6Match.getIcmpv6Code());
531                 matchEntriesBuilder.addAugmentation(Icmpv6CodeMatchEntry.class, icmpv6CodeBuilder.build());
532                 matchEntriesList.add(matchEntriesBuilder.build());
533             }
534         }
535
536         Layer3Match layer3Match = match.getLayer3Match();
537         if (layer3Match != null) {
538             if (layer3Match instanceof Ipv4Match) {
539                 Ipv4Match ipv4Match = (Ipv4Match) layer3Match;
540                 if (ipv4Match.getIpv4Source() != null) {
541                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
542                     matchEntriesBuilder.setOxmMatchField(Ipv4Src.class);
543                     boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, ipv4Match.getIpv4Source());
544                     matchEntriesBuilder.setHasMask(hasMask);
545                     matchEntriesList.add(matchEntriesBuilder.build());
546                 }
547                 if (ipv4Match.getIpv4Destination() != null) {
548                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
549                     matchEntriesBuilder.setOxmMatchField(Ipv4Dst.class);
550                     boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, ipv4Match.getIpv4Destination());
551                     matchEntriesBuilder.setHasMask(hasMask);
552                     matchEntriesList.add(matchEntriesBuilder.build());
553                 }
554             }
555
556             else if (layer3Match instanceof ArpMatch) {
557                 ArpMatch arpMatch = (ArpMatch) layer3Match;
558                 if (arpMatch.getArpOp() != null) {
559                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
560                     matchEntriesBuilder.setHasMask(false);
561                     matchEntriesBuilder.setOxmMatchField(ArpOp.class);
562                     OpCodeMatchEntryBuilder opcodeBuilder = new OpCodeMatchEntryBuilder();
563                     opcodeBuilder.setOpCode(arpMatch.getArpOp());
564                     matchEntriesBuilder.addAugmentation(OpCodeMatchEntry.class, opcodeBuilder.build());
565                     matchEntriesList.add(matchEntriesBuilder.build());
566                 }
567
568                 if (arpMatch.getArpSourceTransportAddress() != null) {
569                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
570                     matchEntriesBuilder.setOxmMatchField(ArpSpa.class);
571                     boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, arpMatch.getArpSourceTransportAddress());
572                     matchEntriesBuilder.setHasMask(hasMask);
573                     matchEntriesList.add(matchEntriesBuilder.build());
574                 }
575
576                 if (arpMatch.getArpTargetTransportAddress() != null) {
577                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
578                     matchEntriesBuilder.setOxmMatchField(ArpTpa.class);
579                     boolean hasMask = addIpv4PrefixAugmentation(matchEntriesBuilder, arpMatch.getArpTargetTransportAddress());
580                     matchEntriesBuilder.setHasMask(hasMask);
581                     matchEntriesList.add(matchEntriesBuilder.build());
582                 }
583
584                 ArpSourceHardwareAddress arpSourceHardwareAddress = arpMatch.getArpSourceHardwareAddress();
585                 if (arpSourceHardwareAddress != null) {
586                     boolean hasmask = false;
587                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
588                     matchEntriesBuilder.setOxmMatchField(ArpSha.class);
589                     addMacAddressAugmentation(matchEntriesBuilder, arpSourceHardwareAddress.getAddress());
590                     if (arpSourceHardwareAddress.getMask() != null) {
591                         hasmask = true;
592                         addMaskAugmentation(matchEntriesBuilder, arpSourceHardwareAddress.getMask());
593                     }
594                     matchEntriesBuilder.setHasMask(hasmask);
595                     matchEntriesList.add(matchEntriesBuilder.build());
596                 }
597
598                 ArpTargetHardwareAddress arpTargetHardwareAddress = arpMatch.getArpTargetHardwareAddress();
599                 if (arpTargetHardwareAddress != null) {
600                     boolean hasmask = false;
601                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
602                     matchEntriesBuilder.setOxmMatchField(ArpTha.class);
603                     addMacAddressAugmentation(matchEntriesBuilder, arpTargetHardwareAddress.getAddress());
604                     if (arpTargetHardwareAddress.getMask() != null) {
605                         hasmask = true;
606                         addMaskAugmentation(matchEntriesBuilder, arpTargetHardwareAddress.getMask());
607                     }
608                     matchEntriesBuilder.setHasMask(hasmask);
609                     matchEntriesList.add(matchEntriesBuilder.build());
610                 }
611             }
612
613             else if (layer3Match instanceof Ipv6Match) {
614                 Ipv6Match ipv6Match = (Ipv6Match) layer3Match;
615                 if (ipv6Match.getIpv6Source() != null) {
616                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
617                     matchEntriesBuilder.setOxmMatchField(Ipv6Src.class);
618                     boolean hasmask = addIpv6PrefixAugmentation(matchEntriesBuilder, ipv6Match.getIpv6Source());
619                     matchEntriesBuilder.setHasMask(hasmask);
620                     matchEntriesList.add(matchEntriesBuilder.build());
621                 }
622
623                 if (ipv6Match.getIpv6Destination() != null) {
624                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
625                     matchEntriesBuilder.setOxmMatchField(Ipv6Dst.class);
626                     boolean hasmask = addIpv6PrefixAugmentation(matchEntriesBuilder, ipv6Match.getIpv6Destination());
627                     matchEntriesBuilder.setHasMask(hasmask);
628                     matchEntriesList.add(matchEntriesBuilder.build());
629                 }
630
631                 if (ipv6Match.getIpv6Label() != null) {
632                     boolean hasmask = false;
633                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
634                     matchEntriesBuilder.setOxmMatchField(Ipv6Flabel.class);
635                     Ipv6FlabelMatchEntryBuilder ipv6FlabelBuilder = new Ipv6FlabelMatchEntryBuilder();
636                     ipv6FlabelBuilder.setIpv6Flabel(ipv6Match.getIpv6Label().getIpv6Flabel());
637                     matchEntriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ipv6FlabelBuilder.build());
638                     if (ipv6Match.getIpv6Label().getFlabelMask() != null) {
639                         hasmask = true;
640                         addMaskAugmentation(matchEntriesBuilder, ipv6Match.getIpv6Label().getFlabelMask());
641                     }
642                     matchEntriesBuilder.setHasMask(hasmask);
643                     matchEntriesList.add(matchEntriesBuilder.build());
644                 }
645
646                 if (ipv6Match.getIpv6NdTarget() != null) {
647                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
648                     matchEntriesBuilder.setHasMask(false);
649                     matchEntriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
650                     addIpv6AddressAugmentation(matchEntriesBuilder, ipv6Match.getIpv6NdTarget());
651                     matchEntriesList.add(matchEntriesBuilder.build());
652                 }
653
654                 if (ipv6Match.getIpv6NdSll() != null) {
655                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
656                     matchEntriesBuilder.setHasMask(false);
657                     matchEntriesBuilder.setOxmMatchField(Ipv6NdSll.class);
658                     addMacAddressAugmentation(matchEntriesBuilder, ipv6Match.getIpv6NdSll());
659                     matchEntriesList.add(matchEntriesBuilder.build());
660                 }
661
662                 if (ipv6Match.getIpv6NdTll() != null) {
663                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
664                     matchEntriesBuilder.setHasMask(false);
665                     matchEntriesBuilder.setOxmMatchField(Ipv6NdTll.class);
666                     addMacAddressAugmentation(matchEntriesBuilder, ipv6Match.getIpv6NdTll());
667                     matchEntriesList.add(matchEntriesBuilder.build());
668                 }
669
670                 if (ipv6Match.getIpv6Exthdr() != null) {
671                     // TODO: verify
672                     matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
673                     matchEntriesBuilder.setOxmMatchField(Ipv6Exthdr.class);
674                     PseudoFieldMatchEntryBuilder pseudoBuilder = new PseudoFieldMatchEntryBuilder();
675                     Integer bitmap = ipv6Match.getIpv6Exthdr();
676                     final Boolean NONEXT = ((bitmap) & (1 << 0)) != 0;
677                     final Boolean ESP = ((bitmap) & (1 << 1)) != 0;
678                     final Boolean AUTH = ((bitmap) & (1 << 2)) != 0;
679                     final Boolean DEST = ((bitmap) & (1 << 3)) != 0;
680                     final Boolean FRAG = ((bitmap) & (1 << 4)) != 0;
681                     final Boolean ROUTER = ((bitmap) & (1 << 5)) != 0;
682                     final Boolean HOP = ((bitmap) & (1 << 6)) != 0;
683                     final Boolean UNREP = ((bitmap) & (1 << 7)) != 0;
684                     final Boolean UNSEQ = ((bitmap) & (1 << 8)) != 0;
685                     pseudoBuilder.setPseudoField(new PseudoField(AUTH, DEST, ESP, FRAG, HOP, NONEXT, ROUTER, UNREP,
686                             UNSEQ));
687                     matchEntriesBuilder.addAugmentation(PseudoFieldMatchEntry.class, pseudoBuilder.build());
688                     addMaskAugmentation(matchEntriesBuilder, ByteBuffer.allocate(2).putInt(bitmap).array());
689                     matchEntriesBuilder.setHasMask(false);
690                     matchEntriesList.add(matchEntriesBuilder.build());
691                 }
692             }
693         }
694
695         ProtocolMatchFields protocolMatchFields = match.getProtocolMatchFields();
696         if (protocolMatchFields != null) {
697             if (protocolMatchFields.getMplsLabel() != null) {
698                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
699                 matchEntriesBuilder.setHasMask(false);
700                 matchEntriesBuilder.setOxmMatchField(MplsLabel.class);
701                 MplsLabelMatchEntryBuilder mplsLabelBuilder = new MplsLabelMatchEntryBuilder();
702                 mplsLabelBuilder.setMplsLabel(protocolMatchFields.getMplsLabel());
703                 matchEntriesBuilder.addAugmentation(MplsLabelMatchEntry.class, mplsLabelBuilder.build());
704                 matchEntriesList.add(matchEntriesBuilder.build());
705             }
706
707             if (protocolMatchFields.getMplsBos() != null) {
708                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
709                 matchEntriesBuilder.setHasMask(false);
710                 matchEntriesBuilder.setOxmMatchField(MplsBos.class);
711                 BosMatchEntryBuilder bosBuilder = new BosMatchEntryBuilder();
712                 if (protocolMatchFields.getMplsBos() != 0) {
713                     bosBuilder.setBos(true);
714                 } else {
715                     bosBuilder.setBos(false);
716                 }
717                 matchEntriesBuilder.addAugmentation(BosMatchEntry.class, bosBuilder.build());
718                 matchEntriesList.add(matchEntriesBuilder.build());
719             }
720
721             if (protocolMatchFields.getMplsTc() != null) {
722                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
723                 matchEntriesBuilder.setHasMask(false);
724                 matchEntriesBuilder.setOxmMatchField(MplsTc.class);
725                 TcMatchEntryBuilder tcBuilder = new TcMatchEntryBuilder();
726                 tcBuilder.setTc(protocolMatchFields.getMplsTc());
727                 matchEntriesBuilder.addAugmentation(TcMatchEntry.class, tcBuilder.build());
728                 matchEntriesList.add(matchEntriesBuilder.build());
729             }
730
731             if (protocolMatchFields.getPbb() != null) {
732                 boolean hasmask = false;
733                 matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
734                 matchEntriesBuilder.setOxmMatchField(PbbIsid.class);
735                 IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();
736                 isidBuilder.setIsid(protocolMatchFields.getPbb().getPbbIsid());
737                 matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
738                 if (protocolMatchFields.getPbb().getPbbMask() != null) {
739                     hasmask = true;
740                     addMaskAugmentation(matchEntriesBuilder, protocolMatchFields.getPbb().getPbbMask());
741                 }
742                 matchEntriesBuilder.setHasMask(hasmask);
743                 matchEntriesList.add(matchEntriesBuilder.build());
744             }
745         }
746
747         if (match.getTunnel() != null) {
748             boolean hasmask = false;
749             matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
750             matchEntriesBuilder.setOxmMatchField(TunnelId.class);
751             addMetadataAugmentation(matchEntriesBuilder, match.getTunnel().getTunnelId());
752             if (match.getTunnel().getTunnelMask() != null) {
753                 hasmask = true;
754                 addMaskAugmentation(matchEntriesBuilder, match.getTunnel().getTunnelMask());
755             }
756             matchEntriesBuilder.setHasMask(false);
757             matchEntriesList.add(matchEntriesBuilder.build());
758         }
759
760         return matchEntriesList;
761     }
762
763     private static List<Instructions> toInstructions(
764             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions,
765             short version) {
766         List<Instructions> instructionsList = new ArrayList<>();
767
768         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
769                 .getInstruction()) {
770             InstructionsBuilder instructionBuilder = new InstructionsBuilder();
771             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
772                     .getInstruction();
773             if (curInstruction instanceof GoToTable) {
774                 GoToTable goToTable = (GoToTable) curInstruction;
775                 instructionBuilder
776                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class);
777                 TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();
778                 tableBuilder.setTableId(goToTable.getTableId());
779                 instructionBuilder.addAugmentation(TableIdInstruction.class, tableBuilder.build());
780                 instructionsList.add(instructionBuilder.build());
781             }
782
783             else if (curInstruction instanceof WriteMetadata) {
784                 WriteMetadata writeMetadata = (WriteMetadata) curInstruction;
785                 instructionBuilder
786                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class);
787                 MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();
788                 metadataBuilder.setMetadata(writeMetadata.getMetadata().toByteArray());
789                 metadataBuilder.setMetadataMask(writeMetadata.getMetadataMask().toByteArray());
790                 instructionBuilder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());
791                 instructionsList.add(instructionBuilder.build());
792             }
793
794             else if (curInstruction instanceof WriteActions) {
795                 WriteActions writeActions = (WriteActions) curInstruction;
796                 instructionBuilder
797                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class);
798                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
799                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(writeActions.getAction(),
800                         version));
801                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
802                 instructionsList.add(instructionBuilder.build());
803             }
804
805             else if (curInstruction instanceof ApplyActions) {
806                 ApplyActions applyActions = (ApplyActions) curInstruction;
807                 instructionBuilder
808                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
809                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
810                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(applyActions.getAction(),
811                         version));
812                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
813                 instructionsList.add(instructionBuilder.build());
814             }
815
816             else if (curInstruction instanceof ClearActions) {
817                 ClearActions clearActions = (ClearActions) curInstruction;
818                 instructionBuilder
819                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class);
820                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
821                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(clearActions.getAction(),
822                         version));
823                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
824                 instructionsList.add(instructionBuilder.build());
825             }
826
827             else if (curInstruction instanceof Meter) {
828                 Meter meter = (Meter) curInstruction;
829                 instructionBuilder
830                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class);
831                 MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
832                 Long meterId = Long.parseLong(meter.getMeter());
833                 meterBuilder.setMeterId(meterId);
834                 instructionBuilder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
835                 instructionsList.add(instructionBuilder.build());
836             }
837         }
838         return instructionsList;
839     }
840
841     private static void addMaskAugmentation(MatchEntriesBuilder builder, byte[] mask) {
842         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
843         maskBuilder.setMask(mask);
844         builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
845         builder.setHasMask(true);
846     }
847
848     private static void addIpv6AddressAugmentation(MatchEntriesBuilder builder, Ipv6Address address) {
849         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
850         ipv6AddressBuilder.setIpv6Address(address);
851         builder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
852     }
853
854     private static boolean addIpv6PrefixAugmentation(MatchEntriesBuilder builder, Ipv6Prefix address) {
855         boolean hasMask = false;
856         String[] addressParts = address.getValue().split(PREFIX_SEPARATOR);
857         Integer prefix = null;
858         if (addressParts.length == 2) {
859             prefix = Integer.parseInt(addressParts[1]);
860         }
861
862         Ipv6Address ipv6Address = new Ipv6Address(addressParts[0]);
863         Ipv6AddressMatchEntryBuilder ipv6AddressBuilder = new Ipv6AddressMatchEntryBuilder();
864         ipv6AddressBuilder.setIpv6Address(ipv6Address);
865         builder.addAugmentation(Ipv6AddressMatchEntry.class, ipv6AddressBuilder.build());
866         if (prefix != null) {
867             hasMask = true;
868             addMaskAugmentation(builder, convertIpv6PrefixToByteArray(prefix));
869         }
870         return hasMask;
871     }
872
873     private static byte[] convertIpv6PrefixToByteArray(int prefix) {
874         // TODO: Temporary fix. Has performance impacts.
875         byte[] mask = new byte[16];
876         int oneCount = prefix;
877         for (int count = 0; count < 16; count++) {
878             int byteBits = 0;
879             if (oneCount >= 8) {
880                 byteBits = 8;
881                 oneCount = oneCount - 8;
882             } else {
883                 byteBits = oneCount;
884                 oneCount = 0;
885             }
886
887             mask[count] = (byte) (256 - Math.pow(2, 8 - byteBits));
888         }
889         return mask;
890     }
891
892     private static void addMetadataAugmentation(MatchEntriesBuilder builder, BigInteger metadata) {
893         MetadataMatchEntryBuilder metadataMatchEntry = new MetadataMatchEntryBuilder();
894         metadataMatchEntry.setMetadata(metadata.toByteArray());
895         builder.addAugmentation(MetadataMatchEntry.class, metadataMatchEntry.build());
896     }
897
898     /**
899      * @return true if Ipv4Prefix contains prefix (and it is used in mask), false otherwise
900      */
901     private static boolean addIpv4PrefixAugmentation(MatchEntriesBuilder builder, Ipv4Prefix address) {
902         boolean hasMask = false;
903         String[] addressParts = address.getValue().split(PREFIX_SEPARATOR);
904         Integer prefix = null;
905         if (addressParts.length < 2) {
906             prefix = 0;
907         } else {
908             prefix = Integer.parseInt(addressParts[1]);
909         }
910
911         Ipv4Address ipv4Address = new Ipv4Address(addressParts[0]);
912         Ipv4AddressMatchEntryBuilder ipv4AddressBuilder = new Ipv4AddressMatchEntryBuilder();
913         ipv4AddressBuilder.setIpv4Address(ipv4Address);
914         builder.addAugmentation(Ipv4AddressMatchEntry.class, ipv4AddressBuilder.build());
915         if (prefix != 0) {
916             int mask = 0xffffffff << (32 - prefix);
917             byte[] maskBytes = new byte[] { (byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
918                     (byte) mask };
919             addMaskAugmentation(builder, maskBytes);
920             hasMask = true;
921         }
922         return hasMask;
923     }
924
925     private static void addMacAddressAugmentation(MatchEntriesBuilder builder, MacAddress address) {
926         MacAddressMatchEntryBuilder macAddress = new MacAddressMatchEntryBuilder();
927         macAddress.setMacAddress(address);
928         builder.addAugmentation(MacAddressMatchEntry.class, macAddress.build());
929     }
930
931     private static void addPortAugmentation(MatchEntriesBuilder builder,
932             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber port) {
933         PortMatchEntryBuilder portBuilder = new PortMatchEntryBuilder();
934         portBuilder.setPort(port);
935         builder.addAugmentation(PortMatchEntry.class, portBuilder.build());
936     }
937
938     /**
939      * Method converts OF SetField Match to SAL SetFiled matches
940      * @param action
941      * @return
942      */
943     public static SetField ofToSALSetField(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.Action action) {
944         logger.info("OF SetField match to SAL SetField match converstion begins");
945         SetFieldBuilder setField = new SetFieldBuilder();
946 /*        OxmFieldsAction oxmFields = action.getAugmentation(OxmFieldsAction.class);
947
948         List<MatchEntries> matchEntries = oxmFields.getMatchEntries();
949         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field.MatchBuilder match =new org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field.MatchBuilder();
950
951         EthernetMatchBuilder ethernetMatchBuilder = null;
952         VlanMatchBuilder vlanMatchBuilder = null;
953         IpMatchBuilder ipMatchBuilder = null;
954         TcpMatchBuilder tcpMatchBuilder = null;
955         UdpMatchBuilder udpMatchBuilder = null;
956         SctpMatchBuilder sctpMatchBuilder = null;
957         Icmpv4MatchBuilder icmpv4MatchBuilder = null;
958         Icmpv6MatchBuilder icmpv6MatchBuilder = null;
959         Ipv4MatchBuilder ipv4MatchBuilder = null;
960         ArpMatchBuilder arpMatchBuilder = null;
961         Ipv6MatchBuilder ipv6MatchBuilder = null;
962         ProtocolMatchFieldsBuilder protocolMatchFieldsBuilder = null;
963
964         for(MatchEntries matchEntry : matchEntries){
965             if(matchEntry instanceof InPort){
966                 PortNumberMatchEntry inPort = matchEntry.getAugmentation(PortNumberMatchEntry.class);
967                 match.setInPort(inPort.getPortNumber().getValue());
968             }else if (matchEntry instanceof InPhyPort){
969                 PortNumberMatchEntry phyPort = matchEntry.getAugmentation(PortNumberMatchEntry.class);
970                 match.setInPhyPort(phyPort.getPortNumber().getValue());
971             }else if (matchEntry instanceof Metadata){
972                 MetadataMatchEntry metadataMatch = matchEntry.getAugmentation(MetadataMatchEntry.class);
973                 MetadataBuilder metadataBuilder = new MetadataBuilder();
974                 metadataBuilder.setMetadata(new BigInteger(metadataMatch.getMetadata()));
975                 MaskMatchEntry maskMatch = matchEntry.getAugmentation(MaskMatchEntry.class);
976                 if (maskMatch != null){
977                     metadataBuilder.setMetadataMask(maskMatch.getMask());
978                 }
979                 match.setMetadata(metadataBuilder.build());
980             }else if (matchEntry instanceof EthDst){
981
982                 if(ethernetMatchBuilder == null)
983                     ethernetMatchBuilder = new EthernetMatchBuilder();
984
985                 MacAddressMatchEntry macAddressMatch = matchEntry.getAugmentation(MacAddressMatchEntry.class);
986                 MaskMatchEntry maskMatch = matchEntry.getAugmentation(MaskMatchEntry.class);
987                 EthernetDestinationBuilder ethernetDestination =  new EthernetDestinationBuilder();
988                 ethernetDestination.setAddress(macAddressMatch.getMacAddress());
989                 if(maskMatch != null){
990                     ethernetDestination.setMask(maskMatch.getMask());
991                 }
992                 ethernetMatchBuilder.setEthernetDestination(ethernetDestination.build());
993             }else if (matchEntry instanceof EthSrc){
994                 if(ethernetMatchBuilder == null)
995                     ethernetMatchBuilder = new EthernetMatchBuilder();
996
997                 MacAddressMatchEntry macAddressMatch = matchEntry.getAugmentation(MacAddressMatchEntry.class);
998                 MaskMatchEntry maskMatch = matchEntry.getAugmentation(MaskMatchEntry.class);
999                 EthernetSourceBuilder ethernetSource =  new EthernetSourceBuilder();
1000                 ethernetSource.setAddress(macAddressMatch.getMacAddress());
1001                 if(maskMatch != null){
1002                     ethernetSource.setMask(maskMatch.getMask());
1003                 }
1004                 ethernetMatchBuilder.setEthernetSource(ethernetSource.build());
1005             }else if (matchEntry instanceof EthType){
1006                 if(ethernetMatchBuilder == null)
1007                     ethernetMatchBuilder = new EthernetMatchBuilder();
1008
1009                 EthTypeMatchEntry etherTypeMatch = matchEntry.getAugmentation(EthTypeMatchEntry.class);
1010                 EthernetTypeBuilder ethernetType=  new EthernetTypeBuilder();
1011                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType etherType = new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType((long)etherTypeMatch.getEthType().getValue());
1012                 ethernetType.setType(etherType);
1013                 ethernetMatchBuilder.setEthernetType(ethernetType.build());
1014             }else if (matchEntry instanceof VlanVid){
1015                 if(vlanMatchBuilder == null)
1016                     vlanMatchBuilder = new VlanMatchBuilder();
1017
1018                 VlanVidMatchEntry vlanVidMatch = matchEntry.getAugmentation(VlanVidMatchEntry.class);
1019                 MaskMatchEntry maskMatch = matchEntry.getAugmentation(MaskMatchEntry.class);
1020
1021                 VlanIdBuilder vlanIdBuilder = new  VlanIdBuilder();
1022                 vlanIdBuilder.setVlanId(
1023                         new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanVidMatch.getVlanVid()));
1024                 if(maskMatch != null){
1025                     vlanIdBuilder.setMask(maskMatch.getMask());
1026                 }
1027                 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
1028
1029             }else if (matchEntry instanceof VlanPcp){
1030                 if(vlanMatchBuilder == null)
1031                     vlanMatchBuilder = new VlanMatchBuilder();
1032
1033                 VlanPcpMatchEntry vlanPcpMatch = matchEntry.getAugmentation(VlanPcpMatchEntry.class);
1034                 vlanMatchBuilder.setVlanPcp(
1035                         new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp(vlanPcpMatch.getVlanPcp()));
1036             }else if (matchEntry instanceof IpDscp){
1037                 if(ipMatchBuilder == null)
1038                     ipMatchBuilder = new IpMatchBuilder();
1039
1040                 DscpMatchEntry dscpMatchEntry = matchEntry.getAugmentation(DscpMatchEntry.class);
1041                 ipMatchBuilder.setIpDscp(dscpMatchEntry.getDscp());
1042
1043             }else if (matchEntry instanceof IpEcn){
1044                 if(ipMatchBuilder == null)
1045                     ipMatchBuilder = new IpMatchBuilder();
1046
1047                 EcnMatchEntry ecnMatchEntry = matchEntry.getAugmentation(EcnMatchEntry.class);
1048                 ipMatchBuilder.setIpEcn(ecnMatchEntry.getEcn());
1049
1050             }else if (matchEntry instanceof IpProto){
1051                 if(ipMatchBuilder == null)
1052                     ipMatchBuilder = new IpMatchBuilder();
1053
1054                 ProtocolNumberMatchEntry protocolNumberMatch = matchEntry.getAugmentation(ProtocolNumberMatchEntry.class);
1055                 ipMatchBuilder.setIpProtocol(protocolNumberMatch.getProtocolNumber());
1056             }else if (matchEntry instanceof TcpSrc){
1057                 if(tcpMatchBuilder == null)
1058                     tcpMatchBuilder = new TcpMatchBuilder();
1059
1060                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1061                 tcpMatchBuilder.setTcpSourcePort(portMatchEntry.getPort());
1062
1063             }else if (matchEntry instanceof TcpDst){
1064                 if(tcpMatchBuilder == null)
1065                     tcpMatchBuilder = new TcpMatchBuilder();
1066
1067                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1068                 tcpMatchBuilder.setTcpDestinationPort(portMatchEntry.getPort());
1069
1070             }else if (matchEntry instanceof UdpSrc){
1071                 if(udpMatchBuilder == null)
1072                     udpMatchBuilder = new UdpMatchBuilder();
1073
1074                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1075                 udpMatchBuilder.setUdpSourcePort(portMatchEntry.getPort());
1076
1077
1078             }else if (matchEntry instanceof UdpDst){
1079                 if(udpMatchBuilder == null)
1080                     udpMatchBuilder = new UdpMatchBuilder();
1081
1082                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1083                 udpMatchBuilder.setUdpDestinationPort(portMatchEntry.getPort());
1084             }else if (matchEntry instanceof SctpSrc){
1085                 if(sctpMatchBuilder == null)
1086                     sctpMatchBuilder = new SctpMatchBuilder();
1087
1088                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1089                 sctpMatchBuilder.setSctpSourcePort(portMatchEntry.getPort());
1090
1091             }else if (matchEntry instanceof SctpDst){
1092                 if(sctpMatchBuilder == null)
1093                     sctpMatchBuilder = new SctpMatchBuilder();
1094
1095                 PortMatchEntry portMatchEntry = matchEntry.getAugmentation(PortMatchEntry.class);
1096                 sctpMatchBuilder.setSctpDestinationPort(portMatchEntry.getPort());
1097             }else if (matchEntry instanceof Icmpv4Type){
1098                 if(icmpv4MatchBuilder == null)
1099                     icmpv4MatchBuilder = new Icmpv4MatchBuilder();
1100
1101                 Icmpv4TypeMatchEntry icmpv4TypeMatchEntry = matchEntry.getAugmentation(Icmpv4TypeMatchEntry.class);
1102                 icmpv4MatchBuilder.setIcmpv4Type(icmpv4TypeMatchEntry.getIcmpv4Type());
1103
1104             }else if (matchEntry instanceof Icmpv4Code){
1105                 if(icmpv4MatchBuilder == null)
1106                     icmpv4MatchBuilder = new Icmpv4MatchBuilder();
1107
1108                 Icmpv4CodeMatchEntry icmpv4CodeMatchEntry = matchEntry.getAugmentation(Icmpv4CodeMatchEntry.class);
1109                 icmpv4MatchBuilder.setIcmpv4Code(icmpv4CodeMatchEntry.getIcmpv4Code());
1110
1111             }else if (matchEntry instanceof Icmpv6Type){
1112                 if(icmpv6MatchBuilder == null)
1113                     icmpv6MatchBuilder = new Icmpv6MatchBuilder();
1114
1115                 Icmpv6TypeMatchEntry icmpv6TypeMatchEntry = matchEntry.getAugmentation(Icmpv6TypeMatchEntry.class);
1116                 icmpv6MatchBuilder.setIcmpv6Type(icmpv6TypeMatchEntry.getIcmpv6Type());
1117             }else if (matchEntry instanceof Icmpv6Code){
1118                 if(icmpv6MatchBuilder == null)
1119                     icmpv6MatchBuilder = new Icmpv6MatchBuilder();
1120
1121                 Icmpv6CodeMatchEntry icmpv6CodeMatchEntry = matchEntry.getAugmentation(Icmpv6CodeMatchEntry.class);
1122                 icmpv6MatchBuilder.setIcmpv6Code(icmpv6CodeMatchEntry.getIcmpv6Code());
1123             }else if (matchEntry instanceof Ipv4Src){
1124                 if(ipv4MatchBuilder == null)
1125                     ipv4MatchBuilder = new Ipv4MatchBuilder();
1126
1127                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = matchEntry.getAugmentation(Ipv4AddressMatchEntry.class);
1128                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1129                 ipv4MatchBuilder.setIpv4Source(
1130                         new Ipv4Prefix(ipv4AddressMatchEntry.getIpv4Address().getValue()
1131                                 +"/"+new String(maskMatchEntry.getMask())));
1132
1133             }else if (matchEntry instanceof Ipv4Dst){
1134                 if(ipv4MatchBuilder == null)
1135                     ipv4MatchBuilder = new Ipv4MatchBuilder();
1136
1137                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = matchEntry.getAugmentation(Ipv4AddressMatchEntry.class);
1138                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1139                 ipv4MatchBuilder.setIpv4Destination(
1140                         new Ipv4Prefix(ipv4AddressMatchEntry.getIpv4Address().getValue()
1141                                 +"/"+new String(maskMatchEntry.getMask())));
1142             }else if (matchEntry instanceof ArpOp){
1143                 if(arpMatchBuilder == null)
1144                     arpMatchBuilder = new ArpMatchBuilder();
1145
1146                 OpCodeMatchEntry opCodeMatchEntry = matchEntry.getAugmentation(OpCodeMatchEntry.class);
1147                 arpMatchBuilder.setArpOp(opCodeMatchEntry.getOpCode());
1148
1149             }else if (matchEntry instanceof ArpSpa){
1150                 if(arpMatchBuilder == null)
1151                     arpMatchBuilder = new ArpMatchBuilder();
1152
1153                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = matchEntry.getAugmentation(Ipv4AddressMatchEntry.class);
1154                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1155                 arpMatchBuilder.setArpSourceTransportAddress(
1156                         new Ipv4Prefix(ipv4AddressMatchEntry.getIpv4Address().getValue()
1157                                 +"/"+new String(maskMatchEntry.getMask())));
1158
1159             }else if (matchEntry instanceof ArpTpa){
1160                 if(arpMatchBuilder == null)
1161                     arpMatchBuilder = new ArpMatchBuilder();
1162
1163                 Ipv4AddressMatchEntry ipv4AddressMatchEntry = matchEntry.getAugmentation(Ipv4AddressMatchEntry.class);
1164                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1165                 arpMatchBuilder.setArpTargetTransportAddress(
1166                         new Ipv4Prefix(ipv4AddressMatchEntry.getIpv4Address().getValue()
1167                                 +"/"+new String(maskMatchEntry.getMask())));
1168
1169             }else if (matchEntry instanceof ArpSha){
1170                 if(arpMatchBuilder == null)
1171                     arpMatchBuilder = new ArpMatchBuilder();
1172
1173                 MacAddressMatchEntry macAddressMatchEntry = matchEntry.getAugmentation(MacAddressMatchEntry.class);
1174                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1175                 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
1176                 arpSourceHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
1177                 arpSourceHardwareAddressBuilder.setMask(maskMatchEntry.getMask());
1178                 arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
1179
1180             }else if (matchEntry instanceof ArpTha){
1181                 if(arpMatchBuilder == null)
1182                     arpMatchBuilder = new ArpMatchBuilder();
1183
1184                 MacAddressMatchEntry macAddressMatchEntry = matchEntry.getAugmentation(MacAddressMatchEntry.class);
1185                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1186                 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
1187                 arpTargetHardwareAddressBuilder.setAddress(macAddressMatchEntry.getMacAddress());
1188                 arpTargetHardwareAddressBuilder.setMask(maskMatchEntry.getMask());
1189                 arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
1190             }else if (matchEntry instanceof Ipv6Src){
1191                 if(ipv6MatchBuilder == null)
1192                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1193
1194                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = matchEntry.getAugmentation(Ipv6AddressMatchEntry.class);
1195                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1196                 ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix (ipv6AddressMatchEntry.getIpv6Address().getValue()+
1197                         "/"+new String(maskMatchEntry.getMask())));
1198
1199             }else if (matchEntry instanceof Ipv6Dst){
1200                 if(ipv6MatchBuilder == null)
1201                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1202
1203                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = matchEntry.getAugmentation(Ipv6AddressMatchEntry.class);
1204                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1205                 ipv6MatchBuilder.setIpv6Destination(new Ipv6Prefix (ipv6AddressMatchEntry.getIpv6Address().getValue()+
1206                         "/"+new String(maskMatchEntry.getMask())));
1207
1208             }else if (matchEntry instanceof Ipv6Flabel){
1209                 if(ipv6MatchBuilder == null)
1210                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1211
1212                 Ipv6FlabelMatchEntry ipv6FlabelMatchEntry = matchEntry.getAugmentation(Ipv6FlabelMatchEntry.class);
1213                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1214                 Ipv6LabelBuilder ipv6LabelBuilder = new Ipv6LabelBuilder();
1215                 ipv6LabelBuilder.setIpv6Flabel(ipv6FlabelMatchEntry.getIpv6Flabel());
1216                 ipv6LabelBuilder.setFlabelMask(maskMatchEntry.getMask());
1217                 ipv6MatchBuilder.setIpv6Label(ipv6LabelBuilder.build());
1218
1219             }else if (matchEntry instanceof Ipv6NdTarget){
1220                 if(ipv6MatchBuilder == null)
1221                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1222                 Ipv6AddressMatchEntry ipv6AddressMatchEntry = matchEntry.getAugmentation(Ipv6AddressMatchEntry.class);
1223                 ipv6MatchBuilder.setIpv6NdTarget(ipv6AddressMatchEntry.getIpv6Address());
1224
1225             }else if (matchEntry instanceof Ipv6NdSll){
1226                 if(ipv6MatchBuilder == null)
1227                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1228
1229                 MacAddressMatchEntry macAddressMatchEntry = matchEntry.getAugmentation(MacAddressMatchEntry.class);
1230                 ipv6MatchBuilder.setIpv6NdSll(macAddressMatchEntry.getMacAddress());
1231             }else if (matchEntry instanceof Ipv6NdTll){
1232                 if(ipv6MatchBuilder == null)
1233                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1234
1235                 MacAddressMatchEntry macAddressMatchEntry = matchEntry.getAugmentation(MacAddressMatchEntry.class);
1236                 ipv6MatchBuilder.setIpv6NdTll(macAddressMatchEntry.getMacAddress());
1237
1238             }else if (matchEntry instanceof Ipv6Exthdr){
1239                 if(ipv6MatchBuilder == null)
1240                     ipv6MatchBuilder = new Ipv6MatchBuilder();
1241
1242                 PseudoFieldMatchEntry pseudoFieldMatchEntry = matchEntry.getAugmentation(PseudoFieldMatchEntry.class);
1243                 PseudoField pseudoField = pseudoFieldMatchEntry.getPseudoField();
1244                 int pseudoFieldInt = 0;
1245                 pseudoFieldInt |= pseudoField.isNonext()?(1 << 0):~(1 << 0);
1246                 pseudoFieldInt |= pseudoField.isEsp()?(1 << 1):~(1 << 1);
1247                 pseudoFieldInt |= pseudoField.isAuth()?(1 << 2):~(1 << 2);
1248                 pseudoFieldInt |= pseudoField.isDest()?(1 << 3):~(1 << 3);
1249                 pseudoFieldInt |= pseudoField.isFrag()?(1 << 4):~(1 << 4);
1250                 pseudoFieldInt |= pseudoField.isRouter()?(1 << 5):~(1 << 5);
1251                 pseudoFieldInt |= pseudoField.isHop()?(1 << 6):~(1 << 6);
1252                 pseudoFieldInt |= pseudoField.isUnrep()?(1 << 7):~(1 << 7);
1253                 pseudoFieldInt |= pseudoField.isUnseq()?(1 << 8):~(1 << 8);
1254
1255                 ipv6MatchBuilder.setIpv6Exthdr(pseudoFieldInt);
1256             }else if (matchEntry instanceof MplsLabel){
1257                 if(protocolMatchFieldsBuilder == null)
1258                     protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
1259
1260                 MplsLabelMatchEntry MplsLabelMatchEntry = matchEntry.getAugmentation(MplsLabelMatchEntry.class);
1261                 protocolMatchFieldsBuilder.setMplsLabel(MplsLabelMatchEntry.getMplsLabel());
1262
1263             }else if (matchEntry instanceof MplsBos){
1264                 if(protocolMatchFieldsBuilder == null)
1265                     protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
1266
1267                 BosMatchEntry bosMatchEntry = matchEntry.getAugmentation(BosMatchEntry.class);
1268                 protocolMatchFieldsBuilder.setMplsBos(bosMatchEntry.isBos()?(short)1:(short)0);
1269
1270             }else if (matchEntry instanceof MplsTc) {
1271                 if(protocolMatchFieldsBuilder == null)
1272                     protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
1273
1274                 TcMatchEntry tcMatchEntry = matchEntry.getAugmentation(TcMatchEntry.class);
1275                 protocolMatchFieldsBuilder.setMplsTc(tcMatchEntry.getTc());
1276
1277             }else if (matchEntry instanceof PbbIsid){
1278                 if(protocolMatchFieldsBuilder == null)
1279                     protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
1280
1281                 IsidMatchEntry isidMatchEntry = matchEntry.getAugmentation(IsidMatchEntry.class);
1282                 PbbBuilder pbbBuilder = new PbbBuilder();
1283                 pbbBuilder.setPbbIsid(isidMatchEntry.getIsid());
1284                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1285                 if(maskMatchEntry != null)
1286                     pbbBuilder.setPbbMask(maskMatchEntry.getMask());
1287
1288                 protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
1289             }else if (matchEntry instanceof TunnelId){
1290                 MetadataMatchEntry metadataMatchEntry = matchEntry.getAugmentation(MetadataMatchEntry.class);
1291                 MaskMatchEntry maskMatchEntry = matchEntry.getAugmentation(MaskMatchEntry.class);
1292                 TunnelBuilder tunnelBuilder = new TunnelBuilder();
1293                 tunnelBuilder.setTunnelId(new BigInteger(metadataMatchEntry.getMetadata()));
1294                 tunnelBuilder.setTunnelMask(maskMatchEntry.getMask());
1295                 match.setTunnel(tunnelBuilder.build());
1296             }
1297         }
1298         if(ethernetMatchBuilder != null){
1299             match.setEthernetMatch(ethernetMatchBuilder.build());
1300         }
1301         if (vlanMatchBuilder != null){
1302             match.setVlanMatch(vlanMatchBuilder.build());
1303         }
1304         if(ipMatchBuilder != null){
1305             match.setIpMatch(ipMatchBuilder.build());
1306         }
1307         if(tcpMatchBuilder != null){
1308             match.setLayer4Match(tcpMatchBuilder.build());
1309         }
1310         if(udpMatchBuilder != null){
1311             match.setLayer4Match(udpMatchBuilder.build());
1312         }
1313         if(sctpMatchBuilder != null){
1314             match.setLayer4Match(sctpMatchBuilder.build());
1315         }
1316         if(icmpv4MatchBuilder != null){
1317             match.setIcmpv4Match(icmpv4MatchBuilder.build());
1318         }
1319         if(icmpv6MatchBuilder != null){
1320             match.setIcmpv6Match(icmpv6MatchBuilder.build());
1321         }
1322         if(ipv4MatchBuilder != null){
1323             match.setLayer3Match(ipv4MatchBuilder.build());
1324         }
1325         if(arpMatchBuilder != null){
1326             match.setLayer3Match(arpMatchBuilder.build());
1327         }
1328         if(ipv6MatchBuilder != null){
1329             match.setLayer3Match(ipv6MatchBuilder.build());
1330         }
1331         if(protocolMatchFieldsBuilder != null){
1332             match.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
1333         }
1334         setField.setMatch(match.build());
1335
1336   */      return setField.build();
1337     }
1338
1339 }