Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -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.app; |
| 17 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 18 | import java.net.URI; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 19 | import java.util.List; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 20 | import java.util.Optional; |
| 21 | import java.util.Set; |
| 22 | |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationRole; |
| 24 | import org.onosproject.core.Version; |
| 25 | import org.onosproject.security.Permission; |
| 26 | |
| 27 | import com.google.common.collect.ImmutableList; |
| 28 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Default implementation of network control/management application descriptor. |
| 35 | */ |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 36 | public final class DefaultApplicationDescription implements ApplicationDescription { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 37 | |
| 38 | private final String name; |
| 39 | private final Version version; |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 40 | private final String title; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 41 | private final String description; |
| 42 | private final String origin; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 43 | private final String category; |
| 44 | private final String url; |
| 45 | private final String readme; |
| 46 | private final byte[] icon; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 47 | private final ApplicationRole role; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 48 | private final Set<Permission> permissions; |
| 49 | private final Optional<URI> featuresRepo; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 50 | private final List<String> features; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 51 | private final List<String> requiredApps; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 54 | * 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 62 | * Creates a new application descriptor using the supplied data. |
| 63 | * |
| 64 | * @param name application name |
| 65 | * @param version application version |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 66 | * @param title application title |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 67 | * @param description application description |
| 68 | * @param origin origin company |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 69 | * @param category application category |
| 70 | * @param url application URL |
| 71 | * @param readme application readme |
| 72 | * @param icon application icon |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 73 | * @param role application role |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 74 | * @param permissions requested permissions |
| 75 | * @param featuresRepo optional features repo URI |
| 76 | * @param features application features |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 77 | * @param requiredApps list of required application names |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | */ |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 79 | 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 Li | 01b0f595 | 2016-01-20 11:02:07 -0800 | [diff] [blame] | 91 | this.url = url; |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 92 | this.readme = readme; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 93 | this.icon = icon; |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 94 | this.role = role; |
| 95 | this.permissions = permissions; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | this.featuresRepo = Optional.ofNullable(featuresRepo); |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 97 | this.features = ImmutableList.copyOf(features); |
| 98 | this.requiredApps = ImmutableList.copyOf(requiredApps); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 99 | } |
| 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 Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 112 | public String title() { |
| 113 | return title; |
| 114 | } |
| 115 | |
| 116 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 117 | public String description() { |
| 118 | return description; |
| 119 | } |
| 120 | |
| 121 | @Override |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 122 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 142 | public String origin() { |
| 143 | return origin; |
| 144 | } |
| 145 | |
| 146 | @Override |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 147 | public ApplicationRole role() { |
| 148 | return role; |
| 149 | } |
| 150 | |
| 151 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 152 | public Set<Permission> permissions() { |
| 153 | return permissions; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Optional<URI> featuresRepo() { |
| 158 | return featuresRepo; |
| 159 | } |
| 160 | |
| 161 | @Override |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 162 | public List<String> features() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 163 | return features; |
| 164 | } |
| 165 | |
| 166 | @Override |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 167 | public List<String> requiredApps() { |
| 168 | return requiredApps; |
| 169 | } |
| 170 | |
| 171 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 172 | public String toString() { |
| 173 | return toStringHelper(this) |
| 174 | .add("name", name) |
| 175 | .add("version", version) |
| 176 | .add("description", description) |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 177 | .add("title", title) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 178 | .add("origin", origin) |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 179 | .add("category", category) |
| 180 | .add("url", url) |
| 181 | .add("readme", readme) |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 182 | .add("role", role) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 183 | .add("permissions", permissions) |
| 184 | .add("featuresRepo", featuresRepo) |
| 185 | .add("features", features) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 186 | .add("requiredApps", requiredApps) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 187 | .toString(); |
| 188 | } |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 189 | |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 405 | } |