samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | * |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 45 | * @param ifaceName tunnel interface ifaceName |
| 46 | * @param local source tunnel endpoint |
| 47 | * @param remote destination tunnel endpoint |
| 48 | * @param type tunnel type |
| 49 | * @param annotations optional key/value annotations |
| 50 | */ |
| 51 | private DefaultTunnelDescription(Optional<String> deviceId, |
| 52 | String ifaceName, |
| 53 | Type type, |
| 54 | Optional<TunnelEndPoint> local, |
| 55 | Optional<TunnelEndPoint> remote, |
| 56 | Optional<TunnelKey> key, |
| 57 | SparseAnnotations... annotations) { |
| 58 | super(annotations); |
| 59 | this.deviceId = deviceId; |
| 60 | this.ifaceName = checkNotNull(ifaceName); |
| 61 | this.type = type; |
| 62 | this.local = local; |
| 63 | this.remote = remote; |
| 64 | this.key = key; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Optional<String> deviceId() { |
| 69 | return deviceId; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String ifaceName() { |
| 74 | return ifaceName; |
| 75 | } |
| 76 | |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 77 | @Override |
| 78 | public Type type() { |
| 79 | return type; |
| 80 | } |
| 81 | |
| 82 | @Override |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 83 | public Optional<TunnelEndPoint> local() { |
| 84 | return local; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Optional<TunnelEndPoint> remote() { |
| 89 | return remote; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Optional<TunnelKey> key() { |
| 94 | return key; |
| 95 | } |
| 96 | |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(this) |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 100 | .add("deviceId", deviceId) |
| 101 | .add("ifaceName", ifaceName) |
| 102 | .add("type", type) |
| 103 | .add("local", local) |
| 104 | .add("remote", remote) |
| 105 | .add("key", key) |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 106 | .toString(); |
| 107 | } |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Creates and returns a new builder instance. |
| 111 | * |
| 112 | * @return default tunnel description builder |
| 113 | */ |
| 114 | public static Builder builder() { |
| 115 | return new Builder(); |
| 116 | } |
| 117 | |
| 118 | public static final class Builder implements TunnelDescription.Builder { |
| 119 | private Optional<String> deviceId = Optional.empty(); |
| 120 | private String ifaceName; |
| 121 | private Type type; |
| 122 | private Optional<TunnelEndPoint> local = Optional.empty(); |
| 123 | private Optional<TunnelEndPoint> remote = Optional.empty(); |
| 124 | private Optional<TunnelKey> key = Optional.empty(); |
| 125 | private Optional<SparseAnnotations> otherConfigs = Optional.empty(); |
| 126 | |
| 127 | private Builder() { |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public TunnelDescription build() { |
| 132 | if (otherConfigs.isPresent()) { |
| 133 | return new DefaultTunnelDescription(deviceId, ifaceName, type, |
| 134 | local, remote, key, |
| 135 | otherConfigs.get()); |
| 136 | } else { |
| 137 | return new DefaultTunnelDescription(deviceId, ifaceName, type, |
| 138 | local, remote, key); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder deviceId(String deviceId) { |
| 144 | this.deviceId = Optional.ofNullable(deviceId); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Builder ifaceName(String ifaceName) { |
| 150 | checkArgument(!Strings.isNullOrEmpty(ifaceName)); |
| 151 | this.ifaceName = ifaceName; |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Builder type(Type type) { |
| 157 | this.type = type; |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Builder local(TunnelEndPoint endpoint) { |
| 163 | local = Optional.ofNullable(endpoint); |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Builder remote(TunnelEndPoint endpoint) { |
| 169 | remote = Optional.ofNullable(endpoint); |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Builder key(TunnelKey key) { |
| 175 | this.key = Optional.ofNullable(key); |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Builder otherConfigs(SparseAnnotations configs) { |
| 181 | otherConfigs = Optional.ofNullable(configs); |
| 182 | return this; |
| 183 | } |
| 184 | } |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 185 | } |