Merge "Utility api to configure icmpv6 type"
[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     private static final long serialVersionUID = 1L;
29
30     private final BigInteger destIp;
31     private final boolean groupBucket;
32
33     public ActionSetTunnelDestinationIp(BigInteger destIp) {
34         this(0, destIp);
35     }
36
37     public ActionSetTunnelDestinationIp(IpAddress destIp) {
38         this(0, destIp);
39     }
40
41     public ActionSetTunnelDestinationIp(int actionKey, BigInteger destIp) {
42         super(actionKey);
43         this.destIp = destIp;
44         this.groupBucket = false;
45     }
46
47     public ActionSetTunnelDestinationIp(int actionKey, IpAddress destIp) {
48         this(actionKey, MDSALUtil.getBigIntIpFromIpAddress(destIp));
49     }
50
51     @Override
52     public Action buildAction() {
53         return buildAction(getActionKey());
54     }
55
56     @Override
57     public Action buildAction(int newActionKey) {
58         NxRegLoadBuilder nxRegLoadBuilder = new NxRegLoadBuilder();
59         Dst dst = new DstBuilder()
60             .setDstChoice(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build())
61             .setStart(0)
62             .setEnd(31)
63             .build();
64         nxRegLoadBuilder.setDst(dst);
65         nxRegLoadBuilder.setValue(destIp);
66         ActionBuilder ab = new ActionBuilder();
67
68         if (groupBucket) {
69             ab.setAction(new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder()
70                 .setNxRegLoad(nxRegLoadBuilder.build()).build());
71         } else {
72             ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder()
73                 .setNxRegLoad(nxRegLoadBuilder.build()).build());
74         }
75         ab.setKey(new ActionKey(newActionKey));
76         return ab.build();
77     }
78
79     @Override
80     public boolean equals(Object other) {
81         if (this == other) {
82             return true;
83         }
84         if (other == null || getClass() != other.getClass()) {
85             return false;
86         }
87         if (!super.equals(other)) {
88             return false;
89         }
90
91         ActionSetTunnelDestinationIp that = (ActionSetTunnelDestinationIp) other;
92
93         if (groupBucket != that.groupBucket) {
94             return false;
95         }
96         return destIp != null ? destIp.equals(that.destIp) : that.destIp == null;
97     }
98
99     @Override
100     public int hashCode() {
101         int result = super.hashCode();
102         result = 31 * result + (destIp != null ? destIp.hashCode() : 0);
103         result = 31 * result + (groupBucket ? 1 : 0);
104         return result;
105     }
106 }