blob: 117195f50ab2fadcfaf5c2a0e35d568b1fca9369 [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;
30import org.onosproject.core.Permission;
31import org.onosproject.core.Version;
32import org.onosproject.store.AbstractStore;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.io.ByteArrayInputStream;
37import java.io.File;
38import java.io.FileInputStream;
39import java.io.FileNotFoundException;
40import java.io.IOException;
41import java.io.InputStream;
42import java.net.URI;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080043import java.nio.charset.Charset;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080044import java.nio.file.NoSuchFileException;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080045import java.util.List;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046import java.util.Set;
47import java.util.zip.ZipEntry;
48import java.util.zip.ZipInputStream;
49
50import static com.google.common.io.ByteStreams.toByteArray;
51import static com.google.common.io.Files.createParentDirs;
52import static com.google.common.io.Files.write;
53
54/**
55 * Facility for reading application archive stream and managing application
56 * directory structure.
57 */
58public class ApplicationArchive
59 extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> {
60
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080061 // Magic strings to search for at the beginning of the archive stream
62 private static final String XML_MAGIC = "<?xml ";
63
64 // Magic strings to search for and how deep to search it into the archive stream
65 private static final String APP_MAGIC = "<app ";
66 private static final int APP_MAGIC_DEPTH = 1024;
67
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 private static final String NAME = "[@name]";
69 private static final String ORIGIN = "[@origin]";
70 private static final String VERSION = "[@version]";
71 private static final String FEATURES_REPO = "[@featuresRepo]";
72 private static final String FEATURES = "[@features]";
73 private static final String DESCRIPTION = "description";
74
75 private static Logger log = LoggerFactory.getLogger(ApplicationArchive.class);
76 private static final String APP_XML = "app.xml";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080077 private static final String M2_PREFIX = "m2";
78
Thomas Vachuska40a398b2015-04-03 22:26:30 -070079 private static final String ROOT = "../";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080080 private static final String M2_ROOT = "system/";
Thomas Vachuskad5d9bcb2015-03-18 17:46:20 -070081 private static final String APPS_ROOT = "apps/";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082
Thomas Vachuska40a398b2015-04-03 22:26:30 -070083 private File root = new File(ROOT);
84 private File appsDir = new File(root, APPS_ROOT);
85 private File m2Dir = new File(M2_ROOT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080086
87 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -070088 * Sets the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -070090 * @param root top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 */
Thomas Vachuska40a398b2015-04-03 22:26:30 -070092 protected void setRootPath(String root) {
93 this.root = new File(root);
94 this.appsDir = new File(this.root, APPS_ROOT);
95 this.m2Dir = new File(M2_ROOT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 }
97
98 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -070099 * Returns the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700101 * @return top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800102 */
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800103 protected String getRootPath() {
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700104 return root.getPath();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 }
106
107 /**
108 * Returns the set of installed application names.
109 *
110 * @return installed application names
111 */
112 public Set<String> getApplicationNames() {
113 ImmutableSet.Builder<String> names = ImmutableSet.builder();
114 File[] files = appsDir.listFiles(File::isDirectory);
115 if (files != null) {
116 for (File file : files) {
117 names.add(file.getName());
118 }
119 }
120 return names.build();
121 }
122
123 /**
Thomas Vachuskacf960112015-03-06 22:36:51 -0800124 * Returns the timestamp in millis since start of epoch, of when the
125 * specified application was last modified or changed state.
126 *
127 * @param appName application name
128 * @return number of millis since start of epoch
129 */
130 public long getUpdateTime(String appName) {
131 return appFile(appName, APP_XML).lastModified();
132 }
133
134 /**
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800135 * Loads the application descriptor from the specified application archive
136 * stream and saves the stream in the appropriate application archive
137 * directory.
138 *
139 * @param appName application name
140 * @return application descriptor
141 * @throws org.onosproject.app.ApplicationException if unable to read application description
142 */
143 public ApplicationDescription getApplicationDescription(String appName) {
144 try {
145 return loadAppDescription(new XMLConfiguration(appFile(appName, APP_XML)));
146 } catch (Exception e) {
147 throw new ApplicationException("Unable to get app description", e);
148 }
149 }
150
151 /**
152 * Loads the application descriptor from the specified application archive
153 * stream and saves the stream in the appropriate application archive
154 * directory.
155 *
156 * @param stream application archive stream
157 * @return application descriptor
158 * @throws org.onosproject.app.ApplicationException if unable to read the
159 * archive stream or store
160 * the application archive
161 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800162 public synchronized ApplicationDescription saveApplication(InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163 try (InputStream ais = stream) {
164 byte[] cache = toByteArray(ais);
165 InputStream bis = new ByteArrayInputStream(cache);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800166
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800167 boolean plainXml = isPlainXml(cache);
168 ApplicationDescription desc = plainXml ?
169 parsePlainAppDescription(bis) : parseZippedAppDescription(bis);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800170
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800171 if (plainXml) {
172 expandPlainApplication(cache, desc);
173 } else {
174 bis.reset();
175 expandZippedApplication(bis, desc);
176
177 bis.reset();
178 saveApplication(bis, desc);
179 }
180
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800181 installArtifacts(desc);
182 return desc;
183 } catch (IOException e) {
184 throw new ApplicationException("Unable to save application", e);
185 }
186 }
187
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800188 // Indicates whether the stream encoded in the given bytes is plain XML.
189 private boolean isPlainXml(byte[] bytes) {
190 return substring(bytes, XML_MAGIC.length()).equals(XML_MAGIC) ||
191 substring(bytes, APP_MAGIC_DEPTH).contains(APP_MAGIC);
192 }
193
194 // Returns the substring of maximum possible length from the specified bytes.
195 private String substring(byte[] bytes, int length) {
196 return new String(bytes, 0, Math.min(bytes.length, length), Charset.forName("UTF-8"));
197 }
198
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800199 /**
200 * Purges the application archive directory.
201 *
202 * @param appName application name
203 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800204 public synchronized void purgeApplication(String appName) {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800205 File appDir = new File(appsDir, appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800206 try {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800207 Tools.removeDirectory(appDir);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800208 } catch (IOException e) {
209 throw new ApplicationException("Unable to purge application " + appName, e);
210 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800211 if (appDir.exists()) {
212 throw new ApplicationException("Unable to purge application " + appName);
213 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800214 }
215
216 /**
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800217 * Returns application archive stream for the specified application. This
218 * will be either the application ZIP file or the application XML file.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800219 *
220 * @param appName application name
221 * @return application archive stream
222 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800223 public synchronized InputStream getApplicationInputStream(String appName) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800224 try {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800225 File appFile = appFile(appName, appName + ".zip");
226 return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 } catch (FileNotFoundException e) {
228 throw new ApplicationException("Application " + appName + " not found");
229 }
230 }
231
232 // Scans the specified ZIP stream for app.xml entry and parses it producing
233 // an application descriptor.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800234 private ApplicationDescription parseZippedAppDescription(InputStream stream)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800235 throws IOException {
236 try (ZipInputStream zis = new ZipInputStream(stream)) {
237 ZipEntry entry;
238 while ((entry = zis.getNextEntry()) != null) {
239 if (entry.getName().equals(APP_XML)) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800240 byte[] data = ByteStreams.toByteArray(zis);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800241 return parsePlainAppDescription(new ByteArrayInputStream(data));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800242 }
243 zis.closeEntry();
244 }
245 }
246 throw new IOException("Unable to locate " + APP_XML);
247 }
248
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800249 // Scans the specified XML stream and parses it producing an application descriptor.
250 private ApplicationDescription parsePlainAppDescription(InputStream stream)
251 throws IOException {
252 XMLConfiguration cfg = new XMLConfiguration();
253 try {
254 cfg.load(stream);
255 return loadAppDescription(cfg);
256 } catch (ConfigurationException e) {
257 throw new IOException("Unable to parse " + APP_XML, e);
258 }
259 }
260
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800261 private ApplicationDescription loadAppDescription(XMLConfiguration cfg) {
262 cfg.setAttributeSplittingDisabled(true);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800263 cfg.setDelimiterParsingDisabled(true);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800264 String name = cfg.getString(NAME);
265 Version version = Version.version(cfg.getString(VERSION));
266 String desc = cfg.getString(DESCRIPTION);
267 String origin = cfg.getString(ORIGIN);
268 Set<Permission> perms = ImmutableSet.of();
269 String featRepo = cfg.getString(FEATURES_REPO);
270 URI featuresRepo = featRepo != null ? URI.create(featRepo) : null;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800271 List<String> features = ImmutableList.copyOf(cfg.getStringArray(FEATURES));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800272
273 return new DefaultApplicationDescription(name, version, desc, origin,
274 perms, featuresRepo, features);
275 }
276
277 // Expands the specified ZIP stream into app-specific directory.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800278 private void expandZippedApplication(InputStream stream, ApplicationDescription desc)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800279 throws IOException {
280 ZipInputStream zis = new ZipInputStream(stream);
281 ZipEntry entry;
282 File appDir = new File(appsDir, desc.name());
283 while ((entry = zis.getNextEntry()) != null) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800284 if (!entry.isDirectory()) {
285 byte[] data = ByteStreams.toByteArray(zis);
286 zis.closeEntry();
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800287 File file = new File(appDir, entry.getName());
288 createParentDirs(file);
289 write(data, file);
290 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800291 }
292 zis.close();
293 }
294
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800295 // Saves the specified XML stream into app-specific directory.
296 private void expandPlainApplication(byte[] stream, ApplicationDescription desc)
297 throws IOException {
298 File file = appFile(desc.name(), APP_XML);
299 createParentDirs(file);
300 write(stream, file);
301 }
302
303
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800304 // Saves the specified ZIP stream into a file under app-specific directory.
305 private void saveApplication(InputStream stream, ApplicationDescription desc)
306 throws IOException {
307 Files.write(toByteArray(stream), appFile(desc.name(), desc.name() + ".zip"));
308 }
309
310 // Installs application artifacts into M2 repository.
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800311 private void installArtifacts(ApplicationDescription desc) throws IOException {
312 try {
313 Tools.copyDirectory(appFile(desc.name(), M2_PREFIX), m2Dir);
314 } catch (NoSuchFileException e) {
315 log.debug("Application {} has no M2 artifacts", desc.name());
316 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800317 }
318
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800319 /**
320 * Marks the app as active by creating token file in the app directory.
321 *
322 * @param appName application name
323 * @return true if file was created
324 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800325 protected boolean setActive(String appName) {
326 try {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800327 return appFile(appName, "active").createNewFile() && updateTime(appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800328 } catch (IOException e) {
329 throw new ApplicationException("Unable to mark app as active", e);
330 }
331 }
332
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800333 /**
334 * Clears the app as active by deleting token file in the app directory.
335 *
336 * @param appName application name
337 * @return true if file was deleted
338 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800339 protected boolean clearActive(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800340 return appFile(appName, "active").delete() && updateTime(appName);
341 }
342
343 /**
344 * Updates the time-stamp of the app descriptor file.
345 *
346 * @param appName application name
347 * @return true if the app descriptor was updated
348 */
Thomas Vachuska161baf52015-03-27 16:15:39 -0700349 protected boolean updateTime(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800350 return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800351 }
352
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800353 /**
354 * Indicates whether the app was marked as active by checking for token file.
355 *
356 * @param appName application name
357 * @return true if the app is marked as active
358 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800359 protected boolean isActive(String appName) {
360 return appFile(appName, "active").exists();
361 }
362
363
364 // Returns the name of the file located under the specified app directory.
365 private File appFile(String appName, String fileName) {
366 return new File(new File(appsDir, appName), fileName);
367 }
368
369}