blob: 7737ef84e72970b8ce9e038ee97b0fee0a81df7e [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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;
39 private final String description;
Jian Lic35415d2016-01-14 17:22:31 -080040 private final String category;
41 private final String url;
42 private final String readme;
43 private final byte[] icon;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080044 private final String origin;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090045 private final ApplicationRole role;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046 private final Set<Permission> permissions;
47 private final Optional<URI> featuresRepo;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080048 private final List<String> features;
Thomas Vachuska761f0042015-11-11 19:10:17 -080049 private final List<String> requiredApps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050
51 /**
52 * Creates a new application descriptor using the supplied data.
53 *
54 * @param appId application identifier
55 * @param version application version
56 * @param description application description
57 * @param origin origin company
Jian Lic35415d2016-01-14 17:22:31 -080058 * @param category application category
59 * @param url application URL
60 * @param readme application readme
61 * @param icon application icon
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090062 * @param role application role
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063 * @param permissions requested permissions
64 * @param featuresRepo optional features repo URI
65 * @param features application features
Thomas Vachuska761f0042015-11-11 19:10:17 -080066 * @param requiredApps list of required application names
Thomas Vachuska02aeb032015-01-06 22:36:30 -080067 */
68 public DefaultApplication(ApplicationId appId, Version version,
Jian Lic35415d2016-01-14 17:22:31 -080069 String description, String origin, String category,
70 String url, String readme, byte[] icon,
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090071 ApplicationRole role, Set<Permission> permissions,
Thomas Vachuska761f0042015-11-11 19:10:17 -080072 Optional<URI> featuresRepo, List<String> features,
73 List<String> requiredApps) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 this.appId = checkNotNull(appId, "ID cannot be null");
75 this.version = checkNotNull(version, "Version cannot be null");
76 this.description = checkNotNull(description, "Description cannot be null");
77 this.origin = checkNotNull(origin, "Origin cannot be null");
Jian Lic35415d2016-01-14 17:22:31 -080078 this.category = checkNotNull(category, "Category cannot be null");
Jian Li01b0f5952016-01-20 11:02:07 -080079 this.url = url;
Jian Li8bcb4f22016-01-20 10:36:18 -080080 this.readme = checkNotNull(readme, "Readme cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080081 this.icon = icon == null ? new byte[0] : icon.clone();
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090082 this.role = checkNotNull(role, "Role cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080083 this.permissions = ImmutableSet.copyOf(
84 checkNotNull(permissions, "Permissions cannot be null")
85 );
Thomas Vachuska02aeb032015-01-06 22:36:30 -080086 this.featuresRepo = checkNotNull(featuresRepo, "Features repo cannot be null");
Simon Huntc2da4882016-01-21 13:24:47 -080087 this.features = ImmutableList.copyOf(
88 checkNotNull(features, "Features cannot be null")
89 );
90 this.requiredApps = ImmutableList.copyOf(
91 checkNotNull(requiredApps, "Required apps cannot be null")
92 );
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 checkArgument(!features.isEmpty(), "There must be at least one feature");
94 }
95
96 @Override
97 public ApplicationId id() {
98 return appId;
99 }
100
101 @Override
102 public Version version() {
103 return version;
104 }
105
106 @Override
107 public String description() {
108 return description;
109 }
110
111 @Override
Jian Lic35415d2016-01-14 17:22:31 -0800112 public String category() {
113 return category;
114 }
115
116 @Override
117 public String url() {
118 return url;
119 }
120
121 @Override
122 public String readme() {
123 return readme;
124 }
125
126 @Override
127 public byte[] icon() {
Simon Huntc2da4882016-01-21 13:24:47 -0800128 return icon.clone();
Jian Lic35415d2016-01-14 17:22:31 -0800129 }
130
131 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800132 public String origin() {
133 return origin;
134 }
135
136 @Override
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900137 public ApplicationRole role() {
138 return role;
139 }
140
141 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 public Set<Permission> permissions() {
143 return permissions;
144 }
145
146 @Override
147 public Optional<URI> featuresRepo() {
148 return featuresRepo;
149 }
150
151 @Override
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800152 public List<String> features() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800153 return features;
154 }
155
156 @Override
Thomas Vachuska761f0042015-11-11 19:10:17 -0800157 public List<String> requiredApps() {
158 return requiredApps;
159 }
160
161 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800162 public int hashCode() {
Jian Lic35415d2016-01-14 17:22:31 -0800163 return Objects.hash(appId, version, description, origin, category, url,
164 readme, role, permissions, featuresRepo, features, requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 }
166
167 @Override
168 public boolean equals(Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (obj == null || getClass() != obj.getClass()) {
173 return false;
174 }
175 final DefaultApplication other = (DefaultApplication) obj;
176 return Objects.equals(this.appId, other.appId) &&
177 Objects.equals(this.version, other.version) &&
178 Objects.equals(this.description, other.description) &&
179 Objects.equals(this.origin, other.origin) &&
Jian Lic35415d2016-01-14 17:22:31 -0800180 Objects.equals(this.category, other.category) &&
181 Objects.equals(this.url, other.url) &&
182 Objects.equals(this.readme, other.readme) &&
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900183 Objects.equals(this.role, other.role) &&
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800184 Objects.equals(this.permissions, other.permissions) &&
185 Objects.equals(this.featuresRepo, other.featuresRepo) &&
Thomas Vachuska761f0042015-11-11 19:10:17 -0800186 Objects.equals(this.features, other.features) &&
187 Objects.equals(this.requiredApps, other.requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800188 }
189
190 @Override
191 public String toString() {
192 return toStringHelper(this)
193 .add("appId", appId)
194 .add("version", version)
195 .add("description", description)
196 .add("origin", origin)
Jian Lic35415d2016-01-14 17:22:31 -0800197 .add("category", category)
198 .add("url", url)
199 .add("readme", readme)
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900200 .add("role", role)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800201 .add("permissions", permissions)
202 .add("featuresRepo", featuresRepo)
203 .add("features", features)
Thomas Vachuska761f0042015-11-11 19:10:17 -0800204 .add("requiredApps", requiredApps)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800205 .toString();
206 }
207}