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;
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Represents the generic matching field
26 @XmlAccessorType(XmlAccessType.NONE)
27 public class MatchField implements Cloneable, Serializable {
28 private static final long serialVersionUID = 1L;
29 private static final Logger logger = LoggerFactory.getLogger(MatchField.class);
30 private MatchType type; // the field we want to match
31 private Object value; // the value of the field we want to match
32 private Object mask; // the value of the mask we want to match on the
34 private transient boolean isValid;
37 @SuppressWarnings("unused")
38 private MatchField() {
42 * Mask based match constructor
47 * has to be of the same class type of value. A null mask means
50 public MatchField(MatchType type, Object value, Object mask) {
54 // Keep this logic, value checked only if type check is fine
55 this.isValid = checkValueType() && checkValues();
59 * Full match constructor
64 public MatchField(MatchType type, Object value) {
68 // Keep this logic, value checked only if type check is fine
69 this.isValid = checkValueType() && checkValues();
73 * Returns the value set for this match field
77 public Object getValue() {
81 @XmlElement(name = "value")
82 private String getValueString() {
83 return type.stringify(value);
87 * Returns the type field this match field object is for
91 public MatchType getType() {
95 @XmlElement(name = "type")
96 private String getTypeString() {
97 return type.toString();
101 * Returns the mask value set for this field match A null mask means this is
106 public Object getMask() {
110 @XmlElement(name = "mask")
111 private String getMaskString() {
112 return type.stringify(mask);
116 * Returns the bitmask set for this field match
120 public long getBitMask() {
121 return type.getBitMask(mask);
125 * Returns whether the field match configuration is valid or not
129 public boolean isValid() {
133 private boolean checkValueType() {
134 if (type.isCongruentType(value, mask) == false) {
135 String valueClass = (value == null) ? "null" : value.getClass().getSimpleName();
136 String maskClass = (mask == null) ? "null" : mask.getClass().getSimpleName();
137 String error = "Invalid match field's value or mask types.For field: " + type.id() + " Expected:"
138 + type.dataType().getSimpleName() + " or equivalent," + " Got:(" + valueClass + "," + maskClass
140 throwException(error);
146 private boolean checkValues() {
147 if (type.isValid(value, mask) == false) {
148 String maskString = (mask == null) ? "null" : ("0x" + Integer
149 .toHexString(Integer.parseInt(mask.toString())));
150 String error = "Invalid match field's value or mask assignement.For field: " + type.id() + " Expected: "
151 + type.getRange() + ", Got:(0x" + Integer.toHexString(Integer.parseInt(value.toString())) + ","
154 throwException(error);
160 private static void throwException(String error) {
162 throw new Exception(error);
163 } catch (Exception e) {
164 logger.error(e.getMessage());
169 public MatchField clone() {
170 MatchField cloned = null;
172 cloned = (MatchField) super.clone();
173 if (value instanceof byte[]) {
174 cloned.value = ((byte[]) this.value).clone();
175 if (this.mask != null) {
176 cloned.mask = ((byte[]) this.mask).clone();
179 } catch (CloneNotSupportedException e) {
186 public String toString() {
187 return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
188 String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
192 public int hashCode() {
193 return type.hashCode(value, mask);
197 public boolean equals(Object obj) {
204 if (getClass() != obj.getClass()) {
207 MatchField other = (MatchField) obj;
208 if (type != other.type) {
211 return type.equals(this.value, other.value, this.mask, other.mask);