blob: bcad3bd8acafc8c26e9fab0178936af26dabd06a [file] [log] [blame]
pierventre30368ab2021-02-24 23:23:22 +01001/*
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 */
16package org.onosproject.segmentrouting.policy.api;
17
18
19import com.google.common.hash.Funnel;
20import com.google.common.hash.HashCode;
21import com.google.common.hash.HashFunction;
22import com.google.common.hash.Hashing;
23import org.onosproject.net.flow.TrafficSelector;
24
25import java.util.Objects;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28
29/**
30 * Representation of a traffic match.
31 */
32public final class TrafficMatch {
33 // Traffic match internal state
34 private TrafficMatchId trafficMatchId;
35 private TrafficSelector trafficSelector;
36 private PolicyId policyId;
Wailok Shum37dd29a2021-04-27 18:13:55 +080037 private TrafficMatchPriority trafficMatchPriority;
pierventre30368ab2021-02-24 23:23:22 +010038
39 /**
40 * Builds a traffic match.
41 *
Wailok Shum37dd29a2021-04-27 18:13:55 +080042 * @param trafficSelector the traffic selector
43 * @param policyId the associated policy id
44 * @deprecated in version 3.0.2
pierventre30368ab2021-02-24 23:23:22 +010045 */
Wailok Shum37dd29a2021-04-27 18:13:55 +080046 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;
pierventre30368ab2021-02-24 23:23:22 +010065 }
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 Shum37dd29a2021-04-27 18:13:55 +080094 /**
95 * Returns the priority.
96 *
97 * @return the priority
98 */
99 public TrafficMatchPriority trafficMatchPriority() {
100 return trafficMatchPriority;
101 }
102
pierventre30368ab2021-02-24 23:23:22 +0100103 @Override
104 public int hashCode() {
Wailok Shum37dd29a2021-04-27 18:13:55 +0800105 return Objects.hash(trafficMatchId, trafficSelector, policyId, trafficMatchPriority);
pierventre30368ab2021-02-24 23:23:22 +0100106 }
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 Shum37dd29a2021-04-27 18:13:55 +0800117 Objects.equals(policyId, other.policyId) &&
118 Objects.equals(trafficMatchPriority, other.trafficMatchPriority);
pierventre30368ab2021-02-24 23:23:22 +0100119 }
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 Shum37dd29a2021-04-27 18:13:55 +0800129 .add("trafficMatchPriority", trafficMatchPriority)
pierventre30368ab2021-02-24 23:23:22 +0100130 .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}