add learn action
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / ActionInfo.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
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;
9
10 import com.google.common.base.MoreObjects;
11 import java.io.Serializable;
12 import java.math.BigInteger;
13 import java.util.Arrays;
14 import java.util.Objects;
15 import org.opendaylight.genius.utils.MoreObjects2;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
17
18 public class ActionInfo implements Serializable {
19
20     private static final long serialVersionUID = 1L;
21
22     private final ActionType m_actionType;
23     private final String[] m_asActionValues;
24     private final String[][] m_asActionValuesMatrix;
25     private final BigInteger[] m_aBigIntValues;
26     private final int m_actionKey;
27
28     public ActionInfo(ActionInfo action) {
29         super();
30         m_actionType = action.m_actionType;
31         m_actionKey = action.m_actionKey;
32         m_asActionValuesMatrix = new String[action.m_asActionValuesMatrix.length][];
33         for(int i = 0; i < action.m_asActionValuesMatrix.length; i++){
34             m_asActionValuesMatrix[i] = Arrays.copyOf(action.m_asActionValuesMatrix[i], action.m_asActionValuesMatrix[i].length);
35         }
36
37         m_asActionValues = Arrays.copyOf(action.m_asActionValues, action.m_asActionValues.length);
38         m_aBigIntValues = null;
39     }
40
41     public ActionInfo(ActionType actionType, String[] asActionValues) {
42         m_actionType = actionType;
43         m_actionKey = 0;
44         m_asActionValues = asActionValues;
45         m_asActionValuesMatrix = null;
46         m_aBigIntValues = null;
47     }
48
49     public ActionInfo(ActionType actionType, String[] asActionValues, int actionKey) {
50         m_actionType = actionType;
51         m_actionKey = actionKey;
52         m_asActionValues = asActionValues;
53         m_asActionValuesMatrix = null;
54         m_aBigIntValues = null;
55     }
56
57     public ActionInfo(ActionType actionType, BigInteger[] aBigIntValues) {
58         m_actionType = actionType;
59         m_actionKey = 0;
60         m_aBigIntValues = aBigIntValues;
61         m_asActionValuesMatrix = null;
62         m_asActionValues = null;
63     }
64
65     public ActionInfo(ActionType actionType, BigInteger[] aBigIntValues, int actionKey) {
66         m_actionType = actionType;
67         m_actionKey = actionKey;
68         m_aBigIntValues = aBigIntValues;
69         m_asActionValuesMatrix = null;
70         m_asActionValues = null;
71     }
72
73     public ActionInfo(ActionType actionType, String[] asActionValues, String[][] asActionValuesMatrix, int actionKey) {
74         m_actionType = actionType;
75         m_actionKey = actionKey;
76         m_aBigIntValues = null;
77         m_asActionValuesMatrix = asActionValuesMatrix;
78         m_asActionValues = asActionValues;
79     }
80
81     public ActionInfo(ActionType actionType, String[] asActionValues, String[][] asActionValuesMatrix) {
82         this(actionType, asActionValues, asActionValuesMatrix, 0);
83     }
84
85     public int getActionKey() {
86         return m_actionKey;
87     }
88
89     public Action buildAction() {
90         return m_actionType.buildAction(getActionKey(), this);
91     }
92
93     public ActionType getActionType() {
94         return m_actionType;
95     }
96
97     public String[] getActionValues() {
98         return m_asActionValues;
99     }
100
101     public BigInteger[] getBigActionValues() {
102         return m_aBigIntValues;
103     }
104
105     public String[][] getActionValuesMatrix() {
106         return m_asActionValuesMatrix;
107     }
108
109     @Override
110     public String toString() {
111         return MoreObjects.toStringHelper(this).omitNullValues().add("actionType", m_actionType)
112                 .add("actionValues", Arrays.deepToString(m_asActionValues))
113                 .add("bigActionValues", Arrays.deepToString(m_aBigIntValues))
114                 .add("actionKey", m_actionKey).toString();
115     }
116
117     @Override
118     public int hashCode() {
119         // BEWARE, Caveat Emptor: Array ([]) type fields must use
120         // Arrays.hashCode(). deepHashCode() would have to be used for nested
121         // arrays.
122         return Objects.hash(m_actionType, Arrays.hashCode(m_asActionValues), Arrays.hashCode(m_aBigIntValues),
123                 m_actionKey);
124     }
125
126     @Override
127     public boolean equals(Object obj) {
128         // BEWARE, Caveat Emptor: Array ([]) type fields must use
129         // Arrays.equals(). deepEquals() would have to be used for nested
130         // arrays. Use == only for primitive types; if ever changing
131         // those field types, must change to Objects.equals.
132         return MoreObjects2.equalsHelper(this, obj,
133             (self, other) -> Objects.equals(self.m_actionType, other.m_actionType)
134                           && Arrays.equals(self.m_asActionValues, other.m_asActionValues)
135                           && Arrays.equals(self.m_aBigIntValues, other.m_aBigIntValues)
136                           && self.m_actionKey == other.m_actionKey);
137     }
138 }