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 org.apache.commons.lang3.builder.EqualsBuilder;
13 import org.apache.commons.lang3.builder.HashCodeBuilder;
14 import org.opendaylight.controller.sal.utils.HexEncode;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Represents the generic matching field
24 public class MatchField implements Cloneable {
25 private static final Logger logger = LoggerFactory
26 .getLogger(MatchField.class);
27 private MatchType type; // the field we want to match
28 private Object value; // the value of the field we want to match
29 private Object mask; // the value of the mask we want to match on the specified field
30 private transient boolean isValid;
33 * Mask based match constructor
37 * @param mask has to be of the same class type of value. A null mask means full match
39 public MatchField(MatchType type, Object value, Object mask) {
43 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
47 * Full match constructor
52 public MatchField(MatchType type, Object value) {
56 this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
60 * Returns the value set for this match field
64 public Object getValue() {
69 * Returns the type field this match field object is for
73 public MatchType getType() {
78 * Returns the mask value set for this field match
79 * A null mask means this is a full match
82 public Object getMask() {
87 * Returns the bitmask set for this field match
91 public long getBitMask() {
92 return type.getBitMask(mask);
96 * Returns whether the field match configuration is valid or not
100 public boolean isValid() {
104 private boolean checkValueType() {
105 if (type.isCongruentType(value, mask) == false) {
106 String valueClass = (value == null) ? "null" : value.getClass()
108 String maskClass = (mask == null) ? "null" : mask.getClass()
110 String error = "Invalid match field's value or mask types.For field: "
113 + type.dataType().getSimpleName()
115 + " Got:(" + valueClass + "," + maskClass + ")";
116 throwException(error);
122 private boolean checkValues() {
123 if (type.isValid(value, mask) == false) {
124 String maskString = (mask == null) ? "null" : ("0x" + Integer
125 .toHexString(Integer.parseInt(mask.toString())));
126 String error = "Invalid match field's value or mask assignement.For field: "
131 + Integer.toHexString(Integer.parseInt(value.toString()))
132 + "," + maskString + ")";
134 throwException(error);
140 private static void throwException(String error) {
142 throw new Exception(error);
143 } catch (Exception e) {
144 logger.error(e.getMessage());
149 public MatchField clone() {
150 MatchField cloned = null;
152 cloned = (MatchField) super.clone();
153 if (value instanceof byte[]) {
154 cloned.value = ((byte[]) this.value).clone();
155 if (this.mask != null) {
156 cloned.mask = ((byte[]) this.mask).clone();
159 } catch (CloneNotSupportedException e) {
166 public int hashCode() {
167 return HashCodeBuilder.reflectionHashCode(this);
171 public boolean equals(Object obj) {
172 return EqualsBuilder.reflectionEquals(this, obj);
176 public String toString() {
177 String valueString = (value == null) ? "null"
178 : (value instanceof byte[]) ? HexEncode
179 .bytesToHexString((byte[]) value) : value.toString();
180 String maskString = (mask == null) ? "null"
181 : (mask instanceof byte[]) ? HexEncode
182 .bytesToHexString((byte[]) mask) : mask.toString();
184 return type + "(" + valueString + "," + maskString + ")";