fix ofoverlay renderer
[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.openflowjava.nx.action.rev140421.action.container.action.choice.ActionResubmitBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.resubmit.grouping.NxActionResubmitBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNsh;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.push.nsh.grouping.NxPushNshBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNsh;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.pop.nsh.grouping.NxPopNshBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpShaCaseBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxArpThaCaseBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNspCaseBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCaseBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1CaseBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2CaseBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc3CaseBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc4CaseBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshMdtypeCaseBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshNpCaseBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunGpeNpCaseBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIdCaseBuilder;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpOpCaseBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpSpaCaseBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfArpTpaCaseBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfEthDstCaseBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder;
131 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;
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.NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder;
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.NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder;
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.NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder;
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.NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputReg;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputRegBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMove;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.NxRegMoveBuilder;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.SrcBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxArpShaCaseBuilder;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunGpeNpCaseBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIdCaseBuilder;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxTunIpv4DstCaseBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfArpSpaCaseBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcOfEthSrcCaseBuilder;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc1Key;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc2Key;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc3Key;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc4Key;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIpv4DstKey;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._1.grouping.NxmNxNshc1Builder;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._2.grouping.NxmNxNshc2Builder;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._3.grouping.NxmNxNshc3Builder;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._4.grouping.NxmNxNshc4Builder;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.ipv4.dst.grouping.NxmNxTunIpv4DstBuilder;
179 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
180
181 import com.google.common.collect.ImmutableList;
182 import com.google.common.net.InetAddresses;
183
184 /**
185  * Utilities for constructing OpenFlow flows
186  */
187 public final class FlowUtils {
188
189     /**
190      * ARP ethertype
191      */
192     public static final Long ARP = 0x0806L;
193     /**
194      * IPv4 ethertype
195      */
196     public static final Long IPv4 = 0x0800L;
197     /**
198      * IPv6 ethertype
199      */
200     public static final Long IPv6 = 0x86DDL;
201     /**
202      * VLAN ethertype
203      */
204     public static final Integer VLAN = 0x8100;
205
206     /**
207      * Creates an Instance Identifier (path) for node with specified id
208      *
209      * @param nodeId the ID of the node
210      * @return the {@link InstanceIdentifier}
211      */
212     public static InstanceIdentifier<Node> createNodePath(final NodeId nodeId) {
213         return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId)).build();
214     }
215
216     /**
217      * Creates a table path from a node ID and table ID
218      *
219      * @param nodeId the ID of the node
220      * @param tableId the ID of the table
221      * @return the {@link InstanceIdentifier}
222      */
223     public static InstanceIdentifier<Table> createTablePath(final NodeId nodeId, final short tableId) {
224         return createNodePath(nodeId).builder()
225             .augmentation(FlowCapableNode.class)
226             .child(Table.class, new TableKey(tableId))
227             .build();
228     }
229
230     /**
231      * Creates a group path from a node ID and group ID
232      *
233      * @param nodeId the Id of the node
234      * @param groupId the ID of the group table
235      * @return the {@link InstanceIdentifier}
236      */
237     public static InstanceIdentifier<Group> createGroupPath(final NodeId nodeId, final GroupId groupId) {
238         return createNodePath(nodeId).builder()
239             .augmentation(FlowCapableNode.class)
240             .child(Group.class, new GroupKey(groupId))
241             .build();
242     }
243
244     public static InstanceIdentifier<Group> createGroupPath(final NodeId nodeId, final Long groupId) {
245         return createGroupPath(nodeId, new GroupId(groupId));
246     }
247
248     /**
249      * Creates a group path from a node ID and group ID
250      *
251      * @param nodeId the Id of the node
252      * @param groupId the ID of the group table
253      * @param bucketId the ID of the bucket in the group table
254      * @return the {@link InstanceIdentifier}
255      */
256     public static InstanceIdentifier<Bucket> createBucketPath(final NodeId nodeId, final GroupId groupId,
257                                                               final BucketId bucketId) {
258         return createNodePath(nodeId).builder()
259             .augmentation(FlowCapableNode.class)
260             .child(Group.class, new GroupKey(groupId))
261             .child(Buckets.class)
262             .child(Bucket.class, new BucketKey(bucketId))
263             .build();
264     }
265
266     /**
267      * Creates a path for particular flow, by appending flow-specific information
268      * to table path.
269      *
270      * @param table the table iid
271      * @param flowKey the flow key
272      * @return the {@link InstanceIdentifier}
273      */
274     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table,
275             final FlowKey flowKey) {
276         return table.child(Flow.class, flowKey);
277     }
278
279     /**
280      * Creates a path for particular flow, by appending flow-specific information
281      * to table path.
282      *
283      * @param table the table iid
284      * @param flowId the flow id
285      * @return the {@link InstanceIdentifier}
286      */
287     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table, final FlowId flowId) {
288         return createFlowPath(table, new FlowKey(flowId));
289     }
290
291     public static Instructions gotoTableInstructions(short tableId) {
292         return new InstructionsBuilder().setInstruction(
293                 ImmutableList.of(new InstructionBuilder().setOrder(0)
294                     .setInstruction(gotoTableIns(tableId))
295                     .build())).build();
296     }
297
298     public static Instruction gotoTableIns(short tableId) {
299         return new GoToTableCaseBuilder().setGoToTable(new GoToTableBuilder().setTableId(tableId).build()).build();
300     }
301
302     public static ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList(
303             Action... actions) {
304         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist =
305                 new ArrayList<>();
306         int count = 0;
307         for (Action action : actions) {
308             alist.add(new ActionBuilder().setOrder(count++).setAction(action).build());
309         }
310         return alist;
311     }
312
313     public static ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList(
314             List<ActionBuilder> actions) {
315         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist =
316                 new ArrayList<>();
317         int count = 0;
318         for (ActionBuilder action : actions) {
319             alist.add(action.setOrder(count++).build());
320         }
321         return alist;
322     }
323
324     public static Instruction applyActionIns(Action... actions) {
325         return new ApplyActionsCaseBuilder()
326             .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actions)).build()).build();
327     }
328
329     public static Instruction applyActionIns(List<ActionBuilder> actions) {
330         return new ApplyActionsCaseBuilder()
331             .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actions)).build()).build();
332     }
333
334     public static Instruction writeActionIns(List<ActionBuilder> actions) {
335         return new WriteActionsCaseBuilder()
336             .setWriteActions(new WriteActionsBuilder().setAction(actionList(actions)).build()).build();
337     }
338
339     public static Instruction writeActionIns(Action... actions) {
340         return new WriteActionsCaseBuilder()
341             .setWriteActions(new WriteActionsBuilder().setAction(actionList(actions)).build()).build();
342     }
343
344     public static Instructions instructions(Instruction... instructions) {
345         ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> ins =
346                 new ArrayList<>();
347         int order = 0;
348         for (Instruction i : instructions) {
349             ins.add(new InstructionBuilder().setOrder(order++).setInstruction(i).build());
350         }
351         return new InstructionsBuilder().setInstruction(ins).build();
352     }
353
354     public static Instructions dropInstructions() {
355         return instructions(applyActionIns(dropAction()));
356     }
357
358     public static Action dropAction() {
359         return new DropActionCaseBuilder().setDropAction(new DropActionBuilder().build()).build();
360     }
361
362     public static Action createActionResubmit(Integer port, final short toTable) {
363         NxActionResubmitBuilder resubmit = new NxActionResubmitBuilder().setTable(toTable);
364
365         if (port != null) {
366             resubmit.setInPort(port);
367         }
368
369         ActionResubmitBuilder actionResubmitBuilder = new ActionResubmitBuilder();
370         actionResubmitBuilder.setNxActionResubmit(resubmit.build());
371
372         ResubmitConvertor convertor = new ResubmitConvertor();
373         ActionPath ap = ActionPath.INVENTORY_FLOWNODE_TABLE_WRITE_ACTIONS;
374
375         return convertor.convert(ActionUtil.createAction(actionResubmitBuilder.build()), ap);
376     }
377
378     public static Action outputAction(NodeConnectorId id) {
379         return new OutputActionCaseBuilder()
380             .setOutputAction(new OutputActionBuilder().setOutputNodeConnector(new Uri(id.getValue())).build()).build();
381     }
382
383     public static Action groupAction(Long id) {
384         return new GroupActionCaseBuilder().setGroupAction(new GroupActionBuilder().setGroupId(id).build()).build();
385     }
386
387     public static Action setDlSrcAction(MacAddress mac) {
388         return new SetDlSrcActionCaseBuilder().setSetDlSrcAction(new SetDlSrcActionBuilder().setAddress(mac).build())
389             .build();
390     }
391
392     public static Action setDlDstAction(MacAddress mac) {
393         return new SetDlDstActionCaseBuilder().setSetDlDstAction(new SetDlDstActionBuilder().setAddress(mac).build())
394             .build();
395     }
396
397     public static VlanMatch vlanMatch(int vlanId, boolean vlanIdPresent) {
398         return new VlanMatchBuilder()
399             .setVlanId(new VlanIdBuilder().setVlanId(new VlanId(vlanId)).setVlanIdPresent(vlanIdPresent).build())
400             .build();
401     }
402
403     public static List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder> pushVlanActions(
404             int vlanId) {
405         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder> actions =
406                 new ArrayList<>();
407         actions.add(new ActionBuilder().setAction(pushVlanAction()).setOrder(0));
408         actions.add(new ActionBuilder().setAction(setVlanId(vlanId)).setOrder(1));
409         return actions;
410     }
411
412     public static Action pushVlanAction() {
413         return new PushVlanActionCaseBuilder().setPushVlanAction(
414                 new PushVlanActionBuilder().setEthernetType(VLAN).build()).build();
415     }
416
417     public static Action setVlanId(int vlanId) {
418         return new SetVlanIdActionCaseBuilder().setSetVlanIdAction(
419                 new SetVlanIdActionBuilder().setVlanId(new VlanId(vlanId)).build()).build();
420     }
421
422     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction popVlanInstruction(
423             int order) {
424         List<ActionBuilder> actionBuilders = new ArrayList<>();
425         actionBuilders.add(new ActionBuilder().setAction(popVlanAction()).setOrder(0));
426         return new InstructionBuilder()
427             .setInstruction(new ApplyActionsCaseBuilder()
428                 .setApplyActions(new ApplyActionsBuilder().setAction(actionList(actionBuilders)).build()).build())
429             .setOrder(order)
430             .build();
431     }
432
433     public static Action popVlanAction() {
434         return new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build();
435     }
436
437     public static Action setIpv4DstAction(Ipv4Address ipAddress) {
438         Ipv4Builder ipDest = new Ipv4Builder();
439         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
440         ipDest.setIpv4Address(prefixdst);
441         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
442         setNwDstActionBuilder.setAddress(ipDest.build());
443         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
444     }
445
446     public static Action setIpv6DstAction(Ipv6Address ipAddress) {
447         Ipv6Builder ipDest = new Ipv6Builder();
448         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
449         ipDest.setIpv6Address(prefixdst);
450         SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
451         setNwDstActionBuilder.setAddress(ipDest.build());
452         return new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build();
453     }
454
455     public static Action setIpv4SrcAction(Ipv4Address ipAddress) {
456         Ipv4Builder ipSrc = new Ipv4Builder();
457         Ipv4Prefix prefixdst = new Ipv4Prefix(new Ipv4Prefix(ipAddress.getValue() + "/32"));
458         ipSrc.setIpv4Address(prefixdst);
459         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
460         setNwSrcActionBuilder.setAddress(ipSrc.build());
461         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
462     }
463
464     public static Action setIpv6SrcAction(Ipv6Address ipAddress) {
465         Ipv6Builder ipSrc = new Ipv6Builder();
466         Ipv6Prefix prefixdst = new Ipv6Prefix(new Ipv6Prefix(ipAddress.getValue() + "/128"));
467         ipSrc.setIpv6Address(prefixdst);
468         SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
469         setNwSrcActionBuilder.setAddress(ipSrc.build());
470         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwSrcActionBuilder.build()).build();
471     }
472
473     public static Action decNwTtlAction() {
474         return new DecNwTtlCaseBuilder().setDecNwTtl(new DecNwTtlBuilder().build()).build();
475     }
476
477     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value, int endOffset, boolean groupBucket) {
478         NxRegLoad r = new NxRegLoadBuilder().setDst(
479                 new DstBuilder().setDstChoice(dstChoice)
480                     .setStart(0)
481                     .setEnd(endOffset)
482                     .build())
483             .setValue(value)
484             .build();
485         if (groupBucket) {
486             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegLoad(r).build();
487         } else {
488             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(r).build();
489         }
490     }
491
492     public static Action nxPushNshAction() {
493         NxPushNshBuilder builder = new NxPushNshBuilder();
494         NxPushNsh r = builder.build();
495         return new NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPushNsh(r).build();
496     }
497
498     public static Action nxPopNshAction() {
499         NxPopNshBuilder builder = new NxPopNshBuilder();
500         NxPopNsh r = builder.build();
501         return new NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder().setNxPopNsh(r).build();
502     }
503
504     public static Action nxLoadNshMdtypeAction(Short value) {
505         return nxLoadRegAction(
506             new DstNxNshMdtypeCaseBuilder().setNxNshMdtype(Boolean.TRUE).build(),
507             BigInteger.valueOf(value.longValue()),
508             7,
509             false
510         );
511     }
512
513     public static Action nxLoadNshNpAction(Short value) {
514         return nxLoadRegAction(
515             new DstNxNshNpCaseBuilder().setNxNshNp(Boolean.TRUE).build(),
516             BigInteger.valueOf(value),
517             7,
518             false
519         );
520     }
521
522     public static Action nxSetNsiAction(Short nsi) {
523         return nxLoadRegAction(
524             new DstNxNsiCaseBuilder().setNxNsiDst(Boolean.TRUE).build(),
525             BigInteger.valueOf(nsi.longValue()),
526             7,
527             false
528         );
529     }
530
531     public static Action nxSetNspAction(Long nsp) {
532         return nxLoadRegAction(
533             new DstNxNspCaseBuilder().setNxNspDst(Boolean.TRUE).build(),
534             BigInteger.valueOf(nsp.longValue()),
535             23,
536             false
537         );
538     }
539
540     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value) {
541         return nxLoadRegAction(dstChoice, value, 31, false);
542     }
543
544     public static Action nxLoadRegAction(Class<? extends NxmNxReg> reg, BigInteger value) {
545         return nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(reg).build(), value);
546     }
547
548     public static Action nxLoadNshc1RegAction(Long value) {
549         return nxLoadRegAction(
550             new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(),
551             BigInteger.valueOf(value.longValue()),
552             31,
553             false
554         );
555     }
556
557     public static Action nxLoadNshc2RegAction(Long value) {
558         return nxLoadRegAction(
559             new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(),
560             BigInteger.valueOf(value.longValue()),
561             31,
562             false
563         );
564     }
565
566     public static Action nxLoadNshc3RegAction(Long value) {
567         return nxLoadRegAction(
568             new DstNxNshc3CaseBuilder().setNxNshc3Dst(Boolean.TRUE).build(),
569             BigInteger.valueOf(value.longValue()),
570             31,
571             false
572         );
573     }
574
575     public static Action nxLoadNshc4RegAction(Long value) {
576         return nxLoadRegAction(
577             new DstNxNshc4CaseBuilder().setNxNshc4Dst(Boolean.TRUE).build(),
578             BigInteger.valueOf(value.longValue()),
579             31,
580             false
581         );
582     }
583
584     public static Action nxLoadTunIPv4Action(String ipAddress, boolean groupBucket) {
585         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
586         long ipl = ip & 0xffffffffL;
587         return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
588                 BigInteger.valueOf(ipl), 31, groupBucket);
589     }
590
591     public static Action nxLoadArpOpAction(BigInteger value) {
592         return nxLoadRegAction(new DstOfArpOpCaseBuilder().setOfArpOp(Boolean.TRUE).build(), value, 15, false);
593     }
594
595     public static Action nxLoadArpShaAction(BigInteger value) {
596         return nxLoadRegAction(new DstNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(), value, 47, false);
597     }
598
599     public static Action nxLoadArpSpaAction(BigInteger value) {
600         return nxLoadRegAction(new DstOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(), value);
601     }
602
603     public static Action nxLoadArpSpaAction(String ipAddress) {
604         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
605         long ipl = ip & 0xffffffffL;
606         return nxLoadArpSpaAction(BigInteger.valueOf(ipl));
607     }
608
609     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice, int endOffset, boolean groupBucket) {
610         NxRegMove r = new NxRegMoveBuilder().setSrc(
611                 new SrcBuilder().setSrcChoice(srcChoice)
612                     .setStart(0)
613                     .setEnd(endOffset)
614                     .build())
615             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder().setDstChoice(
616                     dstChoice)
617                 .setStart(0)
618                 .setEnd(endOffset)
619                 .build())
620             .setDst(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.move.grouping.nx.reg.move.DstBuilder()
621                 .setDstChoice(dstChoice).setStart(0).setEnd(endOffset).build())
622             .build();
623         if (groupBucket) {
624             return new NxActionRegMoveNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegMove(r).build();
625         } else {
626             return new NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegMove(r).build();
627         }
628     }
629
630     public static Action nxMoveRegAction(SrcChoice srcChoice, DstChoice dstChoice) {
631         return nxMoveRegAction(srcChoice, dstChoice, 31, false);
632     }
633
634     public static Action nxMoveRegTunDstToNshc1() {
635         return nxMoveRegAction(new SrcNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
636                 new DstNxNshc1CaseBuilder().setNxNshc1Dst(Boolean.TRUE).build(), 31, false);
637     }
638
639     public static Action nxMoveTunGpeNp() {
640         return nxMoveRegAction(
641                 new SrcNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(),
642                 new DstNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(),
643                 7,
644                 false);
645     }
646
647     public static Action nxLoadTunGpeNpAction(BigInteger tunGpeNp, boolean groupBucket) {
648         return nxLoadRegAction(new DstNxTunGpeNpCaseBuilder().setNxTunGpeNp(Boolean.TRUE).build(), tunGpeNp, 7, groupBucket);
649     }
650
651     public static Action nxMoveTunIdtoNshc2() {
652         return nxMoveRegAction(new SrcNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(),
653                 new DstNxNshc2CaseBuilder().setNxNshc2Dst(Boolean.TRUE).build(), 31, false);
654     }
655
656     public static Action nxMoveRegTunIdAction(Class<? extends NxmNxReg> src, boolean groupBucket) {
657         return nxMoveRegAction(new SrcNxRegCaseBuilder().setNxReg(src).build(),
658                 new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), 31, groupBucket);
659     }
660
661     public static Action nxLoadTunIdAction(BigInteger tunnelId, boolean groupBucket) {
662         return nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), tunnelId, 31, groupBucket);
663     }
664
665     public static Action nxMoveArpShaToArpThaAction() {
666         return nxMoveRegAction(new SrcNxArpShaCaseBuilder().setNxArpSha(Boolean.TRUE).build(),
667                 new DstNxArpThaCaseBuilder().setNxArpTha(Boolean.TRUE).build(), 47, false);
668     }
669
670     public static Action nxMoveEthSrcToEthDstAction() {
671         return nxMoveRegAction(new SrcOfEthSrcCaseBuilder().setOfEthSrc(Boolean.TRUE).build(),
672                 new DstOfEthDstCaseBuilder().setOfEthDst(Boolean.TRUE).build(), 47, false);
673     }
674
675     public static Action nxMoveArpSpaToArpTpaAction() {
676         return nxMoveRegAction(new SrcOfArpSpaCaseBuilder().setOfArpSpa(Boolean.TRUE).build(),
677                 new DstOfArpTpaCaseBuilder().setOfArpTpa(Boolean.TRUE).build());
678     }
679
680     public static Action nxOutputRegAction(SrcChoice srcChoice) {
681         NxOutputReg r = new NxOutputRegBuilder().setSrc(
682                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.nx.output.reg.SrcBuilder().setSrcChoice(
683                         srcChoice)
684                     .setOfsNbits(31)
685                     .build())
686             .setMaxLen(0xffff)
687             .build();
688         return new NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder().setNxOutputReg(r).build();
689     }
690
691     public static Action nxOutputRegAction(Class<? extends NxmNxReg> reg) {
692         return nxOutputRegAction(new SrcNxRegCaseBuilder().setNxReg(reg).build());
693     }
694
695     public static class RegMatch {
696
697         final Class<? extends NxmNxReg> reg;
698         final Long value;
699
700         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
701             super();
702             this.reg = reg;
703             this.value = value;
704         }
705
706         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
707             return new RegMatch(reg, value);
708         }
709     }
710
711     public static void addNxRegMatch(MatchBuilder match, RegMatch... matches) {
712         ArrayList<ExtensionList> extensions = new ArrayList<>();
713         for (RegMatch rm : matches) {
714             Class<? extends ExtensionKey> key;
715             if (NxmNxReg0.class.equals(rm.reg)) {
716                 key = NxmNxReg0Key.class;
717             } else if (NxmNxReg1.class.equals(rm.reg)) {
718                 key = NxmNxReg1Key.class;
719             } else if (NxmNxReg2.class.equals(rm.reg)) {
720                 key = NxmNxReg2Key.class;
721             } else if (NxmNxReg3.class.equals(rm.reg)) {
722                 key = NxmNxReg3Key.class;
723             } else if (NxmNxReg4.class.equals(rm.reg)) {
724                 key = NxmNxReg4Key.class;
725             } else if (NxmNxReg5.class.equals(rm.reg)) {
726                 key = NxmNxReg5Key.class;
727             } else if (NxmNxReg6.class.equals(rm.reg)) {
728                 key = NxmNxReg6Key.class;
729             } else {
730                 key = NxmNxReg7Key.class;
731             }
732             NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
733                 .setNxmNxReg(new NxmNxRegBuilder().setReg(rm.reg).setValue(rm.value).build()).build();
734             extensions.add(new ExtensionListBuilder().setExtensionKey(key)
735                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
736                 .build());
737         }
738         GeneralAugMatchNodesNodeTableFlow m =
739                 new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
740         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
741     }
742
743     public static void addNxNshc1RegMatch(MatchBuilder match, Long value) {
744         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
745             .setNxmNxNshc1(new NxmNxNshc1Builder().setValue(value).build()).build();
746         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc1Key.class, am, match);
747         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
748     }
749
750     public static void addNxNshc2RegMatch(MatchBuilder match, Long value) {
751         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
752             .setNxmNxNshc2(new NxmNxNshc2Builder().setValue(value).build()).build();
753         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc2Key.class, am, match);
754         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
755     }
756
757     public static void addNxNshc3RegMatch(MatchBuilder match, Long value) {
758         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
759             .setNxmNxNshc3(new NxmNxNshc3Builder().setValue(value).build()).build();
760         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc3Key.class, am, match);
761         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
762     }
763
764     public static void addNxNshc4RegMatch(MatchBuilder match, Long value) {
765         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
766             .setNxmNxNshc4(new NxmNxNshc4Builder().setValue(value).build()).build();
767         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNshc4Key.class, am, match);
768         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
769     }
770
771     public static void addNxTunIdMatch(MatchBuilder match, int tunId) {
772         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
773             .setNxmNxTunId(new NxmNxTunIdBuilder().setValue(BigInteger.valueOf(tunId)).build()).build();
774         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIdKey.class, am, match);
775         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
776     }
777
778     public static void addNxTunIpv4DstMatch(MatchBuilder match, Ipv4Address ipv4Address) {
779         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
780             .setNxmNxTunIpv4Dst(new NxmNxTunIpv4DstBuilder().setIpv4Address(ipv4Address).build()).build();
781         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxTunIpv4DstKey.class, am, match);
782         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
783     }
784
785     public static void addNxNsiMatch(MatchBuilder match, short nsi) {
786         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
787             .setNxmNxNsi(new NxmNxNsiBuilder().setNsi(nsi).build()).build();
788         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNsiKey.class, am, match);
789         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
790     }
791
792     public static void addNxNspMatch(MatchBuilder match, Long nsp) {
793         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
794             .setNxmNxNsp(new NxmNxNspBuilder().setValue(nsp).build()).build();
795         GeneralAugMatchNodesNodeTableFlow m = addExtensionKeyAugmentationMatcher(NxmNxNspKey.class, am, match);
796         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
797     }
798
799     public static EthernetMatch ethernetMatch(MacAddress srcMac, MacAddress dstMac, Long etherType) {
800         EthernetMatchBuilder emb = new EthernetMatchBuilder();
801         if (srcMac != null)
802             emb.setEthernetSource(new EthernetSourceBuilder().setAddress(srcMac).build());
803         if (dstMac != null)
804             emb.setEthernetDestination(new EthernetDestinationBuilder().setAddress(dstMac).build());
805         if (etherType != null)
806             emb.setEthernetType(new EthernetTypeBuilder().setType(new EtherType(etherType)).build());
807         return emb.build();
808     }
809
810     private static List<ExtensionList> getExistingGeneralAugMatchNodesNodeTableFlow(MatchBuilder match) {
811         ArrayList<ExtensionList> extensions = new ArrayList<>();
812         if (match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class) != null) {
813             List<ExtensionList> existingExtensions =
814                     match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class).getExtensionList();
815             if (existingExtensions != null && !existingExtensions.isEmpty()) {
816                 extensions.addAll(existingExtensions);
817             }
818         }
819         return extensions;
820     }
821
822     private static GeneralAugMatchNodesNodeTableFlow addExtensionKeyAugmentationMatcher(
823             Class<? extends ExtensionKey> key, NxAugMatchNodesNodeTableFlow am, MatchBuilder match) {
824         List<ExtensionList> extensions = getExistingGeneralAugMatchNodesNodeTableFlow(match);
825         extensions.add(new ExtensionListBuilder().setExtensionKey(key)
826             .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, am).build())
827             .build());
828         return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
829     }
830
831     /**
832      * Parse an OF port number from a node connector ID
833      *
834      * @param id the ID
835      * @return the port number
836      */
837     public static long getOfPortNum(NodeConnectorId id) {
838         String cnid = id.getValue();
839         int ci = cnid.lastIndexOf(':');
840         if (ci < 0 || (ci + 1 >= cnid.length()))
841             throw new NumberFormatException("Invalid node connector ID " + cnid);
842         return Long.parseLong(cnid.substring(ci + 1));
843     }
844
845     /**
846      * Get a base flow builder with some common features already set
847      *
848      * @return {@link FlowBuilder}
849      */
850     public static FlowBuilder base(short tableId) {
851         return new FlowBuilder()
852                 .setTableId(tableId)
853                 .setBarrier(false)
854                 .setHardTimeout(0)
855                 .setIdleTimeout(0);
856     }
857
858     /**
859      * Convert string value to byte array
860      *
861      * @param values input value as a (@link String)
862      * @return byte representation of input data
863      */
864     public static byte[] bytesFromHexString(String values) {
865         String target = "";
866         if (values != null) {
867             target = values;
868         }
869         String[] octets = target.split(":");
870
871         byte[] ret = new byte[octets.length];
872         for (int i = 0; i < octets.length; i++) {
873             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
874         }
875         return ret;
876     }
877 }