3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.match;
12 import java.io.Serializable;
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
19 import org.apache.commons.lang3.builder.EqualsBuilder;
20 import org.apache.commons.lang3.builder.HashCodeBuilder;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Represents the generic matching field
30 @XmlAccessorType(XmlAccessType.NONE)
32 public class MatchField implements Cloneable, Serializable {
33 private static final long serialVersionUID = 1L;
34 private static final Logger logger = LoggerFactory
35 .getLogger(MatchField.class);
36 private MatchType type; // the field we want to match
37 private Object value; // the value of the field we want to match
38 private Object mask; // the value of the mask we want to match on the specified field
39 private transient boolean isValid;
42 private MatchField() {
45 * Mask based match constructor
49 * @param mask has to be of the same class type of value. A null mask means full match
51 public MatchField(MatchType type, Object value, Object mask) {
55 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
59 * Full match constructor
64 public MatchField(MatchType type, Object value) {
68 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
72 * Returns the value set for this match field
76 public Object getValue() {
80 @XmlElement(name="value")
81 private String getValueString() {
82 return type.stringify(value);
86 * Returns the type field this match field object is for
90 public MatchType getType() {
94 @XmlElement(name="type")
95 private String getTypeString() {
96 return type.toString();
100 * Returns the mask value set for this field match
101 * A null mask means this is a full match
104 public Object getMask() {
108 @XmlElement(name="mask")
109 private String getMaskString() {
110 return type.stringify(mask);
114 * Returns the bitmask set for this field match
118 public long getBitMask() {
119 return type.getBitMask(mask);
123 * Returns whether the field match configuration is valid or not
127 public boolean isValid() {
131 private boolean checkValueType() {
132 if (type.isCongruentType(value, mask) == false) {
133 String valueClass = (value == null) ? "null" : value.getClass()
135 String maskClass = (mask == null) ? "null" : mask.getClass()
137 String error = "Invalid match field's value or mask types.For field: "
140 + type.dataType().getSimpleName()
142 + " Got:(" + valueClass + "," + maskClass + ")";
143 throwException(error);
149 private boolean checkValues() {
150 if (type.isValid(value, mask) == false) {
151 String maskString = (mask == null) ? "null" : ("0x" + Integer
152 .toHexString(Integer.parseInt(mask.toString())));
153 String error = "Invalid match field's value or mask assignement.For field: "
158 + Integer.toHexString(Integer.parseInt(value.toString()))
159 + "," + maskString + ")";
161 throwException(error);
167 private static void throwException(String error) {
169 throw new Exception(error);
170 } catch (Exception e) {
171 logger.error(e.getMessage());
176 public MatchField clone() {
177 MatchField cloned = null;
179 cloned = (MatchField) super.clone();
180 if (value instanceof byte[]) {
181 cloned.value = ((byte[]) this.value).clone();
182 if (this.mask != null) {
183 cloned.mask = ((byte[]) this.mask).clone();
186 } catch (CloneNotSupportedException e) {
193 public int hashCode() {
194 return HashCodeBuilder.reflectionHashCode(this);
198 public boolean equals(Object obj) {
199 return EqualsBuilder.reflectionEquals(this, obj);
203 public String toString() {
204 return type + "(" + getValueString() + "," + getMaskString() + ")";