Skip to content

Commit

Permalink
feat: store configuration in .yaml files, disable raw and olap retent…
Browse files Browse the repository at this point in the history
…ion data feature
  • Loading branch information
akardapolov committed Feb 21, 2020
1 parent d2b2df6 commit a81cd3f
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 250 deletions.
5 changes: 3 additions & 2 deletions ashv/src/main/java/config/profile/ConfigProfile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package config.profile;

import core.manager.ConstantManager;
import lombok.Data;

import java.util.List;
Expand All @@ -9,8 +10,8 @@ public class ConfigProfile {
private String configName;
private boolean isRunning;

private int rawRetainDays;
private int olapRetainDays;
private int rawRetainDays = ConstantManager.RETAIN_DAYS_MAX;
private int olapRetainDays = ConstantManager.RETAIN_DAYS_MAX;

private ConnProfile connProfile;
private List<SqlColProfile> sqlColProfileList;
Expand Down
49 changes: 30 additions & 19 deletions ashv/src/main/java/config/yaml/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import config.FileConfig;
import config.profile.ConfigProfile;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand All @@ -30,30 +32,39 @@ public YamlConfig(FileConfig fileConfig,
this.yaml = yaml;
this.configList = configList;

this.loadConfigFromFs();
this.loadConfigsFromFs();
}

public void loadConfigToFile(ConfigProfile configuration) {
try {
yaml.dump(configuration, fileConfig.getFileWriter(FileConfig.CONFIGURATION_DIR,
configuration.getConfigName()));
} catch (IOException e) {
log.error(e.getMessage());
}
@SneakyThrows
public void saveConfigToFile(ConfigProfile configuration) {
FileWriter fileWriter = fileConfig.getFileWriter(FileConfig.CONFIGURATION_DIR,
configuration.getConfigName());
yaml.dump(configuration, fileWriter);

fileWriter.close();

loadConfigsFromFs();
}

private void loadConfigFromFs() {
@SneakyThrows
public void deleteConfig(String configName) {
fileConfig.removeFile(FileConfig.CONFIGURATION_DIR + FileConfig.FILE_SEPARATOR + configName);
}

public void loadConfigsFromFs() {
configList.clear();

try {
fileConfig.listFilesInDirectory(FileConfig.CONFIGURATION_DIR).forEach(e -> {
try {
this.configList
.put(e.getFileName().normalize().toString(), loadConfigurationFile(e));
} catch (IOException ex) {
log.error(ex.getMessage());
}
});
} catch(IOException ex){
log.error(ex.getMessage());
fileConfig.listFilesInDirectory(FileConfig.CONFIGURATION_DIR).forEach(e -> {
try {
this.configList
.put(e.getFileName().normalize().toString(), loadConfigurationFile(e));
} catch (IOException ex) {
log.error(ex.getMessage());
}
});
} catch (IOException ex) {
log.error(ex.getMessage());
}
}

Expand Down
221 changes: 95 additions & 126 deletions ashv/src/main/java/core/manager/ConfigurationManager.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,77 @@
package core.manager;

import config.Labels;
import config.profile.ConfigProfile;
import config.profile.ConnProfile;
import config.profile.SqlColProfile;
import config.yaml.YamlConfig;
import core.parameter.ConnectionBuilder;
import excp.SqlColMetadataException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import profile.*;
import store.StoreManager;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

@Slf4j
@Singleton
public class ConfigurationManager {
private StoreManager storeManager;
private YamlConfig yamlConfig;
private HashMap<String, ConfigProfile> configList;

@Getter @Setter
private ConfigProfile currentConfiguration;

@Getter @Setter
private String configurationName;

@Getter @Setter
private String connectionName;

@Getter @Setter
private IProfile iProfile;
@Getter @Setter private ConfigProfile currentConfiguration;
@Getter @Setter private String configurationName;
@Getter @Setter private IProfile iProfile;

@Inject
public ConfigurationManager(StoreManager storeManager,
YamlConfig yamlConfig,
public ConfigurationManager(YamlConfig yamlConfig,
@Named("ConfigList") HashMap<String, ConfigProfile> configList) {
this.storeManager = storeManager;
this.yamlConfig = yamlConfig;
this.configList = configList;
}

public void loadCurrentConfiguration(String configurationName, ConnProfile connection){
ConfigProfile configProfile = new ConfigProfile();
configProfile.setConnProfile(connection);
loadProfile(connection.getProfileName());
public void loadCurrentConfiguration(String configurationName) {
ConfigProfile configProfile = getConnProfileList().stream()
.filter(e -> e.getConfigName().equalsIgnoreCase(configurationName))
.findAny().get();

configProfile.setConfigName(configurationName);
setCurrentConfiguration(configProfile);
loadProfile(configProfile.getConnProfile().getProfileName());

loadConfigToFile(configProfile);
setConfigurationName(configurationName);
setCurrentConfiguration(configProfile);
}

public void loadSqlColumnMetadata(List<SqlColProfile> columnPojos){
getCurrentConfiguration().setSqlColProfileList(columnPojos);
loadConfigToFile(getCurrentConfiguration());
public void loadSqlColumnMetadata(List<SqlColProfile> profilesDb) throws SqlColMetadataException {
Optional<List<SqlColProfile>> profileCurr = Optional.ofNullable(getCurrentConfiguration().getSqlColProfileList());

if (!profileCurr.isPresent()) {
getCurrentConfiguration().setSqlColProfileList(profilesDb);
loadConfigToFile(getCurrentConfiguration());
} else {
if (profilesDb.size() != profileCurr.get().size()) {
throw new SqlColMetadataException("ASH sql column metadata changes detected.. " +
"Create the new configuration profile!");
}
}
}

public void loadConfigToFile(ConfigProfile configuration) {
yamlConfig.loadConfigToFile(configuration);
yamlConfig.saveConfigToFile(configuration);
}

public void loadProfile(String profileName){
public List<ConfigProfile> getConnProfileList() {
return (List<ConfigProfile>) new ArrayList(configList.values());
}

public void deleteConfig(String configurationName) {
yamlConfig.deleteConfig(configurationName);
yamlConfig.loadConfigsFromFs();
}

public void loadProfile(String profileName) {
switch (profileName) {
case "OracleEE":
setIProfile(new OracleEE());
Expand All @@ -87,113 +90,79 @@ public void loadProfile(String profileName){
}
}

/**
* For migration purposes (from BDB store to yaml configs)
* Delete in future release
* @param list
*/
public void unloadConfigFromBdbToFile(List<ConnProfile> list){
list.stream().forEach(e -> {
Optional<Map.Entry<String, ConfigProfile>> in = configList.entrySet().stream()
.filter(m -> m.getValue().getConfigName()
.equalsIgnoreCase(e.getConnName())).findFirst();

if (!in.isPresent()){
loadCurrentConfiguration(e.getConnName(), e);
}
public ConnectionBuilder getConnectionParameters(String connName) {
Map.Entry<String,ConfigProfile> orElseEntry =
new AbstractMap.SimpleImmutableEntry<>(connName, new ConfigProfile());
orElseEntry.getValue().setConfigName(connName);
orElseEntry.getValue().setConnProfile(new ConnProfile());

});
}
Map.Entry<String, ConfigProfile> cfg = configList.entrySet().stream()
.filter(e -> e.getValue().getConfigName().equalsIgnoreCase(connName))
.findAny().orElse(orElseEntry);

public ConnectionBuilder getConnectionParameters(String connName){
connectionName = connName;
ConnProfile connOut = cfg.getValue().getConnProfile();

return new ConnectionBuilder.Builder(connName)
.userName(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.username")))
.password(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.password")))
.url(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.url")))
.jar(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.jar")))
.profile(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.profile")))
.rawRetainDays(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.other.raw")))
.olapRetainDays(this.storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connName, Labels.getLabel("local.sql.metadata.connection.other.olap")))
.build();
.userName(connOut.getUserName())
.password(connOut.getPassword())
.url(connOut.getUrl())
.jar(connOut.getJar())
.profile(connOut.getProfileName())
.rawRetainDays(String.valueOf(cfg.getValue().getRawRetainDays()))
.olapRetainDays(String.valueOf(cfg.getValue().getOlapRetainDays()))
.build();
}

public void saveConnection(ConnectionBuilder connParameters) {
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.name"), connParameters.getConnectionName());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.username"), connParameters.getUserName());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.password"), connParameters.getPassword());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.url"), connParameters.getUrl());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.jar"), connParameters.getJar());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.profile"), connParameters.getProfile());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.driver"), connParameters.getDriverName());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.other.raw"), connParameters.getRawRetainDays());
storeManager.getRepositoryDAO().metadataEAVDAO.putMainDataEAVWithCheck(
Labels.getLabel("local.sql.metadata.connection"), connParameters.getConnectionName(),
Labels.getLabel("local.sql.metadata.connection.other.olap"), connParameters.getOlapRetainDays());
}
public void saveConnection(ConnectionBuilder connIn) {
Map.Entry<String,ConfigProfile> orElseEntry = new AbstractMap.SimpleImmutableEntry<>("", new ConfigProfile());
orElseEntry.getValue().setConnProfile(new ConnProfile());
orElseEntry.getValue().setConfigName(connIn.getConnectionName());

public int getRawRetainDays(){
int intDays = ConstantManager.RETAIN_DAYS_MAX;
Map.Entry<String, ConfigProfile> cfg = configList.entrySet().stream()
.filter(e -> e.getValue().getConfigName().equalsIgnoreCase(connIn.getConnectionName()))
.findAny().orElse(orElseEntry);
ConnProfile connOut = cfg.getValue().getConnProfile();

String strDays = storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connectionName, Labels.getLabel("local.sql.metadata.connection.other.raw"));
connOut.setConnName(connIn.getConnectionName());
connOut.setUserName(connIn.getUserName());
connOut.setPassword(connIn.getPassword());
connOut.setUrl(connIn.getUrl());
connOut.setJar(connIn.getJar());
connOut.setProfileName(connIn.getProfile());
connOut.setDriver(connIn.getDriverName());

try {
intDays = Integer.parseInt(strDays);
} catch (NumberFormatException ex) {
log.info("Raw data days retain text field contains char data or empty");
}
cfg.getValue().setRawRetainDays(getRawRetainDays());
cfg.getValue().setOlapRetainDays(getOlapRetainDays());

return intDays;
yamlConfig.saveConfigToFile(cfg.getValue());
}

public int getOlapRetainDays(){
int intDays = ConstantManager.RETAIN_DAYS_MAX;

String strDays = storeManager.getRepositoryDAO().getMetaDataAttributeValue(
Labels.getLabel("local.sql.metadata.connection"),
connectionName, Labels.getLabel("local.sql.metadata.connection.other.olap"));

try {
intDays = Integer.parseInt(strDays);
} catch (NumberFormatException ex) {
log.info("Raw data days retain text field contains char data or empty");
}
public int getRawRetainDays() {
return ConstantManager.RETAIN_DAYS_MAX;
}

return intDays;
public int getOlapRetainDays() {
return ConstantManager.RETAIN_DAYS_MAX;
}

/**
* For migration purposes (from BDB store to yaml configs)
* Delete in future release
*
* @param list
*/
public void unloadConfigFromBdbToFile(List<ConnProfile> list) {
list.forEach(e -> {
Optional<Map.Entry<String, ConfigProfile>> in = configList.entrySet().stream()
.filter(m -> m.getValue().getConfigName()
.equalsIgnoreCase(e.getConnName())).findFirst();

if (!in.isPresent()) {
ConfigProfile configProfile = new ConfigProfile();
configProfile.setConfigName(e.getConnName());
configProfile.setConnProfile(e);
loadConfigToFile(configProfile);
}
});
}
}
Loading

0 comments on commit a81cd3f

Please sign in to comment.