blob: 1fe42e86bb76ef9ba196ac9f6bc356620c05139d [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.MacAddress;
19
20import java.util.Objects;
21
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070022/**
23 * Implementation of MAC address criterion.
24 */
25public final class EthCriterion implements Criterion {
26 private final MacAddress mac;
Saurav Das9d6c86b2016-02-19 09:01:07 -080027 private final MacAddress mask;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070028 private final Type type;
29
30 /**
31 * Constructor.
32 *
33 * @param mac the source or destination MAC address to match
Saurav Das9d6c86b2016-02-19 09:01:07 -080034 * @param mask the mask for the address
35 * @param type the match type. Should be either Type.ETH_DST_MASKED or
36 * Type.ETH_SRC_MASKED
37 */
38 EthCriterion(MacAddress mac, MacAddress mask, Type type) {
39 this.mac = mac;
40 this.mask = mask;
41 this.type = type;
42 }
43
44 /**
45 * Constructor.
46 *
47 * @param mac the source or destination MAC address to match
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070048 * @param type the match type. Should be either Type.ETH_DST or
Saurav Das9d6c86b2016-02-19 09:01:07 -080049 * Type.ETH_SRC
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070050 */
51 EthCriterion(MacAddress mac, Type type) {
Saurav Das9d6c86b2016-02-19 09:01:07 -080052 this(mac, null, type);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070053 }
54
55 @Override
56 public Type type() {
57 return this.type;
58 }
59
60 /**
61 * Gets the MAC address to match.
62 *
63 * @return the MAC address to match
64 */
65 public MacAddress mac() {
66 return this.mac;
67 }
68
Saurav Das9d6c86b2016-02-19 09:01:07 -080069 /**
70 * Gets the mask for the MAC address to match.
71 *
72 * @return the MAC address to match
73 */
74 public MacAddress mask() {
75 return this.mask;
76 }
77
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070078 @Override
79 public String toString() {
Charles Chanb38f88e2016-02-22 19:43:28 -080080 return (mask != null) ?
81 type().toString() + SEPARATOR + mac + "/" + mask :
82 type().toString() + SEPARATOR + mac;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070083 }
84
85 @Override
86 public int hashCode() {
Saurav Das9d6c86b2016-02-19 09:01:07 -080087 return Objects.hash(type.ordinal(), mac, mask);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070088 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 }
95 if (obj instanceof EthCriterion) {
96 EthCriterion that = (EthCriterion) obj;
97 return Objects.equals(mac, that.mac) &&
Saurav Das9d6c86b2016-02-19 09:01:07 -080098 Objects.equals(mask, that.mask) &&
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070099 Objects.equals(type, that.type);
100 }
101 return false;
102 }
103}