blob: 34ae7e9f6b54422e32b0bf62c8973c842413108f [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 java.util.Objects;
19
alshabibfa0dc662016-01-13 11:23:53 -080020import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070021
22/**
23 * Implementation of VLAN priority criterion (3 bits).
24 */
25public final class VlanPcpCriterion implements Criterion {
26 private static final byte MASK = 0x7;
27 private final byte vlanPcp; // VLAN pcp value: 3 bits
alshabibfa0dc662016-01-13 11:23:53 -080028 private final Type type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070029
30 /**
31 * Constructor.
32 *
33 * @param vlanPcp the VLAN priority to match (3 bits)
34 */
35 VlanPcpCriterion(byte vlanPcp) {
36 this.vlanPcp = (byte) (vlanPcp & MASK);
alshabibfa0dc662016-01-13 11:23:53 -080037 this.type = Type.VLAN_PCP;
38 }
39
40 /**
41 * Constructs a vlan priority criterion with a specific type.
42 *
43 * @param vlanPcp the VLAN priority to match (3 bits)
44 * @param type a criterion type (only INNER_VLAN_PCP and VLAN_PCP are supported)
45 */
46 VlanPcpCriterion(byte vlanPcp, Type type) {
47 checkArgument(
48 type == Type.INNER_VLAN_PCP || type == Type.VLAN_PCP,
49 "Type can only be inner vlan or vlan");
50 this.vlanPcp = (byte) (vlanPcp & MASK);
51 this.type = type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070052 }
53
54 @Override
55 public Type type() {
alshabibfa0dc662016-01-13 11:23:53 -080056 return type;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070057 }
58
59 /**
60 * Gets the VLAN priority to match.
61 *
62 * @return the VLAN priority to match (3 bits)
63 */
64 public byte priority() {
65 return vlanPcp;
66 }
67
68 @Override
69 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -080070 return type().toString() + SEPARATOR + Long.toHexString(vlanPcp);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070071 }
72
73 @Override
74 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -070075 return Objects.hash(type().ordinal(), vlanPcp);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070076 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83 if (obj instanceof VlanPcpCriterion) {
84 VlanPcpCriterion that = (VlanPcpCriterion) obj;
85 return Objects.equals(vlanPcp, that.vlanPcp) &&
86 Objects.equals(this.type(), that.type());
87 }
88 return false;
89 }
90}