pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.segmentrouting.policy.api; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import java.util.Comparator; |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | import java.util.TreeSet; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of the redirect policy. |
| 33 | */ |
| 34 | public 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 | } |