Support new actions in new ovs nsh patches
[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.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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.FlowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionResubmitBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.resubmit.grouping.NxActionResubmitBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNsh;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNshBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNsh;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNshBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpShaCaseBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpThaCaseBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNspCaseBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCaseBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1CaseBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2CaseBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc3CaseBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc4CaseBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshMdtypeCaseBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshNpCaseBuilder;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunGpeNpCaseBuilder;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIdCaseBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpOpCaseBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpSpaCaseBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpTpaCaseBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfEthDstCaseBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder;
132 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;
133 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;
134 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;
135 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;
136 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;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputReg;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputRegBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMove;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMoveBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.SrcBuilder;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxArpShaCaseBuilder;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunGpeNpCaseBuilder;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIdCaseBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIpv4DstCaseBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfArpSpaCaseBuilder;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfEthSrcCaseBuilder;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc1Key;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc2Key;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc3Key;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc4Key;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunGpeNpKey;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIpv4DstKey;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._1.grouping.NxmNxNshc1Builder;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._2.grouping.NxmNxNshc2Builder;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._3.grouping.NxmNxNshc3Builder;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._4.grouping.NxmNxNshc4Builder;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.gpe.np.grouping.NxmNxTunGpeNpBuilder;
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 Action pushVlanAction() {
417         return new PushVlanActionCaseBuilder().setPushVlanAction(
418                 new PushVlanActionBuilder().setEthernetType(VLAN).build()).build();
419     }
420
421     public static Action setVlanId(int vlanId) {
422         return new SetVlanIdActionCaseBuilder().setSetVlanIdAction(
423                 new SetVlanIdActionBuilder().setVlanId(new VlanId(vlanId)).build()).build();
424     }
425
426     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction popVlanInstruction(
427             int order) {
428         List<ActionBuilder> actionBuilders = new ArrayList<>();
429         actionBuilders.add(new ActionBuilder().setAction(popVlanAction()).setOrder(0));
430         return new InstructionBuilder()
431             .setInstruction(new ApplyActionsCaseBuilder()
432                 .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actionBuilders)).build()).build())
433             .setOrder(order)
434             .build();
435     }
436
437     public static Action popVlanAction() {
438         return new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build();
439     }
440
441     public static Action setIpv4DstAction(Ipv4Address ipAddress) {
442         Ipv4Builder ipDest = new Ipv4Builder();
443         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
444         ipDest.setIpv4Address(prefixdst);
445         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
446         setNwDstActionBuilder.setAddress(ipDest.build());
447         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
448     }
449
450     public static Action setIpv6DstAction(Ipv6Address ipAddress) {
451         Ipv6Builder ipDest = new Ipv6Builder();
452         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
453         ipDest.setIpv6Address(prefixdst);
454         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
455         setNwDstActionBuilder.setAddress(ipDest.build());
456         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
457     }
458
459     public static Action setIpv4SrcAction(Ipv4Address ipAddress) {
460         Ipv4Builder ipSrc = new Ipv4Builder();
461         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
462         ipSrc.setIpv4Address(prefixdst);
463         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
464         setNwSrcActionBuilder.setAddress(ipSrc.build());
465         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
466     }
467
468     public static Action setIpv6SrcAction(Ipv6Address ipAddress) {
469         Ipv6Builder ipSrc = new Ipv6Builder();
470         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
471         ipSrc.setIpv6Address(prefixdst);
472         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
473         setNwSrcActionBuilder.setAddress(ipSrc.build());
474         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
475     }
476
477     public static Action decNwTtlAction() {
478         return new DecNwTtlCaseBuilder().setDecNwTtl(new DecNwTtlBuilder().build()).build();
479     }
480
481     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value, int endOffset, boolean groupBucket) {
482         NxRegLoad r = new NxRegLoadBuilder().setDst(
483                 new DstBuilder().setDstChoice(dstChoice)
484                     .setStart(0)
485                     .setEnd(endOffset)
486                     .build())
487             .setValue(value)
488             .build();
489         if (groupBucket) {
490             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegLoad(r).build();
491         } else {
492             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(r).build();
493         }
494     }
495
496     public static Action nxPushNshAction() {
497         NxPushNshBuilder builder = new NxPushNshBuilder();
498         NxPushNsh r = builder.build();
499         return new NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPushNsh(r).build();
500     }
501
502     public static Action nxPopNshAction() {
503         NxPopNshBuilder builder = new NxPopNshBuilder();
504         NxPopNsh r = builder.build();
505         return new NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPopNsh(r).build();
506     }
507
508     public static Action nxLoadNshMdtypeAction(Short value) {
509         return nxLoadRegAction(
510             new DstNxNshMdtypeCaseBuilder().setNxNshMdtype(Boolean.TRUE).build(),
511             BigInteger.valueOf(value.longValue()),
512             7,
513             false
514         );
515     }
516
517     public static Action nxLoadNshNpAction(Short value) {
518         return nxLoadRegAction(
519             new DstNxNshNpCaseBuilder().setNxNshNp(Boolean.TRUE).build(),
520             BigInteger.valueOf(value),
521             7,
522             false
523         );
524     }
525
526     public static Action nxSetNsiAction(Short nsi) {
527         return nxLoadRegAction(
528             new DstNxNsiCaseBuilder().setNxNsiDst(Boolean.TRUE).build(),
529             BigInteger.valueOf(nsi.longValue()),
530             7,
531             false
532         );
533     }
534
535     public static Action nxSetNspAction(Long nsp) {
536         return nxLoadRegAction(
537             new DstNxNspCaseBuilder().setNxNspDst(Boolean.TRUE).build(),
538             BigInteger.valueOf(nsp.longValue()),
539             23,
540             false
541         );
542     }
543
544     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value) {
545         return nxLoadRegAction(dstChoice, value, 31, false);
546     }
547
548     public static Action nxLoadRegAction(Class<? extends NxmNxReg> reg, BigInteger value) {
549         return nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(reg).build(), value);
550     }
551
552     public static Action nxLoadNshc1RegAction(Long value) {
553         return nxLoadRegAction(
554             new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(),
555             BigInteger.valueOf(value.longValue()),
556             31,
557             false
558         );
559     }
560
561     public static Action nxLoadNshc2RegAction(Long value) {
562         return nxLoadRegAction(
563             new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(),
564             BigInteger.valueOf(value.longValue()),
565             31,
566             false
567         );
568     }
569
570     public static Action nxLoadNshc3RegAction(Long value) {
571         return nxLoadRegAction(
572             new DstNxNshc3CaseBuilder().setNxNshc3Dst(Boolean.TRUE).build(),
573             BigInteger.valueOf(value.longValue()),
574             31,
575             false
576         );
577     }
578
579     public static Action nxLoadNshc4RegAction(Long value) {
580         return nxLoadRegAction(
581             new DstNxNshc4CaseBuilder().setNxNshc4Dst(Boolean.TRUE).build(),
582             BigInteger.valueOf(value.longValue()),
583             31,
584             false
585         );
586     }
587
588     public static Action nxLoadTunIPv4Action(String ipAddress, boolean groupBucket) {
589         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
590         long ipl = ip & 0xffffffffL;
591         return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
592                 BigInteger.valueOf(ipl), 31, groupBucket);
593     }
594
595     public static Action nxLoadArpOpAction(BigInteger value) {
596         return nxLoadRegAction(new DstOfArpOpCaseBuilder().setOfArpOp(Boolean.TRUE).build(), value, 15, false);
597     }
598
599     public static Action nxLoadArpShaAction(BigInteger value) {
600         return nxLoadRegAction(new DstNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(), value, 47, false);
601     }
602
603     public static Action nxLoadArpSpaAction(BigInteger value) {
604         return nxLoadRegAction(new DstOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(), value);
605     }
606
607     public static Action nxLoadArpSpaAction(String ipAddress) {
608         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
609         long ipl = ip & 0xffffffffL;
610         return nxLoadArpSpaAction(BigInteger.valueOf(ipl));
611     }
612
613     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice, int endOffset, boolean groupBucket) {
614         NxRegMove r = new NxRegMoveBuilder().setSrc(
615                 new SrcBuilder().setSrcChoice(srcChoice)
616                     .setStart(0)
617                     .setEnd(endOffset)
618                     .build())
619             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder().setDstChoice(
620                     dstChoice)
621                 .setStart(0)
622                 .setEnd(endOffset)
623                 .build())
624             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder()
625                 .setDstChoice(dstChoice).setStart(0).setEnd(endOffset).build())
626             .build();
627         if (groupBucket) {
628             return new NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegMove(r).build();
629         } else {
630             return new NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegMove(r).build();
631         }
632     }
633
634     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice) {
635         return nxMoveRegAction(srcChoice, dstChoice, 31, false);
636     }
637
638     public static Action nxMoveRegTunDstToNshc1() {
639         return nxMoveRegAction(new SrcNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
640                 new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(), 31, false);
641     }
642
643     public static Action nxMoveTunGpeNp() {
644         return nxMoveRegAction(
645                 new SrcNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(),
646                 new DstNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(),
647                 7,
648                 false);
649     }
650
651     public static Action nxLoadTunGpeNpAction(BigInteger tunGpeNp, boolean groupBucket) {
652         return nxLoadRegAction(new DstNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(), tunGpeNp, 7, groupBucket);
653     }
654
655     public static Action nxMoveTunIdtoNshc2() {
656         return nxMoveRegAction(new SrcNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
657                 new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(), 31, false);
658     }
659
660     public static Action nxMoveRegTunIdAction(Class<? extends NxmNxReg> src, boolean groupBucket) {
661         return nxMoveRegAction(new SrcNxRegCaseBuilder().setNxReg(src).build(),
662                 new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), 31, groupBucket);
663     }
664
665     public static Action nxLoadTunIdAction(BigInteger tunnelId, boolean groupBucket) {
666         return nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), tunnelId, 31, groupBucket);
667     }
668
669     public static Action nxMoveArpShaToArpThaAction() {
670         return nxMoveRegAction(new SrcNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(),
671                 new DstNxArpThaCaseBuilder().setNxArpTha(Boolean.TRUE).build(), 47, false);
672     }
673
674     public static Action nxMoveEthSrcToEthDstAction() {
675         return nxMoveRegAction(new SrcOfEthSrcCaseBuilder().setOfEthSrc(Boolean.TRUE).build(),
676                 new DstOfEthDstCaseBuilder().setOfEthDst(Boolean.TRUE).build(), 47, false);
677     }
678
679     public static Action nxMoveArpSpaToArpTpaAction() {
680         return nxMoveRegAction(new SrcOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(),
681                 new DstOfArpTpaCaseBuilder().setOfArpTpa(Boolean.TRUE).build());
682     }
683
684     public static Action nxOutputRegAction(SrcChoice srcChoice) {
685         NxOutputReg r = new NxOutputRegBuilder().setSrc(
686                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.nx.output.reg.SrcBuilder().setSrcChoice(
687                         srcChoice)
688                     .setOfsNbits(31)
689                     .build())
690             .setMaxLen(0xffff)
691             .build();
692         return new NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder().setNxOutputReg(r).build();
693     }
694
695     public static Action nxOutputRegAction(Class<? extends NxmNxReg> reg) {
696         return nxOutputRegAction(new SrcNxRegCaseBuilder().setNxReg(reg).build());
697     }
698
699     public static class RegMatch {
700
701         final Class<? extends NxmNxReg> reg;
702         final Long value;
703
704         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
705             super();
706             this.reg = reg;
707             this.value = value;
708         }
709
710         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
711             return new RegMatch(reg, value);
712         }
713     }
714
715     public static void addNxRegMatch(MatchBuilder match, RegMatch... matches) {
716         ArrayList<ExtensionList> extensions = new ArrayList<>();
717         for (RegMatch rm : matches) {
718             Class<? extends ExtensionKey> key;
719             if (NxmNxReg0.class.equals(rm.reg)) {
720                 key = NxmNxReg0Key.class;
721             } else if (NxmNxReg1.class.equals(rm.reg)) {
722                 key = NxmNxReg1Key.class;
723             } else if (NxmNxReg2.class.equals(rm.reg)) {
724                 key = NxmNxReg2Key.class;
725             } else if (NxmNxReg3.class.equals(rm.reg)) {
726                 key = NxmNxReg3Key.class;
727             } else if (NxmNxReg4.class.equals(rm.reg)) {
728                 key = NxmNxReg4Key.class;
729             } else if (NxmNxReg5.class.equals(rm.reg)) {
730                 key = NxmNxReg5Key.class;
731             } else if (NxmNxReg6.class.equals(rm.reg)) {
732                 key = NxmNxReg6Key.class;
733             } else {
734                 key = NxmNxReg7Key.class;
735             }
736             NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
737                 .setNxmNxReg(new NxmNxRegBuilder().setReg(rm.reg).setValue(rm.value).build()).build();
738             extensions.add(new ExtensionListBuilder().setExtensionKey(key)
739                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
740                 .build());
741         }
742         GeneralAugMatchNodesNodeTableFlow m =
743                 new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
744         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
745     }
746
747     public static void addNxNshc1RegMatch(MatchBuilder match, Long value) {
748         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
749             .setNxmNxNshc1(new NxmNxNshc1Builder().setValue(value).build()).build();
750         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc1Key.class, am, match);
751         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
752     }
753
754     public static void addNxNshc2RegMatch(MatchBuilder match, Long value) {
755         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
756             .setNxmNxNshc2(new NxmNxNshc2Builder().setValue(value).build()).build();
757         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc2Key.class, am, match);
758         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
759     }
760
761     public static void addNxNshc3RegMatch(MatchBuilder match, Long value) {
762         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
763             .setNxmNxNshc3(new NxmNxNshc3Builder().setValue(value).build()).build();
764         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc3Key.class, am, match);
765         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
766     }
767
768     public static void addNxNshc4RegMatch(MatchBuilder match, Long value) {
769         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
770             .setNxmNxNshc4(new NxmNxNshc4Builder().setValue(value).build()).build();
771         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc4Key.class, am, match);
772         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
773     }
774
775     public static void addNxTunIdMatch(MatchBuilder match, int tunId) {
776         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
777             .setNxmNxTunId(new NxmNxTunIdBuilder().setValue(BigInteger.valueOf(tunId)).build()).build();
778         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIdKey.class, am, match);
779         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
780     }
781
782     public static void addNxTunIpv4DstMatch(MatchBuilder match, Ipv4Address ipv4Address) {
783         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
784             .setNxmNxTunIpv4Dst(new NxmNxTunIpv4DstBuilder().setIpv4Address(ipv4Address).build()).build();
785         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIpv4DstKey.class, am, match);
786         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
787     }
788
789     public static void addNxNsiMatch(MatchBuilder match, short nsi) {
790         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
791             .setNxmNxNsi(new NxmNxNsiBuilder().setNsi(nsi).build()).build();
792         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNsiKey.class, am, match);
793         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
794     }
795
796     public static void addNxNspMatch(MatchBuilder match, Long nsp) {
797         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
798             .setNxmNxNsp(new NxmNxNspBuilder().setValue(nsp).build()).build();
799         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNspKey.class, am, match);
800         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
801     }
802
803     public static EthernetMatch ethernetMatch(MacAddress srcMac, MacAddress dstMac, Long etherType) {
804         EthernetMatchBuilder emb = new EthernetMatchBuilder();
805         if (srcMac != null)
806             emb.setEthernetSource(new EthernetSourceBuilder().setAddress(srcMac).build());
807         if (dstMac != null)
808             emb.setEthernetDestination(new EthernetDestinationBuilder().setAddress(dstMac).build());
809         if (etherType != null)
810             emb.setEthernetType(new EthernetTypeBuilder().setType(new EtherType(etherType)).build());
811         return emb.build();
812     }
813
814     private static List<ExtensionList> getExistingGeneralAugMatchNodesNodeTableFlow(MatchBuilder match) {
815         ArrayList<ExtensionList> extensions = new ArrayList<>();
816         if (match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class) != null) {
817             List<ExtensionList> existingExtensions =
818                     match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class).getExtensionList();
819             if (existingExtensions != null && !existingExtensions.isEmpty()) {
820                 extensions.addAll(existingExtensions);
821             }
822         }
823         return extensions;
824     }
825
826     private static GeneralAugMatchNodesNodeTableFlow addExtensionKeyAugmentationMatcher(
827             Class<? extends ExtensionKey> key, NxAugMatchNodesNodeTableFlow am, MatchBuilder match) {
828         List<ExtensionList> extensions = getExistingGeneralAugMatchNodesNodeTableFlow(match);
829         extensions.add(new ExtensionListBuilder().setExtensionKey(key)
830             .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
831             .build());
832         return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
833     }
834
835     /**
836      * Parse an OF port number from a node connector ID
837      *
838      * @param id the ID
839      * @return the port number
840      */
841     public static long getOfPortNum(NodeConnectorId id) {
842         String cnid = id.getValue();
843         int ci = cnid.lastIndexOf(':');
844         if (ci < 0 || (ci + 1 >= cnid.length()))
845             throw new NumberFormatException("Invalid node connector ID " + cnid);
846         return Long.parseLong(cnid.substring(ci + 1));
847     }
848
849     /**
850      * Get a base flow builder with some common features already set
851      *
852      * @return {@link FlowBuilder}
853      */
854     public static FlowBuilder base(short tableId) {
855         return new FlowBuilder()
856                 .setTableId(tableId)
857                 .setBarrier(false)
858                 .setHardTimeout(0)
859                 .setIdleTimeout(0);
860     }
861
862     /**
863      * Convert string value to byte array
864      *
865      * @param values input value as a (@link String)
866      * @return byte representation of input data
867      */
868     public static byte[] bytesFromHexString(String values) {
869         String target = "";
870         if (values != null) {
871             target = values;
872         }
873         String[] octets = target.split(":");
874
875         byte[] ret = new byte[octets.length];
876         for (int i = 0; i < octets.length; i++) {
877             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
878         }
879         return ret;
880     }
881 }