blob: 44e618e37f69b358181b1108a1f51c46b8ad24de [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.impl;
17
18import org.onosproject.net.DeviceId;
19import org.onosproject.segmentrouting.policy.api.TrafficMatchId;
20
21import java.util.Objects;
22import java.util.StringTokenizer;
23
24/**
pierventre4f68ffa2021-03-09 22:52:14 +010025 * Traffic match key used by the store to track the operations ongoing on the devices.
26 *
27 * Traffic match is the high level intent expressed by the user that gets translated by
28 * the PolicyManager in modifications operated on the devices. The policy operations
29 * are more important than the policy itself without them the policy cannot be
30 * considered fulfilled.
pierventre30368ab2021-02-24 23:23:22 +010031 */
32public class TrafficMatchKey {
33 private DeviceId deviceId;
34 private TrafficMatchId trafficMatchId;
35
36 /**
37 * Constructs new traffic match key with given device id and traffic match id.
38 *
39 * @param deviceId device id
40 * @param trafficMatchId traffic match id
41 */
42 public TrafficMatchKey(DeviceId deviceId, TrafficMatchId trafficMatchId) {
43 this.deviceId = deviceId;
44 this.trafficMatchId = trafficMatchId;
45 }
46
47 /**
48 * Gets device id.
49 *
50 * @return device id of the policy key
51 */
52 public DeviceId deviceId() {
53 return deviceId;
54 }
55
56 /**
57 * Gets traffic match id.
58 *
59 * @return the id of the traffic match
60 */
61 public TrafficMatchId trafficMatchId() {
62 return trafficMatchId;
63 }
64
65 @Override
66 public boolean equals(final Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (!(obj instanceof TrafficMatchKey)) {
71 return false;
72 }
73 final TrafficMatchKey other = (TrafficMatchKey) obj;
74 return Objects.equals(this.deviceId, other.deviceId) &&
75 Objects.equals(this.trafficMatchId, other.trafficMatchId);
76 }
77
78 @Override
79 public int hashCode() {
80 return Objects.hash(deviceId, trafficMatchId);
81 }
82
83 /**
84 * Parses from a string the police key.
85 *
86 * @param str the string to parse
87 * @return the policy key if present in the str, null otherwise
88 */
89 public static TrafficMatchKey fromString(String str) {
90 TrafficMatchKey policyKey = null;
91 if (str != null && str.contains(PolicyManager.KEY_SEPARATOR)) {
92 StringTokenizer tokenizer = new StringTokenizer(str, PolicyManager.KEY_SEPARATOR);
93 if (tokenizer.countTokens() == 2) {
94 policyKey = new TrafficMatchKey(DeviceId.deviceId(tokenizer.nextToken()),
95 TrafficMatchId.of(tokenizer.nextToken()));
96 }
97 }
98 return policyKey;
99 }
100
101 @Override
102 public String toString() {
103 return deviceId.toString() + PolicyManager.KEY_SEPARATOR + trafficMatchId.toString();
104 }
105}