blob: e5feae1824cffdd0cd13423454e5ae31eb725ee5 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Changhoon Yoonb856b812015-08-10 03:47:19 +090020import org.onosproject.security.Permission;
21
Thomas Vachuska02aeb032015-01-06 22:36:30 -080022import java.net.URI;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090023import java.util.Set;
24import java.util.Optional;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080025import java.util.List;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080026import java.util.Objects;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080027
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Default implementation of network control/management application descriptor.
34 */
35public class DefaultApplication implements Application {
36
37 private final ApplicationId appId;
38 private final Version version;
Simon Huntafae2f72016-03-04 21:18:23 -080039 private final String title;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040 private final String description;
Jian Lic35415d2016-01-14 17:22:31 -080041 private final String category;
42 private final String url;
43 private final String readme;
44 private final byte[] icon;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045 private final String origin;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090046 private final ApplicationRole role;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080047 private final Set<Permission> permissions;
48 private final Optional<URI> featuresRepo;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080049 private final List<String> features;
Thomas Vachuska761f0042015-11-11 19:10:17 -080050 private final List<String> requiredApps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051
52 /**
53 * Creates a new application descriptor using the supplied data.
54 *
55 * @param appId application identifier
56 * @param version application version
Simon Huntafae2f72016-03-04 21:18:23 -080057 * @param title application title
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058 * @param description application description
59 * @param origin origin company
Jian Lic35415d2016-01-14 17:22:31 -080060 * @param category application category
61 * @param url application URL
62 * @param readme application readme
63 * @param icon application icon
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090064 * @param role application role
Thomas Vachuska02aeb032015-01-06 22:36:30 -080065 * @param permissions requested permissions
66 * @param featuresRepo optional features repo URI
67 * @param features application features
Thomas Vachuska761f0042015-11-11 19:10:17 -080068 * @param requiredApps list of required application names
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 */
Simon Huntafae2f72016-03-04 21:18:23 -080070 public DefaultApplication(ApplicationId appId, Version version, String title,
Jian Lic35415d2016-01-14 17:22:31 -080071 String description, String origin, String category,
72 String url, String readme, byte[] icon,
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090073 ApplicationRole role, Set<Permission> permissions,
Thomas Vachuska761f0042015-11-11 19:10:17 -080074 Optional<URI> featuresRepo, List<String> features,
75 List<String> requiredApps) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080076 this.appId = checkNotNull(appId, "ID cannot be null");
77 this.version = checkNotNull(version, "Version cannot be null");
Simon Huntafae2f72016-03-04 21:18:23 -080078 this.title = checkNotNull(title, "Title cannot be null");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 this.description = checkNotNull(description, "Description cannot be null");
80 this.origin = checkNotNull(origin, "Origin cannot be null");
Jian Lic35415d2016-01-14 17:22:31 -080081 this.category = checkNotNull(category, "Category cannot be null");
Jian Li01b0f5952016-01-20 11:02:07 -080082 this.url = url;
Jian Li8bcb4f22016-01-20 10:36:18 -080083 this.readme = checkNotNull(readme, "Readme cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080084 this.icon = icon == null ? new byte[0] : icon.clone();
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090085 this.role = checkNotNull(role, "Role cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080086 this.permissions = ImmutableSet.copyOf(
87 checkNotNull(permissions, "Permissions cannot be null")
88 );
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 this.featuresRepo = checkNotNull(featuresRepo, "Features repo cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080090 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 Vachuska02aeb032015-01-06 22:36:30 -080096 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 Huntafae2f72016-03-04 21:18:23 -0800110 public String title() {
111 return title;
112 }
113
114 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800115 public String description() {
116 return description;
117 }
118
119 @Override
Jian Lic35415d2016-01-14 17:22:31 -0800120 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 Huntc2da4882016-01-21 13:24:47 -0800136 return icon.clone();
Jian Lic35415d2016-01-14 17:22:31 -0800137 }
138
139 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800140 public String origin() {
141 return origin;
142 }
143
144 @Override
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900145 public ApplicationRole role() {
146 return role;
147 }
148
149 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800150 public Set<Permission> permissions() {
151 return permissions;
152 }
153
154 @Override
155 public Optional<URI> featuresRepo() {
156 return featuresRepo;
157 }
158
159 @Override
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800160 public List<String> features() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800161 return features;
162 }
163
164 @Override
Thomas Vachuska761f0042015-11-11 19:10:17 -0800165 public List<String> requiredApps() {
166 return requiredApps;
167 }
168
169 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800170 public int hashCode() {
Simon Huntafae2f72016-03-04 21:18:23 -0800171 return Objects.hash(appId, version, title, description, origin, category, url,
Jian Lic35415d2016-01-14 17:22:31 -0800172 readme, role, permissions, featuresRepo, features, requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800173 }
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 Huntafae2f72016-03-04 21:18:23 -0800184 // 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 Vachuska02aeb032015-01-06 22:36:30 -0800187 return Objects.equals(this.appId, other.appId) &&
188 Objects.equals(this.version, other.version) &&
Simon Huntafae2f72016-03-04 21:18:23 -0800189 Objects.equals(this.title, other.title) &&
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800190 Objects.equals(this.description, other.description) &&
191 Objects.equals(this.origin, other.origin) &&
Jian Lic35415d2016-01-14 17:22:31 -0800192 Objects.equals(this.category, other.category) &&
193 Objects.equals(this.url, other.url) &&
194 Objects.equals(this.readme, other.readme) &&
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900195 Objects.equals(this.role, other.role) &&
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800196 Objects.equals(this.permissions, other.permissions) &&
197 Objects.equals(this.featuresRepo, other.featuresRepo) &&
Thomas Vachuska761f0042015-11-11 19:10:17 -0800198 Objects.equals(this.features, other.features) &&
199 Objects.equals(this.requiredApps, other.requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800200 }
201
202 @Override
203 public String toString() {
204 return toStringHelper(this)
205 .add("appId", appId)
206 .add("version", version)
Simon Huntafae2f72016-03-04 21:18:23 -0800207 .add("title", title)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800208 .add("description", description)
209 .add("origin", origin)
Jian Lic35415d2016-01-14 17:22:31 -0800210 .add("category", category)
211 .add("url", url)
212 .add("readme", readme)
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900213 .add("role", role)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800214 .add("permissions", permissions)
215 .add("featuresRepo", featuresRepo)
216 .add("features", features)
Thomas Vachuska761f0042015-11-11 19:10:17 -0800217 .add("requiredApps", requiredApps)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800218 .toString();
219 }
220}