Skip to content

Commit

Permalink
Merge branch 'master' into documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Nov 16, 2024
2 parents af0f3f4 + 58a091a commit aee1268
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
14 changes: 7 additions & 7 deletions common/src/main/java/com/google/udmi/util/SiteModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,6 @@ public SiteModel(String specPath, Supplier<String> specSupplier, ExecutionConfig
}
}

private static void loadVersionInfo(ExecutionConfiguration exeConfig) {
exeConfig.udmi_version = System.getenv(UDMI_VERSION_ENV);
exeConfig.udmi_commit = System.getenv(UDMI_COMMIT_ENV);
exeConfig.udmi_ref = System.getenv(UDMI_REF_ENV);
exeConfig.udmi_timever = System.getenv(UDMI_TIMEVER_ENV);
}

public SiteModel(String toolName, List<String> argList) {
this(removeStringArg(argList, "site_model"), projectSpecSupplier(argList), null);
ExecutionConfiguration executionConfiguration = getExecutionConfiguration();
Expand All @@ -166,6 +159,13 @@ public SiteModel(ExecutionConfiguration executionConfiguration) {
exeConfig.registry_suffix = executionConfiguration.registry_suffix;
}

private static void loadVersionInfo(ExecutionConfiguration exeConfig) {
exeConfig.udmi_version = System.getenv(UDMI_VERSION_ENV);
exeConfig.udmi_commit = System.getenv(UDMI_COMMIT_ENV);
exeConfig.udmi_ref = System.getenv(UDMI_REF_ENV);
exeConfig.udmi_timever = System.getenv(UDMI_TIMEVER_ENV);
}

private static Supplier<String> projectSpecSupplier(List<String> argList) {
return () -> {
if (argList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static com.google.udmi.util.Common.UPGRADED_FROM;
import static com.google.udmi.util.Common.getExceptionMessage;
import static com.google.udmi.util.Common.getNamespacePrefix;
import static com.google.udmi.util.Common.removeNextArg;
import static com.google.udmi.util.GeneralUtils.friendlyStackTrace;
import static com.google.udmi.util.GeneralUtils.getTimestamp;
import static com.google.udmi.util.GeneralUtils.ifNotNullGet;
Expand Down Expand Up @@ -70,6 +69,8 @@
import com.google.daq.mqtt.util.MessagePublisher.QuerySpeed;
import com.google.daq.mqtt.util.PubSubClient;
import com.google.daq.mqtt.util.ValidationException;
import com.google.udmi.util.CommandLineOption;
import com.google.udmi.util.CommandLineProcessor;
import com.google.udmi.util.Common;
import com.google.udmi.util.GeneralUtils;
import com.google.udmi.util.JsonUtil;
Expand All @@ -94,7 +95,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.MissingFormatArgumentException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -188,7 +188,8 @@ public class Validator {
private static final String TOOL_NAME = "validator";
public static final String VALIDATOR_TOOL_NAME = "validator";
public static final String REGISTRY_DEVICE_DEFAULT = "_regsitry";
private long reportingDelay = DEFAULT_INTERVAL_SEC;
private long reportingDelaySec = DEFAULT_INTERVAL_SEC;
private final CommandLineProcessor commandLineProcessor = new CommandLineProcessor(this);
private final Map<String, ReportingDevice> reportingDevices = new TreeMap<>();
private final Set<String> extraDevices = new TreeSet<>();
private final Set<String> processedDevices = new TreeSet<>();
Expand Down Expand Up @@ -256,10 +257,13 @@ public Validator() {

Validator processArgs(List<String> argListRaw) {
List<String> argList = new ArrayList<>(argListRaw);
siteModel = new SiteModel(TOOL_NAME, argList);
try {
siteModel = new SiteModel(TOOL_NAME, argList);
} catch (IllegalArgumentException e) {
commandLineProcessor.showUsage(e.getMessage());
}
processProfile(siteModel.getExecutionConfiguration());
postProcessArgs(argList);
targetDevices = Set.copyOf(argList);
targetDevices = Set.copyOf(postProcessArgs(argList));
initializeExpectedDevices();
return this;
}
Expand Down Expand Up @@ -349,53 +353,42 @@ private List<String> parseArgs(List<String> argList) {

private List<String> postProcessArgs(List<String> argList) {
try {
while (!argList.isEmpty()) {
String option = removeNextArg(argList);
try {
switch (option) {
case "-p" -> setProjectId(removeNextArg(argList));
case "-s" -> setSiteDir(removeNextArg(argList));
case "-a" -> setSchemaSpec(removeNextArg(argList));
case "-t" -> validatePubSub(removeNextArg(argList), true);
case "-f" -> validateFilesOutput(removeNextArg(argList));
case "-c" -> setValidateCurrent(true);
case "-d" -> setReportDelay(removeNextArg(argList));
case "-u" -> forceUpgrade = true;
case "-r" -> validateMessageTrace(removeNextArg(argList));
case "-n" -> client = new NullPublisher();
case "-w" -> setMessageTraceDir(removeNextArg(argList));
case "-z" -> setProfileMode();
case "--" -> {
// All remaining arguments remain in the return list.
return argList;
}
default -> throw new RuntimeException("Unknown cmdline option " + option);
}
} catch (MissingFormatArgumentException e) {
throw new RuntimeException("For command line option " + option, e);
}
}
return argList;
List<String> remainingArgs = commandLineProcessor.processArgs(argList);
return ofNullable(remainingArgs).orElse(ImmutableList.of());
} finally {
if (schemaMap == null) {
setSchemaSpec(new File(UDMI_ROOT, "schema").getAbsolutePath());
}
}
}

@CommandLineOption(short_form = "-n", description = "Use null client")
private void setNullClient() {
client = new NullPublisher();
}

@CommandLineOption(short_form = "-u", description = "Set force message upgrade")
private void setForceUpgrade() {
forceUpgrade = true;
}

@CommandLineOption(short_form = "-z", description = "Turn on system profiling")
private void setProfileMode() {
IotReflectorClient.reportStatistics = true;
MqttPublisher.reportStatistics = true;
}

@CommandLineOption(short_form = "-d", arg_type = "n", description = "Report delay (sec)")
private void setReportDelay(String arg) {
reportingDelay = Integer.parseInt(arg);
reportingDelaySec = Integer.parseInt(arg);
}

private void setValidateCurrent(boolean current) {
validateCurrent = current;
@CommandLineOption(short_form = "-c", description = "Validate current device messages")
private void setValidateCurrent() {
validateCurrent = true;
}

@CommandLineOption(short_form = "-p", arg_type = "s", description = "Set project id")
private void setProjectId(String projectId) {
if (!projectId.startsWith(PROJECT_PROVIDER_PREFIX)) {
config.project_id = projectId;
Expand All @@ -406,6 +399,11 @@ private void setProjectId(String projectId) {
config.project_id = parts[1];
}

@CommandLineOption(short_form = "-t", arg_type = "s", description = "Validate pub sub target")
private void validatePubSub(String pubSubCombo) {
validatePubSub(pubSubCombo, true);
}

private void validatePubSub(String pubSubCombo, boolean reflect) {
String[] parts = pubSubCombo.split("/");
Preconditions.checkArgument(parts.length <= 2, "Too many parts in pubsub path " + pubSubCombo);
Expand Down Expand Up @@ -441,6 +439,7 @@ MessageReadingClient getMessageReadingClient() {
return (MessageReadingClient) client;
}

@CommandLineOption(short_form = "-r", arg_type = "s", description = "Validate message trace")
private void validateMessageTrace(String messageDir) {
client = new MessageReadingClient(getRegistryId(), messageDir);
dataSinks.add(client);
Expand All @@ -457,7 +456,8 @@ Validator prepForMock() {
*
* @param siteDir site model directory
*/
public void setSiteDir(String siteDir) {
@CommandLineOption(short_form = "-s", arg_type = "s", description = "Set site directory")
private void setSiteDir(String siteDir) {
config.site_model = siteDir;
final File baseDir;
if (NO_SITE.equals(siteDir)) {
Expand All @@ -482,6 +482,7 @@ private ExecutionConfiguration resolveSiteConfig(ExecutionConfiguration config,
return GeneralUtils.mergeObject(siteConfig, config);
}

@CommandLineOption(short_form = "-w", arg_type = "s", description = "Write trace output")
private void setMessageTraceDir(String writeDirArg) {
traceDir = new File(writeDirArg);
outputLogger.info("Tracing message capture to " + traceDir.getAbsolutePath());
Expand Down Expand Up @@ -515,7 +516,7 @@ private void initializeExpectedDevices() {

private ReportingDevice newReportingDevice(String device) {
ReportingDevice reportingDevice = new ReportingDevice(device);
ifTrueThen(validateCurrent, () -> reportingDevice.setThreshold(reportingDelay));
ifTrueThen(validateCurrent, () -> reportingDevice.setThreshold(reportingDelaySec));
return reportingDevice;
}

Expand All @@ -524,7 +525,8 @@ private ReportingDevice newReportingDevice(String device) {
*
* @param schemaPath schema specification directory
*/
public void setSchemaSpec(String schemaPath) {
@CommandLineOption(short_form = "-a", arg_type = "s", description = "Set schema path")
private void setSchemaSpec(String schemaPath) {
File schemaPart = new File(schemaPath);
boolean rawPath = schemaPart.isAbsolute() || Strings.isNullOrEmpty(config.udmi_root);
File schemaFile = rawPath ? schemaPart : new File(new File(config.udmi_root), schemaPath);
Expand Down Expand Up @@ -607,7 +609,7 @@ void messageLoop() {
processValidationReport();
ScheduledFuture<?> reportSender =
simulatedMessages ? null : executor.scheduleAtFixedRate(this::processValidationReport,
reportingDelay, reportingDelay, TimeUnit.SECONDS);
reportingDelaySec, reportingDelaySec, TimeUnit.SECONDS);
try {
while (client.isActive()) {
try {
Expand Down Expand Up @@ -1090,7 +1092,7 @@ private synchronized void sendDeviceValidationReports(Map<String, ValidationStat
.map(Entry::getKey).toList();
summaryDevices.addAll(keys);
}
long batchSize = reportingDelay * REPORTS_PER_SEC;
long batchSize = reportingDelaySec * REPORTS_PER_SEC;
List<String> sendList = summaryDevices.stream().limit(batchSize).toList();
System.err.printf("Sending %d device validation state updates out of an available %d%n",
sendList.size(), summaryDevices.size());
Expand Down Expand Up @@ -1177,6 +1179,7 @@ private void validateFiles(String schemaSpec, String prefix, String targetSpec)
schemaExceptions.throwIfNotEmpty();
}

@CommandLineOption(short_form = "-f", arg_type = "s", description = "Validate from files")
private void validateFilesOutput(String targetSpec) {
try {
String[] parts = targetSpec.split(":");
Expand Down

0 comments on commit aee1268

Please sign in to comment.