samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [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; |
| 17 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 19 | import org.onosproject.net.AbstractDescription; |
| 20 | import org.onosproject.net.SparseAnnotations; |
| 21 | |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
| 23 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 24 | import java.util.Optional; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 29 | /** |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 30 | * Default implementation of immutable tunnel interface description entity. |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 31 | */ |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 32 | public final class DefaultTunnelDescription extends AbstractDescription |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 33 | implements TunnelDescription { |
| 34 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 35 | private final Optional<String> deviceId; |
| 36 | private final String ifaceName; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 37 | private final Type type; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 38 | private final Optional<TunnelEndPoint> local; |
| 39 | private final Optional<TunnelEndPoint> remote; |
| 40 | private final Optional<TunnelKey> key; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Creates a tunnel description using the supplied information. |
| 44 | * |
| 45 | * @param src TunnelPoint source |
| 46 | * @param dst TunnelPoint destination |
| 47 | * @param type tunnel type |
| 48 | * @param tunnelName tunnel name |
| 49 | * @param annotations optional key/value annotations |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 50 | * @deprecated version 1.7.0 - Hummingbird |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 51 | */ |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 52 | @Deprecated |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 53 | public DefaultTunnelDescription(TunnelEndPoint src, |
| 54 | TunnelEndPoint dst, Type type, |
| 55 | TunnelName tunnelName, |
| 56 | SparseAnnotations... annotations) { |
| 57 | super(annotations); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 58 | this.deviceId = Optional.empty(); |
| 59 | this.local = Optional.ofNullable(src); |
| 60 | this.remote = Optional.ofNullable(dst); |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 61 | this.type = type; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 62 | this.ifaceName = tunnelName.value(); |
| 63 | this.key = Optional.empty(); |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Creates a tunnel description using the supplied information. |
| 68 | * |
| 69 | * @param ifaceName tunnel interface ifaceName |
| 70 | * @param local source tunnel endpoint |
| 71 | * @param remote destination tunnel endpoint |
| 72 | * @param type tunnel type |
| 73 | * @param annotations optional key/value annotations |
| 74 | */ |
| 75 | private DefaultTunnelDescription(Optional<String> deviceId, |
| 76 | String ifaceName, |
| 77 | Type type, |
| 78 | Optional<TunnelEndPoint> local, |
| 79 | Optional<TunnelEndPoint> remote, |
| 80 | Optional<TunnelKey> key, |
| 81 | SparseAnnotations... annotations) { |
| 82 | super(annotations); |
| 83 | this.deviceId = deviceId; |
| 84 | this.ifaceName = checkNotNull(ifaceName); |
| 85 | this.type = type; |
| 86 | this.local = local; |
| 87 | this.remote = remote; |
| 88 | this.key = key; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Optional<String> deviceId() { |
| 93 | return deviceId; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String ifaceName() { |
| 98 | return ifaceName; |
| 99 | } |
| 100 | |
| 101 | @Deprecated |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 102 | @Override |
| 103 | public TunnelEndPoint src() { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 104 | return local.isPresent() ? local.get() : null; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 107 | @Deprecated |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 108 | @Override |
| 109 | public TunnelEndPoint dst() { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 110 | return remote.isPresent() ? remote.get() : null; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Type type() { |
| 115 | return type; |
| 116 | } |
| 117 | |
| 118 | @Override |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 119 | public Optional<TunnelEndPoint> local() { |
| 120 | return local; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Optional<TunnelEndPoint> remote() { |
| 125 | return remote; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Optional<TunnelKey> key() { |
| 130 | return key; |
| 131 | } |
| 132 | |
| 133 | @Deprecated |
| 134 | @Override |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 135 | public TunnelName tunnelName() { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 136 | return TunnelName.tunnelName(ifaceName); |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | return MoreObjects.toStringHelper(this) |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 142 | .add("deviceId", deviceId) |
| 143 | .add("ifaceName", ifaceName) |
| 144 | .add("type", type) |
| 145 | .add("local", local) |
| 146 | .add("remote", remote) |
| 147 | .add("key", key) |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 148 | .toString(); |
| 149 | } |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Creates and returns a new builder instance. |
| 153 | * |
| 154 | * @return default tunnel description builder |
| 155 | */ |
| 156 | public static Builder builder() { |
| 157 | return new Builder(); |
| 158 | } |
| 159 | |
| 160 | public static final class Builder implements TunnelDescription.Builder { |
| 161 | private Optional<String> deviceId = Optional.empty(); |
| 162 | private String ifaceName; |
| 163 | private Type type; |
| 164 | private Optional<TunnelEndPoint> local = Optional.empty(); |
| 165 | private Optional<TunnelEndPoint> remote = Optional.empty(); |
| 166 | private Optional<TunnelKey> key = Optional.empty(); |
| 167 | private Optional<SparseAnnotations> otherConfigs = Optional.empty(); |
| 168 | |
| 169 | private Builder() { |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public TunnelDescription build() { |
| 174 | if (otherConfigs.isPresent()) { |
| 175 | return new DefaultTunnelDescription(deviceId, ifaceName, type, |
| 176 | local, remote, key, |
| 177 | otherConfigs.get()); |
| 178 | } else { |
| 179 | return new DefaultTunnelDescription(deviceId, ifaceName, type, |
| 180 | local, remote, key); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Builder deviceId(String deviceId) { |
| 186 | this.deviceId = Optional.ofNullable(deviceId); |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Builder ifaceName(String ifaceName) { |
| 192 | checkArgument(!Strings.isNullOrEmpty(ifaceName)); |
| 193 | this.ifaceName = ifaceName; |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Builder type(Type type) { |
| 199 | this.type = type; |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Builder local(TunnelEndPoint endpoint) { |
| 205 | local = Optional.ofNullable(endpoint); |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Builder remote(TunnelEndPoint endpoint) { |
| 211 | remote = Optional.ofNullable(endpoint); |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Builder key(TunnelKey key) { |
| 217 | this.key = Optional.ofNullable(key); |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public Builder otherConfigs(SparseAnnotations configs) { |
| 223 | otherConfigs = Optional.ofNullable(configs); |
| 224 | return this; |
| 225 | } |
| 226 | } |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 227 | } |