blob: 83098feeb79909f2ddde4437a557fcb16f9f2f85 [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;
37
38 /**
39 * Builds a traffic match.
40 *
41 * @param trafficselector the traffic selector
42 * @param policyid the associated policy id
43 */
44 public TrafficMatch(TrafficSelector trafficselector, PolicyId policyid) {
45 trafficSelector = trafficselector;
46 trafficMatchId = TrafficMatchId.trafficMatchId(computeTrafficMatchId());
47 policyId = policyid;
48 }
49
50 /**
51 * Returns the traffic match id.
52 *
53 * @return the id of the traffic match
54 */
55 public TrafficMatchId trafficMatchId() {
56 return trafficMatchId;
57 }
58
59 /**
60 * Returns the id of the policy associated with.
61 *
62 * @return the policy id
63 */
64 public PolicyId policyId() {
65 return policyId;
66 }
67
68 /**
69 * Returns the traffic selector associated with.
70 *
71 * @return the traffic selector
72 */
73 public TrafficSelector trafficSelector() {
74 return trafficSelector;
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hash(trafficMatchId, trafficSelector, policyId);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (obj instanceof TrafficMatch) {
88 final TrafficMatch other = (TrafficMatch) obj;
89 return Objects.equals(this.trafficMatchId, other.trafficMatchId) &&
90 Objects.equals(trafficSelector, other.trafficSelector) &&
91 Objects.equals(policyId, other.policyId);
92 }
93 return false;
94 }
95
96 @Override
97 public String toString() {
98 return toStringHelper(this)
99 .add("trafficMatchId", trafficMatchId)
100 .add("trafficSelector", trafficSelector)
101 .add("policyId", policyId)
102 .toString();
103 }
104
105 // Compute the id using the traffic selector. This method results to be consistent across the cluster.
106 private int computeTrafficMatchId() {
107 Funnel<TrafficSelector> selectorFunnel = (from, into) -> from.criteria()
108 .forEach(c -> into.putUnencodedChars(c.toString()));
109 HashFunction hashFunction = Hashing.murmur3_32();
110 HashCode hashCode = hashFunction.newHasher()
111 .putObject(trafficSelector, selectorFunnel)
112 .hash();
113 return hashCode.asInt();
114 }
115}