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.checkNotNull; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 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.ImmutableMap; |
| 28 | |
| 29 | /** |
| 30 | * State of a underlying path endpoint, forming protected path. |
| 31 | */ |
| 32 | @Beta |
| 33 | @Immutable |
| 34 | public class TransportEndpointState { |
| 35 | |
| 36 | /** |
| 37 | * ID assigned by the implementation. |
| 38 | */ |
| 39 | private final TransportEndpointId id; |
| 40 | |
| 41 | // meant to show liveness state by OAM probe, etc. |
| 42 | private final boolean live; |
| 43 | |
| 44 | // TODO do we need opaque config here? ⇨ May be. Comments? |
| 45 | @Beta |
| 46 | private final Map<String, String> attributes; |
| 47 | |
| 48 | // TODO Do we need reference to the config object? |
| 49 | private final TransportEndpointDescription description; |
| 50 | |
| 51 | protected TransportEndpointState(TransportEndpointDescription description, |
| 52 | TransportEndpointId id, |
| 53 | boolean live, |
| 54 | Map<String, String> attributes) { |
| 55 | this.id = checkNotNull(id); |
| 56 | this.live = live; |
| 57 | this.description = checkNotNull(description); |
| 58 | this.attributes = ImmutableMap.copyOf(attributes); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns {@link TransportEndpointId}. |
| 63 | * |
| 64 | * @return identifier |
| 65 | */ |
| 66 | public TransportEndpointId id() { |
| 67 | return id; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns liveness state of this endpoint. |
| 72 | * |
| 73 | * @return true if this endpoint is live. |
| 74 | */ |
| 75 | public boolean isLive() { |
| 76 | return live; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns description associated to this state. |
| 81 | * |
| 82 | * @return the description |
| 83 | */ |
| 84 | public TransportEndpointDescription description() { |
| 85 | return description; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns implementation defined attributes. |
| 90 | * |
| 91 | * @return the attributes |
| 92 | */ |
| 93 | @Beta |
| 94 | public Map<String, String> attributes() { |
| 95 | return attributes; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return MoreObjects.toStringHelper(this) |
| 101 | .add("id", id) |
| 102 | .add("live", live) |
| 103 | .add("description", description) |
| 104 | .add("attributes", attributes) |
| 105 | .toString(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns {@link TransportEndpointState} builder. |
| 110 | * |
| 111 | * @return builder |
| 112 | */ |
| 113 | public static Builder builder() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
| 117 | public static class Builder { |
| 118 | private TransportEndpointId id; |
| 119 | private boolean live; |
| 120 | private Map<String, String> attributes = new HashMap<>(); |
| 121 | private TransportEndpointDescription description; |
| 122 | |
| 123 | /** |
| 124 | * Copies all the fields from {@code src}. |
| 125 | * |
| 126 | * @param src object to copy from |
| 127 | * @return this |
| 128 | */ |
| 129 | public Builder copyFrom(TransportEndpointState src) { |
| 130 | this.id = src.id(); |
| 131 | this.live = src.isLive(); |
| 132 | this.attributes.putAll(src.attributes()); |
| 133 | this.description = src.description(); |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets id. |
| 139 | * |
| 140 | * @param id {@link TransportEndpointId} |
| 141 | * @return this |
| 142 | */ |
| 143 | public Builder withId(TransportEndpointId id) { |
| 144 | this.id = checkNotNull(id); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets liveness state. |
| 150 | * |
| 151 | * @param live liveness state |
| 152 | * @return this |
| 153 | */ |
| 154 | public Builder withLive(boolean live) { |
| 155 | this.live = live; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the description. |
| 161 | * |
| 162 | * @param description description |
| 163 | * @return this |
| 164 | */ |
| 165 | public Builder withDescription(TransportEndpointDescription description) { |
| 166 | this.description = checkNotNull(description); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Adds specified attributes. |
| 172 | * |
| 173 | * @param attributes to add |
| 174 | * @return this |
| 175 | */ |
| 176 | public Builder addAttributes(Map<String, String> attributes) { |
| 177 | this.attributes.putAll(attributes); |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Replaces attributes with the specified Map. |
| 183 | * |
| 184 | * @param attributes to add |
| 185 | * @return this |
| 186 | */ |
| 187 | public Builder replaceAttributes(Map<String, String> attributes) { |
| 188 | this.attributes.clear(); |
| 189 | return addAttributes(attributes); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Builds {@link TransportEndpointState}. |
| 194 | * |
| 195 | * @return {@link TransportEndpointState} |
| 196 | */ |
| 197 | public TransportEndpointState build() { |
| 198 | checkNotNull(id, "id field is mandatory"); |
| 199 | checkNotNull(description, "description field is mandatory"); |
| 200 | return new TransportEndpointState(description, id, live, attributes); |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |