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 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.apache.commons.lang3.builder.EqualsBuilder;
18 import org.apache.commons.lang3.builder.HashCodeBuilder;
19 import org.opendaylight.controller.sal.utils.HexEncode;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Represents the generic matching field
29 @XmlAccessorType(XmlAccessType.NONE)
31 public class MatchField implements Cloneable {
32 private static final Logger logger = LoggerFactory
33 .getLogger(MatchField.class);
34 private MatchType type; // the field we want to match
35 private Object value; // the value of the field we want to match
36 private Object mask; // the value of the mask we want to match on the specified field
37 private transient boolean isValid;
40 private MatchField() {
43 * Mask based match constructor
47 * @param mask has to be of the same class type of value. A null mask means full match
49 public MatchField(MatchType type, Object value, Object mask) {
53 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
57 * Full match constructor
62 public MatchField(MatchType type, Object value) {
66 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
70 * Returns the value set for this match field
74 public Object getValue() {
78 @XmlElement(name="value")
79 private String getValueString() {
80 return type.stringify(value);
84 * Returns the type field this match field object is for
88 public MatchType getType() {
92 @XmlElement(name="type")
93 private String getTypeString() {
94 return type.toString();
98 * Returns the mask value set for this field match
99 * A null mask means this is a full match
102 public Object getMask() {
106 @XmlElement(name="mask")
107 private String getMaskString() {
108 return type.stringify(mask);
112 * Returns the bitmask set for this field match
116 public long getBitMask() {
117 return type.getBitMask(mask);
121 * Returns whether the field match configuration is valid or not
125 public boolean isValid() {
129 private boolean checkValueType() {
130 if (type.isCongruentType(value, mask) == false) {
131 String valueClass = (value == null) ? "null" : value.getClass()
133 String maskClass = (mask == null) ? "null" : mask.getClass()
135 String error = "Invalid match field's value or mask types.For field: "
138 + type.dataType().getSimpleName()
140 + " 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: "
156 + Integer.toHexString(Integer.parseInt(value.toString()))
157 + "," + maskString + ")";
159 throwException(error);
165 private static void throwException(String error) {
167 throw new Exception(error);
168 } catch (Exception e) {
169 logger.error(e.getMessage());
174 public MatchField clone() {
175 MatchField cloned = null;
177 cloned = (MatchField) super.clone();
178 if (value instanceof byte[]) {
179 cloned.value = ((byte[]) this.value).clone();
180 if (this.mask != null) {
181 cloned.mask = ((byte[]) this.mask).clone();
184 } catch (CloneNotSupportedException e) {
191 public int hashCode() {
192 return HashCodeBuilder.reflectionHashCode(this);
196 public boolean equals(Object obj) {
197 return EqualsBuilder.reflectionEquals(this, obj);
201 public String toString() {
202 return type + "(" + getValueString() + "," + getMaskString() + ")";