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.getLogger(MatchField.class);
31 private MatchType type; // the field we want to match
32 private Object value; // the value of the field we want to match
33 private Object mask; // the value of the mask we want to match on the
35 private boolean isValid;
38 @SuppressWarnings("unused")
39 private MatchField() {
43 * Mask based match constructor
48 * has to be of the same class type of value. A null mask means
51 public MatchField(MatchType type, Object value, Object mask) {
55 // Keep this logic, value checked only if type check is fine
56 this.isValid = checkValueType() && checkValues();
60 * Full match constructor
65 public MatchField(MatchType type, Object value) {
69 // Keep this logic, value checked only if type check is fine
70 this.isValid = checkValueType() && checkValues();
74 * Returns the value set for this match field
78 public Object getValue() {
82 @XmlElement(name = "value")
83 private String getValueString() {
84 return type.stringify(value);
88 * Returns the type field this match field object is for
92 public MatchType getType() {
96 @XmlElement(name = "type")
97 private String getTypeString() {
98 return type.toString();
102 * Returns the mask value set for this field match A null mask means this is
107 public Object getMask() {
111 @XmlElement(name = "mask")
112 private String getMaskString() {
113 return type.stringify(mask);
117 * Returns the bitmask set for this field match
121 public long getBitMask() {
122 return type.getBitMask(mask);
126 * Returns whether the field match configuration is valid or not
130 public boolean isValid() {
134 private boolean checkValueType() {
135 if (type.isCongruentType(value, mask) == false) {
136 String valueClass = (value == null) ? "null" : value.getClass().getSimpleName();
137 String maskClass = (mask == null) ? "null" : mask.getClass().getSimpleName();
138 String error = "Invalid match field's value or mask types.For field: " + type.id() + " Expected:"
139 + type.dataType().getSimpleName() + " or equivalent," + " Got:(" + valueClass + "," + maskClass
141 throwException(error);
147 private boolean checkValues() {
148 if (type.isValid(value, mask) == false) {
149 String maskString = (mask == null) ? "null" : ("0x" + Integer
150 .toHexString(Integer.parseInt(mask.toString())));
151 String error = "Invalid match field's value or mask assignement.For field: " + type.id() + " Expected: "
152 + type.getRange() + ", Got:(0x" + Integer.toHexString(Integer.parseInt(value.toString())) + ","
155 throwException(error);
161 private static void throwException(String error) {
163 throw new Exception(error);
164 } catch (Exception e) {
165 logger.error(e.getMessage());
170 public MatchField clone() {
171 MatchField cloned = null;
173 cloned = (MatchField) super.clone();
174 if (value instanceof byte[]) {
175 cloned.value = ((byte[]) this.value).clone();
176 if (this.mask != null) {
177 cloned.mask = ((byte[]) this.mask).clone();
180 cloned.type = this.type;
181 cloned.isValid = this.isValid;
182 } catch (CloneNotSupportedException e) {
189 public String toString() {
190 return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
191 String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
195 public int hashCode() {
196 return type.hashCode(value, mask);
200 public boolean equals(Object obj) {
207 if (getClass() != obj.getClass()) {
210 MatchField other = (MatchField) obj;
211 if (type != other.type) {
214 return type.equals(this.value, other.value, this.mask, other.mask);