Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.common.app; |
| 17 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.io.ByteStreams; |
| 21 | import com.google.common.io.Files; |
| 22 | import org.apache.commons.configuration.ConfigurationException; |
| 23 | import org.apache.commons.configuration.XMLConfiguration; |
| 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.app.ApplicationDescription; |
| 26 | import org.onosproject.app.ApplicationEvent; |
| 27 | import org.onosproject.app.ApplicationException; |
| 28 | import org.onosproject.app.ApplicationStoreDelegate; |
| 29 | import org.onosproject.app.DefaultApplicationDescription; |
| 30 | import org.onosproject.core.Permission; |
| 31 | import org.onosproject.core.Version; |
| 32 | import org.onosproject.store.AbstractStore; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.io.ByteArrayInputStream; |
| 37 | import java.io.File; |
| 38 | import java.io.FileInputStream; |
| 39 | import java.io.FileNotFoundException; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.net.URI; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 43 | import java.nio.charset.Charset; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 44 | import java.nio.file.NoSuchFileException; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 45 | import java.util.List; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | import java.util.zip.ZipEntry; |
| 48 | import java.util.zip.ZipInputStream; |
| 49 | |
| 50 | import static com.google.common.io.ByteStreams.toByteArray; |
| 51 | import static com.google.common.io.Files.createParentDirs; |
| 52 | import static com.google.common.io.Files.write; |
| 53 | |
| 54 | /** |
| 55 | * Facility for reading application archive stream and managing application |
| 56 | * directory structure. |
| 57 | */ |
| 58 | public class ApplicationArchive |
| 59 | extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> { |
| 60 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 61 | // 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | 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 Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 77 | private static final String M2_PREFIX = "m2"; |
| 78 | |
Thomas Vachuska | d5d9bcb | 2015-03-18 17:46:20 -0700 | [diff] [blame^] | 79 | private static final String KARAF_ROOT = "../"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 80 | private static final String M2_ROOT = "system/"; |
Thomas Vachuska | d5d9bcb | 2015-03-18 17:46:20 -0700 | [diff] [blame^] | 81 | private static final String APPS_ROOT = "apps/"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 82 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 83 | private File karafRoot = new File(KARAF_ROOT); |
| 84 | private File m2Dir = new File(karafRoot, M2_ROOT); |
| 85 | private File appsDir = new File(karafRoot, APPS_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Sets the root directory where application artifacts are kept. |
| 89 | * |
| 90 | * @param appsRoot top-level applications directory path |
| 91 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 92 | protected void setRootPath(String appsRoot) { |
| 93 | this.karafRoot = new File(appsRoot); |
| 94 | this.appsDir = new File(karafRoot, APPS_ROOT); |
| 95 | this.m2Dir = new File(karafRoot, M2_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the root directory where application artifacts are kept. |
| 100 | * |
| 101 | * @return top-level applications directory path |
| 102 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 103 | protected String getRootPath() { |
| 104 | return karafRoot.getPath(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 105 | } |
| 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 Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 124 | * 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 135 | * 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 Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 162 | public synchronized ApplicationDescription saveApplication(InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 163 | try (InputStream ais = stream) { |
| 164 | byte[] cache = toByteArray(ais); |
| 165 | InputStream bis = new ByteArrayInputStream(cache); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 166 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 167 | boolean plainXml = isPlainXml(cache); |
| 168 | ApplicationDescription desc = plainXml ? |
| 169 | parsePlainAppDescription(bis) : parseZippedAppDescription(bis); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 170 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 171 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 181 | installArtifacts(desc); |
| 182 | return desc; |
| 183 | } catch (IOException e) { |
| 184 | throw new ApplicationException("Unable to save application", e); |
| 185 | } |
| 186 | } |
| 187 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 188 | // 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Purges the application archive directory. |
| 201 | * |
| 202 | * @param appName application name |
| 203 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 204 | public synchronized void purgeApplication(String appName) { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 205 | File appDir = new File(appsDir, appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 206 | try { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 207 | Tools.removeDirectory(appDir); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 208 | } catch (IOException e) { |
| 209 | throw new ApplicationException("Unable to purge application " + appName, e); |
| 210 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 211 | if (appDir.exists()) { |
| 212 | throw new ApplicationException("Unable to purge application " + appName); |
| 213 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 217 | * Returns application archive stream for the specified application. This |
| 218 | * will be either the application ZIP file or the application XML file. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 219 | * |
| 220 | * @param appName application name |
| 221 | * @return application archive stream |
| 222 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 223 | public synchronized InputStream getApplicationInputStream(String appName) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 224 | try { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 225 | File appFile = appFile(appName, appName + ".zip"); |
| 226 | return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 227 | } 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 Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 234 | private ApplicationDescription parseZippedAppDescription(InputStream stream) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 235 | 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 Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 240 | byte[] data = ByteStreams.toByteArray(zis); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 241 | return parsePlainAppDescription(new ByteArrayInputStream(data)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 242 | } |
| 243 | zis.closeEntry(); |
| 244 | } |
| 245 | } |
| 246 | throw new IOException("Unable to locate " + APP_XML); |
| 247 | } |
| 248 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 249 | // 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 261 | private ApplicationDescription loadAppDescription(XMLConfiguration cfg) { |
| 262 | cfg.setAttributeSplittingDisabled(true); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 263 | cfg.setDelimiterParsingDisabled(true); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 264 | 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 Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 271 | List<String> features = ImmutableList.copyOf(cfg.getStringArray(FEATURES)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 272 | |
| 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 Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 278 | private void expandZippedApplication(InputStream stream, ApplicationDescription desc) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 279 | 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 Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 284 | if (!entry.isDirectory()) { |
| 285 | byte[] data = ByteStreams.toByteArray(zis); |
| 286 | zis.closeEntry(); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 287 | File file = new File(appDir, entry.getName()); |
| 288 | createParentDirs(file); |
| 289 | write(data, file); |
| 290 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 291 | } |
| 292 | zis.close(); |
| 293 | } |
| 294 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 295 | // 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 304 | // 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 Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 311 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 319 | /** |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 325 | protected boolean setActive(String appName) { |
| 326 | try { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 327 | return appFile(appName, "active").createNewFile() && updateTime(appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 328 | } catch (IOException e) { |
| 329 | throw new ApplicationException("Unable to mark app as active", e); |
| 330 | } |
| 331 | } |
| 332 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 333 | /** |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 339 | protected boolean clearActive(String appName) { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 340 | 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 | */ |
| 349 | private boolean updateTime(String appName) { |
| 350 | return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 353 | /** |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 359 | 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 | } |