Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 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 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Implementation of VLAN priority criterion (3 bits). |
| 24 | */ |
| 25 | public final class VlanPcpCriterion implements Criterion { |
| 26 | private static final byte MASK = 0x7; |
| 27 | private final byte vlanPcp; // VLAN pcp value: 3 bits |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 28 | private final Type type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | * |
| 33 | * @param vlanPcp the VLAN priority to match (3 bits) |
| 34 | */ |
| 35 | VlanPcpCriterion(byte vlanPcp) { |
| 36 | this.vlanPcp = (byte) (vlanPcp & MASK); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 37 | this.type = Type.VLAN_PCP; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructs a vlan priority criterion with a specific type. |
| 42 | * |
| 43 | * @param vlanPcp the VLAN priority to match (3 bits) |
| 44 | * @param type a criterion type (only INNER_VLAN_PCP and VLAN_PCP are supported) |
| 45 | */ |
| 46 | VlanPcpCriterion(byte vlanPcp, Type type) { |
| 47 | checkArgument( |
| 48 | type == Type.INNER_VLAN_PCP || type == Type.VLAN_PCP, |
| 49 | "Type can only be inner vlan or vlan"); |
| 50 | this.vlanPcp = (byte) (vlanPcp & MASK); |
| 51 | this.type = type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Type type() { |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 56 | return type; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets the VLAN priority to match. |
| 61 | * |
| 62 | * @return the VLAN priority to match (3 bits) |
| 63 | */ |
| 64 | public byte priority() { |
| 65 | return vlanPcp; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 70 | return type().toString() + SEPARATOR + Long.toHexString(vlanPcp); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 75 | return Objects.hash(type().ordinal(), vlanPcp); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (this == obj) { |
| 81 | return true; |
| 82 | } |
| 83 | if (obj instanceof VlanPcpCriterion) { |
| 84 | VlanPcpCriterion that = (VlanPcpCriterion) obj; |
| 85 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 86 | Objects.equals(this.type(), that.type()); |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | } |