io.deepsense.neptune.clientlibrary.parsers.jobargumentsparser.JobArgumentsParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neptune-client-library Show documentation
Show all versions of neptune-client-library Show documentation
Enables integration with Neptune in your Java code
/**
* Copyright (c) 2016, CodiLime Inc.
*/
package io.deepsense.neptune.clientlibrary.parsers.jobargumentsparser;
import com.google.common.base.Preconditions;
import io.deepsense.neptune.clientlibrary.models.JobParameters;
import io.deepsense.neptune.clientlibrary.parsers.additionalargumentsparser.AdditionalArgumentsParser;
import io.deepsense.neptune.clientlibrary.utils.SystemEnvironment;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import static io.deepsense.neptune.clientlibrary.utils.ArgumentsParserConsts.PARAMETER_PREFIX;
public class JobArgumentsParser {
private static final Logger logger = LoggerFactory.getLogger(JobArgumentsParser.class);
private static final String REST_API_URL_PARAMETER = "rest_api_url";
private static final String WEBSOCKET_API_URL_PARAMETER = "ws_api_url";
private static final String JOB_ID_PARAMETER = "job_id";
private static final String DEBUG_PARAMETER = "debug";
private static final String OFFLINE_PARAMETER = "offline";
private static final List PARAMETERS_REQUIRED_IN_ONLINE_MODE =
Arrays.asList(REST_API_URL_PARAMETER, WEBSOCKET_API_URL_PARAMETER, JOB_ID_PARAMETER);
static final String NEPTUNE_ONLINE_CONTEXT_ENV_VARIABLE = "NEPTUNE_ONLINE_CONTEXT";
private final ArgumentParser argparseParser;
private final AdditionalArgumentsParser additionalArgumentsParser;
public JobArgumentsParser(AdditionalArgumentsParser additionalArgumentsParser) {
argparseParser = ArgumentParsers.newArgumentParser("neptune-job");
Arrays.asList(REST_API_URL_PARAMETER, WEBSOCKET_API_URL_PARAMETER, JOB_ID_PARAMETER).forEach(
this::configureStringParameter);
Arrays.asList(DEBUG_PARAMETER, OFFLINE_PARAMETER).forEach(this::configureBooleanParameter);
this.additionalArgumentsParser = Preconditions.checkNotNull(additionalArgumentsParser);
}
private boolean isOfflineJob(Namespace argumentsNamespace, SystemEnvironment environment) {
if (environment.getEnv(NEPTUNE_ONLINE_CONTEXT_ENV_VARIABLE) == null) {
return true;
} else {
return argumentsNamespace.getBoolean(OFFLINE_PARAMETER);
}
}
public JobArguments parse(String[] programArguments, SystemEnvironment environment) {
try {
List additionalArguments = new ArrayList<>();
Namespace argumentsNamespace = argparseParser.parseKnownArgs(Preconditions.checkNotNull(programArguments),
additionalArguments);
validateProgramArguments(argumentsNamespace, environment);
boolean offline = isOfflineJob(argumentsNamespace, environment);
if (offline) {
return parseOfflineArguments(argumentsNamespace, additionalArguments);
} else {
return parseOnlineArguments(argumentsNamespace);
}
} catch (ArgumentParserException exc) {
logger.error("Invalid arguments passed from CLI to the job!", exc);
throw new RuntimeException(exc);
}
}
private UUID getJobId(Namespace argumentsNamespace) {
return UUID.fromString(Preconditions.checkNotNull(argumentsNamespace.getString(JOB_ID_PARAMETER)));
}
private boolean getDebug(Namespace argumentsNamespace) {
return argumentsNamespace.getBoolean(DEBUG_PARAMETER);
}
private UUID getOrGenerateJobId(Namespace argumentsNamespace) {
String jobIdArgument = argumentsNamespace.getString(JOB_ID_PARAMETER);
if (!StringUtils.isEmpty(jobIdArgument)) {
return UUID.fromString(jobIdArgument);
} else {
return UUID.randomUUID();
}
}
private JobArguments parseOfflineArguments(Namespace argumentsNamespace, List additionalArguments) {
UUID jobId = getOrGenerateJobId(argumentsNamespace);
boolean debug = getDebug(argumentsNamespace);
JobParameters jobParameters = additionalArgumentsParser.parse(additionalArguments);
return new OfflineJobArguments(jobId, debug, jobParameters);
}
private JobArguments parseOnlineArguments(Namespace argumentsNamespace) {
String restApiUrl = Preconditions.checkNotNull(
argumentsNamespace.getString(REST_API_URL_PARAMETER));
String websocketApiUrl = Preconditions.checkNotNull(
argumentsNamespace.getString(WEBSOCKET_API_URL_PARAMETER));
UUID jobId = getJobId(argumentsNamespace);
boolean debug = getDebug(argumentsNamespace);
return new OnlineJobArguments(URI.create(restApiUrl), URI.create(websocketApiUrl), jobId, debug);
}
private void configureStringParameter(String parameterName) {
argparseParser.addArgument(PARAMETER_PREFIX + parameterName.replace('_', '-'))
.type(String.class);
}
private void configureBooleanParameter(String parameterName) {
argparseParser.addArgument(PARAMETER_PREFIX + parameterName)
.action(Arguments.storeTrue());
}
private void validateProgramArguments(Namespace argumentsNamespace, SystemEnvironment environment) {
boolean isOffline = isOfflineJob(argumentsNamespace, environment);
if (!isOffline) {
PARAMETERS_REQUIRED_IN_ONLINE_MODE.forEach(argumentName -> {
String programArgument = argumentsNamespace.getString(argumentName);
if (StringUtils.isEmpty(programArgument)) {
throw new RuntimeException("argument " + PARAMETER_PREFIX + argumentName
+ " is required for online context");
}
});
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy