import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Sets;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;
-import org.opendaylight.controller.config.persist.api.storage.StorageAdapter;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolderImpl;
+import org.opendaylight.controller.config.persist.api.Persister;
+import org.opendaylight.controller.config.persist.api.PropertiesProvider;
+import org.opendaylight.controller.config.persist.api.StorageAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
-import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
/**
* StorageAdapter that stores configuration in a plan file.
*/
-public class FileStorageAdapter implements StorageAdapter {
+public class FileStorageAdapter implements StorageAdapter, Persister {
private static final Logger logger = LoggerFactory.getLogger(FileStorageAdapter.class);
- // TODO prefix properties
private static final Charset ENCODING = Charsets.UTF_8;
public static final String FILE_STORAGE_PROP = "fileStorage";
public static final String NUMBER_OF_BACKUPS = "numberOfBackups";
+
private static final String SEPARATOR_E_PURE = "//END OF CONFIG";
private static final String SEPARATOR_E = newLine(SEPARATOR_E_PURE);
private File storage;
@Override
- public void setProperties(PropertiesProvider propertiesProvider) {
+ public Persister instantiate(PropertiesProvider propertiesProvider) {
File storage = extractStorageFileFromProperties(propertiesProvider);
logger.debug("Using file {}", storage.getAbsolutePath());
// Create file if it does not exist
+ " property should be either set to positive value, or ommited. Can not be set to 0.");
}
setFileStorage(storage);
-
+ return this;
}
@VisibleForTesting
} else {
numberOfStoredBackups = Integer.MAX_VALUE;
}
-
+ logger.trace("Property {} set to {}", NUMBER_OF_BACKUPS, numberOfStoredBackups);
return result;
}
}
@Override
- public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
+ public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
Preconditions.checkNotNull(storage, "Storage file is null");
if (!storage.exists()) {
- return Optional.absent();
+ return Collections.emptyList();
}
final LineProcessor lineProcessor = new LineProcessor();
- String result = Files.readLines(storage, ENCODING, lineProcessor);
-
- try {
- if (lineProcessor.getConfigSnapshot().isPresent() == false) {
- return Optional.absent();
- } else {
- return Optional.<ConfigSnapshotHolder> of(new PersistedConfigImpl(lineProcessor.getConfigSnapshot(),
- lineProcessor.getCapabilities()));
- }
+ Files.readLines(storage, ENCODING, lineProcessor);
- } catch (ParserConfigurationException | SAXException e) {
- throw new IOException("Unable to load last config ", e);
+ if (lineProcessor.getConfigSnapshot().isPresent() == false) {
+ return Collections.emptyList();
+ } else {
+ return Arrays.<ConfigSnapshotHolder>asList(new ConfigSnapshotHolderImpl(lineProcessor.getConfigSnapshot().get(),
+ lineProcessor.getCapabilities(), storage.getAbsolutePath()));
}
+
}
private static final class LineProcessor implements com.google.common.io.LineProcessor<String> {
private boolean inLastConfig, inLastSnapshot;
private final StringBuffer snapshotBuffer = new StringBuffer();
- private final Set<String> caps = Sets.newHashSet();
+ private final SortedSet<String> caps = new TreeSet<>();
@Override
public String getResult() {
return true;
}
- Optional<String> getConfigSnapshot() throws IOException, SAXException, ParserConfigurationException {
+ Optional<String> getConfigSnapshot() {
final String xmlContent = snapshotBuffer.toString();
- if (xmlContent == null || xmlContent.equals("")) {
+ if (xmlContent.equals("")) {
return Optional.absent();
- } else
+ } else {
return Optional.of(xmlContent);
+ }
}
- Set<String> getCapabilities() throws IOException, SAXException, ParserConfigurationException {
+ SortedSet<String> getCapabilities() {
return caps;
}
}
@Override
- public void close() throws IOException {
+ public void close() {
}
return "FileStorageAdapter [storage=" + storage + "]";
}
- private class PersistedConfigImpl implements ConfigSnapshotHolder {
-
- private final String snapshot;
- private final Set<String> caps;
-
- public PersistedConfigImpl(Optional<String> configSnapshot, Set<String> capabilities) {
- this.snapshot = configSnapshot.get();
- this.caps = capabilities;
- }
-
- @Override
- public String getConfigSnapshot() {
- return snapshot;
- }
-
- @Override
- public Set<String> getCapabilities() {
- return caps;
- }
- }
-
}