Yuta HIGUCHI | 4d0f89a | 2016-09-14 15:40:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | 4d0f89a | 2016-09-14 15:40:39 -0700 | [diff] [blame] | 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.net.behaviour.protection; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import javax.annotation.concurrent.Immutable; |
| 24 | |
| 25 | import com.google.common.annotations.Beta; |
| 26 | import com.google.common.base.MoreObjects; |
| 27 | import com.google.common.collect.ImmutableList; |
| 28 | |
| 29 | /** |
| 30 | * State of protected path endpoint. |
| 31 | */ |
| 32 | @Beta |
| 33 | @Immutable |
| 34 | public class ProtectedTransportEndpointState { |
| 35 | |
| 36 | /** |
| 37 | * Active path is currently unknown. |
| 38 | */ |
| 39 | public static final int ACTIVE_UNKNOWN = -1; |
| 40 | |
| 41 | /** |
| 42 | * List of underlying path/flow in priority order. |
| 43 | */ |
| 44 | private final List<TransportEndpointState> pathStates; |
| 45 | |
| 46 | /** |
| 47 | * {@link #pathStates} index of the active Path or {@link #ACTIVE_UNKNOWN}. |
| 48 | */ |
| 49 | private final int activePathIndex; |
| 50 | |
| 51 | // TODO Do we need reference to the config object? |
| 52 | private final ProtectedTransportEndpointDescription description; |
| 53 | |
| 54 | |
| 55 | protected ProtectedTransportEndpointState(ProtectedTransportEndpointDescription description, |
| 56 | List<TransportEndpointState> pathStates, |
| 57 | int activePathIndex) { |
| 58 | this.description = checkNotNull(description); |
| 59 | this.pathStates = ImmutableList.copyOf(pathStates); |
| 60 | this.activePathIndex = activePathIndex; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the description of this ProtectedPathEndPoint. |
| 65 | * |
| 66 | * @return the description |
| 67 | */ |
| 68 | public ProtectedTransportEndpointDescription description() { |
| 69 | return description; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the {@link TransportEndpointState}s forming the ProtectedPathEndPoint. |
| 74 | * |
| 75 | * @return the pathStates |
| 76 | */ |
| 77 | public List<TransportEndpointState> pathStates() { |
| 78 | return pathStates; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the index of the working Path in {@link #pathStates()} List or |
| 83 | * {@link #ACTIVE_UNKNOWN}. |
| 84 | * |
| 85 | * @return the activePathIndex |
| 86 | */ |
| 87 | public int workingPathIndex() { |
| 88 | return activePathIndex; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String toString() { |
| 93 | return MoreObjects.toStringHelper(this) |
| 94 | .add("pathState", pathStates) |
| 95 | .add("activePathIndex", activePathIndex) |
| 96 | .add("description", description) |
| 97 | .toString(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns {@link ProtectedTransportEndpointState} builder. |
| 102 | * |
| 103 | * @return builder |
| 104 | */ |
| 105 | public static Builder builder() { |
| 106 | return new Builder(); |
| 107 | } |
| 108 | |
| 109 | public static class Builder { |
| 110 | private List<TransportEndpointState> pathStates; |
| 111 | private int activePathIndex = ACTIVE_UNKNOWN; |
| 112 | private ProtectedTransportEndpointDescription description; |
| 113 | |
| 114 | /** |
| 115 | * Copies all the fields from {@code src}. |
| 116 | * |
| 117 | * @param src object to copy from |
| 118 | * @return this |
| 119 | */ |
| 120 | public Builder copyFrom(ProtectedTransportEndpointState src) { |
| 121 | this.pathStates = src.pathStates(); |
| 122 | this.activePathIndex = src.workingPathIndex(); |
| 123 | this.description = src.description(); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets the path states. |
| 129 | * |
| 130 | * @param pathStates the path states |
| 131 | * @return this |
| 132 | */ |
| 133 | public Builder withPathStates(List<TransportEndpointState> pathStates) { |
| 134 | this.pathStates = pathStates; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets the activePathIndex. |
| 140 | * |
| 141 | * @param activePathIndex the activePathIndex |
| 142 | * @return this |
| 143 | */ |
| 144 | public Builder withActivePathIndex(int activePathIndex) { |
| 145 | this.activePathIndex = activePathIndex; |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets the description. |
| 151 | * |
| 152 | * @param description of this {@link ProtectedTransportEndpointState} |
| 153 | * @return this |
| 154 | */ |
| 155 | public Builder withDescription(ProtectedTransportEndpointDescription description) { |
| 156 | this.description = description; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Builds {@link ProtectedTransportEndpointState}. |
| 162 | * |
| 163 | * @return {@link ProtectedTransportEndpointState} |
| 164 | */ |
| 165 | public ProtectedTransportEndpointState build() { |
| 166 | checkNotNull(description, "description field is mandatory"); |
| 167 | checkNotNull(pathStates, "pathStates field is mandatory"); |
| 168 | checkArgument(activePathIndex < pathStates.size(), |
| 169 | "Invalid active path index %s > %s", |
| 170 | activePathIndex, pathStates.size()); |
| 171 | return new ProtectedTransportEndpointState(description, pathStates, activePathIndex); |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | } |