blob: 68181a61164654944216f54562db8c7b6a21ea68 [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/**
25 * Traffic match key used by the store.
26 */
27public class TrafficMatchKey {
28 private DeviceId deviceId;
29 private TrafficMatchId trafficMatchId;
30
31 /**
32 * Constructs new traffic match key with given device id and traffic match id.
33 *
34 * @param deviceId device id
35 * @param trafficMatchId traffic match id
36 */
37 public TrafficMatchKey(DeviceId deviceId, TrafficMatchId trafficMatchId) {
38 this.deviceId = deviceId;
39 this.trafficMatchId = trafficMatchId;
40 }
41
42 /**
43 * Gets device id.
44 *
45 * @return device id of the policy key
46 */
47 public DeviceId deviceId() {
48 return deviceId;
49 }
50
51 /**
52 * Gets traffic match id.
53 *
54 * @return the id of the traffic match
55 */
56 public TrafficMatchId trafficMatchId() {
57 return trafficMatchId;
58 }
59
60 @Override
61 public boolean equals(final Object obj) {
62 if (this == obj) {
63 return true;
64 }
65 if (!(obj instanceof TrafficMatchKey)) {
66 return false;
67 }
68 final TrafficMatchKey other = (TrafficMatchKey) obj;
69 return Objects.equals(this.deviceId, other.deviceId) &&
70 Objects.equals(this.trafficMatchId, other.trafficMatchId);
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(deviceId, trafficMatchId);
76 }
77
78 /**
79 * Parses from a string the police key.
80 *
81 * @param str the string to parse
82 * @return the policy key if present in the str, null otherwise
83 */
84 public static TrafficMatchKey fromString(String str) {
85 TrafficMatchKey policyKey = null;
86 if (str != null && str.contains(PolicyManager.KEY_SEPARATOR)) {
87 StringTokenizer tokenizer = new StringTokenizer(str, PolicyManager.KEY_SEPARATOR);
88 if (tokenizer.countTokens() == 2) {
89 policyKey = new TrafficMatchKey(DeviceId.deviceId(tokenizer.nextToken()),
90 TrafficMatchId.of(tokenizer.nextToken()));
91 }
92 }
93 return policyKey;
94 }
95
96 @Override
97 public String toString() {
98 return deviceId.toString() + PolicyManager.KEY_SEPARATOR + trafficMatchId.toString();
99 }
100}