blob: 64b2bbba14d5023b7894f9f0afc178f0eda62c61 [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/**
pierventre4f68ffa2021-03-09 22:52:14 +010025 * Policy key used by the store to track the operations ongoing on the devices.
26 *
27 * Policy 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 PolicyKey {
33 private DeviceId deviceId;
34 private PolicyId policyId;
35
36 /**
37 * Constructs new policy key with given device id and policy id.
38 *
39 * @param deviceId device id
40 * @param policyId policy id
41 */
42 public PolicyKey(DeviceId deviceId, PolicyId policyId) {
43 this.deviceId = deviceId;
44 this.policyId = policyId;
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 policy id.
58 *
59 * @return the id of the policy
60 */
61 public PolicyId policyId() {
62 return policyId;
63 }
64
65 @Override
66 public boolean equals(final Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (!(obj instanceof PolicyKey)) {
71 return false;
72 }
73 final PolicyKey other = (PolicyKey) obj;
74 return Objects.equals(this.deviceId, other.deviceId) &&
75 Objects.equals(this.policyId, other.policyId);
76 }
77
78 @Override
79 public int hashCode() {
80 return Objects.hash(deviceId, policyId);
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 PolicyKey fromString(String str) {
90 PolicyKey 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 PolicyKey(DeviceId.deviceId(tokenizer.nextToken()),
95 PolicyId.of(tokenizer.nextToken()));
96 }
97 }
98 return policyKey;
99 }
100
101 @Override
102 public String toString() {
103 return deviceId.toString() + PolicyManager.KEY_SEPARATOR + policyId.toString();
104 }
105}