Fix ActionNxConntrack.NxNat problems with xtendbeans (with a test)
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / actions / ActionNxConntrack.java
1 /*
2  * Copyright © 2016, 2017 Red Hat, Inc. and others.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.genius.mdsalutil.actions;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import org.opendaylight.genius.mdsalutil.ActionInfo;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.NxConntrackBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.nx.conntrack.CtActions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.nx.conntrack.CtActionsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.ofpact.actions.ofpact.actions.NxActionCtMarkCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.ofpact.actions.ofpact.actions.NxActionNatCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.ofpact.actions.ofpact.actions.nx.action.ct.mark._case.NxActionCtMarkBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.ofpact.actions.ofpact.actions.nx.action.nat._case.NxActionNatBuilder;
27
28 /**
29  * NX conntrack action.
30  */
31 public class ActionNxConntrack extends ActionInfo {
32
33     private final int flags;
34     private final long zoneSrc;
35     private final int conntrackZone;
36     private final short recircTable;
37     private final List<NxCtAction> ctActions = new ArrayList<>();
38
39     public ActionNxConntrack(int flags, long zoneSrc, int conntrackZone, short recircTable) {
40         this(0, flags, zoneSrc, conntrackZone, recircTable, Collections.emptyList());
41     }
42
43     public ActionNxConntrack(int flags, long zoneSrc, int conntrackZone, short recircTable,
44             List<NxCtAction> ctActions) {
45         this(0, flags, zoneSrc, conntrackZone, recircTable, ctActions);
46     }
47
48     public ActionNxConntrack(int actionKey, int flags, long zoneSrc, int conntrackZone, short recircTable) {
49         this(actionKey, flags, zoneSrc, conntrackZone, recircTable, Collections.emptyList());
50     }
51
52     public ActionNxConntrack(int actionKey, int flags, long zoneSrc, int conntrackZone, short recircTable,
53             List<NxCtAction> ctActions) {
54         super(actionKey);
55         this.flags = flags;
56         this.zoneSrc = zoneSrc;
57         this.conntrackZone = conntrackZone;
58         this.recircTable = recircTable;
59         this.ctActions.addAll(ctActions);
60     }
61
62     @Override
63     public Action buildAction() {
64         return buildAction(getActionKey());
65     }
66
67     @Override
68     public Action buildAction(int newActionKey) {
69         NxConntrackBuilder ctb = new NxConntrackBuilder()
70                 .setFlags(flags)
71                 .setZoneSrc(zoneSrc)
72                 .setConntrackZone(conntrackZone)
73                 .setRecircTable(recircTable);
74         ctb.setCtActions(this.ctActions.stream().map(NxCtAction::buildCtActions).collect(Collectors.toList()));
75         ActionBuilder ab = new ActionBuilder();
76         ab.setAction(new NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder()
77                 .setNxConntrack(ctb.build()).build());
78         ab.setKey(new ActionKey(newActionKey));
79         return ab.build();
80     }
81
82     public int getFlags() {
83         return flags;
84     }
85
86     public long getZoneSrc() {
87         return zoneSrc;
88     }
89
90     public int getConntrackZone() {
91         return conntrackZone;
92     }
93
94     public short getRecircTable() {
95         return recircTable;
96     }
97
98     public List<NxCtAction> getCtActions() {
99         return ctActions;
100     }
101
102     @Override
103     public boolean equals(Object other) {
104         if (this == other) {
105             return true;
106         }
107         if (other == null || getClass() != other.getClass()) {
108             return false;
109         }
110         if (!super.equals(other)) {
111             return false;
112         }
113
114         ActionNxConntrack that = (ActionNxConntrack) other;
115
116         if (flags != that.flags) {
117             return false;
118         }
119         if (zoneSrc != that.zoneSrc) {
120             return false;
121         }
122         if (conntrackZone != that.conntrackZone) {
123             return false;
124         }
125         if (recircTable != that.recircTable) {
126             return false;
127         }
128         return ctActions.equals(that.ctActions);
129     }
130
131     @Override
132     public int hashCode() {
133         int result = super.hashCode();
134         result = 31 * result + flags;
135         result = 31 * result + (int) (zoneSrc ^ zoneSrc >>> 32);
136         result = 31 * result + conntrackZone;
137         result = 31 * result + recircTable;
138         result = 31 * result + ctActions.hashCode();
139         return result;
140     }
141
142     @Override
143     public String toString() {
144         return "ActionNxConntrack [flags=" + flags + ", zoneSrc=" + zoneSrc + ", conntrackZone=" + conntrackZone
145                 + ", recircTable=" + recircTable + ", ctActions=" + ctActions + ", getActionKey()=" + getActionKey()
146                 + "]";
147     }
148
149     public interface NxCtAction {
150         CtActions buildCtActions();
151     }
152
153     public static class NxNat implements NxCtAction {
154         private final int actionKey;
155         private final int flags;
156         private final int rangePresent;
157         private final IpAddress ipAddressMin;
158         private final IpAddress ipAddressMax;
159         private final int portMin;
160         private final int portMax;
161
162         public NxNat(int actionKey, int flags, int rangePresent, IpAddress ipAddressMin,
163                 IpAddress ipAddressMax,int portMin, int portMax) {
164             this.actionKey = actionKey;
165             this.flags = flags;
166             this.rangePresent = rangePresent;
167             this.ipAddressMin = ipAddressMin;
168             this.ipAddressMax = ipAddressMax;
169             this.portMin = portMin;
170             this.portMax = portMax;
171         }
172
173         public int getActionKey() {
174             return actionKey;
175         }
176
177         public int getFlags() {
178             return flags;
179         }
180
181         public int getRangePresent() {
182             return rangePresent;
183         }
184
185         public IpAddress getIpAddressMin() {
186             return ipAddressMin;
187         }
188
189         public IpAddress getIpAddressMax() {
190             return ipAddressMax;
191         }
192
193         public int getPortMin() {
194             return portMin;
195         }
196
197         public int getPortMax() {
198             return portMax;
199         }
200
201         @Override
202         public CtActions buildCtActions() {
203             NxActionNatBuilder nxActionNatBuilder = new NxActionNatBuilder()
204                     .setFlags(flags)
205                     .setRangePresent(rangePresent)
206                     .setIpAddressMin(ipAddressMin)
207                     .setIpAddressMax(ipAddressMax)
208                     .setPortMin(portMin)
209                     .setPortMax(portMax);
210
211             CtActionsBuilder ctActionsBuilder = new CtActionsBuilder();
212             NxActionNatCaseBuilder caseBuilder = new NxActionNatCaseBuilder();
213             caseBuilder.setNxActionNat(nxActionNatBuilder.build());
214             ctActionsBuilder.setOfpactActions(caseBuilder.build());
215             return ctActionsBuilder.build();
216         }
217
218         @Override
219         public boolean equals(Object other) {
220             if (this == other) {
221                 return true;
222             }
223             if (other == null || getClass() != other.getClass()) {
224                 return false;
225             }
226
227             NxNat that = (NxNat) other;
228
229             if (flags != that.flags) {
230                 return false;
231             }
232             if (rangePresent != that.rangePresent) {
233                 return false;
234             }
235             if (ipAddressMin != null ? !ipAddressMin.equals(that.ipAddressMin) : that.ipAddressMin != null) {
236                 return false;
237             }
238             if (ipAddressMax != null ? !ipAddressMax.equals(that.ipAddressMax) : that.ipAddressMax != null) {
239                 return false;
240             }
241             if (portMin != that.portMin) {
242                 return false;
243             }
244             return portMax == that.portMax;
245         }
246
247         @Override
248         public int hashCode() {
249             int result = flags;
250             result = 31 * result + rangePresent;
251             result = 31 * result + ipAddressMin.hashCode();
252             result = 31 * result + ipAddressMax.hashCode();
253             result = 31 * result + portMin;
254             result = 31 * result + portMax;
255             return result;
256         }
257
258         @Override
259         public String toString() {
260             return "NxNat [flags=" + flags + ", rangePresent=" + rangePresent + ", ipAddressMin=" + ipAddressMin
261                     + ", ipAddressMax=" + ipAddressMax + ", portMin=" + portMin + ", portMax=" + portMax + "]";
262         }
263     }
264
265     public static class NxCtMark implements NxCtAction {
266         private final long ctMark;
267
268         public NxCtMark(long ctMark) {
269             this.ctMark = ctMark;
270         }
271
272         public long getCtMark() {
273             return ctMark;
274         }
275
276         @Override
277         public CtActions buildCtActions() {
278             NxActionCtMarkBuilder nxActionCtMarkBuilder = new NxActionCtMarkBuilder()
279                     .setCtMark(ctMark);
280
281             CtActionsBuilder ctActionsBuilder = new CtActionsBuilder();
282             NxActionCtMarkCaseBuilder caseBuilder = new NxActionCtMarkCaseBuilder();
283             caseBuilder.setNxActionCtMark(nxActionCtMarkBuilder.build());
284             ctActionsBuilder.setOfpactActions(caseBuilder.build());
285             return ctActionsBuilder.build();
286         }
287
288         @Override
289         public boolean equals(Object other) {
290             if (this == other) {
291                 return true;
292             }
293             if (other == null || getClass() != other.getClass()) {
294                 return false;
295             }
296
297             NxCtMark that = (NxCtMark) other;
298
299             return ctMark == that.ctMark;
300         }
301
302         @Override
303         public int hashCode() {
304             return 31 * (int) (ctMark ^ ctMark >>> 32);
305         }
306     }
307 }