blob: 2954d071889ecc873639c1340b71fa0e55362ed1 [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.common.app;
17
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080018import com.google.common.collect.ImmutableList;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import com.google.common.collect.ImmutableSet;
20import com.google.common.io.ByteStreams;
21import com.google.common.io.Files;
22import org.apache.commons.configuration.ConfigurationException;
23import org.apache.commons.configuration.XMLConfiguration;
24import org.onlab.util.Tools;
25import org.onosproject.app.ApplicationDescription;
26import org.onosproject.app.ApplicationEvent;
27import org.onosproject.app.ApplicationException;
28import org.onosproject.app.ApplicationStoreDelegate;
29import org.onosproject.app.DefaultApplicationDescription;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090030import org.onosproject.core.ApplicationRole;
31import org.onosproject.core.DefaultPermission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032import org.onosproject.core.Permission;
33import org.onosproject.core.Version;
34import org.onosproject.store.AbstractStore;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.io.ByteArrayInputStream;
39import java.io.File;
40import java.io.FileInputStream;
41import java.io.FileNotFoundException;
42import java.io.IOException;
43import java.io.InputStream;
44import java.net.URI;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080045import java.nio.charset.Charset;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080046import java.nio.file.NoSuchFileException;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090047import java.util.ArrayList;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080048import java.util.List;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090049import java.util.Locale;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050import java.util.Set;
51import java.util.zip.ZipEntry;
52import java.util.zip.ZipInputStream;
53
54import static com.google.common.io.ByteStreams.toByteArray;
55import static com.google.common.io.Files.createParentDirs;
56import static com.google.common.io.Files.write;
57
58/**
59 * Facility for reading application archive stream and managing application
60 * directory structure.
61 */
62public class ApplicationArchive
63 extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> {
64
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070065 private static Logger log = LoggerFactory.getLogger(ApplicationArchive.class);
66
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080067 // Magic strings to search for at the beginning of the archive stream
68 private static final String XML_MAGIC = "<?xml ";
69
70 // Magic strings to search for and how deep to search it into the archive stream
71 private static final String APP_MAGIC = "<app ";
72 private static final int APP_MAGIC_DEPTH = 1024;
73
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 private static final String NAME = "[@name]";
75 private static final String ORIGIN = "[@origin]";
76 private static final String VERSION = "[@version]";
77 private static final String FEATURES_REPO = "[@featuresRepo]";
78 private static final String FEATURES = "[@features]";
79 private static final String DESCRIPTION = "description";
80
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090081 private static final String ROLE = "security.role";
82 private static final String PERMISSIONS = "security.permissions.permission";
83
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070084 private static final String OAR = ".oar";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 private static final String APP_XML = "app.xml";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080086 private static final String M2_PREFIX = "m2";
87
Thomas Vachuska40a398b2015-04-03 22:26:30 -070088 private static final String ROOT = "../";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080089 private static final String M2_ROOT = "system/";
Thomas Vachuskad5d9bcb2015-03-18 17:46:20 -070090 private static final String APPS_ROOT = "apps/";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091
Thomas Vachuska40a398b2015-04-03 22:26:30 -070092 private File root = new File(ROOT);
93 private File appsDir = new File(root, APPS_ROOT);
94 private File m2Dir = new File(M2_ROOT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080095
96 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -070097 * Sets the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -070099 * @param root top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 */
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700101 protected void setRootPath(String root) {
102 this.root = new File(root);
103 this.appsDir = new File(this.root, APPS_ROOT);
104 this.m2Dir = new File(M2_ROOT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 }
106
107 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700108 * Returns the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800109 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700110 * @return top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 */
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800112 protected String getRootPath() {
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700113 return root.getPath();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800114 }
115
116 /**
117 * Returns the set of installed application names.
118 *
119 * @return installed application names
120 */
121 public Set<String> getApplicationNames() {
122 ImmutableSet.Builder<String> names = ImmutableSet.builder();
123 File[] files = appsDir.listFiles(File::isDirectory);
124 if (files != null) {
125 for (File file : files) {
126 names.add(file.getName());
127 }
128 }
129 return names.build();
130 }
131
132 /**
Thomas Vachuskacf960112015-03-06 22:36:51 -0800133 * Returns the timestamp in millis since start of epoch, of when the
134 * specified application was last modified or changed state.
135 *
136 * @param appName application name
137 * @return number of millis since start of epoch
138 */
139 public long getUpdateTime(String appName) {
140 return appFile(appName, APP_XML).lastModified();
141 }
142
143 /**
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800144 * Loads the application descriptor from the specified application archive
145 * stream and saves the stream in the appropriate application archive
146 * directory.
147 *
148 * @param appName application name
149 * @return application descriptor
150 * @throws org.onosproject.app.ApplicationException if unable to read application description
151 */
152 public ApplicationDescription getApplicationDescription(String appName) {
153 try {
154 return loadAppDescription(new XMLConfiguration(appFile(appName, APP_XML)));
155 } catch (Exception e) {
156 throw new ApplicationException("Unable to get app description", e);
157 }
158 }
159
160 /**
161 * Loads the application descriptor from the specified application archive
162 * stream and saves the stream in the appropriate application archive
163 * directory.
164 *
165 * @param stream application archive stream
166 * @return application descriptor
167 * @throws org.onosproject.app.ApplicationException if unable to read the
168 * archive stream or store
169 * the application archive
170 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800171 public synchronized ApplicationDescription saveApplication(InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 try (InputStream ais = stream) {
173 byte[] cache = toByteArray(ais);
174 InputStream bis = new ByteArrayInputStream(cache);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800175
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800176 boolean plainXml = isPlainXml(cache);
177 ApplicationDescription desc = plainXml ?
178 parsePlainAppDescription(bis) : parseZippedAppDescription(bis);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800179
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800180 if (plainXml) {
181 expandPlainApplication(cache, desc);
182 } else {
183 bis.reset();
184 expandZippedApplication(bis, desc);
185
186 bis.reset();
187 saveApplication(bis, desc);
188 }
189
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800190 installArtifacts(desc);
191 return desc;
192 } catch (IOException e) {
193 throw new ApplicationException("Unable to save application", e);
194 }
195 }
196
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800197 // Indicates whether the stream encoded in the given bytes is plain XML.
198 private boolean isPlainXml(byte[] bytes) {
199 return substring(bytes, XML_MAGIC.length()).equals(XML_MAGIC) ||
200 substring(bytes, APP_MAGIC_DEPTH).contains(APP_MAGIC);
201 }
202
203 // Returns the substring of maximum possible length from the specified bytes.
204 private String substring(byte[] bytes, int length) {
205 return new String(bytes, 0, Math.min(bytes.length, length), Charset.forName("UTF-8"));
206 }
207
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800208 /**
209 * Purges the application archive directory.
210 *
211 * @param appName application name
212 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800213 public synchronized void purgeApplication(String appName) {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800214 File appDir = new File(appsDir, appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800215 try {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800216 Tools.removeDirectory(appDir);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800217 } catch (IOException e) {
218 throw new ApplicationException("Unable to purge application " + appName, e);
219 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800220 if (appDir.exists()) {
221 throw new ApplicationException("Unable to purge application " + appName);
222 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223 }
224
225 /**
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800226 * Returns application archive stream for the specified application. This
227 * will be either the application ZIP file or the application XML file.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800228 *
229 * @param appName application name
230 * @return application archive stream
231 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800232 public synchronized InputStream getApplicationInputStream(String appName) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 try {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700234 File appFile = appFile(appName, appName + OAR);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800235 return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800236 } catch (FileNotFoundException e) {
237 throw new ApplicationException("Application " + appName + " not found");
238 }
239 }
240
241 // Scans the specified ZIP stream for app.xml entry and parses it producing
242 // an application descriptor.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800243 private ApplicationDescription parseZippedAppDescription(InputStream stream)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800244 throws IOException {
245 try (ZipInputStream zis = new ZipInputStream(stream)) {
246 ZipEntry entry;
247 while ((entry = zis.getNextEntry()) != null) {
248 if (entry.getName().equals(APP_XML)) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800249 byte[] data = ByteStreams.toByteArray(zis);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800250 return parsePlainAppDescription(new ByteArrayInputStream(data));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800251 }
252 zis.closeEntry();
253 }
254 }
255 throw new IOException("Unable to locate " + APP_XML);
256 }
257
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800258 // Scans the specified XML stream and parses it producing an application descriptor.
259 private ApplicationDescription parsePlainAppDescription(InputStream stream)
260 throws IOException {
261 XMLConfiguration cfg = new XMLConfiguration();
262 try {
263 cfg.load(stream);
264 return loadAppDescription(cfg);
265 } catch (ConfigurationException e) {
266 throw new IOException("Unable to parse " + APP_XML, e);
267 }
268 }
269
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800270 private ApplicationDescription loadAppDescription(XMLConfiguration cfg) {
271 cfg.setAttributeSplittingDisabled(true);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800272 cfg.setDelimiterParsingDisabled(true);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800273 String name = cfg.getString(NAME);
274 Version version = Version.version(cfg.getString(VERSION));
275 String desc = cfg.getString(DESCRIPTION);
276 String origin = cfg.getString(ORIGIN);
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900277 ApplicationRole role = getRole(cfg.getString(ROLE));
278 Set<Permission> perms = getPermissions(cfg);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800279 String featRepo = cfg.getString(FEATURES_REPO);
280 URI featuresRepo = featRepo != null ? URI.create(featRepo) : null;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800281 List<String> features = ImmutableList.copyOf(cfg.getStringArray(FEATURES));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800282
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900283 return new DefaultApplicationDescription(name, version, desc, origin, role,
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800284 perms, featuresRepo, features);
285 }
286
287 // Expands the specified ZIP stream into app-specific directory.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800288 private void expandZippedApplication(InputStream stream, ApplicationDescription desc)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800289 throws IOException {
290 ZipInputStream zis = new ZipInputStream(stream);
291 ZipEntry entry;
292 File appDir = new File(appsDir, desc.name());
293 while ((entry = zis.getNextEntry()) != null) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800294 if (!entry.isDirectory()) {
295 byte[] data = ByteStreams.toByteArray(zis);
296 zis.closeEntry();
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800297 File file = new File(appDir, entry.getName());
298 createParentDirs(file);
299 write(data, file);
300 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800301 }
302 zis.close();
303 }
304
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800305 // Saves the specified XML stream into app-specific directory.
306 private void expandPlainApplication(byte[] stream, ApplicationDescription desc)
307 throws IOException {
308 File file = appFile(desc.name(), APP_XML);
309 createParentDirs(file);
310 write(stream, file);
311 }
312
313
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800314 // Saves the specified ZIP stream into a file under app-specific directory.
315 private void saveApplication(InputStream stream, ApplicationDescription desc)
316 throws IOException {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700317 Files.write(toByteArray(stream), appFile(desc.name(), desc.name() + OAR));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800318 }
319
320 // Installs application artifacts into M2 repository.
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800321 private void installArtifacts(ApplicationDescription desc) throws IOException {
322 try {
323 Tools.copyDirectory(appFile(desc.name(), M2_PREFIX), m2Dir);
324 } catch (NoSuchFileException e) {
325 log.debug("Application {} has no M2 artifacts", desc.name());
326 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800327 }
328
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800329 /**
330 * Marks the app as active by creating token file in the app directory.
331 *
332 * @param appName application name
333 * @return true if file was created
334 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800335 protected boolean setActive(String appName) {
336 try {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800337 return appFile(appName, "active").createNewFile() && updateTime(appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800338 } catch (IOException e) {
339 throw new ApplicationException("Unable to mark app as active", e);
340 }
341 }
342
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800343 /**
344 * Clears the app as active by deleting token file in the app directory.
345 *
346 * @param appName application name
347 * @return true if file was deleted
348 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800349 protected boolean clearActive(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800350 return appFile(appName, "active").delete() && updateTime(appName);
351 }
352
353 /**
354 * Updates the time-stamp of the app descriptor file.
355 *
356 * @param appName application name
357 * @return true if the app descriptor was updated
358 */
Thomas Vachuska161baf52015-03-27 16:15:39 -0700359 protected boolean updateTime(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800360 return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800361 }
362
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800363 /**
364 * Indicates whether the app was marked as active by checking for token file.
365 *
366 * @param appName application name
367 * @return true if the app is marked as active
368 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800369 protected boolean isActive(String appName) {
370 return appFile(appName, "active").exists();
371 }
372
373
374 // Returns the name of the file located under the specified app directory.
375 private File appFile(String appName, String fileName) {
376 return new File(new File(appsDir, appName), fileName);
377 }
378
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900379 // Returns the set of Permissions specified in the app.xml file
380 private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) {
381 List<Permission> perms = new ArrayList();
382 for (Object o : cfg.getList(PERMISSIONS)) {
383 DefaultPermission perm = null;
384 if (o != null) {
385 String permStr = (String) o;
386 perm = new DefaultPermission(permStr);
387 }
388 if (perm != null) {
389 perms.add(perm);
390 }
391 }
392
393 return ImmutableSet.copyOf(perms);
394 }
395
396 // Returns application role type
397 public ApplicationRole getRole(String value) {
398 if (value == null) {
399 return ApplicationRole.UNSPECIFIED;
400 } else {
401 try {
402 return ApplicationRole.valueOf(value.toUpperCase(Locale.ENGLISH));
403 } catch (IllegalArgumentException e) {
404 log.debug("Unknown role value: %s", value);
405 return ApplicationRole.UNSPECIFIED;
406 }
407 }
408 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800409}