import java.io.IOException;
import java.util.Collections;
import java.util.List;
+import java.util.Set;
import java.util.SortedSet;
import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
import org.opendaylight.controller.config.persist.api.Persister;
private static Integer numberOfStoredBackups;
private File storage;
+ private static volatile XmlFileStorageAdapter instance;
+ private volatile ConfigSnapshot lastCfgSnapshotCache;
+ private volatile Optional<FeatureListProvider> featuresService = Optional.absent();
+
+ @VisibleForTesting
+ public void reset() {
+ instance = null;
+ lastCfgSnapshotCache = null;
+ featuresService = null;
+ }
+
@Override
public Persister instantiate(PropertiesProvider propertiesProvider) {
- File storage = extractStorageFileFromProperties(propertiesProvider);
- LOG.debug("Using file {}", storage.getAbsolutePath());
+ if(instance != null) {
+ return instance;
+ }
+
+ File localStorage = extractStorageFileFromProperties(propertiesProvider);
+ LOG.debug("Using file {}", localStorage.getAbsolutePath());
// Create file if it does not exist
- File parentFile = storage.getAbsoluteFile().getParentFile();
- if (parentFile.exists() == false) {
+ File parentFile = localStorage.getAbsoluteFile().getParentFile();
+ if (!parentFile.exists()) {
LOG.debug("Creating parent folders {}", parentFile);
parentFile.mkdirs();
}
- if (storage.exists() == false) {
+ if (!localStorage.exists()) {
LOG.debug("Storage file does not exist, creating empty file");
try {
- boolean result = storage.createNewFile();
- if (result == false)
- throw new RuntimeException("Unable to create storage file " + storage);
+ boolean result = localStorage.createNewFile();
+ if (!result) {
+ throw new RuntimeException("Unable to create storage file " + localStorage);
+ }
} catch (IOException e) {
- throw new RuntimeException("Unable to create storage file " + storage, e);
+ throw new RuntimeException("Unable to create storage file " + localStorage, e);
}
}
if (numberOfStoredBackups == 0) {
throw new RuntimeException(NUMBER_OF_BACKUPS
+ " property should be either set to positive value, or ommited. Can not be set to 0.");
}
- setFileStorage(storage);
+ setFileStorage(localStorage);
+
+ instance = this;
return this;
}
+ public static Optional<XmlFileStorageAdapter> getInstance() {
+ return Optional.fromNullable(instance);
+ }
+
+ public Set<String> getPersistedFeatures() {
+ return lastCfgSnapshotCache == null ? Collections.<String>emptySet() : lastCfgSnapshotCache.getFeatures();
+ }
+
+ public void setFeaturesService(final FeatureListProvider featuresService) {
+ this.featuresService = Optional.of(featuresService);
+ }
+
@VisibleForTesting
public void setFileStorage(File storage) {
this.storage = storage;
public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
Preconditions.checkNotNull(storage, "Storage file is null");
+ Set<String> installedFeatureIds = Collections.emptySet();
+ if(featuresService.isPresent()) {
+ installedFeatureIds = featuresService.get().listFeatures();
+ }
+
Config cfg = Config.fromXml(storage);
- cfg.addConfigSnapshot(ConfigSnapshot.fromConfigSnapshot(holder), numberOfStoredBackups);
+ cfg.addConfigSnapshot(ConfigSnapshot.fromConfigSnapshot(holder, installedFeatureIds), numberOfStoredBackups);
cfg.toXml(storage);
}
Optional<ConfigSnapshot> lastSnapshot = Config.fromXml(storage).getLastSnapshot();
if (lastSnapshot.isPresent()) {
- return Lists.newArrayList(toConfigSnapshot(lastSnapshot.get()));
+ lastCfgSnapshotCache = lastSnapshot.get();
+ return Lists.newArrayList(toConfigSnapshot(lastCfgSnapshotCache));
} else {
return Collections.emptyList();
}