Merge "Make ActionInfo.toString() generic"
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / actions / ActionNxConntrack.java
1 /*
2  * Copyright © 2016 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
15 import org.opendaylight.genius.mdsalutil.ActionInfo;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.NxConntrackBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.nx.conntrack.CtActions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.conntrack.grouping.nx.conntrack.CtActionsBuilder;
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.nat._case.NxActionNatBuilder;
26
27 /**
28  * NX conntrack action.
29  */
30 public class ActionNxConntrack extends ActionInfo {
31     private static final long serialVersionUID = 1L;
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     @Override
99     public boolean equals(Object other) {
100         if (this == other) {
101             return true;
102         }
103         if (other == null || getClass() != other.getClass()) {
104             return false;
105         }
106         if (!super.equals(other)) {
107             return false;
108         }
109
110         ActionNxConntrack that = (ActionNxConntrack) other;
111
112         if (flags != that.flags) {
113             return false;
114         }
115         if (zoneSrc != that.zoneSrc) {
116             return false;
117         }
118         if (conntrackZone != that.conntrackZone) {
119             return false;
120         }
121         if (recircTable != that.recircTable) {
122             return false;
123         }
124         return ctActions.equals(that.ctActions);
125     }
126
127     @Override
128     public int hashCode() {
129         int result = super.hashCode();
130         result = 31 * result + flags;
131         result = 31 * result + (int) (zoneSrc ^ zoneSrc >>> 32);
132         result = 31 * result + conntrackZone;
133         result = 31 * result + recircTable;
134         result = 31 * result + ctActions.hashCode();
135         return result;
136     }
137
138     public interface NxCtAction {
139         CtActions buildCtActions();
140     }
141
142
143     public static class NxNat implements NxCtAction {
144         private final int flags;
145         private final int rangePresent;
146         private final IpAddress ipAddressMin;
147         private final IpAddress ipAddressMax;
148         private final int portMin;
149         private final int portMax;
150
151         public NxNat(int actionKey, int flags, int natType, IpAddress ipAddressMin,
152                 IpAddress ipAddressMax,int portMin, int portMax) {
153             this.flags = flags;
154             this.rangePresent = natType;
155             this.ipAddressMin = ipAddressMin;
156             this.ipAddressMax = ipAddressMax;
157             this.portMin = portMin;
158             this.portMax = portMax;
159         }
160
161         @Override
162         public CtActions buildCtActions() {
163             NxActionNatBuilder nxActionNatBuilder = new NxActionNatBuilder()
164                     .setFlags(flags)
165                     .setRangePresent(rangePresent)
166                     .setIpAddressMin(ipAddressMin)
167                     .setIpAddressMax(ipAddressMax)
168                     .setPortMin(portMin)
169                     .setPortMax(portMax);
170
171             CtActionsBuilder ctActionsBuilder = new CtActionsBuilder();
172             NxActionNatCaseBuilder caseBuilder = new NxActionNatCaseBuilder();
173             caseBuilder.setNxActionNat(nxActionNatBuilder.build());
174             ctActionsBuilder.setOfpactActions(caseBuilder.build());
175             return ctActionsBuilder.build();
176         }
177
178         @Override
179         public boolean equals(Object other) {
180             if (this == other) {
181                 return true;
182             }
183             if (other == null || getClass() != other.getClass()) {
184                 return false;
185             }
186
187             NxNat that = (NxNat) other;
188
189             if (flags != that.flags) {
190                 return false;
191             }
192             if (rangePresent != that.rangePresent) {
193                 return false;
194             }
195             if (ipAddressMin != null ? !ipAddressMin.equals(that.ipAddressMin) : that.ipAddressMin != null) {
196                 return false;
197             }
198             if (ipAddressMax != null ? !ipAddressMax.equals(that.ipAddressMax) : that.ipAddressMax != null) {
199                 return false;
200             }
201             if (portMin != that.portMin) {
202                 return false;
203             }
204             return portMax == that.portMax;
205         }
206
207         @Override
208         public int hashCode() {
209             int result = flags;
210             result = 31 * result + rangePresent;
211             result = 31 * result + ipAddressMin.hashCode();
212             result = 31 * result + ipAddressMax.hashCode();
213             result = 31 * result + portMin;
214             result = 31 * result + portMax;
215             return result;
216         }
217     }
218 }