Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / match / MatchField.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.sal.match;
10
11 import java.io.Serializable;
12
13 import javax.xml.bind.annotation.XmlAccessType;
14 import javax.xml.bind.annotation.XmlAccessorType;
15 import javax.xml.bind.annotation.XmlElement;
16 import javax.xml.bind.annotation.XmlRootElement;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Represents the generic matching field
23  *
24  */
25
26 @XmlRootElement
27 @XmlAccessorType(XmlAccessType.NONE)
28 @Deprecated
29 public class MatchField implements Cloneable, Serializable {
30     private static final long serialVersionUID = 1L;
31     private static final Logger logger = LoggerFactory.getLogger(MatchField.class);
32     private MatchType type; // the field we want to match
33     private Object value; // the value of the field we want to match
34     private Object mask; // the value of the mask we want to match on the
35     // specified field
36     private boolean isValid;
37
38     // To satisfy JAXB
39     @SuppressWarnings("unused")
40     private MatchField() {
41     }
42
43     /**
44      * Mask based match constructor
45      *
46      * @param type
47      * @param value
48      * @param mask
49      *            has to be of the same class type of value. A null mask means
50      *            full match
51      */
52     public MatchField(MatchType type, Object value, Object mask) {
53         this.type = type;
54         this.value = value;
55         this.mask = mask;
56         // Keep this logic, value checked only if type check is fine
57         this.isValid = checkValueType() && checkValues();
58     }
59
60     /**
61      * Full match constructor
62      *
63      * @param type
64      * @param value
65      */
66     public MatchField(MatchType type, Object value) {
67         this.type = type;
68         this.value = value;
69         this.mask = null;
70         // Keep this logic, value checked only if type check is fine
71         this.isValid = checkValueType() && checkValues();
72     }
73
74     /**
75      * Returns the value set for this match field
76      *
77      * @return
78      */
79     public Object getValue() {
80         return value;
81     }
82
83     @XmlElement(name = "value")
84     private String getValueString() {
85         return type.stringify(value);
86     }
87
88     /**
89      * Returns the type field this match field object is for
90      *
91      * @return
92      */
93     public MatchType getType() {
94         return type;
95     }
96
97     @XmlElement(name = "type")
98     private String getTypeString() {
99         return type.toString();
100     }
101
102     /**
103      * Returns the mask value set for this field match A null mask means this is
104      * a full match
105      *
106      * @return
107      */
108     public Object getMask() {
109         return mask;
110     }
111
112     @XmlElement(name = "mask")
113     private String getMaskString() {
114         return type.stringify(mask);
115     }
116
117     /**
118      * Returns the bitmask set for this field match
119      *
120      * @return
121      */
122     public long getBitMask() {
123         return type.getBitMask(mask);
124     }
125
126     /**
127      * Returns whether the field match configuration is valid or not
128      *
129      * @return
130      */
131     public boolean isValid() {
132         return isValid;
133     }
134
135     private boolean checkValueType() {
136         if (type.isCongruentType(value, mask) == false) {
137             String valueClass = (value == null) ? "null" : value.getClass().getSimpleName();
138             String maskClass = (mask == null) ? "null" : mask.getClass().getSimpleName();
139             String error = "Invalid match field's value or mask types.For field: " + type.id() + " Expected:"
140                     + type.dataType().getSimpleName() + " or equivalent," + " Got:(" + valueClass + "," + maskClass
141                     + ")";
142             throwException(error);
143             return false;
144         }
145         return true;
146     }
147
148     private boolean checkValues() {
149         if (type.isValid(value, mask) == false) {
150             String maskString = (mask == null) ? "null" : ("0x" + Integer
151                     .toHexString(Integer.parseInt(mask.toString())));
152             String error = "Invalid match field's value or mask assignement.For field: " + type.id() + " Expected: "
153                     + type.getRange() + ", Got:(0x" + Integer.toHexString(Integer.parseInt(value.toString())) + ","
154                     + maskString + ")";
155
156             throwException(error);
157             return false;
158         }
159         return true;
160     }
161
162     private static void throwException(String error) {
163         try {
164             throw new Exception(error);
165         } catch (Exception e) {
166             logger.error(e.getMessage());
167         }
168     }
169
170     @Override
171     public MatchField clone() {
172         MatchField cloned = null;
173         try {
174             cloned = (MatchField) super.clone();
175             if (value instanceof byte[]) {
176                 cloned.value = ((byte[]) this.value).clone();
177                 if (this.mask != null) {
178                     cloned.mask = ((byte[]) this.mask).clone();
179                 }
180             }
181             cloned.type = this.type;
182             cloned.isValid = this.isValid;
183         } catch (CloneNotSupportedException e) {
184             logger.error("", e);
185         }
186         return cloned;
187     }
188
189     @Override
190     public String toString() {
191         return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
192             String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
193     }
194
195     @Override
196     public int hashCode() {
197         return type.hashCode(value, mask);
198     }
199
200     @Override
201     public boolean equals(Object obj) {
202         if (this == obj) {
203             return true;
204         }
205         if (obj == null) {
206             return false;
207         }
208         if (getClass() != obj.getClass()) {
209             return false;
210         }
211         MatchField other = (MatchField) obj;
212         if (type != other.type) {
213             return false;
214         }
215         return type.equals(this.value, other.value, this.mask, other.mask);
216     }
217 }