blob: 99607c8a1f5d988750cc054cd3118c3938e81d3f [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 java.util.Objects;
19
20import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfa0dc662016-01-13 11:23:53 -080021import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070022
23/**
24 * Implementation of VLAN priority criterion (3 bits).
25 */
26public final class VlanPcpCriterion implements Criterion {
27 private static final byte MASK = 0x7;
28 private final byte vlanPcp; // VLAN pcp value: 3 bits
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 vlanPcp the VLAN priority to match (3 bits)
35 */
36 VlanPcpCriterion(byte vlanPcp) {
37 this.vlanPcp = (byte) (vlanPcp & MASK);
alshabibfa0dc662016-01-13 11:23:53 -080038 this.type = Type.VLAN_PCP;
39 }
40
41 /**
42 * Constructs a vlan priority criterion with a specific type.
43 *
44 * @param vlanPcp the VLAN priority to match (3 bits)
45 * @param type a criterion type (only INNER_VLAN_PCP and VLAN_PCP are supported)
46 */
47 VlanPcpCriterion(byte vlanPcp, Type type) {
48 checkArgument(
49 type == Type.INNER_VLAN_PCP || type == Type.VLAN_PCP,
50 "Type can only be inner vlan or vlan");
51 this.vlanPcp = (byte) (vlanPcp & MASK);
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 priority to match.
62 *
63 * @return the VLAN priority to match (3 bits)
64 */
65 public byte priority() {
66 return vlanPcp;
67 }
68
69 @Override
70 public String toString() {
71 return toStringHelper(type().toString())
72 .add("priority", Long.toHexString(vlanPcp)).toString();
73 }
74
75 @Override
76 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -070077 return Objects.hash(type().ordinal(), vlanPcp);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070078 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj instanceof VlanPcpCriterion) {
86 VlanPcpCriterion that = (VlanPcpCriterion) obj;
87 return Objects.equals(vlanPcp, that.vlanPcp) &&
88 Objects.equals(this.type(), that.type());
89 }
90 return false;
91 }
92}