blob: e0eac06e8029cdced9e62d51eeced910c7475a21 [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.PolicyId;
20
21import java.util.Objects;
22import java.util.StringTokenizer;
23
24/**
25 * Policy key used by the store.
26 */
27public class PolicyKey {
28 private DeviceId deviceId;
29 private PolicyId policyId;
30
31 /**
32 * Constructs new policy key with given device id and policy id.
33 *
34 * @param deviceId device id
35 * @param policyId policy id
36 */
37 public PolicyKey(DeviceId deviceId, PolicyId policyId) {
38 this.deviceId = deviceId;
39 this.policyId = policyId;
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 policy id.
53 *
54 * @return the id of the policy
55 */
56 public PolicyId policyId() {
57 return policyId;
58 }
59
60 @Override
61 public boolean equals(final Object obj) {
62 if (this == obj) {
63 return true;
64 }
65 if (!(obj instanceof PolicyKey)) {
66 return false;
67 }
68 final PolicyKey other = (PolicyKey) obj;
69 return Objects.equals(this.deviceId, other.deviceId) &&
70 Objects.equals(this.policyId, other.policyId);
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(deviceId, policyId);
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 PolicyKey fromString(String str) {
85 PolicyKey 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 PolicyKey(DeviceId.deviceId(tokenizer.nextToken()),
90 PolicyId.of(tokenizer.nextToken()));
91 }
92 }
93 return policyKey;
94 }
95
96 @Override
97 public String toString() {
98 return deviceId.toString() + PolicyManager.KEY_SEPARATOR + policyId.toString();
99 }
100}