Add support for official OVS NSH implementation
[netvirt.git] / sfc / classifier / impl / src / main / java / org / opendaylight / netvirt / sfc / classifier / utils / OpenFlow13Utils.java
1 /*
2  * Copyright © 2017 Ericsson, 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 package org.opendaylight.netvirt.sfc.classifier.utils;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.PacketTypeMatch;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.PacketTypeMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
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.DstNxNshc4CaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCaseBuilder;
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.DstNxRegCase;
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.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder;
64 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.NxActionDecapNodesNodeTableFlowApplyActionsCaseBuilder;
65 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.NxActionEncapNodesNodeTableFlowApplyActionsCaseBuilder;
66 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;
67 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;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionResubmitNodesNodeTableFlowWriteActionsCaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.decap.grouping.NxDecap;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.decap.grouping.NxDecapBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.encap.grouping.NxEncap;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.encap.grouping.NxEncapBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.Dst;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMove;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMoveBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.SrcBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.resubmit.grouping.NxResubmitBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxNshc4CaseBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIdCaseBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIpv4DstKey;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxReg;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.ipv4.dst.grouping.NxmNxTunIpv4DstBuilder;
98
99 public final class OpenFlow13Utils {
100     public static final long ETHERTYPE_NSH = 0x894f;
101     public static final long PACKET_TYPE_NSH = 0x1894f;
102     public static final long PACKET_TYPE_ETH = 0;
103
104     private OpenFlow13Utils() {
105     }
106
107     public static void addMatchTunId(MatchBuilder match, long value) {
108         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
109                 .setNxmNxTunId(new NxmNxTunIdBuilder().setValue(BigInteger.valueOf(value)).build()).build();
110         addExtension(match, NxmNxTunIdKey.class, am);
111     }
112
113     public static void addMatchTunDstIp(MatchBuilder match, Ipv4Address ipv4Address) {
114         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
115                 .setNxmNxTunIpv4Dst(new NxmNxTunIpv4DstBuilder().setIpv4Address(ipv4Address).build()).build();
116         addExtension(match, NxmNxTunIpv4DstKey.class, am);
117     }
118
119     public static void addMatchEthNsh(MatchBuilder match) {
120         EtherType etherType = new EtherType(ETHERTYPE_NSH);
121         EthernetType ethernetType = new EthernetTypeBuilder().setType(etherType).build();
122         EthernetMatch ethernetMatch = new EthernetMatchBuilder().setEthernetType(ethernetType).build();
123         match.setEthernetMatch(ethernetMatch);
124     }
125
126     public static void addMatchPacketTypeNsh(MatchBuilder match) {
127         PacketTypeMatch packetTypeMatch = new PacketTypeMatchBuilder().setPacketType(PACKET_TYPE_NSH).build();
128         match.setPacketTypeMatch(packetTypeMatch);
129     }
130
131     public static void addMatchInPort(MatchBuilder match, NodeId nodeId, long inPort) {
132         match.setInPort(new NodeConnectorId(nodeId.getValue() + ":" + inPort));
133     }
134
135     public static void addMatchNsp(MatchBuilder match, long nsp) {
136         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
137                 .setNxmNxNsp(new NxmNxNspBuilder().setValue(nsp).build()).build();
138         addExtension(match, NxmNxNspKey.class, am);
139     }
140
141     public static void addMatchNsi(MatchBuilder match, short nsi) {
142         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
143                 .setNxmNxNsi(new NxmNxNsiBuilder().setNsi(nsi).build()).build();
144         addExtension(match, NxmNxNsiKey.class, am);
145     }
146
147     public static void addMatchReg2(MatchBuilder match, long value) {
148         NxmNxReg nxmNxReg = new NxmNxRegBuilder().setReg(NxmNxReg2.class).setValue(value).build();
149         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxReg(nxmNxReg).build();
150         addExtension(match, NxmNxReg2Key.class, am);
151     }
152
153     private static void addExtension(MatchBuilder match, Class<? extends ExtensionKey> extensionKey,
154                                      NxAugMatchNodesNodeTableFlow am) {
155         GeneralAugMatchNodesNodeTableFlow existingAugmentations = match
156             .augmentation(GeneralAugMatchNodesNodeTableFlow.class);
157         List<ExtensionList> extensions = null;
158         if (existingAugmentations != null) {
159             extensions = existingAugmentations.getExtensionList();
160         }
161         if (extensions == null) {
162             extensions = new ArrayList<>();
163         }
164
165         extensions.add(new ExtensionListBuilder().setExtensionKey(extensionKey)
166             .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
167             .build());
168
169         GeneralAugMatchNodesNodeTableFlow generalAugMatchNodesNode = new GeneralAugMatchNodesNodeTableFlowBuilder()
170             .setExtensionList(extensions).build();
171         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, generalAugMatchNodesNode);
172     }
173
174     public static Action createActionResubmitTable(final short toTable, int order) {
175         return createActionBuilder(order)
176                 .setAction(new NxActionResubmitNodesNodeTableFlowWriteActionsCaseBuilder()
177                         .setNxResubmit(new NxResubmitBuilder()
178                                 .setTable(toTable)
179                                 .build())
180                         .build())
181                 .build();
182     }
183
184     public static Action createActionNxLoadTunIpv4Dst(long value, int order) {
185         ActionBuilder ab = createActionBuilder(order);
186         ab.setAction(nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
187                 BigInteger.valueOf(value), 0,31, false));
188
189         return ab.build();
190     }
191
192     public static Action createActionNxLoadTunId(long value, int order) {
193         ActionBuilder ab = createActionBuilder(order);
194         ab.setAction(nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
195                 BigInteger.valueOf(value), 0,31, false));
196
197         return ab.build();
198     }
199
200     public static Action createActionNxEncapNsh(int order) {
201         return createActionNxEncap(order, PACKET_TYPE_NSH);
202     }
203
204     public static Action createActionNxEncapEthernet(int order) {
205         return createActionNxEncap(order, PACKET_TYPE_ETH);
206     }
207
208     private static Action createActionNxEncap(int order, long packetType) {
209         NxEncap nxEncap = new NxEncapBuilder().setPacketType(packetType).build();
210         ActionBuilder ab = createActionBuilder(order);
211         ab.setAction(new NxActionEncapNodesNodeTableFlowApplyActionsCaseBuilder().setNxEncap(nxEncap).build());
212         return ab.build();
213     }
214
215     public static Action createActionNxDecap(int order) {
216         NxDecap nxDecap = new NxDecapBuilder().build();
217         ActionBuilder ab = createActionBuilder(order);
218         ab.setAction(new NxActionDecapNodesNodeTableFlowApplyActionsCaseBuilder().setNxDecap(nxDecap).build());
219         return ab.build();
220     }
221
222     public static Action createActionNxMoveTunIdToNsc2Register(int order) {
223         ActionBuilder ab = createActionBuilder(order);
224         ab.setAction(nxMoveRegAction(new SrcNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), 0, 31,
225             new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(), 0, 31,
226                 false));
227
228         return ab.build();
229     }
230
231     public static Action createActionNxMoveReg0ToNsc1Register(int order) {
232         ActionBuilder ab = createActionBuilder(order);
233         ab.setAction(nxMoveRegAction(
234                 new SrcNxRegCaseBuilder().setNxReg(NxmNxReg0.class).build(), 0, 31,
235                 new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(), 0,31,
236                 false));
237
238         return ab.build();
239     }
240
241     public static Action createActionNxMoveReg6ToNsc4Register(int order) {
242         ActionBuilder ab = createActionBuilder(order);
243         ab.setAction(nxMoveRegAction(new SrcNxRegCaseBuilder().setNxReg(NxmNxReg6.class).build(), 0, 31,
244                 new DstNxNshc4CaseBuilder().setNxNshc4Dst(Boolean.TRUE).build(), 0, 31,
245                 false));
246
247         return ab.build();
248     }
249
250     public static Action createActionNxMoveNsc4ToReg6Register(int order) {
251         ActionBuilder ab = createActionBuilder(order);
252         ab.setAction(nxMoveRegAction(new SrcNxNshc4CaseBuilder().setNxNshc4Dst(Boolean.TRUE).build(), 0, 31,
253                 new DstNxRegCaseBuilder().setNxReg(NxmNxReg6.class).build(), 0, 31,
254                 false));
255
256         return ab.build();
257     }
258
259     public static Action createActionNxLoadNspToReg2High(long value, int order) {
260         ActionBuilder ab = createActionBuilder(order);
261         DstNxRegCase dstNxRegCase = new DstNxRegCaseBuilder().setNxReg(NxmNxReg2.class).build();
262         ab.setAction(
263                 nxLoadRegAction(dstNxRegCase, BigInteger.valueOf(value), 8, 31, false));
264         return ab.build();
265     }
266
267     public static Action createActionNxMoveReg2HighToNsp(int order) {
268         ActionBuilder ab = createActionBuilder(order);
269         ab.setAction(nxMoveRegAction(
270                 new SrcNxRegCaseBuilder().setNxReg(NxmNxReg2.class).build(), 8, 31,
271                 new DstNxNspCaseBuilder().setNxNspDst(true).build(), 0, 23,
272                 false));
273
274         return ab.build();
275     }
276
277     public static Action createActionNxLoadNsiToReg2Low(long value, int order) {
278         ActionBuilder ab = createActionBuilder(order);
279         DstNxRegCase dstNxRegCase = new DstNxRegCaseBuilder().setNxReg(NxmNxReg2.class).build();
280         ab.setAction(
281                 nxLoadRegAction(dstNxRegCase, BigInteger.valueOf(value), 0, 7, false));
282         return ab.build();
283     }
284
285     public static Action createActionNxMoveReg2LowToNsi(int order) {
286         ActionBuilder ab = createActionBuilder(order);
287         ab.setAction(nxMoveRegAction(
288                 new SrcNxRegCaseBuilder().setNxReg(NxmNxReg2.class).build(), 0, 7,
289                 new DstNxNsiCaseBuilder().setNxNsiDst(true).build(), 0, 7,
290                 false));
291
292         return ab.build();
293     }
294
295     public static Action createActionNxLoadReg2(long value, int order) {
296         ActionBuilder ab = createActionBuilder(order);
297         DstNxRegCase dstNxRegCase = new DstNxRegCaseBuilder().setNxReg(NxmNxReg2.class).build();
298         ab.setAction(
299                 nxLoadRegAction(dstNxRegCase, BigInteger.valueOf(value), 0, 31, false));
300         return ab.build();
301     }
302
303
304     private static org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nxLoadRegAction(
305             DstChoice dstChoice, BigInteger value, int startOffset, int endOffset, boolean groupBucket) {
306         Dst dst = new DstBuilder().setDstChoice(dstChoice).setStart(startOffset).setEnd(endOffset).build();
307         NxRegLoad regLoad = new NxRegLoadBuilder().setDst(dst).setValue(value).build();
308
309         if (groupBucket) {
310             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegLoad(regLoad).build();
311         } else {
312             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(regLoad).build();
313         }
314     }
315
316     public static org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nxMoveRegAction(
317         SrcChoice srcChoice, int srcStartOffset, int srcEndOffset,
318         DstChoice dstChoice, int dstStartOffset, int dstEndOffset,
319         boolean groupBucket) {
320
321         NxRegMove nxRegMove = new NxRegMoveBuilder()
322             .setSrc(new SrcBuilder().setSrcChoice(srcChoice).setStart(srcStartOffset).setEnd(srcEndOffset).build())
323             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira
324                 .action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder()
325                 .setDstChoice(dstChoice).setStart(dstStartOffset).setEnd(dstEndOffset).build())
326             .build();
327
328         if (groupBucket) {
329             return new NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegMove(nxRegMove).build();
330         } else {
331             return new NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegMove(nxRegMove).build();
332         }
333     }
334
335     public static Action createActionOutPort(final String portUri, final int order) {
336         OutputActionBuilder output = new OutputActionBuilder();
337         Uri value = new Uri(portUri);
338         output.setOutputNodeConnector(value);
339         ActionBuilder ab = createActionBuilder(order);
340         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
341
342         return ab.build();
343     }
344
345     public static ActionBuilder createActionBuilder(int order) {
346         ActionBuilder ab = new ActionBuilder();
347         ab.setOrder(order);
348         ab.withKey(new ActionKey(order));
349
350         return ab;
351     }
352
353     public static InstructionsBuilder wrapActionsIntoApplyActionsInstruction(List<Action> theActions) {
354         // Create an Apply Action
355         ApplyActionsBuilder aab = new ApplyActionsBuilder();
356         aab.setAction(theActions);
357
358         // Wrap our Apply Action in an Instruction
359         InstructionBuilder ib = new InstructionBuilder();
360         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
361         ib.setOrder(0);
362         ib.withKey(new InstructionKey(0));
363
364         // Put our Instruction in a list of Instructions
365         List<Instruction> instructions = new ArrayList<>();
366         instructions.add(ib.build());
367         return new InstructionsBuilder().setInstruction(instructions);
368     }
369
370     public static FlowBuilder createFlowBuilder(final short table, final int priority, final BigInteger cookieValue,
371                                                 final String flowName, final String flowIdStr, MatchBuilder match,
372                                                 InstructionsBuilder isb) {
373         FlowBuilder flow = new FlowBuilder();
374         flow.setId(new FlowId(flowIdStr));
375         flow.withKey(new FlowKey(new FlowId(flowIdStr)));
376         flow.setTableId(table);
377         flow.setFlowName(flowName);
378         flow.setCookie(new FlowCookie(cookieValue));
379         flow.setCookieMask(new FlowCookie(cookieValue));
380         flow.setContainerName(null);
381         flow.setStrict(false);
382         flow.setMatch(match.build());
383         flow.setInstructions(isb.build());
384         flow.setPriority(priority);
385         flow.setHardTimeout(0);
386         flow.setIdleTimeout(0);
387         flow.setFlags(new FlowModFlags(false, false, false, false, false));
388         if (null == flow.isBarrier()) {
389             flow.setBarrier(Boolean.FALSE);
390         }
391
392         return flow;
393     }
394
395     public static InstructionsBuilder appendGotoTableInstruction(InstructionsBuilder isb, short nextTableId) {
396         if (isb.getInstruction() == null) {
397             isb.setInstruction(new ArrayList<>());
398         }
399         isb.getInstruction().add(createGotoTableInstruction(nextTableId, isb.getInstruction().size()));
400         return isb;
401     }
402
403     public static Instruction createGotoTableInstruction(short nextTableId, int order) {
404         GoToTableBuilder gotoIngress = createActionGotoTable(nextTableId);
405
406         return new InstructionBuilder().withKey(new InstructionKey(order)).setOrder(order)
407             .setInstruction(new GoToTableCaseBuilder().setGoToTable(gotoIngress.build()).build()).build();
408     }
409
410     public static GoToTableBuilder createActionGotoTable(final short toTable) {
411         GoToTableBuilder gotoTb = new GoToTableBuilder();
412         gotoTb.setTableId(toTable);
413
414         return gotoTb;
415     }
416
417     public static NxRegLoad createNxLoadReg0(long value) {
418         Dst dst = new DstBuilder()
419                 .setDstChoice(new DstNxRegCaseBuilder().setNxReg(NxmNxReg0.class).build())
420                 .setStart(0)
421                 .setEnd(31)
422                 .build();
423         return new NxRegLoadBuilder()
424                 .setDst(dst)
425                 .setValue(BigInteger.valueOf(value))
426                 .build();
427     }
428
429     public static Action createAction(
430             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action, int order) {
431         return new ActionBuilder()
432                 .setOrder(order)
433                 .withKey(new ActionKey(order))
434                 .setAction(action)
435                 .build();
436     }
437 }