Skip to content

Commit

Permalink
Release old utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg committed Nov 14, 2016
1 parent 6ff9f1b commit c7ae9bd
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.histodb.client.impl.util;

import eu.itesla_project.histodb.client.impl.HistoDbCacheImpl;
import eu.itesla_project.histodb.client.impl.HistoDbClientImpl;
import eu.itesla_project.modules.histo.HistoDbClient;
import eu.itesla_project.modules.histo.HistoDbClientFactory;
import eu.itesla_project.histodb.client.impl.HistoDbConfig;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class CacheOnlyHistoDbClientFactory implements HistoDbClientFactory {

@Override
public HistoDbClient create(boolean cache) {
return new HistoDbClientImpl(HistoDbConfig.load(), new DummyHistoDbHttpClient(new HistoDbCacheImpl()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.histodb.client.impl.util;

import eu.itesla_project.histodb.client.impl.HistoDbHttpClient;
import eu.itesla_project.histodb.client.impl.HistoDbUrl;
import eu.itesla_project.modules.histo.cache.HistoDbCache;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
* In the case of an MPI workflow we only want to work with cached data, so
* we use a dummy http client that throws an exception in case of data not
* in the cache.
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
class DummyHistoDbHttpClient implements HistoDbHttpClient {

private final HistoDbCache cache;

DummyHistoDbHttpClient(HistoDbCache cache) {
Objects.requireNonNull(cache);
this.cache = cache;
}

@Override
public HistoDbCache getCache() {
return cache;
}

private InputStream getData(String url) throws IOException {
InputStream is = cache.getData(url);
if (is == null) {
throw new RuntimeException("Query '" + url + "' not cached");
}
return is;
}

@Override
public InputStream deleteHttpRequest(HistoDbUrl url) throws IOException {
return getData(url.format());
}

@Override
public InputStream getHttpRequest(HistoDbUrl url) throws IOException {
return getData(url.format());
}

@Override
public InputStream postHttpRequest(HistoDbUrl url, byte[] content) throws IOException {
return getData(url.format());
}

@Override
public void close() throws Exception {
}

}
5 changes: 5 additions & 0 deletions modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
<artifactId>security-analysis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>eu.itesla_project</groupId>
<artifactId>iidm-xml-converter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.modules.offline;

import eu.itesla_project.iidm.network.Country;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.simulation.securityindexes.SecurityIndex;
import eu.itesla_project.simulation.securityindexes.SecurityIndexId;

import java.io.Writer;
import java.util.*;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class ForwardingOfflineDb<T extends OfflineDb> implements OfflineDb {

protected final T delegate;

public ForwardingOfflineDb(T delegate) {
this.delegate = Objects.requireNonNull(delegate);
}

@Override
public List<String> listWorkflows() {
return delegate.listWorkflows();
}

@Override
public String createWorkflow(String workflowId, OfflineWorkflowCreationParameters parameters) {
return delegate.createWorkflow(workflowId, parameters);
}

@Override
public OfflineWorkflowCreationParameters getParameters(String workflowId) {
return delegate.getParameters(workflowId);
}

@Override
public void deleteWorkflow(String workflowId) {
delegate.deleteWorkflow(workflowId);
}

@Override
public int createSample(String workflowId) {
return delegate.createSample(workflowId);
}

@Override
public void storeState(String workflowId, int sampleId, Network network, Set<Country> countryFilter) {
delegate.storeState(workflowId, sampleId, network, countryFilter);
}

@Override
public void storeTaskStatus(String workflowId, int sampleId, OfflineTaskType taskType, OfflineTaskStatus taskStatus, String taskFailureReason) {
delegate.storeTaskStatus(workflowId, sampleId, taskType, taskStatus, taskFailureReason);
}

@Override
public void storeSecurityIndexes(String workflowId, int sampleId, Collection<SecurityIndex> securityIndexes) {
delegate.storeSecurityIndexes(workflowId, sampleId, securityIndexes);
}

@Override
public int getSampleCount(String workflowId) {
return delegate.getSampleCount(workflowId);
}

@Override
public Collection<SecurityIndexId> getSecurityIndexIds(String workflowId) {
return delegate.getSecurityIndexIds(workflowId);
}

@Override
public Map<Integer, SecurityIndex> getSecurityIndexes(String workflowId, SecurityIndexId securityIndexId) {
return delegate.getSecurityIndexes(workflowId, securityIndexId);
}

@Override
public SecurityIndexSynthesis getSecurityIndexesSynthesis(String workflowId) {
return delegate.getSecurityIndexesSynthesis(workflowId);
}

@Override
public void exportCsv(String workflowId, Writer writer, OfflineDbCsvExportConfig config) {
delegate.exportCsv(workflowId, writer, config);
}

@Override
public void close() throws Exception {
delegate.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.modules.validation;

import eu.itesla_project.iidm.datasource.DataSource;
import eu.itesla_project.iidm.datasource.GzFileDataSource;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.iidm.xml.NetworkXml;
import eu.itesla_project.iidm.xml.XMLExportOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
class XmlValidationDb implements ValidationDb {

private static final Logger LOGGER = LoggerFactory.getLogger(XmlValidationDb.class);

private final Path dbDir;

XmlValidationDb(Path dbDir) {
this.dbDir = Objects.requireNonNull(dbDir);
}

private Path getDir(String path) {
return dbDir.resolve(path);
}

@Override
public void init(String path) {
Path dir = getDir(path);
try {
if (Files.exists(dir)) {
try (Stream<Path> stream = Files.list(dir)) {
stream.filter(p -> p.getFileName().toString().endsWith(".xml.gz")).forEach(p -> {
try {
Files.delete(p);
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
});
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void save(Network network, String path, String name) {
LOGGER.info("Save {} in {} of validation db", name, path);
Path dir = getDir(path);
try {
Files.createDirectories(dir);
DataSource outputStreamFactory = new GzFileDataSource(dir, name);
try (OutputStream os = new BufferedOutputStream(outputStreamFactory.newOutputStream(null, "xml", false))) {
XMLExportOptions options = new XMLExportOptions(true, false, true, false);
NetworkXml.write(network, options, os);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.modules.validation;

import eu.itesla_project.commons.config.PlatformConfig;

import java.nio.file.Path;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class XmlValidationDbFactory implements ValidationDbFactory {

@Override
public ValidationDb create() {
Path dir = PlatformConfig.defaultConfig().getModuleConfig("xml-validation-db").getPathProperty("directory");
return new XmlValidationDb(dir);
}

}

0 comments on commit c7ae9bd

Please sign in to comment.