2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.persist.storage.directory.xml;
10 import com.google.common.base.Optional;
11 import com.google.common.io.Files;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.config.persist.api.Persister;
14 import org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import javax.xml.bind.JAXBContext;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.Unmarshaller;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.SortedSet;
31 import static com.google.common.base.Preconditions.checkArgument;
33 public class XmlDirectoryPersister implements Persister {
34 private static final Logger logger = LoggerFactory.getLogger(XmlDirectoryPersister.class);
36 private final File storage;
37 private final Optional<FilenameFilter> extensionsFilter;
40 * Creates XmlDirectoryPersister that picks up all files in specified folder
42 public XmlDirectoryPersister(File storage) {
43 this(storage, Optional.<FilenameFilter>absent());
47 * Creates XmlDirectoryPersister that picks up files only with specified file extension
49 public XmlDirectoryPersister(File storage, Set<String> fileExtensions) {
50 this(storage, Optional.of(getFilter(fileExtensions)));
53 private XmlDirectoryPersister(File storage, Optional<FilenameFilter> extensionsFilter) {
54 checkArgument(storage.exists() && storage.isDirectory(), "Storage directory does not exist: " + storage);
55 this.storage = storage;
56 this.extensionsFilter = extensionsFilter;
60 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
61 throw new UnsupportedOperationException("This adapter is read only. Please set readonly=true on " + getClass());
65 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
66 File[] filesArray = extensionsFilter.isPresent() ? storage.listFiles(extensionsFilter.get()) : storage.listFiles();
67 if (filesArray == null || filesArray.length == 0) {
68 return Collections.emptyList();
70 List<File> sortedFiles = new ArrayList<>(Arrays.asList(filesArray));
71 Collections.sort(sortedFiles);
72 // combine all found files
73 logger.debug("Reading files in following order: {}", sortedFiles);
75 List<ConfigSnapshotHolder> result = new ArrayList<>();
76 for (File file : sortedFiles) {
77 logger.trace("Adding file '{}' to combined result", file);
78 ConfigSnapshotHolder h = fromXmlSnapshot(file);
84 private ConfigSnapshotHolder fromXmlSnapshot(File file) {
86 return loadLastConfig(file);
87 } catch (JAXBException e) {
88 logger.warn("Unable to restore configuration snapshot from {}", file, e);
89 throw new IllegalStateException("Unable to restore configuration snapshot from " + file, e);
93 public static ConfigSnapshotHolder loadLastConfig(File file) throws JAXBException {
94 JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
95 Unmarshaller um = jaxbContext.createUnmarshaller();
97 return asHolder((ConfigSnapshot) um.unmarshal(file));
100 private static ConfigSnapshotHolder asHolder(final ConfigSnapshot unmarshalled) {
101 return new ConfigSnapshotHolder() {
103 public String getConfigSnapshot() {
104 return unmarshalled.getConfigSnapshot();
108 public SortedSet<String> getCapabilities() {
109 return unmarshalled.getCapabilities();
113 public String toString() {
114 return unmarshalled.toString();
119 private static FilenameFilter getFilter(final Set<String>fileExtensions) {
120 checkArgument(fileExtensions.isEmpty() == false, "No file extension provided", fileExtensions);
122 return new FilenameFilter() {
124 public boolean accept(File dir, String name) {
125 String ext = Files.getFileExtension(name);
126 return fileExtensions.contains(ext);
132 public void close() {
137 public String toString() {
138 final StringBuffer sb = new StringBuffer("XmlDirectoryPersister{");
139 sb.append("storage=").append(storage);
141 return sb.toString();