Use Java declarations instead of Google Collections
[netvirt.git] / openstack / utils / mdsal-openflow / src / main / java / org / opendaylight / netvirt / utils / mdsal / openflow / ActionUtils.java
1 /*
2  * Copyright (c) 2014 - 2016 Red Hat, Inc. 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.netvirt.utils.mdsal.openflow;
10
11 import com.google.common.net.InetAddresses;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjNxHashFields;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjNxMpAlgorithm;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpShaCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpThaCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshMdtypeCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshNpCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1CaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2CaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc3CaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc4CaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNspCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIdCaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpOpCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpSpaCaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpTpaCaseBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfEthDstCaseBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfIpDstCaseBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionMultipathNodesNodeTableFlowApplyActionsCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionResubmitNodesNodeTableFlowApplyActionsCaseBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.NxConntrack;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.NxConntrackBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.NxMultipath;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.NxMultipathBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputReg;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputRegBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNsh;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNshBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNsh;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNshBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMove;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMoveBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.SrcBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.resubmit.grouping.NxResubmit;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.resubmit.grouping.NxResubmitBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxArpShaCaseBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxNshc1CaseBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxNshc2CaseBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIdCaseBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIpv4DstCaseBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfArpSpaCaseBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfEthSrcCaseBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfIpSrcCaseBuilder;
105
106 public final class ActionUtils {
107     public static Action dropAction() {
108         return new DropActionCaseBuilder()
109             .setDropAction(new DropActionBuilder()
110                 .build())
111             .build();
112     }
113
114     public static Action outputAction(NodeConnectorId id) {
115         return new OutputActionCaseBuilder()
116             .setOutputAction(new OutputActionBuilder()
117                 .setOutputNodeConnector(new Uri(id.getValue()))
118                 .build())
119             .build();
120     }
121
122     public static Action groupAction(Long id) {
123         return new GroupActionCaseBuilder()
124             .setGroupAction(new GroupActionBuilder()
125                 .setGroupId(id)
126                 .build())
127             .build();
128     }
129
130     public static Action popVlanAction() {
131         return new PopVlanActionCaseBuilder()
132             .setPopVlanAction(new PopVlanActionBuilder()
133                     .build())
134             .build();
135     }
136
137     public static Action setDlSrcAction(MacAddress mac) {
138         return new SetDlSrcActionCaseBuilder()
139             .setSetDlSrcAction(new SetDlSrcActionBuilder()
140                 .setAddress(mac)
141                 .build())
142             .build();
143     }
144
145     public static Action setDlDstAction(MacAddress mac) {
146         return new SetDlDstActionCaseBuilder()
147             .setSetDlDstAction(new SetDlDstActionBuilder()
148                 .setAddress(mac)
149                 .build())
150             .build();
151     }
152
153     public static Action setNwSrcAction(Address ip) {
154         return new SetNwSrcActionCaseBuilder()
155             .setSetNwSrcAction(new SetNwSrcActionBuilder()
156                 .setAddress(ip)
157                 .build())
158             .build();
159     }
160
161     public static Action setNwDstAction(Address ip) {
162         return new SetNwDstActionCaseBuilder()
163             .setSetNwDstAction(new SetNwDstActionBuilder()
164                 .setAddress(ip)
165                 .build())
166             .build();
167     }
168
169     public static Action decNwTtlAction() {
170         return new DecNwTtlCaseBuilder()
171             .setDecNwTtl(new DecNwTtlBuilder()
172                 .build())
173             .build();
174     }
175
176     public static Action setTunnelIdAction(BigInteger tunnelId) {
177
178         SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
179
180         // Build the Set Tunnel Field Action
181         TunnelBuilder tunnel = new TunnelBuilder();
182         tunnel.setTunnelId(tunnelId);
183         setFieldBuilder.setTunnel(tunnel.build());
184
185         return new SetFieldCaseBuilder()
186                 .setSetField(setFieldBuilder.build())
187                 .build();
188     }
189
190     public static Action nxLoadRegAction(DstChoice dstChoice,
191                                          BigInteger value,
192                                          int endOffset,
193                                          boolean groupBucket) {
194         NxRegLoad r = new NxRegLoadBuilder()
195             .setDst(new DstBuilder()
196                 .setDstChoice(dstChoice)
197                 .setStart(0)
198                 .setEnd(endOffset)
199                 .build())
200             .setValue(value)
201             .build();
202         if (groupBucket) {
203             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder()
204                 .setNxRegLoad(r).build();
205         } else {
206             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder()
207                 .setNxRegLoad(r).build();
208         }
209     }
210
211     public static Action nxLoadRegAction(DstChoice dstChoice,
212                                          BigInteger value) {
213         return nxLoadRegAction(dstChoice, value, 31, false);
214     }
215
216     public static Action nxLoadRegAction(Class<? extends NxmNxReg> reg,
217                                          BigInteger value) {
218         return nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(reg).build(),
219                                value);
220     }
221
222     public static Action nxLoadTunIPv4Action(String ipAddress,
223                                              boolean groupBucket) {
224         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
225         long ipl = ip & 0xffffffffL;
226         return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder()
227                                     .setNxTunIpv4Dst(Boolean.TRUE).build(),
228                                BigInteger.valueOf(ipl),
229                                31,
230                                groupBucket);
231     }
232
233     public static Action nxLoadArpOpAction(BigInteger value) {
234         return nxLoadRegAction(new DstOfArpOpCaseBuilder()
235             .setOfArpOp(Boolean.TRUE).build(), value, 15, false);
236     }
237
238     public static Action nxLoadArpShaAction(MacAddress macAddress) {
239         return nxLoadArpShaAction(BigInteger.valueOf(toLong(macAddress)));
240     }
241     public static Action nxLoadArpShaAction(BigInteger value) {
242         return nxLoadRegAction(new DstNxArpShaCaseBuilder()
243             .setNxArpSha(Boolean.TRUE).build(), value, 47, false);
244     }
245
246     public static Action nxLoadArpSpaAction(BigInteger value) {
247         return nxLoadRegAction(new DstOfArpSpaCaseBuilder()
248             .setOfArpSpa(Boolean.TRUE).build(), value);
249     }
250
251     public static Action nxLoadArpSpaAction(String ipAddress) {
252         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
253         long ipl = ip & 0xffffffffL;
254         return nxLoadArpSpaAction(BigInteger.valueOf(ipl));
255     }
256
257     public static Action setIcmpTypeAction(byte value) {
258         SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
259         setFieldBuilder.setIcmpv4Match(new Icmpv4MatchBuilder().setIcmpv4Type((short)value).build());
260         return new SetFieldCaseBuilder()
261                 .setSetField(setFieldBuilder.build())
262                 .build();
263     }
264
265     public static Action nxMoveRegAction(SrcChoice srcChoice,
266                                          DstChoice dstChoice,
267                                          int endOffset,
268                                          boolean groupBucket) {
269         return nxMoveRegAction(srcChoice, dstChoice, 0, 0, endOffset, groupBucket);
270     }
271
272     public static Action nxMoveRegAction(SrcChoice srcChoice,
273                                          DstChoice dstChoice,
274                                          int srcStartOffset,
275                                          int dstStartOffset,
276                                          int dstEndOffset,
277                                          boolean groupBucket) {
278         NxRegMove r = new NxRegMoveBuilder()
279             .setSrc(new SrcBuilder()
280                 .setSrcChoice(srcChoice)
281                 .setStart(srcStartOffset)
282                 .build())
283             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder()
284                 .setDstChoice(dstChoice)
285                 .setStart(dstStartOffset)
286                 .setEnd(dstEndOffset)
287                 .build())
288             .build();
289         if (groupBucket) {
290             return new NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder()
291                 .setNxRegMove(r).build();
292         } else {
293             return new NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder()
294                 .setNxRegMove(r).build();
295         }
296     }
297
298     public static Action nxMoveRegAction(SrcChoice srcChoice,
299                                          DstChoice dstChoice) {
300         return nxMoveRegAction(srcChoice, dstChoice, 31, false);
301     }
302
303     public static Action nxMoveRegTunIdAction(Class<? extends NxmNxReg> src,
304                                               boolean groupBucket) {
305         return nxMoveRegAction(new SrcNxRegCaseBuilder().setNxReg(src).build(),
306                                new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
307                                31,
308                                groupBucket);
309     }
310
311     public static Action nxMoveArpShaToArpThaAction() {
312         return nxMoveRegAction(new SrcNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(),
313                                new DstNxArpThaCaseBuilder().setNxArpTha(Boolean.TRUE).build(),
314                                47, false);
315     }
316
317     public static Action nxMoveEthSrcToEthDstAction() {
318         return nxMoveRegAction(new SrcOfEthSrcCaseBuilder().setOfEthSrc(Boolean.TRUE).build(),
319                                new DstOfEthDstCaseBuilder().setOfEthDst(Boolean.TRUE).build(),
320                                47, false);
321     }
322
323     public static Action nxMoveArpSpaToArpTpaAction() {
324         return nxMoveRegAction(new SrcOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(),
325                                new DstOfArpTpaCaseBuilder().setOfArpTpa(Boolean.TRUE).build());
326     }
327
328     public static Action nxMoveIpSrcToIpDstAction() {
329         return nxMoveRegAction(new SrcOfIpSrcCaseBuilder().setOfIpSrc(Boolean.TRUE).build(),
330                                new DstOfIpDstCaseBuilder().setOfIpDst(Boolean.TRUE).build());
331     }
332
333
334     public static Action nxOutputRegAction(SrcChoice srcChoice) {
335         NxOutputReg r = new NxOutputRegBuilder()
336             .setSrc(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.nx.output.reg.SrcBuilder()
337                 .setSrcChoice(srcChoice)
338                 .setOfsNbits(31)
339                 .build())
340             .setMaxLen(0xffff)
341             .build();
342         return new NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder()
343             .setNxOutputReg(r).build();
344     }
345
346     public static Action nxOutputRegAction(Class<? extends NxmNxReg> reg) {
347         return nxOutputRegAction(new SrcNxRegCaseBuilder().setNxReg(reg).build());
348     }
349
350     public static Action nxResubmitAction(Integer inPort, Short table) {
351         NxResubmitBuilder builder = new NxResubmitBuilder();
352         if (inPort != null) {
353             builder.setInPort(inPort);
354         }
355         if (table != null) {
356             builder.setTable(table);
357         }
358         NxResubmit r = builder.build();
359         return new NxActionResubmitNodesNodeTableFlowApplyActionsCaseBuilder().setNxResubmit(r).build();
360     }
361
362     public static Action nxPushNshAction() {
363         NxPushNshBuilder builder = new NxPushNshBuilder();
364         NxPushNsh r = builder.build();
365         return new NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPushNsh(r).build();
366     }
367
368     public static Action nxPopNshAction() {
369         NxPopNshBuilder builder = new NxPopNshBuilder();
370         NxPopNsh r = builder.build();
371         return new NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPopNsh(r).build();
372     }
373
374     public static Action nxLoadNshMdtypeAction(Short value) {
375         return nxLoadRegAction(
376             new DstNxNshMdtypeCaseBuilder().setNxNshMdtype(Boolean.TRUE).build(),
377             BigInteger.valueOf(value.longValue()),
378             7,
379             false
380         );
381     }
382
383     public static Action nxLoadNshNpAction(Short value) {
384         return nxLoadRegAction(
385             new DstNxNshNpCaseBuilder().setNxNshNp(Boolean.TRUE).build(),
386             BigInteger.valueOf(value),
387             7,
388             false
389         );
390     }
391
392     public static Action nxSetNspAction(Long nsp) {
393         return nxLoadRegAction(
394             new DstNxNspCaseBuilder().setNxNspDst(Boolean.TRUE).build(),
395             BigInteger.valueOf(nsp),
396             23,
397             false
398         );
399     }
400
401     public static Action nxSetNsiAction(Short nsi) {
402         return nxLoadRegAction(
403             new DstNxNsiCaseBuilder().setNxNsiDst(Boolean.TRUE).build(),
404             BigInteger.valueOf(nsi.longValue()),
405             7,
406             false
407         );
408     }
409
410     public static Action nxLoadNshc1RegAction(Long value) {
411         return nxLoadRegAction(
412             new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(),
413             BigInteger.valueOf(value),
414             31,
415             false
416         );
417     }
418
419     public static Action nxLoadNshc2RegAction(Long value) {
420         return nxLoadRegAction(
421             new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(),
422             BigInteger.valueOf(value),
423             31,
424             false
425         );
426     }
427
428     public static Action nxLoadNshc3RegAction(Long value) {
429         return nxLoadRegAction(
430             new DstNxNshc3CaseBuilder().setNxNshc3Dst(Boolean.TRUE).build(),
431             BigInteger.valueOf(value),
432             31,
433             false
434         );
435     }
436
437     public static Action nxLoadNshc4RegAction(Long value) {
438         return nxLoadRegAction(
439             new DstNxNshc4CaseBuilder().setNxNshc4Dst(Boolean.TRUE).build(),
440             BigInteger.valueOf(value),
441             31,
442             false
443         );
444     }
445
446     public static Action nxMoveRegTunDstToNshc1() {
447         return nxMoveRegAction(
448                 new SrcNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
449                 new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(),
450                 31, false);
451     }
452
453     public static Action nxMoveTunIdtoNshc2() {
454         return nxMoveRegAction(
455                 new SrcNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
456                 new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(),
457                 31, false);
458     }
459
460     public static Action nxMoveNshc1ToTunIpv4Dst() {
461         return nxMoveRegAction(
462                 new SrcNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(),
463                 new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
464                 31, false);
465     }
466
467     public static Action nxMoveNshc2ToTunId() {
468         return nxMoveRegAction(
469                 new SrcNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(),
470                 new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
471                 31, false);
472     }
473
474     public static Action nxLoadTunIdAction(BigInteger tunnelId, boolean groupBucket) {
475         return nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), tunnelId, 31, groupBucket);
476     }
477
478     public static Action nxMultipathAction(OfjNxHashFields fields, Integer basis,
479             OfjNxMpAlgorithm algorithm, Integer maxLink, Long arg, DstChoice dstChoice,
480             Integer start, Integer end) {
481         NxMultipath r = new NxMultipathBuilder()
482             .setFields(fields)
483             .setBasis(basis)
484             .setAlgorithm(algorithm)
485             .setMaxLink(maxLink)
486             .setArg(arg)
487             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.nx.multipath.DstBuilder()
488                     .setDstChoice(dstChoice)
489                 .setStart(start)
490                 .setEnd(end)
491                 .build())
492             .build();
493         return new NxActionMultipathNodesNodeTableFlowApplyActionsCaseBuilder().setNxMultipath(r).build();
494     }
495
496     /**
497      * Builds the  conntrack action.
498      * @param flags the flags for the action
499      * @param zoneSrc the zoneSrc
500      * @param conntrackZone the conntrackZone
501      * @param recircTable the recirc table if it is a recirc action
502      * @return the conntrack action.
503      */
504     public static Action nxConntrackAction(Integer flags, Long zoneSrc,
505                                            Integer conntrackZone, Short recircTable) {
506         NxConntrack r = new NxConntrackBuilder()
507             .setFlags(flags)
508             .setZoneSrc(zoneSrc)
509             .setConntrackZone(conntrackZone)
510             .setRecircTable(recircTable)
511             .build();
512         return new NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder().setNxConntrack(r).build();
513     }
514
515     /**
516      * Builds the apply action builder for the action
517      * @param action the conntrack action.
518      * @return the apply action builder.
519      */
520     public static ApplyActionsBuilder conntrackActionBuilder(Action action) {
521         ActionBuilder ab = new ActionBuilder();
522         ab.setAction(action);
523         ab.setOrder(0);
524         ab.setKey(new ActionKey(0));
525         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList =
526                 new ArrayList<>();
527         actionList.add(ab.build());
528         ApplyActionsBuilder aab = new ApplyActionsBuilder();
529         aab.setAction(actionList);
530         return aab;
531     }
532     /**
533      * Accepts a MAC address and returns the corresponding long, where the
534      * MAC bytes are set on the lower order bytes of the long.
535      * @param macAddress
536      * @return a long containing the mac address bytes
537      */
538     public static long toLong(byte[] macAddress) {
539         long mac = 0;
540         for (int i = 0; i < 6; i++) {
541             long t = (macAddress[i] & 0xffL) << ((5-i)*8);
542             mac |= t;
543         }
544         return mac;
545     }
546
547     /**
548      * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
549      * matter, and returns the corresponding long, where the MAC bytes are set
550      * on the lower order bytes of the long.
551      *
552      * @param macAddress
553      *            in String format
554      * @return a long containing the mac address bytes
555      */
556     public static long toLong(MacAddress macAddress) {
557         return toLong(toMACAddress(macAddress.getValue()));
558     }
559
560     /**
561      * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
562      * matter, and returns a corresponding byte[].
563      * @param macAddress
564      * @return
565      */
566     public static byte[] toMACAddress(String macAddress) {
567         final String HEXES = "0123456789ABCDEF";
568         byte[] address = new byte[6];
569         String[] macBytes = macAddress.split(":");
570         if (macBytes.length != 6) {
571             throw new IllegalArgumentException(
572                     "Specified MAC Address must contain 12 hex digits" +
573                     " separated pairwise by :'s.");
574         }
575         for (int i = 0; i < 6; ++i) {
576             address[i] = (byte) ((HEXES.indexOf(macBytes[i].toUpperCase()
577                                                         .charAt(0)) << 4) | HEXES.indexOf(macBytes[i].toUpperCase()
578                                                                                                   .charAt(1)));
579         }
580         return address;
581     }
582 }