blob: 757e8ce1fc0a8cb902c9d8334c4512c9efab310d [file] [log] [blame]
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07001/*
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 */
16package org.onosproject.net.flow.criteria;
17
18import org.onlab.packet.VlanId;
19
20import java.util.Objects;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfa0dc662016-01-13 11:23:53 -080023import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070024
25/**
26 * Implementation of VLAN ID criterion.
27 */
28public final class VlanIdCriterion implements Criterion {
29 private final VlanId vlanId;
alshabibfa0dc662016-01-13 11:23:53 -080030 private final Type type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070031
32 /**
33 * Constructor.
34 *
35 * @param vlanId the VLAN ID to match
36 */
37 VlanIdCriterion(VlanId vlanId) {
38 this.vlanId = vlanId;
alshabibfa0dc662016-01-13 11:23:53 -080039 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 SHIMIZUfbc80e52015-04-28 10:41:58 -070054 }
55
56 @Override
57 public Type type() {
alshabibfa0dc662016-01-13 11:23:53 -080058 return type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070059 }
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 Vachuska6c8eff32015-05-27 16:11:44 -070078 return Objects.hash(type().ordinal(), vlanId);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070079 }
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}