use of org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList in project controller by opendaylight.
the class DatastoreSnapshotRestore method initialize.
// synchronize this method so that, in case of concurrent access to getAndRemove(),
// no one ends up with partially initialized data
@SuppressWarnings("checkstyle:IllegalCatch")
private synchronized void initialize() {
File restoreDirectoryFile = new File(restoreDirectoryPath);
String[] files = restoreDirectoryFile.list();
if (files == null || files.length == 0) {
LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
return;
}
if (files.length > 1) {
LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", files.length, restoreDirectoryFile);
return;
}
File restoreFile = new File(restoreDirectoryFile, files[0]);
LOG.info("Clustered datastore will be restored from file {}", restoreFile);
try (FileInputStream fis = new FileInputStream(restoreFile)) {
DatastoreSnapshotList snapshots = deserialize(fis);
LOG.debug("Deserialized {} snapshots", snapshots.size());
for (DatastoreSnapshot snapshot : snapshots) {
datastoreSnapshots.put(snapshot.getType(), snapshot);
}
} catch (ClassNotFoundException | IOException e) {
LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
} finally {
if (!restoreFile.delete()) {
LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
}
}
}
use of org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList in project controller by opendaylight.
the class DatastoreSnapshotRestoreTest method test.
@Test
public void test() throws Exception {
assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", newShardManagerSnapshot("config-one", "config-two"), Arrays.asList(new DatastoreSnapshot.ShardSnapshot("config-one", newSnapshot(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)), CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L)))))), new DatastoreSnapshot.ShardSnapshot("config-two", newSnapshot(PeopleModel.BASE_PATH, PeopleModel.emptyContainer()))));
DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("oper-one", newSnapshot(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)))));
DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(Arrays.asList(configSnapshot, operSnapshot));
try (FileOutputStream fos = new FileOutputStream(backupFile)) {
SerializationUtils.serialize(snapshotList, fos);
}
DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
assertDatastoreSnapshotEquals(configSnapshot, instance.getAndRemove("config"));
assertDatastoreSnapshotEquals(operSnapshot, instance.getAndRemove("oper"));
assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config"));
assertFalse(backupFile + " was not deleted", backupFile.exists());
instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config"));
assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper"));
}
Aggregations