Fix ActionNxConntrack.NxNat problems with xtendbeans (with a test)
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / actions / ActionSetTunnelDestinationIp.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.math.BigInteger;
11 import org.opendaylight.genius.mdsalutil.ActionInfo;
12 import org.opendaylight.genius.mdsalutil.MDSALUtil;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
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.NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.Dst;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
23
24 /**
25  * Set tunnel destination IP action.
26  */
27 public class ActionSetTunnelDestinationIp extends ActionInfo {
28
29     private final BigInteger destIp;
30     private final boolean groupBucket;
31
32     public ActionSetTunnelDestinationIp(BigInteger destIp) {
33         this(0, destIp);
34     }
35
36     public ActionSetTunnelDestinationIp(IpAddress destIp) {
37         this(0, destIp);
38     }
39
40     public ActionSetTunnelDestinationIp(int actionKey, BigInteger destIp) {
41         super(actionKey);
42         this.destIp = destIp;
43         this.groupBucket = false;
44     }
45
46     public ActionSetTunnelDestinationIp(int actionKey, IpAddress destIp) {
47         this(actionKey, MDSALUtil.getBigIntIpFromIpAddress(destIp));
48     }
49
50     public BigInteger getDestIp() {
51         return destIp;
52     }
53
54     public boolean isGroupBucket() {
55         return groupBucket;
56     }
57
58     @Override
59     public Action buildAction() {
60         return buildAction(getActionKey());
61     }
62
63     @Override
64     public Action buildAction(int newActionKey) {
65         NxRegLoadBuilder nxRegLoadBuilder = new NxRegLoadBuilder();
66         Dst dst = new DstBuilder()
67             .setDstChoice(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build())
68             .setStart(0)
69             .setEnd(31)
70             .build();
71         nxRegLoadBuilder.setDst(dst);
72         nxRegLoadBuilder.setValue(destIp);
73         ActionBuilder ab = new ActionBuilder();
74
75         if (groupBucket) {
76             ab.setAction(new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder()
77                 .setNxRegLoad(nxRegLoadBuilder.build()).build());
78         } else {
79             ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder()
80                 .setNxRegLoad(nxRegLoadBuilder.build()).build());
81         }
82         ab.setKey(new ActionKey(newActionKey));
83         return ab.build();
84     }
85
86     @Override
87     public boolean equals(Object other) {
88         if (this == other) {
89             return true;
90         }
91         if (other == null || getClass() != other.getClass()) {
92             return false;
93         }
94         if (!super.equals(other)) {
95             return false;
96         }
97
98         ActionSetTunnelDestinationIp that = (ActionSetTunnelDestinationIp) other;
99
100         if (groupBucket != that.groupBucket) {
101             return false;
102         }
103         return destIp != null ? destIp.equals(that.destIp) : that.destIp == null;
104     }
105
106     @Override
107     public int hashCode() {
108         int result = super.hashCode();
109         result = 31 * result + (destIp != null ? destIp.hashCode() : 0);
110         result = 31 * result + (groupBucket ? 1 : 0);
111         return result;
112     }
113
114     @Override
115     public String toString() {
116         return "ActionSetTunnelDestinationIp [destIp=" + destIp + ", groupBucket=" + groupBucket + ", getActionKey()="
117                 + getActionKey() + "]";
118     }
119 }