blob: d64d487949f0ada2803c8bdd2a48c780070998f4 [file] [log] [blame]
pierventre4f68ffa2021-03-09 22:52:14 +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
18import com.google.common.collect.Lists;
19import com.google.common.collect.Sets;
20import org.onosproject.net.DeviceId;
21
22import java.util.Comparator;
23import java.util.List;
24import java.util.Objects;
25import java.util.Set;
26import java.util.TreeSet;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkArgument;
30
31/**
32 * Implementation of the redirect policy.
33 */
34public final class RedirectPolicy extends AbstractPolicy {
35 private List<DeviceId> spinesToEnforce = Lists.newArrayList();
36
37 /**
38 * Builds up a REDIRECT policy.
39 *
40 * @param spines the spines to enforce
41 */
42 public RedirectPolicy(Set<DeviceId> spines) {
43 super(PolicyType.REDIRECT);
44 checkArgument(!spines.isEmpty(), "Must have at least one spine");
45 // Creates an ordered set
46 TreeSet<DeviceId> sortedSpines = Sets.newTreeSet(Comparator.comparing(DeviceId::toString));
47 sortedSpines.addAll(spines);
48 spinesToEnforce.addAll(sortedSpines);
49 policyId = computePolicyId();
50 }
51
52 /**
53 * Returns the spines to be enforced during the path computation.
54 *
55 * @return the spines to be enforced
56 */
57 public List<DeviceId> spinesToEnforce() {
58 return spinesToEnforce;
59 }
60
61 @Override
62 protected PolicyId computePolicyId() {
63 return PolicyId.of(policyType().name() + spinesToEnforce);
64 }
65
66 @Override
67 public boolean equals(final Object obj) {
68 if (this == obj) {
69 return true;
70 }
71 if (!(obj instanceof RedirectPolicy)) {
72 return false;
73 }
74 final RedirectPolicy other = (RedirectPolicy) obj;
75 return Objects.equals(policyType(), other.policyType()) &&
76 Objects.equals(policyId(), other.policyId()) &&
77 Objects.equals(spinesToEnforce, other.spinesToEnforce);
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(policyId(), policyType(), spinesToEnforce);
83 }
84
85 @Override
86 public String toString() {
87 return toStringHelper(this)
88 .add("policyId", policyId())
89 .add("policyType", policyType())
90 .add("spinesToEnforce", spinesToEnforce)
91 .toString();
92 }
93}