blob: 9e94cbaff5f43d3cedb53ad56b688805c1b7b92d [file] [log] [blame]
samuel8d6b0a92015-07-11 13:22:57 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
samuel8d6b0a92015-07-11 13:22:57 +08003 *
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 */
16package org.onosproject.net.behaviour;
17
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070018import com.google.common.base.Strings;
samuel8d6b0a92015-07-11 13:22:57 +080019import org.onosproject.net.AbstractDescription;
20import org.onosproject.net.SparseAnnotations;
21
samuel8d6b0a92015-07-11 13:22:57 +080022import com.google.common.base.MoreObjects;
23
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070024import java.util.Optional;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
samuel8d6b0a92015-07-11 13:22:57 +080029/**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070030 * Default implementation of immutable tunnel interface description entity.
samuel8d6b0a92015-07-11 13:22:57 +080031 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070032public final class DefaultTunnelDescription extends AbstractDescription
samuel8d6b0a92015-07-11 13:22:57 +080033 implements TunnelDescription {
34
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070035 private final Optional<String> deviceId;
36 private final String ifaceName;
samuel8d6b0a92015-07-11 13:22:57 +080037 private final Type type;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070038 private final Optional<TunnelEndPoint> local;
39 private final Optional<TunnelEndPoint> remote;
40 private final Optional<TunnelKey> key;
samuel8d6b0a92015-07-11 13:22:57 +080041
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 Moondd14e8e2016-06-09 16:17:32 -070050 * @deprecated version 1.7.0 - Hummingbird
samuel8d6b0a92015-07-11 13:22:57 +080051 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070052 @Deprecated
samuel8d6b0a92015-07-11 13:22:57 +080053 public DefaultTunnelDescription(TunnelEndPoint src,
54 TunnelEndPoint dst, Type type,
55 TunnelName tunnelName,
56 SparseAnnotations... annotations) {
57 super(annotations);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070058 this.deviceId = Optional.empty();
59 this.local = Optional.ofNullable(src);
60 this.remote = Optional.ofNullable(dst);
samuel8d6b0a92015-07-11 13:22:57 +080061 this.type = type;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070062 this.ifaceName = tunnelName.value();
63 this.key = Optional.empty();
samuel8d6b0a92015-07-11 13:22:57 +080064 }
65
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070066 /**
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
samuel8d6b0a92015-07-11 13:22:57 +0800102 @Override
103 public TunnelEndPoint src() {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700104 return local.isPresent() ? local.get() : null;
samuel8d6b0a92015-07-11 13:22:57 +0800105 }
106
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700107 @Deprecated
samuel8d6b0a92015-07-11 13:22:57 +0800108 @Override
109 public TunnelEndPoint dst() {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700110 return remote.isPresent() ? remote.get() : null;
samuel8d6b0a92015-07-11 13:22:57 +0800111 }
112
113 @Override
114 public Type type() {
115 return type;
116 }
117
118 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700119 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
samuel8d6b0a92015-07-11 13:22:57 +0800135 public TunnelName tunnelName() {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700136 return TunnelName.tunnelName(ifaceName);
samuel8d6b0a92015-07-11 13:22:57 +0800137 }
138
139 @Override
140 public String toString() {
141 return MoreObjects.toStringHelper(this)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700142 .add("deviceId", deviceId)
143 .add("ifaceName", ifaceName)
144 .add("type", type)
145 .add("local", local)
146 .add("remote", remote)
147 .add("key", key)
samuel8d6b0a92015-07-11 13:22:57 +0800148 .toString();
149 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700150
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 }
samuel8d6b0a92015-07-11 13:22:57 +0800227}