Bug 3738: GBP coexistence with SFC
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / FlowUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
16 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.action.ActionUtil;
17 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.action.ResubmitConvertor;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionResubmitBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.resubmit.grouping.NxActionResubmitBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpShaCaseBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpThaCaseBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1CaseBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2CaseBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIdCaseBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpOpCaseBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpSpaCaseBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpTpaCaseBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfEthDstCaseBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder;
119 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;
120 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;
121 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;
122 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.NxActionSetNshc1NodesNodeTableFlowApplyActionsCaseBuilder;
123 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.NxActionSetNshc2NodesNodeTableFlowApplyActionsCaseBuilder;
124 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.NxActionSetNshc3NodesNodeTableFlowApplyActionsCaseBuilder;
125 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.NxActionSetNshc4NodesNodeTableFlowApplyActionsCaseBuilder;
126 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.NxActionSetNsiNodesNodeTableFlowApplyActionsCaseBuilder;
127 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.NxActionSetNspNodesNodeTableFlowApplyActionsCaseBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputReg;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputRegBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMove;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMoveBuilder;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.SrcBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._1.grouping.NxSetNshc1;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._1.grouping.NxSetNshc1Builder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._2.grouping.NxSetNshc2;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._2.grouping.NxSetNshc2Builder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._3.grouping.NxSetNshc3;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._3.grouping.NxSetNshc3Builder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._4.grouping.NxSetNshc4;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._4.grouping.NxSetNshc4Builder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsi.grouping.NxSetNsi;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsi.grouping.NxSetNsiBuilder;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsp.grouping.NxSetNsp;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsp.grouping.NxSetNspBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxArpShaCaseBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIdCaseBuilder;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIpv4DstCaseBuilder;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfArpSpaCaseBuilder;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfEthSrcCaseBuilder;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc1Key;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc2Key;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc3Key;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc4Key;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIpv4DstKey;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._1.grouping.NxmNxNshc1Builder;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._2.grouping.NxmNxNshc2Builder;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._3.grouping.NxmNxNshc3Builder;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._4.grouping.NxmNxNshc4Builder;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.ipv4.dst.grouping.NxmNxTunIpv4DstBuilder;
182 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
183
184 import com.google.common.collect.ImmutableList;
185 import com.google.common.net.InetAddresses;
186
187 /**
188  * Utilities for constructing OpenFlow flows
189  */
190 public final class FlowUtils {
191
192     /**
193      * ARP ethertype
194      */
195     public static final Long ARP = 0x0806L;
196     /**
197      * IPv4 ethertype
198      */
199     public static final Long IPv4 = 0x0800L;
200     /**
201      * IPv6 ethertype
202      */
203     public static final Long IPv6 = 0x86DDL;
204     /**
205      * VLAN ethertype
206      */
207     public static final Integer VLAN = 0x8100;
208
209     /**
210      * Creates an Instance Identifier (path) for node with specified id
211      *
212      * @param nodeId the ID of the node
213      * @return the {@link InstanceIdentifier}
214      */
215     public static InstanceIdentifier<Node> createNodePath(final NodeId nodeId) {
216         return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId)).build();
217     }
218
219     /**
220      * Creates a table path from a node ID and table ID
221      *
222      * @param nodeId the ID of the node
223      * @param tableId the ID of the table
224      * @return the {@link InstanceIdentifier}
225      */
226     public static InstanceIdentifier<Table> createTablePath(final NodeId nodeId, final short tableId) {
227         return createNodePath(nodeId).builder()
228             .augmentation(FlowCapableNode.class)
229             .child(Table.class, new TableKey(tableId))
230             .build();
231     }
232
233     /**
234      * Creates a group path from a node ID and group ID
235      *
236      * @param nodeId the Id of the node
237      * @param groupId the ID of the group table
238      * @return the {@link InstanceIdentifier}
239      */
240     public static InstanceIdentifier<Group> createGroupPath(final NodeId nodeId, final GroupId groupId) {
241         return createNodePath(nodeId).builder()
242             .augmentation(FlowCapableNode.class)
243             .child(Group.class, new GroupKey(groupId))
244             .build();
245     }
246
247     public static InstanceIdentifier<Group> createGroupPath(final NodeId nodeId, final Long groupId) {
248         return createGroupPath(nodeId, new GroupId(groupId));
249     }
250
251     /**
252      * Creates a group path from a node ID and group ID
253      *
254      * @param nodeId the Id of the node
255      * @param groupId the ID of the group table
256      * @param bucketId the ID of the bucket in the group table
257      * @return the {@link InstanceIdentifier}
258      */
259     public static InstanceIdentifier<Bucket> createBucketPath(final NodeId nodeId, final GroupId groupId,
260                                                               final BucketId bucketId) {
261         return createNodePath(nodeId).builder()
262             .augmentation(FlowCapableNode.class)
263             .child(Group.class, new GroupKey(groupId))
264             .child(Buckets.class)
265             .child(Bucket.class, new BucketKey(bucketId))
266             .build();
267     }
268
269     /**
270      * Creates a path for particular flow, by appending flow-specific information
271      * to table path.
272      *
273      * @param table the table iid
274      * @param flowKey the flow key
275      * @return the {@link InstanceIdentifier}
276      */
277     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table,
278             final FlowKey flowKey) {
279         return table.child(Flow.class, flowKey);
280     }
281
282     /**
283      * Creates a path for particular flow, by appending flow-specific information
284      * to table path.
285      *
286      * @param table the table iid
287      * @param flowId the flow id
288      * @return the {@link InstanceIdentifier}
289      */
290     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table, final FlowId flowId) {
291         return createFlowPath(table, new FlowKey(flowId));
292     }
293
294     public static Instructions gotoTableInstructions(short tableId) {
295         return new InstructionsBuilder().setInstruction(
296                 ImmutableList.of(new InstructionBuilder().setOrder(0)
297                     .setInstruction(gotoTableIns(tableId))
298                     .build())).build();
299     }
300
301     public static Instruction gotoTableIns(short tableId) {
302         return new GoToTableCaseBuilder().setGoToTable(new GoToTableBuilder().setTableId(tableId).build()).build();
303     }
304
305     public static ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList(
306             Action... actions) {
307         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist =
308                 new ArrayList<>();
309         int count = 0;
310         for (Action action : actions) {
311             alist.add(new ActionBuilder().setOrder(count++).setAction(action).build());
312         }
313         return alist;
314     }
315
316     public static ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList(
317             List<ActionBuilder> actions) {
318         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist =
319                 new ArrayList<>();
320         int count = 0;
321         for (ActionBuilder action : actions) {
322             alist.add(action.setOrder(count++).build());
323         }
324         return alist;
325     }
326
327     public static Instruction applyActionIns(Action... actions) {
328         return new ApplyActionsCaseBuilder()
329             .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actions)).build()).build();
330     }
331
332     public static Instruction applyActionIns(List<ActionBuilder> actions) {
333         return new ApplyActionsCaseBuilder()
334             .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actions)).build()).build();
335     }
336
337     public static Instruction writeActionIns(List<ActionBuilder> actions) {
338         return new WriteActionsCaseBuilder()
339             .setWriteActions(new WriteActionsBuilder().setAction(actionList(actions)).build()).build();
340     }
341
342     public static Instruction writeActionIns(Action... actions) {
343         return new WriteActionsCaseBuilder()
344             .setWriteActions(new WriteActionsBuilder().setAction(actionList(actions)).build()).build();
345     }
346
347     public static Instructions instructions(Instruction... instructions) {
348         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> ins =
349                 new ArrayList<>();
350         int order = 0;
351         for (Instruction i : instructions) {
352             ins.add(new InstructionBuilder().setOrder(order++).setInstruction(i).build());
353         }
354         return new InstructionsBuilder().setInstruction(ins).build();
355     }
356
357     public static Instructions dropInstructions() {
358         return instructions(applyActionIns(dropAction()));
359     }
360
361     public static Action dropAction() {
362         return new DropActionCaseBuilder().setDropAction(new DropActionBuilder().build()).build();
363     }
364
365     public static Action createActionResubmit(Integer port, final short toTable) {
366         NxActionResubmitBuilder resubmit = new NxActionResubmitBuilder().setTable(toTable);
367
368         if (port != null) {
369             resubmit.setInPort(port);
370         }
371
372         ActionResubmitBuilder actionResubmitBuilder = new ActionResubmitBuilder();
373         actionResubmitBuilder.setNxActionResubmit(resubmit.build());
374
375         ResubmitConvertor convertor = new ResubmitConvertor();
376         ActionPath ap =
377                 ActionPath.NODES_NODE_TABLE_FLOW_INSTRUCTIONS_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION_EXTENSIONLIST_EXTENSION;
378
379         return convertor.convert(ActionUtil.createAction(actionResubmitBuilder.build()), ap);
380     }
381
382     public static Action outputAction(NodeConnectorId id) {
383         return new OutputActionCaseBuilder()
384             .setOutputAction(new OutputActionBuilder().setOutputNodeConnector(new Uri(id.getValue())).build()).build();
385     }
386
387     public static Action groupAction(Long id) {
388         return new GroupActionCaseBuilder().setGroupAction(new GroupActionBuilder().setGroupId(id).build()).build();
389     }
390
391     public static Action setDlSrcAction(MacAddress mac) {
392         return new SetDlSrcActionCaseBuilder().setSetDlSrcAction(new SetDlSrcActionBuilder().setAddress(mac).build())
393             .build();
394     }
395
396     public static Action setDlDstAction(MacAddress mac) {
397         return new SetDlDstActionCaseBuilder().setSetDlDstAction(new SetDlDstActionBuilder().setAddress(mac).build())
398             .build();
399     }
400
401     public static VlanMatch vlanMatch(int vlanId, boolean vlanIdPresent) {
402         return new VlanMatchBuilder()
403             .setVlanId(new VlanIdBuilder().setVlanId(new VlanId(vlanId)).setVlanIdPresent(vlanIdPresent).build())
404             .build();
405     }
406
407     public static List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder> pushVlanActions(
408             int vlanId) {
409         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder> actions =
410                 new ArrayList<>();
411         actions.add(new ActionBuilder().setAction(pushVlanAction()).setOrder(0));
412         actions.add(new ActionBuilder().setAction(setVlanId(vlanId)).setOrder(1));
413         return actions;
414     }
415
416     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction popVlanInstruction(
417             int order) {
418         List<ActionBuilder> actionBuilders = new ArrayList<>();
419         actionBuilders.add(new ActionBuilder().setAction(popVlanAction()).setOrder(0));
420         return new InstructionBuilder()
421             .setInstruction(new ApplyActionsCaseBuilder()
422                 .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actionBuilders)).build()).build())
423             .setOrder(order)
424             .build();
425     }
426
427     public static Action setIpv4DstAction(Ipv4Address ipAddress) {
428         Ipv4Builder ipDest = new Ipv4Builder();
429         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
430         ipDest.setIpv4Address(prefixdst);
431         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
432         setNwDstActionBuilder.setAddress(ipDest.build());
433         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
434     }
435
436     public static Action setIpv6DstAction(Ipv6Address ipAddress) {
437         Ipv6Builder ipDest = new Ipv6Builder();
438         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
439         ipDest.setIpv6Address(prefixdst);
440         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
441         setNwDstActionBuilder.setAddress(ipDest.build());
442         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
443     }
444
445     public static Action setIpv4SrcAction(Ipv4Address ipAddress) {
446         Ipv4Builder ipSrc = new Ipv4Builder();
447         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
448         ipSrc.setIpv4Address(prefixdst);
449         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
450         setNwSrcActionBuilder.setAddress(ipSrc.build());
451         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
452     }
453
454     public static Action setIpv6SrcAction(Ipv6Address ipAddress) {
455         Ipv6Builder ipSrc = new Ipv6Builder();
456         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
457         ipSrc.setIpv6Address(prefixdst);
458         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
459         setNwSrcActionBuilder.setAddress(ipSrc.build());
460         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
461     }
462
463     public static Action decNwTtlAction() {
464         return new DecNwTtlCaseBuilder().setDecNwTtl(new DecNwTtlBuilder().build()).build();
465     }
466
467     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value, int endOffset, boolean groupBucket) {
468         NxRegLoad r = new NxRegLoadBuilder().setDst(
469                 new DstBuilder().setDstChoice(dstChoice)
470                     .setStart(0)
471                     .setEnd(endOffset)
472                     .build())
473             .setValue(value)
474             .build();
475         if (groupBucket) {
476             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegLoad(r).build();
477         } else {
478             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(r).build();
479         }
480     }
481
482     public static Action nxSetNsiAction(Short nsi) {
483         NxSetNsi newNsi = new NxSetNsiBuilder().setNsi(nsi).build();
484         return new NxActionSetNsiNodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNsi(newNsi).build();
485     }
486
487     public static Action nxSetNspAction(Long nsp) {
488         NxSetNsp newNsp = new NxSetNspBuilder().setNsp(nsp).build();
489         return new NxActionSetNspNodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNsp(newNsp).build();
490     }
491
492     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value) {
493         return nxLoadRegAction(dstChoice, value, 31, false);
494     }
495
496     public static Action nxLoadRegAction(Class<? extends NxmNxReg> reg, BigInteger value) {
497         return nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(reg).build(), value);
498     }
499
500     public static Action nxLoadNshc1RegAction(Long value) {
501         NxSetNshc1 newNshc1 = new NxSetNshc1Builder().setNshc(value).build();
502         return new NxActionSetNshc1NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc1(newNshc1).build();
503     }
504
505     public static Action nxLoadNshc2RegAction(Long value) {
506         NxSetNshc2 newNshc2 = new NxSetNshc2Builder().setNshc(value).build();
507         return new NxActionSetNshc2NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc2(newNshc2).build();
508     }
509
510     public static Action nxLoadNshc3RegAction(Long value) {
511         NxSetNshc3 newNshc3 = new NxSetNshc3Builder().setNshc(value).build();
512         return new NxActionSetNshc3NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc3(newNshc3).build();
513     }
514
515     public static Action nxLoadNshc4RegAction(Long value) {
516         NxSetNshc4 newNshc4 = new NxSetNshc4Builder().setNshc(value).build();
517         return new NxActionSetNshc4NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc4(newNshc4).build();
518     }
519
520     public static Action nxLoadTunIPv4Action(String ipAddress, boolean groupBucket) {
521         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
522         long ipl = ip & 0xffffffffL;
523         return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
524                 BigInteger.valueOf(ipl), 31, groupBucket);
525     }
526
527     public static Action nxLoadArpOpAction(BigInteger value) {
528         return nxLoadRegAction(new DstOfArpOpCaseBuilder().setOfArpOp(Boolean.TRUE).build(), value, 15, false);
529     }
530
531     public static Action nxLoadArpShaAction(BigInteger value) {
532         return nxLoadRegAction(new DstNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(), value, 47, false);
533     }
534
535     public static Action nxLoadArpSpaAction(BigInteger value) {
536         return nxLoadRegAction(new DstOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(), value);
537     }
538
539     public static Action nxLoadArpSpaAction(String ipAddress) {
540         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
541         long ipl = ip & 0xffffffffL;
542         return nxLoadArpSpaAction(BigInteger.valueOf(ipl));
543     }
544
545     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice, int endOffset, boolean groupBucket) {
546         NxRegMove r = new NxRegMoveBuilder().setSrc(
547                 new SrcBuilder().setSrcChoice(srcChoice)
548                     .setStart(0)
549                     .setEnd(endOffset)
550                     .build())
551             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder().setDstChoice(
552                     dstChoice)
553                 .setStart(0)
554                 .setEnd(endOffset)
555                 .build())
556             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder()
557                 .setDstChoice(dstChoice).setStart(0).setEnd(endOffset).build())
558             .build();
559         if (groupBucket) {
560             return new NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegMove(r).build();
561         } else {
562             return new NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegMove(r).build();
563         }
564     }
565
566     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice) {
567         return nxMoveRegAction(srcChoice, dstChoice, 31, false);
568     }
569
570     public static Action nxMoveRegTunDstToNshc1() {
571         return nxMoveRegAction(new SrcNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
572                 new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(), 31, false);
573     }
574
575     public static Action nxMoveTunIdtoNshc2() {
576         return nxMoveRegAction(new SrcNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
577                 new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(), 31, false);
578     }
579
580     public static Action nxMoveRegTunIdAction(Class<? extends NxmNxReg> src, boolean groupBucket) {
581         return nxMoveRegAction(new SrcNxRegCaseBuilder().setNxReg(src).build(),
582                 new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), 31, groupBucket);
583     }
584
585     public static Action nxLoadTunIdAction(BigInteger tunnelId, boolean groupBucket) {
586         return nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), tunnelId, 31, groupBucket);
587     }
588
589     public static Action nxMoveArpShaToArpThaAction() {
590         return nxMoveRegAction(new SrcNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(),
591                 new DstNxArpThaCaseBuilder().setNxArpTha(Boolean.TRUE).build(), 47, false);
592     }
593
594     public static Action nxMoveEthSrcToEthDstAction() {
595         return nxMoveRegAction(new SrcOfEthSrcCaseBuilder().setOfEthSrc(Boolean.TRUE).build(),
596                 new DstOfEthDstCaseBuilder().setOfEthDst(Boolean.TRUE).build(), 47, false);
597     }
598
599     public static Action nxMoveArpSpaToArpTpaAction() {
600         return nxMoveRegAction(new SrcOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(),
601                 new DstOfArpTpaCaseBuilder().setOfArpTpa(Boolean.TRUE).build());
602     }
603
604     public static Action nxOutputRegAction(SrcChoice srcChoice) {
605         NxOutputReg r = new NxOutputRegBuilder().setSrc(
606                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.nx.output.reg.SrcBuilder().setSrcChoice(
607                         srcChoice)
608                     .setOfsNbits(31)
609                     .build())
610             .setMaxLen(0xffff)
611             .build();
612         return new NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder().setNxOutputReg(r).build();
613     }
614
615     public static Action nxOutputRegAction(Class<? extends NxmNxReg> reg) {
616         return nxOutputRegAction(new SrcNxRegCaseBuilder().setNxReg(reg).build());
617     }
618
619     public static class RegMatch {
620
621         final Class<? extends NxmNxReg> reg;
622         final Long value;
623
624         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
625             super();
626             this.reg = reg;
627             this.value = value;
628         }
629
630         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
631             return new RegMatch(reg, value);
632         }
633     }
634
635     public static void addNxRegMatch(MatchBuilder match, RegMatch... matches) {
636         ArrayList<ExtensionList> extensions = new ArrayList<>();
637         for (RegMatch rm : matches) {
638             Class<? extends ExtensionKey> key;
639             if (NxmNxReg0.class.equals(rm.reg)) {
640                 key = NxmNxReg0Key.class;
641             } else if (NxmNxReg1.class.equals(rm.reg)) {
642                 key = NxmNxReg1Key.class;
643             } else if (NxmNxReg2.class.equals(rm.reg)) {
644                 key = NxmNxReg2Key.class;
645             } else if (NxmNxReg3.class.equals(rm.reg)) {
646                 key = NxmNxReg3Key.class;
647             } else if (NxmNxReg4.class.equals(rm.reg)) {
648                 key = NxmNxReg4Key.class;
649             } else if (NxmNxReg5.class.equals(rm.reg)) {
650                 key = NxmNxReg5Key.class;
651             } else if (NxmNxReg6.class.equals(rm.reg)) {
652                 key = NxmNxReg6Key.class;
653             } else {
654                 key = NxmNxReg7Key.class;
655             }
656             NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
657                 .setNxmNxReg(new NxmNxRegBuilder().setReg(rm.reg).setValue(rm.value).build()).build();
658             extensions.add(new ExtensionListBuilder().setExtensionKey(key)
659                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
660                 .build());
661         }
662         GeneralAugMatchNodesNodeTableFlow m =
663                 new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
664         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
665     }
666
667     public static void addNxNshc1RegMatch(MatchBuilder match, Long value) {
668         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
669             .setNxmNxNshc1(new NxmNxNshc1Builder().setValue(value).build()).build();
670         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc1Key.class, am, match);
671         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
672     }
673
674     public static void addNxNshc2RegMatch(MatchBuilder match, Long value) {
675         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
676             .setNxmNxNshc2(new NxmNxNshc2Builder().setValue(value).build()).build();
677         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc2Key.class, am, match);
678         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
679     }
680
681     public static void addNxNshc3RegMatch(MatchBuilder match, Long value) {
682         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
683             .setNxmNxNshc3(new NxmNxNshc3Builder().setValue(value).build()).build();
684         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc3Key.class, am, match);
685         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
686     }
687
688     public static void addNxNshc4RegMatch(MatchBuilder match, Long value) {
689         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
690             .setNxmNxNshc4(new NxmNxNshc4Builder().setValue(value).build()).build();
691         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc4Key.class, am, match);
692         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
693     }
694
695     public static void addNxTunIdMatch(MatchBuilder match, int tunId) {
696         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
697             .setNxmNxTunId(new NxmNxTunIdBuilder().setValue(BigInteger.valueOf(tunId)).build()).build();
698         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIdKey.class, am, match);
699         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
700     }
701
702     public static void addNxTunIpv4DstMatch(MatchBuilder match, Ipv4Address ipv4Address) {
703         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
704             .setNxmNxTunIpv4Dst(new NxmNxTunIpv4DstBuilder().setIpv4Address(ipv4Address).build()).build();
705         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIpv4DstKey.class, am, match);
706         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
707     }
708
709     public static void addNxNsiMatch(MatchBuilder match, short nsi) {
710         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
711             .setNxmNxNsi(new NxmNxNsiBuilder().setNsi(nsi).build()).build();
712         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNsiKey.class, am, match);
713         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
714     }
715
716     public static void addNxNspMatch(MatchBuilder match, Long nsp) {
717         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
718             .setNxmNxNsp(new NxmNxNspBuilder().setValue(nsp).build()).build();
719         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNspKey.class, am, match);
720         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
721     }
722
723     public static EthernetMatch ethernetMatch(MacAddress srcMac, MacAddress dstMac, Long etherType) {
724         EthernetMatchBuilder emb = new EthernetMatchBuilder();
725         if (srcMac != null)
726             emb.setEthernetSource(new EthernetSourceBuilder().setAddress(srcMac).build());
727         if (dstMac != null)
728             emb.setEthernetDestination(new EthernetDestinationBuilder().setAddress(dstMac).build());
729         if (etherType != null)
730             emb.setEthernetType(new EthernetTypeBuilder().setType(new EtherType(etherType)).build());
731         return emb.build();
732     }
733
734     private static Action pushVlanAction() {
735         return new PushVlanActionCaseBuilder()
736             .setPushVlanAction(new PushVlanActionBuilder().setEthernetType(VLAN).build()).build();
737     }
738
739     private static Action popVlanAction() {
740         return new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build();
741     }
742
743     private static Action setVlanId(int vlanId) {
744         return new SetVlanIdActionCaseBuilder()
745             .setSetVlanIdAction(new SetVlanIdActionBuilder().setVlanId(new VlanId(vlanId)).build()).build();
746     }
747
748     private static List<ExtensionList> getExistingGeneralAugMatchNodesNodeTableFlow(MatchBuilder match) {
749         ArrayList<ExtensionList> extensions = new ArrayList<>();
750         if (match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class) != null) {
751             List<ExtensionList> existingExtensions =
752                     match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class).getExtensionList();
753             if (existingExtensions != null && !existingExtensions.isEmpty()) {
754                 extensions.addAll(existingExtensions);
755             }
756         }
757         return extensions;
758     }
759
760     private static GeneralAugMatchNodesNodeTableFlow addExtensionKeyAugmentationMatcher(
761             Class<? extends ExtensionKey> key, NxAugMatchNodesNodeTableFlow am, MatchBuilder match) {
762         List<ExtensionList> extensions = getExistingGeneralAugMatchNodesNodeTableFlow(match);
763         extensions.add(new ExtensionListBuilder().setExtensionKey(key)
764             .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
765             .build());
766         return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
767     }
768
769     /**
770      * Parse an OF port number from a node connector ID
771      *
772      * @param id the ID
773      * @return the port number
774      */
775     public static long getOfPortNum(NodeConnectorId id) {
776         String cnid = id.getValue();
777         int ci = cnid.lastIndexOf(':');
778         if (ci < 0 || (ci + 1 >= cnid.length()))
779             throw new NumberFormatException("Invalid node connector ID " + cnid);
780         return Long.parseLong(cnid.substring(ci + 1));
781     }
782 }