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