Skip to content
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

Convert validator to use cmdline args #391

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/test_schema
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ for subset in $subsets; do
reltest=$reldir:$(realpath --relative-to $testdir $testpath)
fi
(
(cd $schemadir; java -jar -Dnashorn.args=--no-deprecation-warning $jarfile -- $schemaname files $reltest --) || true
(cd $schemadir; java -jar -Dnashorn.args=--no-deprecation-warning $jarfile -a $schemaname -f $reltest) || true
if [ $force == y ]; then
diff $output $expected || echo Updating $expected && cp $output $expected
else
Expand Down
19 changes: 17 additions & 2 deletions validator/bin/validate
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ shift 5
ROOT=$(dirname $0)/../..
cd $ROOT

if [[ "$schema" != schema ]]; then
echo Currently only default schema supported.
false
fi

jarfile=validator/build/libs/validator-1.0-SNAPSHOT-all.jar

if [ ! -f $jarfile ]; then
Expand All @@ -30,10 +35,20 @@ rm -rf $sitepath/out/

echo Executing validator $schema $target...

echo java -jar $jarfile $project $schema $target $subscription $sitepath
if [[ $target == reflect ]]; then
srcargs="-r"
elif [[ $target == pubsub ]]; then
srcargs="-t $subscription"
else
echo Unsupported target $target
false
fi

args="-p $project -s $sitepath $srcargs"
echo java -jar $jarfile $args

error=0
java -jar $jarfile $project $schema $target $subscription $sitepath || error=$?
java -jar $jarfile $args || error=$?

echo Validation complete, exit $error
exit $error
106 changes: 62 additions & 44 deletions validator/src/main/java/com/google/daq/mqtt/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
import java.io.PrintStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.MissingFormatArgumentException;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -76,9 +78,6 @@ public class Validator {
private static final String JSON_SUFFIX = ".json";
private static final String SCHEMA_VALIDATION_FORMAT = "Validating %d schemas";
private static final String TARGET_VALIDATION_FORMAT = "Validating %d files against %s";
private static final String PUBSUB_MARKER = "pubsub";
private static final String FILES_MARKER = "files";
private static final String REFLECT_MARKER = "reflect";
private static final String DEVICE_FILE_FORMAT = "devices/%s";
private static final String ATTRIBUTE_FILE_FORMAT = "%s.attr";
private static final String MESSAGE_FILE_FORMAT = "%s.json";
Expand All @@ -105,12 +104,12 @@ public class Validator {
EVENT_POINTSET, PointsetEvent.class,
STATE_POINTSET, PointsetState.class
);
private final String projectId;
private final Map<String, ReportingDevice> expectedDevices = new TreeMap<>();
private final Set<String> extraDevices = new TreeSet<>();
private final Set<String> processedDevices = new TreeSet<>();
private final Set<String> base64Devices = new TreeSet<>();
private final Set<String> ignoredRegistries = new HashSet();
private String projectId;
private File outBaseDir;
private File metadataReportFile;
private DataSink dataSink;
Expand All @@ -119,17 +118,59 @@ public class Validator {
private CloudIotConfig cloudIotConfig;
private CloudIotManager cloudIotManager;
private String siteDir;
private String deviceId;
private List<String> deviceIds;
private MessagePublisher client;
private Map<String, JsonSchema> schemaMap;

/**
* Create validator for the given project id.
* Create validator with the given args.
*
* @param projectId Target cloud project id
* @param argList Argument list
*/
public Validator(String projectId) {
this.projectId = projectId;
public Validator(List<String> argList) {
List<String> listCopy = new ArrayList<>(argList);
parseArgs(listCopy);
if (schemaMap == null) {
setSchemaSpec("schema");
}
deviceIds = listCopy;
}

private void parseArgs(List<String> argList) {
while (argList.size() > 0) {
String option = removeNextArg(argList);
try {
switch (option) {
case "-p":
projectId = removeNextArg(argList);
break;
case "-s":
setSiteDir(removeNextArg(argList));
break;
case "-a":
setSchemaSpec(removeNextArg(argList));
break;
case "-t":
initializeCloudIoT();
initializeFirestoreDataSink();
validatePubSub(removeNextArg(argList));
break;
case "-f":
validateFilesOutput(removeNextArg(argList));
break;
case "-r":
validateReflector();
break;
case "--":
// All remaining arguments remain in the return list.
return;
default:
throw new RuntimeException("Unknown cmdline option " + option);
}
} catch (MissingFormatArgumentException e) {
throw new RuntimeException("For command line option " + option, e);
}
}
}

/**
Expand All @@ -138,31 +179,8 @@ public Validator(String projectId) {
* @param args Arguments for program execution
*/
public static void main(String[] args) {
if (args.length != 5) {
throw new IllegalArgumentException("Args: [project] [schema] [target] [instance] [site]");
}
try {
Validator validator = new Validator(args[0]);
validator.setSchemaSpec(args[1]);
String targetSpec = args[2];
String instName = args[3];
String siteDir = args[4];
validator.setSiteDir(siteDir);
switch (targetSpec) {
case PUBSUB_MARKER:
validator.initializeCloudIoT();
validator.initializeFirestoreDataSink();
validator.validatePubSub(instName);
break;
case FILES_MARKER:
validator.validateFilesOutput(instName);
break;
case REFLECT_MARKER:
validator.validateReflector(instName);
break;
default:
throw new RuntimeException("Unknown target spec " + targetSpec);
}
Validator validator = new Validator(Arrays.asList(args));
validator.messageLoop();
} catch (ExceptionMap processingException) {
System.exit(2);
Expand All @@ -174,6 +192,13 @@ public static void main(String[] args) {
System.exit(0);
}

private String removeNextArg(List<String> argList) {
if (argList.isEmpty()) {
throw new MissingFormatArgumentException("Missing argument");
}
return argList.remove(0);
}

/**
* Set the site directory to use for this validation run.
*
Expand Down Expand Up @@ -292,8 +317,7 @@ private void validatePubSub(String instName) {
}
}

private void validateReflector(String instName) {
deviceId = NO_SITE.equals(instName) ? null : instName;
private void validateReflector() {
String keyFile = new File(siteDir, GCP_REFLECT_KEY_PKCS8).getAbsolutePath();
System.err.println("Loading reflector key file from " + keyFile);
client = new IotCoreClient(projectId, cloudIotConfig, keyFile);
Expand All @@ -303,16 +327,11 @@ private void messageLoop() {
if (client == null) {
return;
}
System.err.println(
"Entering message loop on " + client.getSubscriptionId() + " with device " + deviceId);
sendInitializationQuery();
System.err.println("Entering message loop on " + client.getSubscriptionId());
BiConsumer<Map<String, Object>, Map<String, String>> validator = messageValidator();
boolean initialized = false;
while (client.isActive()) {
try {
if (!initialized) {
initialized = true;
sendInitializationQuery();
}
client.processMessage(validator);
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -322,7 +341,7 @@ private void messageLoop() {
}

private void sendInitializationQuery() {
if (deviceId != null) {
for (String deviceId : deviceIds) {
System.err.println("Sending initialization query messages for device " + deviceId);
client.publish(deviceId, STATE_QUERY_TOPIC, EMPTY_MESSAGE);
}
Expand Down Expand Up @@ -420,7 +439,6 @@ private boolean validateUpdate(
updated = true;
}


try {
validateMessage(schemaMap.get(ENVELOPE_SCHEMA_ID), attributes);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.google.common.collect.ImmutableList;
import com.google.daq.mqtt.validator.Validator.MessageBundle;
import com.google.daq.mqtt.validator.Validator.MetadataReport;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -45,12 +47,11 @@ public class ValidatorTest {
private static final File REPORT_FILE = new File(SITE_DIR + "/out/validation_report.json");
private static final String DEVICE_NUM_ID = "97216312321";
private static final String REGISTRY_ID = "ZZ-TRI-FECTA";
private final Validator validator = new Validator(PROJECT_ID);

{
validator.setSchemaSpec(SCHEMA_SPEC);
validator.setSiteDir(SITE_DIR);
}
private static final List<String> testArgs = ImmutableList.of(
"-p", PROJECT_ID,
"-a", SCHEMA_SPEC,
"-s", SITE_DIR);
private final Validator validator = new Validator(testArgs);

@Test
public void emptySystemBlock() {
Expand Down