pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.segmentrouting.policy.api; |
| 17 | |
| 18 | |
| 19 | import com.google.common.hash.Funnel; |
| 20 | import com.google.common.hash.HashCode; |
| 21 | import com.google.common.hash.HashFunction; |
| 22 | import com.google.common.hash.Hashing; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | |
| 29 | /** |
| 30 | * Representation of a traffic match. |
| 31 | */ |
| 32 | public final class TrafficMatch { |
| 33 | // Traffic match internal state |
| 34 | private TrafficMatchId trafficMatchId; |
| 35 | private TrafficSelector trafficSelector; |
| 36 | private PolicyId policyId; |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 37 | private TrafficMatchPriority trafficMatchPriority; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Builds a traffic match. |
| 41 | * |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 42 | * @param trafficSelector the traffic selector |
| 43 | * @param policyId the associated policy id |
| 44 | * @deprecated in version 3.0.2 |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 45 | */ |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 46 | public TrafficMatch(TrafficSelector trafficSelector, PolicyId policyId) { |
| 47 | this.trafficSelector = trafficSelector; |
| 48 | this.trafficMatchId = TrafficMatchId.trafficMatchId(computeTrafficMatchId()); |
| 49 | this.policyId = policyId; |
| 50 | this.trafficMatchPriority = new TrafficMatchPriority(PolicyService.TRAFFIC_MATCH_PRIORITY); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Builds a traffic match. |
| 55 | * |
| 56 | * @param trafficSelector the traffic selector |
| 57 | * @param policyId the associated policy id |
| 58 | * @param trafficMatchPriority the priority |
| 59 | */ |
| 60 | public TrafficMatch(TrafficSelector trafficSelector, PolicyId policyId, TrafficMatchPriority trafficMatchPriority) { |
| 61 | this.trafficSelector = trafficSelector; |
| 62 | this.trafficMatchId = TrafficMatchId.trafficMatchId(computeTrafficMatchId()); |
| 63 | this.policyId = policyId; |
| 64 | this.trafficMatchPriority = trafficMatchPriority; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the traffic match id. |
| 69 | * |
| 70 | * @return the id of the traffic match |
| 71 | */ |
| 72 | public TrafficMatchId trafficMatchId() { |
| 73 | return trafficMatchId; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the id of the policy associated with. |
| 78 | * |
| 79 | * @return the policy id |
| 80 | */ |
| 81 | public PolicyId policyId() { |
| 82 | return policyId; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the traffic selector associated with. |
| 87 | * |
| 88 | * @return the traffic selector |
| 89 | */ |
| 90 | public TrafficSelector trafficSelector() { |
| 91 | return trafficSelector; |
| 92 | } |
| 93 | |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 94 | /** |
| 95 | * Returns the priority. |
| 96 | * |
| 97 | * @return the priority |
| 98 | */ |
| 99 | public TrafficMatchPriority trafficMatchPriority() { |
| 100 | return trafficMatchPriority; |
| 101 | } |
| 102 | |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 103 | @Override |
| 104 | public int hashCode() { |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 105 | return Objects.hash(trafficMatchId, trafficSelector, policyId, trafficMatchPriority); |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | if (obj instanceof TrafficMatch) { |
| 114 | final TrafficMatch other = (TrafficMatch) obj; |
| 115 | return Objects.equals(this.trafficMatchId, other.trafficMatchId) && |
| 116 | Objects.equals(trafficSelector, other.trafficSelector) && |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 117 | Objects.equals(policyId, other.policyId) && |
| 118 | Objects.equals(trafficMatchPriority, other.trafficMatchPriority); |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String toString() { |
| 125 | return toStringHelper(this) |
| 126 | .add("trafficMatchId", trafficMatchId) |
| 127 | .add("trafficSelector", trafficSelector) |
| 128 | .add("policyId", policyId) |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame^] | 129 | .add("trafficMatchPriority", trafficMatchPriority) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 130 | .toString(); |
| 131 | } |
| 132 | |
| 133 | // Compute the id using the traffic selector. This method results to be consistent across the cluster. |
| 134 | private int computeTrafficMatchId() { |
| 135 | Funnel<TrafficSelector> selectorFunnel = (from, into) -> from.criteria() |
| 136 | .forEach(c -> into.putUnencodedChars(c.toString())); |
| 137 | HashFunction hashFunction = Hashing.murmur3_32(); |
| 138 | HashCode hashCode = hashFunction.newHasher() |
| 139 | .putObject(trafficSelector, selectorFunnel) |
| 140 | .hash(); |
| 141 | return hashCode.asInt(); |
| 142 | } |
| 143 | } |