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