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 java.net.InetAddress;
14 import org.opendaylight.controller.sal.core.NodeConnector;
15 import org.opendaylight.controller.sal.utils.HexEncode;
18 * Represents the binding between the id, the value and mask type and the range values
19 * of the elements type that can be matched on the network frame/packet/message
24 public enum MatchType {
25 IN_PORT("inPort", 1 << 0, NodeConnector.class, 1, 0),
26 DL_SRC("dlSrc", 1 << 1, Byte[].class, 0, 0xffffffffffffL),
27 DL_DST("dlDst", 1 << 2, Byte[].class, 0, 0xffffffffffffL),
28 DL_VLAN("dlVlan", 1 << 3, Short.class, 1, 0xfff), // 2 bytes
29 DL_VLAN_PR("dlVlanPriority", 1 << 4, Byte.class, 0, 0x7), // 3 bits
30 DL_OUTER_VLAN("dlOuterVlan", 1 << 5, Short.class, 1, 0xfff),
31 DL_OUTER_VLAN_PR("dlOuterVlanPriority", 1 << 6, Short.class, 0, 0x7),
32 DL_TYPE("dlType", 1 << 7, Short.class, 0, 0xffff), // 2 bytes
33 NW_TOS("nwTOS", 1 << 8, Byte.class, 0, 0xff), // 1 byte
34 NW_PROTO("nwProto", 1 << 9, Byte.class, 0, 0xff), // 1 byte
35 NW_SRC("nwSrc", 1 << 10, InetAddress.class, 0, 0),
36 NW_DST("nwDst", 1 << 11, InetAddress.class, 0, 0),
37 TP_SRC("tpSrc", 1 << 12, Short.class, 1, 0xffff), // 2 bytes
38 TP_DST("tpDst", 1 << 13, Short.class, 1, 0xffff); // 2 bytes
42 private Class<?> dataType;
43 private long minValue;
44 private long maxValue;
46 private MatchType(String id, int index, Class<?> dataType, long minValue,
50 this.dataType = dataType;
51 this.minValue = minValue;
52 this.maxValue = maxValue;
59 public int getIndex() {
63 public Class<?> dataType() {
67 public String getRange() {
68 return "[0x" + Long.toHexString(minValue) + "-0x"
69 + Long.toHexString(maxValue) + "]";
73 * Perform the assignment type validation
78 public boolean isCongruentType(Object value, Object mask) {
79 // Mask type has to match value type
80 if (mask != null && (mask.getClass() != value.getClass())) {
84 Class<?> e = this.dataType();
85 Class<?> g = value.getClass();
87 // This is all what we need, if value type is same of match required type
92 // This is for the numeric class vs primitive congruence
93 // For what concerns here, for instance, Integer is congruent to int
94 if (e == Short.class) {
95 return g.equals(short.class);
98 if (e == Integer.class) {
99 return g.equals(int.class);
102 if (e == Byte.class) {
103 return g.equals(byte.class);
106 if (e == Byte[].class) {
107 return g.equals(byte[].class);
110 if (e == InetAddress.class) {
111 return g.getSuperclass().equals(InetAddress.class);
117 * Perform the value and mask range validation
122 public boolean isValid(Object value, Object mask) {
123 // Skip if main error
124 if (mask != null && (mask.getClass() != value.getClass())) {
127 // For the complex below types let's skip the value range check for now
128 if (this.dataType == InetAddress.class) {
131 if (this.dataType() == Byte[].class) {
134 if (this.dataType() == NodeConnector.class) {
140 if (value.getClass() == Integer.class || value.getClass() == int.class) {
141 val = ((Integer) value).intValue();
142 msk = (mask != null) ? ((Integer) mask).intValue() : 0;
144 } else if (value.getClass() == Short.class
145 || value.getClass() == short.class) {
146 val = ((Short) value).intValue() & 0xffff;
147 msk = (mask != null) ? ((Short) mask).intValue() & 0xffff : 0;
149 } else if (value.getClass() == Byte.class
150 || value.getClass() == byte.class) {
151 val = ((Byte) value).intValue() & 0xff;
152 msk = (mask != null) ? ((Byte) mask).intValue() & 0xff : 0;
154 return ((val >= minValue && val <= maxValue) && (mask == null || (msk >= minValue && msk <= maxValue)));
159 * Return the mask value in 64 bits bitmask form
163 public long getBitMask(Object mask) {
164 if (this.dataType == InetAddress.class) {
165 //TODO handle Inet v4 and v6 v6 will have a second upper mask
168 if (this.dataType() == Byte[].class) {
170 return 0xffffffffffffL;
172 byte mac[] = (byte[]) mask;
174 for (short i = 0; i < 6; i++) {
175 // bitmask |= (((long)mac[i] & 0xffL) << (long)((5-i)*8));
176 bitmask |= (((long) mac[i] & 0xffL) << ((5 - i) * 8));
180 if (this.dataType == Integer.class || this.dataType == int.class) {
181 return (mask == null) ? this.maxValue : ((Integer) mask)
185 if (this.dataType == Short.class || this.dataType == short.class) {
186 return (mask == null) ? this.maxValue : ((Short) mask).longValue();
188 if (this.dataType == Byte.class || this.dataType == byte.class) {
189 return (mask == null) ? this.maxValue : ((Byte) mask).longValue();
195 public String stringify(Object value) {
203 return HexEncode.bytesToHexStringFormat((byte[])value);
206 if ((Short)value < 0) {
207 return ((Integer) (((Short)value).intValue() & 0x7FFF | 0x8000)).toString();
212 return ((InetAddress)value).getHostAddress();
215 if ((Short)value < 0) {
216 return ((Integer) (((Short)value).intValue() & 0x7FFF | 0x8000)).toString();
222 return value.toString();