blob: 0b39d9ce4beaa3c2ecdf17ae0cdb288159b8ce1a [file] [log] [blame]
samuel8d6b0a92015-07-11 13:22:57 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 *
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070045 * @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
samuel8d6b0a92015-07-11 13:22:57 +080077 @Override
78 public Type type() {
79 return type;
80 }
81
82 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070083 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
samuel8d6b0a92015-07-11 13:22:57 +080097 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(this)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700100 .add("deviceId", deviceId)
101 .add("ifaceName", ifaceName)
102 .add("type", type)
103 .add("local", local)
104 .add("remote", remote)
105 .add("key", key)
samuel8d6b0a92015-07-11 13:22:57 +0800106 .toString();
107 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700108
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 }
samuel8d6b0a92015-07-11 13:22:57 +0800185}