Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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.core; |
| 17 | |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 20 | import org.onosproject.security.Permission; |
| 21 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 22 | import java.net.URI; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | import java.util.Optional; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 25 | import java.util.List; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 26 | import java.util.Objects; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Default implementation of network control/management application descriptor. |
| 34 | */ |
| 35 | public class DefaultApplication implements Application { |
| 36 | |
| 37 | private final ApplicationId appId; |
| 38 | private final Version version; |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 39 | private final String title; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | private final String description; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 41 | private final String category; |
| 42 | private final String url; |
| 43 | private final String readme; |
| 44 | private final byte[] icon; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 45 | private final String origin; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 46 | private final ApplicationRole role; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 47 | private final Set<Permission> permissions; |
| 48 | private final Optional<URI> featuresRepo; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 49 | private final List<String> features; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 50 | private final List<String> requiredApps; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Creates a new application descriptor using the supplied data. |
| 54 | * |
| 55 | * @param appId application identifier |
| 56 | * @param version application version |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 57 | * @param title application title |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 58 | * @param description application description |
| 59 | * @param origin origin company |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 60 | * @param category application category |
| 61 | * @param url application URL |
| 62 | * @param readme application readme |
| 63 | * @param icon application icon |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 64 | * @param role application role |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 65 | * @param permissions requested permissions |
| 66 | * @param featuresRepo optional features repo URI |
| 67 | * @param features application features |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 68 | * @param requiredApps list of required application names |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | */ |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 70 | public DefaultApplication(ApplicationId appId, Version version, String title, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 71 | String description, String origin, String category, |
| 72 | String url, String readme, byte[] icon, |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 73 | ApplicationRole role, Set<Permission> permissions, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 74 | Optional<URI> featuresRepo, List<String> features, |
| 75 | List<String> requiredApps) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 76 | this.appId = checkNotNull(appId, "ID cannot be null"); |
| 77 | this.version = checkNotNull(version, "Version cannot be null"); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 78 | this.title = checkNotNull(title, "Title cannot be null"); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 79 | this.description = checkNotNull(description, "Description cannot be null"); |
| 80 | this.origin = checkNotNull(origin, "Origin cannot be null"); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 81 | this.category = checkNotNull(category, "Category cannot be null"); |
Jian Li | 01b0f595 | 2016-01-20 11:02:07 -0800 | [diff] [blame] | 82 | this.url = url; |
Jian Li | 8bcb4f2 | 2016-01-20 10:36:18 -0800 | [diff] [blame] | 83 | this.readme = checkNotNull(readme, "Readme cannot be null"); |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 84 | this.icon = icon == null ? new byte[0] : icon.clone(); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 85 | this.role = checkNotNull(role, "Role cannot be null"); |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 86 | this.permissions = ImmutableSet.copyOf( |
| 87 | checkNotNull(permissions, "Permissions cannot be null") |
| 88 | ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 89 | this.featuresRepo = checkNotNull(featuresRepo, "Features repo cannot be null"); |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 90 | this.features = ImmutableList.copyOf( |
| 91 | checkNotNull(features, "Features cannot be null") |
| 92 | ); |
| 93 | this.requiredApps = ImmutableList.copyOf( |
| 94 | checkNotNull(requiredApps, "Required apps cannot be null") |
| 95 | ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | checkArgument(!features.isEmpty(), "There must be at least one feature"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public ApplicationId id() { |
| 101 | return appId; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Version version() { |
| 106 | return version; |
| 107 | } |
| 108 | |
| 109 | @Override |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 110 | public String title() { |
| 111 | return title; |
| 112 | } |
| 113 | |
| 114 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 115 | public String description() { |
| 116 | return description; |
| 117 | } |
| 118 | |
| 119 | @Override |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 120 | public String category() { |
| 121 | return category; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String url() { |
| 126 | return url; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String readme() { |
| 131 | return readme; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public byte[] icon() { |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 136 | return icon.clone(); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 140 | public String origin() { |
| 141 | return origin; |
| 142 | } |
| 143 | |
| 144 | @Override |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 145 | public ApplicationRole role() { |
| 146 | return role; |
| 147 | } |
| 148 | |
| 149 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 150 | public Set<Permission> permissions() { |
| 151 | return permissions; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Optional<URI> featuresRepo() { |
| 156 | return featuresRepo; |
| 157 | } |
| 158 | |
| 159 | @Override |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 160 | public List<String> features() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 161 | return features; |
| 162 | } |
| 163 | |
| 164 | @Override |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 165 | public List<String> requiredApps() { |
| 166 | return requiredApps; |
| 167 | } |
| 168 | |
| 169 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 170 | public int hashCode() { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 171 | return Objects.hash(appId, version, title, description, origin, category, url, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 172 | readme, role, permissions, featuresRepo, features, requiredApps); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean equals(Object obj) { |
| 177 | if (this == obj) { |
| 178 | return true; |
| 179 | } |
| 180 | if (obj == null || getClass() != obj.getClass()) { |
| 181 | return false; |
| 182 | } |
| 183 | final DefaultApplication other = (DefaultApplication) obj; |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 184 | // TODO: review -- do ALL the fields need to be included? |
| 185 | // It is debatable whether fields like description, url, and readme, |
| 186 | // need to be included in the notion of equivalence. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 187 | return Objects.equals(this.appId, other.appId) && |
| 188 | Objects.equals(this.version, other.version) && |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 189 | Objects.equals(this.title, other.title) && |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 190 | Objects.equals(this.description, other.description) && |
| 191 | Objects.equals(this.origin, other.origin) && |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 192 | Objects.equals(this.category, other.category) && |
| 193 | Objects.equals(this.url, other.url) && |
| 194 | Objects.equals(this.readme, other.readme) && |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 195 | Objects.equals(this.role, other.role) && |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 196 | Objects.equals(this.permissions, other.permissions) && |
| 197 | Objects.equals(this.featuresRepo, other.featuresRepo) && |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 198 | Objects.equals(this.features, other.features) && |
| 199 | Objects.equals(this.requiredApps, other.requiredApps); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public String toString() { |
| 204 | return toStringHelper(this) |
| 205 | .add("appId", appId) |
| 206 | .add("version", version) |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 207 | .add("title", title) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 208 | .add("description", description) |
| 209 | .add("origin", origin) |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 210 | .add("category", category) |
| 211 | .add("url", url) |
| 212 | .add("readme", readme) |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 213 | .add("role", role) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 214 | .add("permissions", permissions) |
| 215 | .add("featuresRepo", featuresRepo) |
| 216 | .add("features", features) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 217 | .add("requiredApps", requiredApps) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 218 | .toString(); |
| 219 | } |
| 220 | } |