blob: 38627185b17aaad8be148a19733ec221a2fbe1d5 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska02aeb032015-01-06 22:36:30 -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.app;
17
Thomas Vachuska02aeb032015-01-06 22:36:30 -080018import java.net.URI;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080019import java.util.List;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080020import java.util.Optional;
21import java.util.Set;
22
Ray Milkey47c95412017-09-15 10:40:48 -070023import org.onosproject.core.ApplicationRole;
24import org.onosproject.core.Version;
25import org.onosproject.security.Permission;
26
27import com.google.common.collect.ImmutableList;
28
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import static com.google.common.base.MoreObjects.toStringHelper;
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Default implementation of network control/management application descriptor.
35 */
Ray Milkey47c95412017-09-15 10:40:48 -070036public final class DefaultApplicationDescription implements ApplicationDescription {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037
38 private final String name;
39 private final Version version;
Simon Huntafae2f72016-03-04 21:18:23 -080040 private final String title;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080041 private final String description;
42 private final String origin;
Jian Lic35415d2016-01-14 17:22:31 -080043 private final String category;
44 private final String url;
45 private final String readme;
46 private final byte[] icon;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090047 private final ApplicationRole role;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080048 private final Set<Permission> permissions;
49 private final Optional<URI> featuresRepo;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080050 private final List<String> features;
Thomas Vachuska761f0042015-11-11 19:10:17 -080051 private final List<String> requiredApps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052
53 /**
Ray Milkey47c95412017-09-15 10:40:48 -070054 * Default constructor is hidden to prevent calls to new.
55 */
56 private DefaultApplicationDescription() {
57 // Should not happen
58 throw new UnsupportedOperationException();
59 }
60
61 /**
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062 * Creates a new application descriptor using the supplied data.
63 *
64 * @param name application name
65 * @param version application version
Simon Huntafae2f72016-03-04 21:18:23 -080066 * @param title application title
Thomas Vachuska02aeb032015-01-06 22:36:30 -080067 * @param description application description
68 * @param origin origin company
Jian Lic35415d2016-01-14 17:22:31 -080069 * @param category application category
70 * @param url application URL
71 * @param readme application readme
72 * @param icon application icon
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090073 * @param role application role
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 * @param permissions requested permissions
75 * @param featuresRepo optional features repo URI
76 * @param features application features
Thomas Vachuska761f0042015-11-11 19:10:17 -080077 * @param requiredApps list of required application names
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 */
Ray Milkey47c95412017-09-15 10:40:48 -070079 private DefaultApplicationDescription(String name, Version version, String title,
80 String description, String origin, String category,
81 String url, String readme, byte[] icon,
82 ApplicationRole role, Set<Permission> permissions,
83 URI featuresRepo, List<String> features,
84 List<String> requiredApps) {
85 this.name = name;
86 this.version = version;
87 this.title = title;
88 this.description = description;
89 this.origin = origin;
90 this.category = category;
Jian Li01b0f5952016-01-20 11:02:07 -080091 this.url = url;
Ray Milkey47c95412017-09-15 10:40:48 -070092 this.readme = readme;
Jian Lic35415d2016-01-14 17:22:31 -080093 this.icon = icon;
Ray Milkey47c95412017-09-15 10:40:48 -070094 this.role = role;
95 this.permissions = permissions;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 this.featuresRepo = Optional.ofNullable(featuresRepo);
Ray Milkey47c95412017-09-15 10:40:48 -070097 this.features = ImmutableList.copyOf(features);
98 this.requiredApps = ImmutableList.copyOf(requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 }
100
101 @Override
102 public String name() {
103 return name;
104 }
105
106 @Override
107 public Version version() {
108 return version;
109 }
110
111 @Override
Simon Huntafae2f72016-03-04 21:18:23 -0800112 public String title() {
113 return title;
114 }
115
116 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800117 public String description() {
118 return description;
119 }
120
121 @Override
Jian Lic35415d2016-01-14 17:22:31 -0800122 public String category() {
123 return category;
124 }
125
126 @Override
127 public String url() {
128 return url;
129 }
130
131 @Override
132 public String readme() {
133 return readme;
134 }
135
136 @Override
137 public byte[] icon() {
138 return icon;
139 }
140
141 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 public String origin() {
143 return origin;
144 }
145
146 @Override
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900147 public ApplicationRole role() {
148 return role;
149 }
150
151 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800152 public Set<Permission> permissions() {
153 return permissions;
154 }
155
156 @Override
157 public Optional<URI> featuresRepo() {
158 return featuresRepo;
159 }
160
161 @Override
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800162 public List<String> features() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163 return features;
164 }
165
166 @Override
Thomas Vachuska761f0042015-11-11 19:10:17 -0800167 public List<String> requiredApps() {
168 return requiredApps;
169 }
170
171 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 public String toString() {
173 return toStringHelper(this)
174 .add("name", name)
175 .add("version", version)
176 .add("description", description)
Simon Huntafae2f72016-03-04 21:18:23 -0800177 .add("title", title)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800178 .add("origin", origin)
Jian Lic35415d2016-01-14 17:22:31 -0800179 .add("category", category)
180 .add("url", url)
181 .add("readme", readme)
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900182 .add("role", role)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800183 .add("permissions", permissions)
184 .add("featuresRepo", featuresRepo)
185 .add("features", features)
Thomas Vachuska761f0042015-11-11 19:10:17 -0800186 .add("requiredApps", requiredApps)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800187 .toString();
188 }
Ray Milkey47c95412017-09-15 10:40:48 -0700189
190 /**
191 * Returns a default application description builder.
192 *
193 * @return builder
194 */
195 public static Builder builder() {
196 return new Builder();
197 }
198
199 /**
200 * Default application description builder.
201 */
202 public static final class Builder {
203
204 private String name;
205 private Version version;
206 private String title;
207 private String description;
208 private String category;
209 private String url;
210 private String readme;
211 private byte[] icon;
212 private String origin;
213 private ApplicationRole role;
214 private Set<Permission> permissions;
215 private URI featuresRepo;
216 private List<String> features;
217 private List<String> requiredApps;
218
219 /**
220 * Default constructor for the builder.
221 */
222 public Builder() {}
223
224 /**
225 * Adds an application id.
226 *
227 * @param name application name
228 * @return builder
229 */
230 public Builder withName(String name) {
231 this.name = name;
232 return this;
233 }
234
235 /**
236 * Adds a version string.
237 *
238 * @param version version string
239 * @return builder
240 */
241 public Builder withVersion(Version version) {
242 this.version = version;
243 return this;
244 }
245
246 /**
247 * Adds a title string.
248 *
249 * @param title title string
250 * @return builder
251 */
252 public Builder withTitle(String title) {
253 this.title = title;
254 return this;
255 }
256
257 /**
258 * Adds a description string.
259 *
260 * @param description description string
261 * @return builder
262 */
263 public Builder withDescription(String description) {
264 this.description = description;
265 return this;
266 }
267
268 /**
269 * Adds a category string.
270 *
271 * @param category category string
272 * @return builder
273 */
274 public Builder withCategory(String category) {
275 this.category = category;
276 return this;
277 }
278
279 /**
280 * Adds a URL string.
281 *
282 * @param url url string
283 * @return builder
284 */
285 public Builder withUrl(String url) {
286 this.url = url;
287 return this;
288 }
289
290 /**
291 * Adds a readme string.
292 *
293 * @param readme readme string
294 * @return builder
295 */
296 public Builder withReadme(String readme) {
297 this.readme = readme;
298 return this;
299 }
300
301 /**
302 * Adds an icon.
303 *
304 * @param icon icon data
305 * @return builder
306 */
307 public Builder withIcon(byte[] icon) {
308 this.icon = icon;
309 return this;
310 }
311
312 /**
313 * Adds an origin string.
314 *
315 * @param origin origin string
316 * @return builder
317 */
318 public Builder withOrigin(String origin) {
319 this.origin = origin;
320 return this;
321 }
322
323 /**
324 * Adds an application role.
325 *
326 * @param role application role
327 * @return builder
328 */
329 public Builder withRole(ApplicationRole role) {
330 this.role = role;
331 return this;
332 }
333
334 /**
335 * Adds a permissions set.
336 *
337 * @param permissions permissions set
338 * @return builder
339 */
340 public Builder withPermissions(Set<Permission> permissions) {
341 this.permissions = permissions;
342 return this;
343 }
344
345 /**
346 * Adds a URI for a features repository.
347 *
348 * @param featuresRepo Optional URI for a features repository
349 * @return builder
350 */
351 public Builder withFeaturesRepo(URI featuresRepo) {
352 this.featuresRepo = featuresRepo;
353 return this;
354 }
355
356 /**
357 * Adds a features list.
358 *
359 * @param features features list
360 * @return builder
361 */
362 public Builder withFeatures(List<String> features) {
363 this.features = features;
364 return this;
365 }
366
367 /**
368 * Adds a list of required applications.
369 *
370 * @param requiredApps List of name strings of required applications
371 * @return builder
372 */
373 public Builder withRequiredApps(List<String> requiredApps) {
374 this.requiredApps = requiredApps;
375 return this;
376 }
377
378 /**
379 * Builds a default application object from the gathered parameters.
380 *
381 * @return new default application
382 */
383 public DefaultApplicationDescription build() {
384 checkNotNull(name, "Name cannot be null");
385 checkNotNull(version, "Version cannot be null");
386 checkNotNull(title, "Title cannot be null");
387 checkNotNull(description, "Description cannot be null");
388 checkNotNull(origin, "Origin cannot be null");
389 checkNotNull(category, "Category cannot be null");
390 checkNotNull(readme, "Readme cannot be null");
391 checkNotNull(role, "Role cannot be null");
392 checkNotNull(permissions, "Permissions cannot be null");
393 checkNotNull(features, "Features cannot be null");
394 checkNotNull(requiredApps, "Required apps cannot be null");
395 checkArgument(!features.isEmpty(), "There must be at least one feature");
396
397 return new DefaultApplicationDescription(name, version, title,
398 description, origin, category,
399 url, readme, icon,
400 role, permissions,
401 featuresRepo, features,
402 requiredApps);
403 }
404 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800405}