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)
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
36 private 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().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
142 throwException(error);
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())) + ","
156 throwException(error);
162 private static void throwException(String error) {
164 throw new Exception(error);
165 } catch (Exception e) {
166 logger.error(e.getMessage());
171 public MatchField clone() {
172 MatchField cloned = null;
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();
181 cloned.type = this.type;
182 cloned.isValid = this.isValid;
183 } catch (CloneNotSupportedException e) {
190 public String toString() {
191 return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
192 String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
196 public int hashCode() {
197 return type.hashCode(value, mask);
201 public boolean equals(Object obj) {
208 if (getClass() != obj.getClass()) {
211 MatchField other = (MatchField) obj;
212 if (type != other.type) {
215 return type.equals(this.value, other.value, this.mask, other.mask);