Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 org.onlab.packet.VlanId; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Implementation of VLAN ID criterion. |
| 26 | */ |
| 27 | public final class VlanIdCriterion implements Criterion { |
| 28 | private final VlanId vlanId; |
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 vlanId the VLAN ID to match |
| 35 | */ |
| 36 | VlanIdCriterion(VlanId vlanId) { |
| 37 | this.vlanId = vlanId; |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 38 | this.type = Type.VLAN_VID; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructs a vlan criterion with a specific type. |
| 43 | * |
| 44 | * @param vlanId a vlan id to match |
| 45 | * @param type a criterion type (only INNER_VLAN_VID and VLAN_ID are supported) |
| 46 | */ |
| 47 | VlanIdCriterion(VlanId vlanId, Type type) { |
| 48 | checkArgument( |
| 49 | type == Type.INNER_VLAN_VID || type == Type.VLAN_VID, |
| 50 | "Type can only be inner vlan or vlan"); |
| 51 | this.vlanId = vlanId; |
| 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 ID to match. |
| 62 | * |
| 63 | * @return the VLAN ID to match |
| 64 | */ |
| 65 | public VlanId vlanId() { |
| 66 | return vlanId; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 71 | return type().toString() + SEPARATOR + vlanId; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 76 | return Objects.hash(type().ordinal(), vlanId); |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object obj) { |
| 81 | if (this == obj) { |
| 82 | return true; |
| 83 | } |
| 84 | if (obj instanceof VlanIdCriterion) { |
| 85 | VlanIdCriterion that = (VlanIdCriterion) obj; |
| 86 | return Objects.equals(vlanId, that.vlanId) && |
| 87 | Objects.equals(this.type(), that.type()); |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | } |