blob: 2df2f8ffccbf5b7075aa2c7d55283c409da06a85 [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.core;
17
Simon Huntc2da4882016-01-21 13:24:47 -080018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
Ray Milkey47c95412017-09-15 10:40:48 -070020
21import org.onosproject.app.ApplicationDescription;
Changhoon Yoonb856b812015-08-10 03:47:19 +090022import org.onosproject.security.Permission;
23
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import java.net.URI;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090025import java.util.Set;
26import java.util.Optional;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080027import java.util.List;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import java.util.Objects;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029
30import static com.google.common.base.MoreObjects.toStringHelper;
31import static com.google.common.base.Preconditions.checkArgument;
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Default implementation of network control/management application descriptor.
36 */
Ray Milkey47c95412017-09-15 10:40:48 -070037public final class DefaultApplication implements Application {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080038
39 private final ApplicationId appId;
40 private final Version version;
Simon Huntafae2f72016-03-04 21:18:23 -080041 private final String title;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042 private final String description;
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;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080047 private final String origin;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090048 private final ApplicationRole role;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 private final Set<Permission> permissions;
50 private final Optional<URI> featuresRepo;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080051 private final List<String> features;
Thomas Vachuska761f0042015-11-11 19:10:17 -080052 private final List<String> requiredApps;
Carmelo Cascone5bb59f22019-07-09 00:05:46 +000053
Thomas Vachuska02aeb032015-01-06 22:36:30 -080054 /**
Ray Milkey47c95412017-09-15 10:40:48 -070055 * Default constructor is hidden to prevent calls to new.
56 */
57 private DefaultApplication() {
Ray Milkey0c484a72017-09-27 17:25:45 -070058 appId = null;
59 version = null;
60 title = null;
61 description = null;
62 category = null;
63 url = null;
64 readme = null;
65 icon = null;
66 origin = null;
67 role = null;
68 permissions = null;
69 featuresRepo = Optional.empty();
70 features = ImmutableList.of();
71 requiredApps = ImmutableList.of();
Ray Milkey47c95412017-09-15 10:40:48 -070072 }
73
74 /**
Thomas Vachuska02aeb032015-01-06 22:36:30 -080075 * Creates a new application descriptor using the supplied data.
76 *
77 * @param appId application identifier
78 * @param version application version
Simon Huntafae2f72016-03-04 21:18:23 -080079 * @param title application title
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 * @param description application description
81 * @param origin origin company
Jian Lic35415d2016-01-14 17:22:31 -080082 * @param category application category
83 * @param url application URL
84 * @param readme application readme
85 * @param icon application icon
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090086 * @param role application role
Thomas Vachuska02aeb032015-01-06 22:36:30 -080087 * @param permissions requested permissions
88 * @param featuresRepo optional features repo URI
89 * @param features application features
Thomas Vachuska761f0042015-11-11 19:10:17 -080090 * @param requiredApps list of required application names
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 */
Carmelo Cascone5bb59f22019-07-09 00:05:46 +000092 private DefaultApplication(ApplicationId appId, Version version, String title,
Jian Lic35415d2016-01-14 17:22:31 -080093 String description, String origin, String category,
94 String url, String readme, byte[] icon,
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090095 ApplicationRole role, Set<Permission> permissions,
Thomas Vachuska761f0042015-11-11 19:10:17 -080096 Optional<URI> featuresRepo, List<String> features,
Carmelo Cascone5bb59f22019-07-09 00:05:46 +000097 List<String> requiredApps) {
Ray Milkey47c95412017-09-15 10:40:48 -070098 this.appId = appId;
99 this.version = version;
100 this.title = title;
101 this.description = description;
102 this.origin = origin;
103 this.category = category;
Jian Li01b0f5952016-01-20 11:02:07 -0800104 this.url = url;
Ray Milkey47c95412017-09-15 10:40:48 -0700105 this.readme = readme;
Simon Huntc2da4882016-01-21 13:24:47 -0800106 this.icon = icon == null ? new byte[0] : icon.clone();
Ray Milkey47c95412017-09-15 10:40:48 -0700107 this.role = role;
108 this.permissions = ImmutableSet.copyOf(permissions);
109 this.featuresRepo = featuresRepo;
110 this.features = ImmutableList.copyOf(features);
111 this.requiredApps = ImmutableList.copyOf(requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800112 }
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000113
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800114 @Override
115 public ApplicationId id() {
116 return appId;
117 }
118
119 @Override
120 public Version version() {
121 return version;
122 }
123
124 @Override
Simon Huntafae2f72016-03-04 21:18:23 -0800125 public String title() {
126 return title;
127 }
128
129 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800130 public String description() {
131 return description;
132 }
133
134 @Override
Jian Lic35415d2016-01-14 17:22:31 -0800135 public String category() {
136 return category;
137 }
138
139 @Override
140 public String url() {
141 return url;
142 }
143
144 @Override
145 public String readme() {
146 return readme;
147 }
148
149 @Override
150 public byte[] icon() {
Simon Huntc2da4882016-01-21 13:24:47 -0800151 return icon.clone();
Jian Lic35415d2016-01-14 17:22:31 -0800152 }
153
154 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800155 public String origin() {
156 return origin;
157 }
158
159 @Override
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900160 public ApplicationRole role() {
161 return role;
162 }
163
164 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 public Set<Permission> permissions() {
166 return permissions;
167 }
168
169 @Override
170 public Optional<URI> featuresRepo() {
171 return featuresRepo;
172 }
173
174 @Override
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800175 public List<String> features() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800176 return features;
177 }
178
179 @Override
Thomas Vachuska761f0042015-11-11 19:10:17 -0800180 public List<String> requiredApps() {
181 return requiredApps;
182 }
183
184 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800185 public int hashCode() {
Simon Huntafae2f72016-03-04 21:18:23 -0800186 return Objects.hash(appId, version, title, description, origin, category, url,
Jian Lic35415d2016-01-14 17:22:31 -0800187 readme, role, permissions, featuresRepo, features, requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800188 }
189
190 @Override
191 public boolean equals(Object obj) {
192 if (this == obj) {
193 return true;
194 }
195 if (obj == null || getClass() != obj.getClass()) {
196 return false;
197 }
198 final DefaultApplication other = (DefaultApplication) obj;
Simon Huntafae2f72016-03-04 21:18:23 -0800199 // TODO: review -- do ALL the fields need to be included?
200 // It is debatable whether fields like description, url, and readme,
201 // need to be included in the notion of equivalence.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202 return Objects.equals(this.appId, other.appId) &&
203 Objects.equals(this.version, other.version) &&
Simon Huntafae2f72016-03-04 21:18:23 -0800204 Objects.equals(this.title, other.title) &&
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800205 Objects.equals(this.description, other.description) &&
206 Objects.equals(this.origin, other.origin) &&
Jian Lic35415d2016-01-14 17:22:31 -0800207 Objects.equals(this.category, other.category) &&
208 Objects.equals(this.url, other.url) &&
209 Objects.equals(this.readme, other.readme) &&
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900210 Objects.equals(this.role, other.role) &&
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800211 Objects.equals(this.permissions, other.permissions) &&
212 Objects.equals(this.featuresRepo, other.featuresRepo) &&
Thomas Vachuska761f0042015-11-11 19:10:17 -0800213 Objects.equals(this.features, other.features) &&
214 Objects.equals(this.requiredApps, other.requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800215 }
216
217 @Override
218 public String toString() {
219 return toStringHelper(this)
220 .add("appId", appId)
221 .add("version", version)
Simon Huntafae2f72016-03-04 21:18:23 -0800222 .add("title", title)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223 .add("description", description)
224 .add("origin", origin)
Jian Lic35415d2016-01-14 17:22:31 -0800225 .add("category", category)
226 .add("url", url)
227 .add("readme", readme)
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900228 .add("role", role)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800229 .add("permissions", permissions)
230 .add("featuresRepo", featuresRepo)
231 .add("features", features)
Thomas Vachuska761f0042015-11-11 19:10:17 -0800232 .add("requiredApps", requiredApps)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 .toString();
234 }
Ray Milkey47c95412017-09-15 10:40:48 -0700235
236 /**
237 * Returns a default application builder.
238 *
239 * @return builder
240 */
241 public static Builder builder() {
242 return new Builder();
243 }
244
245 /**
246 * Creates a new builder as a copy of an existing builder.
247 *
248 * @param builder existing builder to copy
249 * @return new builder
250 */
251 public static Builder builder(Builder builder) {
252 return new Builder(builder);
253 }
254
255 /**
256 * Creates a new builder as a copy of an existing application.
257 *
258 * @param application existing application to copy
259 * @return new builder
260 */
261 public static Builder builder(Application application) {
262 return new Builder(application);
263 }
264
265 /**
266 * Creates a new builder as a copy of an existing application description.
267 *
268 * @param appDesc existing application description
269 * @return new builder
270 */
271 public static Builder builder(ApplicationDescription appDesc) {
272 return new Builder(appDesc);
273 }
274
275
276 /**
277 * Default application builder.
278 */
279 public static final class Builder {
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000280
Ray Milkey47c95412017-09-15 10:40:48 -0700281 private ApplicationId appId;
282 private Version version;
283 private String title;
284 private String description;
285 private String category;
286 private String url;
287 private String readme;
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000288 private byte[] icon;
Ray Milkey47c95412017-09-15 10:40:48 -0700289 private String origin;
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000290 private ApplicationRole role;
291 private Set<Permission> permissions;
292 private Optional<URI> featuresRepo;
293 private List<String> features;
294 private List<String> requiredApps;
Ray Milkey47c95412017-09-15 10:40:48 -0700295
296 /**
297 * Default constructor for the builder.
298 */
299 public Builder() {}
300
301 /**
302 * Updates the builder to be a copy of an existing builder.
303 *
304 * @param builder existing builder to copy
305 */
306 public Builder(Builder builder) {
307 this.appId = builder.appId;
308 this.version = builder.version;
309 this.title = builder.title;
310 this.description = builder.description;
311 this.category = builder.category;
312 this.url = builder.url;
313 this.readme = builder.readme;
314 this.icon = builder.icon;
315 this.origin = builder.origin;
316 this.role = builder.role;
317 this.permissions = builder.permissions;
318 this.featuresRepo = builder.featuresRepo;
319 this.features = builder.features;
320 this.requiredApps = builder.requiredApps;
321 }
322
323 /**
324 * Updates the builder to be a copy of an existing application.
325 *
326 * @param application existing application to copy
327 */
328 public Builder(Application application) {
329 this.appId = application.id();
330 this.version = application.version();
331 this.title = application.title();
332 this.description = application.description();
333 this.category = application.category();
334 this.url = application.url();
335 this.readme = application.readme();
336 this.icon = application.icon();
337 this.origin = application.origin();
338 this.role = application.role();
339 this.permissions = application.permissions();
340 this.featuresRepo = application.featuresRepo();
341 this.features = application.features();
342 this.requiredApps = application.requiredApps();
343 }
344
345 /**
346 * Updates the builder to be a copy of an existing application description.
347 *
348 * @param appDesc existing application description
349 */
350 public Builder(ApplicationDescription appDesc) {
351 this.version = appDesc.version();
352 this.title = appDesc.title();
353 this.description = appDesc.description();
354 this.category = appDesc.category();
355 this.url = appDesc.url();
356 this.readme = appDesc.readme();
357 this.icon = appDesc.icon();
358 this.origin = appDesc.origin();
359 this.role = appDesc.role();
360 this.permissions = appDesc.permissions();
361 this.featuresRepo = appDesc.featuresRepo();
362 this.features = appDesc.features();
363 this.requiredApps = appDesc.requiredApps();
364 }
365
366 /**
367 * Adds an application id.
368 *
369 * @param appId application id
370 * @return builder
371 */
372 public Builder withAppId(ApplicationId appId) {
373 this.appId = appId;
374 return this;
375 }
376
377 /**
378 * Adds a version string.
379 *
380 * @param version version string
381 * @return builder
382 */
383 public Builder withVersion(Version version) {
384 this.version = version;
385 return this;
386 }
387
388 /**
389 * Adds a title string.
390 *
391 * @param title title string
392 * @return builder
393 */
394 public Builder withTitle(String title) {
395 this.title = title;
396 return this;
397 }
398
399 /**
400 * Adds a description string.
401 *
402 * @param description description string
403 * @return builder
404 */
405 public Builder withDescription(String description) {
406 this.description = description;
407 return this;
408 }
409
410 /**
411 * Adds a category string.
412 *
413 * @param category category string
414 * @return builder
415 */
416 public Builder withCategory(String category) {
417 this.category = category;
418 return this;
419 }
420
421 /**
422 * Adds a URL string.
423 *
424 * @param url url string
425 * @return builder
426 */
427 public Builder withUrl(String url) {
428 this.url = url;
429 return this;
430 }
431
432 /**
433 * Adds a readme string.
434 *
435 * @param readme readme string
436 * @return builder
437 */
438 public Builder withReadme(String readme) {
439 this.readme = readme;
440 return this;
441 }
442
443 /**
444 * Adds an icon.
445 *
446 * @param icon icon data
447 * @return builder
448 */
449 public Builder withIcon(byte[] icon) {
450 this.icon = icon;
451 return this;
452 }
453
454 /**
455 * Adds an origin string.
456 *
457 * @param origin origin string
458 * @return builder
459 */
460 public Builder withOrigin(String origin) {
461 this.origin = origin;
462 return this;
463 }
464
465 /**
466 * Adds an application role.
467 *
468 * @param role application role
469 * @return builder
470 */
471 public Builder withRole(ApplicationRole role) {
472 this.role = role;
473 return this;
474 }
475
476 /**
477 * Adds a permissions set.
478 *
479 * @param permissions permissions set
480 * @return builder
481 */
482 public Builder withPermissions(Set<Permission> permissions) {
483 this.permissions = permissions;
484 return this;
485 }
486
487 /**
488 * Adds a URI for a features repository.
489 *
490 * @param featuresRepo Optional URI for a features repository
491 * @return builder
492 */
493 public Builder withFeaturesRepo(Optional<URI> featuresRepo) {
494 this.featuresRepo = featuresRepo;
495 return this;
496 }
497
498 /**
499 * Adds a features list.
500 *
501 * @param features features list
502 * @return builder
503 */
504 public Builder withFeatures(List<String> features) {
505 this.features = features;
506 return this;
507 }
508
509 /**
510 * Adds a list of required applications.
511 *
512 * @param requiredApps List of name strings of required applications
513 * @return builder
514 */
515 public Builder withRequiredApps(List<String> requiredApps) {
516 this.requiredApps = requiredApps;
517 return this;
518 }
519
520 /**
521 * Builds a default application object from the gathered parameters.
522 *
523 * @return new default application
524 */
525 public DefaultApplication build() {
526 checkNotNull(appId, "ID cannot be null");
527 checkNotNull(version, "Version cannot be null");
528 checkNotNull(title, "Title cannot be null");
529 checkNotNull(description, "Description cannot be null");
530 checkNotNull(origin, "Origin cannot be null");
531 checkNotNull(category, "Category cannot be null");
532 checkNotNull(readme, "Readme cannot be null");
533 checkNotNull(role, "Role cannot be null");
534 checkNotNull(permissions, "Permissions cannot be null");
535 checkNotNull(featuresRepo, "Features repo cannot be null");
536 checkNotNull(features, "Features cannot be null");
537 checkNotNull(requiredApps, "Required apps cannot be null");
538 checkArgument(!features.isEmpty(), "There must be at least one feature");
539
540 return new DefaultApplication(appId, version, title,
541 description, origin, category,
542 url, readme, icon,
543 role, permissions,
544 featuresRepo, features,
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000545 requiredApps);
Ray Milkey47c95412017-09-15 10:40:48 -0700546 }
547 }
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000548}