Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.flow.criteria; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Implementation of VLAN priority criterion (3 bits). |
| 25 | */ |
| 26 | public final class VlanPcpCriterion implements Criterion { |
| 27 | private static final byte MASK = 0x7; |
| 28 | private final byte vlanPcp; // VLAN pcp value: 3 bits |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 29 | private final Type type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | * |
| 34 | * @param vlanPcp the VLAN priority to match (3 bits) |
| 35 | */ |
| 36 | VlanPcpCriterion(byte vlanPcp) { |
| 37 | this.vlanPcp = (byte) (vlanPcp & MASK); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 38 | this.type = Type.VLAN_PCP; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructs a vlan priority criterion with a specific type. |
| 43 | * |
| 44 | * @param vlanPcp the VLAN priority to match (3 bits) |
| 45 | * @param type a criterion type (only INNER_VLAN_PCP and VLAN_PCP are supported) |
| 46 | */ |
| 47 | VlanPcpCriterion(byte vlanPcp, Type type) { |
| 48 | checkArgument( |
| 49 | type == Type.INNER_VLAN_PCP || type == Type.VLAN_PCP, |
| 50 | "Type can only be inner vlan or vlan"); |
| 51 | this.vlanPcp = (byte) (vlanPcp & MASK); |
| 52 | this.type = type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Type type() { |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 57 | return type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets the VLAN priority to match. |
| 62 | * |
| 63 | * @return the VLAN priority to match (3 bits) |
| 64 | */ |
| 65 | public byte priority() { |
| 66 | return vlanPcp; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return toStringHelper(type().toString()) |
| 72 | .add("priority", Long.toHexString(vlanPcp)).toString(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 77 | return Objects.hash(type().ordinal(), vlanPcp); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object obj) { |
| 82 | if (this == obj) { |
| 83 | return true; |
| 84 | } |
| 85 | if (obj instanceof VlanPcpCriterion) { |
| 86 | VlanPcpCriterion that = (VlanPcpCriterion) obj; |
| 87 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 88 | Objects.equals(this.type(), that.type()); |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | } |