Search in sources :

Example 11 with ConfigSnapshotHolder

use of org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder in project controller by opendaylight.

the class FileStorageAdapterTest method testNewFile.

@Test
public void testNewFile() throws Exception {
    PropertiesProviderTest pp = new PropertiesProviderTest();
    pp.addProperty("fileStorage", NON_EXISTENT_DIRECTORY + NON_EXISTENT_FILE);
    pp.addProperty("numberOfBackups", Integer.toString(Integer.MAX_VALUE));
    storage.instantiate(pp);
    final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {

        @Override
        public String getConfigSnapshot() {
            return createConfig();
        }

        @Override
        public SortedSet<String> getCapabilities() {
            return createCaps();
        }
    };
    storage.persistConfig(holder);
    storage.persistConfig(holder);
    assertEquals(storage.toString().replace("\\", "/"), "XmlFileStorageAdapter [storage=" + NON_EXISTENT_DIRECTORY + NON_EXISTENT_FILE + "]");
    delete(new File(NON_EXISTENT_DIRECTORY));
}
Also used : ConfigSnapshotHolder(org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest) File(java.io.File) Test(org.junit.Test) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest)

Example 12 with ConfigSnapshotHolder

use of org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder in project controller by opendaylight.

the class FileStorageAdapterTest method testNoLastConfig.

@Test
public void testNoLastConfig() throws Exception {
    File file = Files.createTempFile("testFilePersist", ".txt").toFile();
    file.deleteOnExit();
    if (!file.exists()) {
        return;
    }
    try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) {
        storage.setFileStorage(file);
        List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
        assertThat(elementOptional.size(), is(0));
    }
}
Also used : ConfigSnapshotHolder(org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder) File(java.io.File) Test(org.junit.Test) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest)

Example 13 with ConfigSnapshotHolder

use of org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder in project controller by opendaylight.

the class FileStorageAdapterTest method testNoFeaturesStored.

@Test
public void testNoFeaturesStored() throws Exception {
    PropertiesProviderTest pp = new PropertiesProviderTest();
    pp.addProperty("fileStorage", file.getPath());
    pp.addProperty("numberOfBackups", Integer.toString(Integer.MAX_VALUE));
    storage.instantiate(pp);
    com.google.common.io.Files.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<persisted-snapshots>\n" + "   <snapshots>\n" + "      <snapshot>\n" + "         <required-capabilities>\n" + "            <capability>cap12</capability>\n" + "         </required-capabilities>\n" + "         <configuration>\n" + "            <config>1</config>\n" + "         </configuration>\n" + "      </snapshot>\n" + "   </snapshots>\n" + "</persisted-snapshots>", file, StandardCharsets.UTF_8);
    List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
    assertEquals(1, lastConf.size());
    ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
    assertXMLEqual("<config>1</config>", configSnapshotHolder.getConfigSnapshot());
    assertTrue(storage.getPersistedFeatures().isEmpty());
}
Also used : ConfigSnapshotHolder(org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest) Test(org.junit.Test) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest)

Example 14 with ConfigSnapshotHolder

use of org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder in project controller by opendaylight.

the class FileStorageAdapterTest method testFileAdapterOneBackup.

@Test
public void testFileAdapterOneBackup() throws Exception {
    PropertiesProviderTest pp = new PropertiesProviderTest();
    pp.addProperty("fileStorage", file.getPath());
    pp.addProperty("numberOfBackups", Integer.toString(Integer.MAX_VALUE));
    storage.instantiate(pp);
    final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {

        @Override
        public String getConfigSnapshot() {
            return createConfig();
        }

        @Override
        public SortedSet<String> getCapabilities() {
            return createCaps();
        }
    };
    storage.persistConfig(holder);
    storage.persistConfig(holder);
    assertEquals(29, com.google.common.io.Files.readLines(file, StandardCharsets.UTF_8).size());
    List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
    assertEquals(1, lastConf.size());
    ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
    assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
}
Also used : ConfigSnapshotHolder(org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest) Test(org.junit.Test) PropertiesProviderTest(org.opendaylight.controller.config.persist.test.PropertiesProviderTest)

Example 15 with ConfigSnapshotHolder

use of org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder in project controller by opendaylight.

the class XmlDirectoryPersister method loadLastConfigs.

@Override
public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
    File[] filesArray = extensionsFilter.isPresent() ? storage.listFiles(extensionsFilter.get()) : storage.listFiles();
    if (filesArray == null || filesArray.length == 0) {
        return Collections.emptyList();
    }
    List<File> sortedFiles = new ArrayList<>(Arrays.asList(filesArray));
    Collections.sort(sortedFiles);
    // combine all found files
    LOG.debug("Reading files in following order: {}", sortedFiles);
    List<ConfigSnapshotHolder> result = new ArrayList<>();
    for (File file : sortedFiles) {
        LOG.trace("Adding file '{}' to combined result", file);
        Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = fromXmlSnapshot(file);
        // Ignore non valid snapshot
        if (!configSnapshotHolderOptional.isPresent()) {
            continue;
        }
        result.add(configSnapshotHolderOptional.get());
    }
    return result;
}
Also used : ConfigSnapshotHolder(org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

ConfigSnapshotHolder (org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder)17 Test (org.junit.Test)13 PropertiesProviderTest (org.opendaylight.controller.config.persist.test.PropertiesProviderTest)12 File (java.io.File)8 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 SortedSet (java.util.SortedSet)1 ConflictingVersionException (org.opendaylight.controller.config.api.ConflictingVersionException)1 ModuleFactoryNotFoundException (org.opendaylight.controller.config.api.ModuleFactoryNotFoundException)1 ValidationException (org.opendaylight.controller.config.api.ValidationException)1 ConfigSubsystemFacadeFactory (org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory)1 PersisterAggregator (org.opendaylight.controller.config.persist.impl.PersisterAggregator)1 PersisterWithConfiguration (org.opendaylight.controller.config.persist.impl.PersisterAggregator.PersisterWithConfiguration)1 DocumentedException (org.opendaylight.controller.config.util.xml.DocumentedException)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1 SAXException (org.xml.sax.SAXException)1