2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.sal.match;
11 import java.io.Serializable;
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;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Represents the generic matching field
27 @XmlAccessorType(XmlAccessType.NONE)
28 public class MatchField implements Cloneable, Serializable {
29 private static final long serialVersionUID = 1L;
30 private static final Logger logger = LoggerFactory
31 .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
36 private transient boolean isValid;
39 @SuppressWarnings("unused")
40 private MatchField() {
44 * Mask based match constructor
49 * has to be of the same class type of value. A null mask means
52 public MatchField(MatchType type, Object value, Object mask) {
56 // Keep this logic, value checked only if type check is fine
57 this.isValid = checkValueType() && checkValues();
61 * Full match constructor
66 public MatchField(MatchType type, Object value) {
70 // Keep this logic, value checked only if type check is fine
71 this.isValid = checkValueType() && checkValues();
75 * Returns the value set for this match field
79 public Object getValue() {
83 @XmlElement(name = "value")
84 private String getValueString() {
85 return type.stringify(value);
89 * Returns the type field this match field object is for
93 public MatchType getType() {
97 @XmlElement(name = "type")
98 private String getTypeString() {
99 return type.toString();
103 * Returns the mask value set for this field match A null mask means this is
108 public Object getMask() {
112 @XmlElement(name = "mask")
113 private String getMaskString() {
114 return type.stringify(mask);
118 * Returns the bitmask set for this field match
122 public long getBitMask() {
123 return type.getBitMask(mask);
127 * Returns whether the field match configuration is valid or not
131 public boolean isValid() {
135 private boolean checkValueType() {
136 if (type.isCongruentType(value, mask) == false) {
137 String valueClass = (value == null) ? "null" : value.getClass()
139 String maskClass = (mask == null) ? "null" : mask.getClass()
141 String error = "Invalid match field's value or mask types.For field: "
144 + type.dataType().getSimpleName()
146 + " Got:(" + valueClass + "," + maskClass + ")";
147 throwException(error);
153 private boolean checkValues() {
154 if (type.isValid(value, mask) == false) {
155 String maskString = (mask == null) ? "null" : ("0x" + Integer
156 .toHexString(Integer.parseInt(mask.toString())));
157 String error = "Invalid match field's value or mask assignement.For field: "
162 + Integer.toHexString(Integer.parseInt(value.toString()))
163 + "," + maskString + ")";
165 throwException(error);
171 private static void throwException(String error) {
173 throw new Exception(error);
174 } catch (Exception e) {
175 logger.error(e.getMessage());
180 public MatchField clone() {
181 MatchField cloned = null;
183 cloned = (MatchField) super.clone();
184 if (value instanceof byte[]) {
185 cloned.value = ((byte[]) this.value).clone();
186 if (this.mask != null) {
187 cloned.mask = ((byte[]) this.mask).clone();
190 } catch (CloneNotSupportedException e) {
197 public String toString() {
198 return type + "(" + getValueString() + "," + getMaskString() + ")";
202 public int hashCode() {
203 final int prime = 31;
205 result = prime * result + ((mask == null) ? 0 : mask.hashCode());
206 result = prime * result + ((type == null) ? 0 : type.hashCode());
207 result = prime * result + ((value == null) ? 0 : value.hashCode());
212 public boolean equals(Object obj) {
219 if (getClass() != obj.getClass()) {
222 MatchField other = (MatchField) obj;
223 if (type != other.type) {
226 return (type.equalValues(this.value, other.value) && type.equalMasks(
227 this.mask, other.mask));