blob: c4590629336ebf16c070315e44fd74615954075f [file] [log] [blame]
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07003 *
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 */
16package org.onosproject.net.flow.criteria;
17
18import org.onlab.packet.VlanId;
19
20import java.util.Objects;
21
alshabibfa0dc662016-01-13 11:23:53 -080022import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070023
24/**
25 * Implementation of VLAN ID criterion.
26 */
27public final class VlanIdCriterion implements Criterion {
28 private final VlanId vlanId;
alshabibfa0dc662016-01-13 11:23:53 -080029 private final Type type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070030
31 /**
32 * Constructor.
33 *
34 * @param vlanId the VLAN ID to match
35 */
36 VlanIdCriterion(VlanId vlanId) {
37 this.vlanId = vlanId;
alshabibfa0dc662016-01-13 11:23:53 -080038 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 SHIMIZUfbc80e52015-04-28 10:41:58 -070053 }
54
55 @Override
56 public Type type() {
alshabibfa0dc662016-01-13 11:23:53 -080057 return type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070058 }
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 Hartc7840bd2016-01-21 23:26:29 -080071 return type().toString() + SEPARATOR + vlanId;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070072 }
73
74 @Override
75 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -070076 return Objects.hash(type().ordinal(), vlanId);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070077 }
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}