Skip to content

Config draft1 #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class AppConfig {
public static final String POSTGRESQL_USER = CONFIG.getString("POSTGRESQL_USER");
public static final String POSTGRESQL_PASSWORD = CONFIG.getString("POSTGRESQL_PASSWORD");
public static final String POSTGRESQL_NOTIFICATIONS_DB = CONFIG.getString("POSTGRESQL_NOTIFICATIONS_DB");
public static final String POSTGRESQL_CONFIGURATION_DB = CONFIG.getString("POSTGRESQL_CONFIGURATION_DB");
public static final String POSTGRESQL_AUDIT_DB = CONFIG.getString("POSTGRESQL_AUDIT_DB");
public static final String KAFKA_BOOTSTRAP_SERVERS = CONFIG.getString("KAFKA_BOOTSTRAP_SERVERS");
public static final String KAFKA_APPLICATION_ID = CONFIG.getString("KAFKA_APPLICATION_ID");
Expand Down
10 changes: 9 additions & 1 deletion JeMPI_Apps/JeMPI_API/src/main/java/org/jembi/jempi/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.AppConfig;
import org.jembi.jempi.libapi.BackEnd;

import org.jembi.jempi.shared.config.Config;
import java.util.UUID;

public final class API {
Expand All @@ -23,6 +23,13 @@ private API() {

public static void main(final String[] args) {
try {
// PostgresConfig postgresConfig = new PostgresConfig("localhost", 5432, "jempi", "postgres", "postgres");
Config.create(AppConfig.POSTGRESQL_IP,
AppConfig.POSTGRESQL_PORT,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.POSTGRESQL_USER,
AppConfig.POSTGRESQL_PASSWORD);

new API().run();
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
Expand All @@ -40,6 +47,7 @@ public Behavior<Void> create() {
AppConfig.POSTGRESQL_PASSWORD,
AppConfig.POSTGRESQL_NOTIFICATIONS_DB,
AppConfig.POSTGRESQL_AUDIT_DB,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.KAFKA_BOOTSTRAP_SERVERS,
"CLIENT_ID_API-" + UUID.randomUUID(),
AppConfig.SYSTEM_CONFIG_DIR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class AppConfig {
public static final String POSTGRESQL_USERS_DB = CONFIG.getString("POSTGRESQL_USERS_DB");
public static final String POSTGRESQL_NOTIFICATIONS_DB = CONFIG.getString("POSTGRESQL_NOTIFICATIONS_DB");
public static final String POSTGRESQL_AUDIT_DB = CONFIG.getString("POSTGRESQL_AUDIT_DB");
public static final String POSTGRESQL_CONFIGURATION_DB = CONFIG.getString("POSTGRESQL_CONFIGURATION_DB");
public static final String KAFKA_BOOTSTRAP_SERVERS = CONFIG.getString("KAFKA_BOOTSTRAP_SERVERS");
public static final String KAFKA_APPLICATION_ID = CONFIG.getString("KAFKA_APPLICATION_ID");
private static final String[] DGRAPH_ALPHA_HOSTS = CONFIG.getString("DGRAPH_HOSTS").split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Behavior<Void> create() {
AppConfig.POSTGRESQL_PASSWORD,
AppConfig.POSTGRESQL_NOTIFICATIONS_DB,
AppConfig.POSTGRESQL_AUDIT_DB,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.KAFKA_BOOTSTRAP_SERVERS,
"CLIENT_ID_API_KC-" + UUID.randomUUID(),
AppConfig.SYSTEM_CONFIG_DIR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class AppConfig {
public static final String POSTGRESQL_USER = CONFIG.getString("POSTGRESQL_USER");
public static final String POSTGRESQL_PASSWORD = CONFIG.getString("POSTGRESQL_PASSWORD");
public static final String POSTGRESQL_NOTIFICATIONS_DB = CONFIG.getString("POSTGRESQL_NOTIFICATIONS_DB");
public static final String POSTGRESQL_CONFIGURATION_DB = CONFIG.getString("POSTGRESQL_CONFIGURATION_DB");
public static final String POSTGRESQL_AUDIT_DB = CONFIG.getString("POSTGRESQL_AUDIT_DB");
public static final String KAFKA_BOOTSTRAP_SERVERS = CONFIG.getString("KAFKA_BOOTSTRAP_SERVERS");
private static final String[] DGRAPH_ALPHA_HOSTS = CONFIG.getString("DGRAPH_HOSTS").split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class BackEnd extends AbstractBehavior<BackEnd.Event> {
private final String pgPassword;
private final String pgNotificationsDb;
private final String pgAuditDb;
private final String pgConfigurationDb;
private final PsqlNotifications psqlNotifications;
private LibMPI libMPI = null;
private String[] dgraphHosts = null;
Expand All @@ -40,6 +41,7 @@ private BackEnd(
final String sqlPassword,
final String sqlNotificationsDb,
final String sqlAuditDb,
final String sqlConfigurationDb,
final String kafkaBootstrapServers,
final String kafkaClientId) {
super(context);
Expand All @@ -53,6 +55,7 @@ private BackEnd(
this.pgPassword = sqlPassword;
this.pgNotificationsDb = sqlNotificationsDb;
this.pgAuditDb = sqlAuditDb;
this.pgConfigurationDb = sqlConfigurationDb;
psqlNotifications = new PsqlNotifications(sqlIP, sqlPort, sqlNotificationsDb, sqlUser, sqlPassword);
openMPI(kafkaBootstrapServers, kafkaClientId, debugLevel);
} catch (Exception e) {
Expand All @@ -72,6 +75,7 @@ public static Behavior<Event> create(
final String sqlPassword,
final String sqlNotificationsDb,
final String sqlAuditDb,
final String sqlConfigurationDb,
final String kafkaBootstrapServers,
final String kafkaClientId) {
return Behaviors.setup(context -> new BackEnd(level,
Expand All @@ -84,6 +88,7 @@ public static Behavior<Event> create(
sqlPassword,
sqlNotificationsDb,
sqlAuditDb,
sqlConfigurationDb,
kafkaBootstrapServers,
kafkaClientId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Behavior<Void> create() {
AppConfig.POSTGRESQL_PASSWORD,
AppConfig.POSTGRESQL_NOTIFICATIONS_DB,
AppConfig.POSTGRESQL_AUDIT_DB,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.KAFKA_BOOTSTRAP_SERVERS,
"CLIENT_ID_API-" + UUID.randomUUID()), "BackEnd");
context.watch(backEnd);
Expand Down
1 change: 1 addition & 0 deletions JeMPI_Apps/JeMPI_Bootstrapper/boostrap.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ POSTGRESQL_USER=postgres
POSTGRESQL_PASSWORD=
POSTGRESQL_USERS_DB=
POSTGRESQL_NOTIFICATIONS_DB=
POSTGRESQL_CONFIGURATION_DB=
POSTGRESQL_AUDIT_DB=
POSTGRESQL_KC_TEST_DB=
KAFKA_BOOTSTRAP_SERVERS=127.0.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public class BootstrapperConfig {
public final String POSTGRESQL_DATABASE;
public final String POSTGRESQL_USERS_DB;
public final String POSTGRESQL_NOTIFICATIONS_DB;
public final String POSTGRESQL_CONFIGURATION_DB;
public final String POSTGRESQL_AUDIT_DB;
public final String POSTGRESQL_KC_TEST_DB;
public final String KAFKA_BOOTSTRAP_SERVERS;
public final String KAFKA_APPLICATION_ID;
public final String[] DGRAPH_ALPHA_HOSTS;
public final int[] DGRAPH_ALPHA_PORTS;
public final String API_CONFIG_REFERENCE_FILENAME;
public final String API_FIELDS_CONFIG_FILENAME;
public final String SYSTEM_CONFIG_DIR;

public BootstrapperConfig(final Config parsedConfig) {
POSTGRESQL_IP = parsedConfig.getString("POSTGRESQL_IP");
Expand All @@ -33,11 +37,15 @@ public BootstrapperConfig(final Config parsedConfig) {
POSTGRESQL_USERS_DB = parsedConfig.getString("POSTGRESQL_USERS_DB");
POSTGRESQL_NOTIFICATIONS_DB = parsedConfig.getString("POSTGRESQL_NOTIFICATIONS_DB");
POSTGRESQL_AUDIT_DB = parsedConfig.getString("POSTGRESQL_AUDIT_DB");
POSTGRESQL_CONFIGURATION_DB = parsedConfig.getString("POSTGRESQL_CONFIGURATION_DB");
POSTGRESQL_KC_TEST_DB = parsedConfig.getString("POSTGRESQL_KC_TEST_DB");

KAFKA_BOOTSTRAP_SERVERS = parsedConfig.getString("KAFKA_BOOTSTRAP_SERVERS");
KAFKA_APPLICATION_ID = parsedConfig.getString("KAFKA_APPLICATION_ID");
DGRAPH_ALPHA_HOSTS = parsedConfig.getString("DGRAPH_HOSTS").split(",");
API_CONFIG_REFERENCE_FILENAME = parsedConfig.getString("API_CONFIG_REFERENCE_FILENAME");
API_FIELDS_CONFIG_FILENAME = parsedConfig.getString("API_FIELDS_CONFIG_FILENAME");
SYSTEM_CONFIG_DIR = parsedConfig.getString("SYSTEM_CONFIG_DIR");
DGRAPH_ALPHA_PORTS = Arrays.stream(parsedConfig.getString("DGRAPH_PORTS").split(",")).mapToInt(s -> {
try {
return Integer.parseInt(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import org.jembi.jempi.bootstrapper.data.DataBootstrapper;
import org.jembi.jempi.bootstrapper.data.utils.DataBootstraperConsts;

import org.jembi.jempi.shared.models.GlobalConstants;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.*;
import java.util.List;
import java.util.stream.Collectors;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public class PostgresDataBootstrapper extends DataBootstrapper {

Expand Down Expand Up @@ -37,6 +42,7 @@ protected List<DBSchemaDetails> getAllDbSchemas() {
new DBSchemaDetails(this.loadedConfig.POSTGRESQL_USERS_DB, DataBootstraperConsts.POSTGRES_INIT_SCHEMA_USERS_DB),
new DBSchemaDetails(this.loadedConfig.POSTGRESQL_NOTIFICATIONS_DB, DataBootstraperConsts.POSTGRES_INIT_SCHEMA_NOTIFICATION_DB),
new DBSchemaDetails(this.loadedConfig.POSTGRESQL_AUDIT_DB, DataBootstraperConsts.POSTGRES_INIT_SCHEMA_AUDIT_DB),
new DBSchemaDetails(this.loadedConfig.POSTGRESQL_CONFIGURATION_DB, DataBootstraperConsts.POSTGRES_INIT_SCHEMA_CONFIGURATION_DB),
new DBSchemaDetails(this.loadedConfig.POSTGRESQL_KC_TEST_DB, null)
);
}
Expand Down Expand Up @@ -112,8 +118,57 @@ public Boolean deleteData() throws SQLException {
}

@Override
public Boolean resetAll() throws SQLException {
public Boolean resetAll() throws SQLException, IOException {
LOGGER.info("Resetting Postgres data and schemas.");
return this.deleteData() && this.deleteTables() && this.createSchema();
return this.deleteData() && this.deleteTables() && this.createSchema() && this.insertConfigurationData();
}

public Boolean insertConfigurationData() throws SQLException, IOException {
String configurationTable = "configuration";
String[][] configFiles = getConfigFiles(configurationTable);
String insertQuery = String.format("INSERT INTO %s (key, json) VALUES (?, ?::jsonb)", configurationTable);
postgresDALLib.runQuery(connection -> {
PreparedStatement pstmt = connection.prepareStatement(insertQuery);
for (String[] configFile : configFiles) {
String filePath = configFile[0];
String key = configFile[1];
try {
String jsonContent = readJsonFile(filePath);
pstmt.setString(1, key);
pstmt.setString(2, jsonContent);
pstmt.addBatch();
LOGGER.info("Added " + key + " configuration to batch");
} catch (IOException e) {
// Handle the exception, e.g., log it or throw a runtime exception
throw new RuntimeException("Error reading JSON file", e);
}
}
pstmt.executeBatch();
LOGGER.info("All configuration data inserted successfully");
return null;
}, true, this.loadedConfig.POSTGRESQL_CONFIGURATION_DB);
return true;
}

private String[][] getConfigFiles(final String configurationTable) {
String configDir = Optional.of(Paths.get(this.loadedConfig.SYSTEM_CONFIG_DIR))
.filter(Files::exists)
.map(Path::toString)
.orElseGet(() -> System.getenv("SYSTEM_CSV_DIR"));

LOGGER.info("Inserting configuration data into " + configurationTable);
LOGGER.info("configDir " + configDir);
// Define an array of config file paths and their corresponding keys
String[][] configFiles = {
{configDir + "/" + this.loadedConfig.API_CONFIG_REFERENCE_FILENAME, GlobalConstants.CONFIGURATION_CONFIG_KEY},
{configDir + "/" + this.loadedConfig.API_FIELDS_CONFIG_FILENAME, GlobalConstants.CONFIGURATION_CONFIG_API_KEY},
// Add more configuration files as needed
};
return configFiles;
}

private String readJsonFile(final String filePath) throws IOException {
Path path = Paths.get(filePath);
return Files.readString(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ protected DataBootstraperConsts() { }
public static final String POSTGRES_INIT_SCHEMA_AUDIT_DB = "/data/postgres/audit-schema.sql";
public static final String POSTGRES_INIT_SCHEMA_NOTIFICATION_DB = "/data/postgres/notifications-schema.sql";
public static final String POSTGRES_INIT_SCHEMA_USERS_DB = "/data/postgres/users-schema.sql";
public static final String POSTGRES_INIT_SCHEMA_CONFIGURATION_DB = "/data/postgres/configuration-schema.sql";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS configuration (
id SERIAL PRIMARY KEY,
key VARCHAR(255) NOT NULL UNIQUE,
json JSON NOT NULL,
dateCreated TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
dateUpdated TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create an index on the key column for faster lookups
CREATE INDEX IF NOT EXISTS idx_configuration_key ON configuration(key);

-- INSERT INTO configuration (key, json) VALUES
-- ('config', pg_read_file('/app/conf_system/config.json')::json);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class AppConfig {
public static final String POSTGRESQL_PASSWORD = CONFIG.getString("POSTGRESQL_PASSWORD");
public static final String POSTGRESQL_NOTIFICATIONS_DB = CONFIG.getString("POSTGRESQL_NOTIFICATIONS_DB");
public static final String POSTGRESQL_AUDIT_DB = CONFIG.getString("POSTGRESQL_AUDIT_DB");
public static final String POSTGRESQL_CONFIGURATION_DB = CONFIG.getString("POSTGRESQL_CONFIGURATION_DB");
public static final Integer CONTROLLER_HTTP_PORT = CONFIG.getInt("CONTROLLER_HTTP_PORT");
public static final String LINKER_IP = CONFIG.getString("LINKER_IP");
public static final Integer LINKER_HTTP_PORT = CONFIG.getInt("LINKER_HTTP_PORT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private void run() {
LOGGER.info("CONFIG: {} {} {} {} {}",
AppConfig.POSTGRESQL_NOTIFICATIONS_DB,
AppConfig.POSTGRESQL_AUDIT_DB,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.KAFKA_BOOTSTRAP_SERVERS,
AppConfig.KAFKA_APPLICATION_ID,
AppConfig.KAFKA_CLIENT_ID);
Expand Down
Loading