Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development' for 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWall89 committed May 15, 2020
2 parents 37325d8 + e357391 commit 4798631
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 154 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ build/

# hibernate-validator
.attach_pid*

/mvn-release.sh
37 changes: 0 additions & 37 deletions mvn-release.sh

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>it.cnit.blueprint</groupId>
<artifactId>validator</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
<name>blueprint-validator</name>
<description>Simple validator for blueprints defined in 5G EVE</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
Expand All @@ -13,6 +17,15 @@
import it.nextworks.nfvmano.libs.ifa.common.DescriptorInformationElement;
import it.nextworks.nfvmano.libs.ifa.common.exceptions.MalformattedElementException;
import it.nextworks.nfvmano.libs.ifa.descriptors.nsd.Nsd;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
Expand All @@ -27,134 +40,133 @@
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;

@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
public class BlueprintValidatorApplication implements CommandLineRunner {
enum TYPE {
vsb,
ctx,
expb,
tcb,
nsd
}

private static final Logger LOG = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());
private static ObjectMapper Y_OBJECT_MAPPER, J_OBJECT_MAPPER;
private static javax.validation.Validator VALIDATOR;
enum TYPE {
vsb,
ctx,
expb,
tcb,
nsd
}

public static void main(String[] args) {
SpringApplication app = new SpringApplication(BlueprintValidatorApplication.class);
app.setLogStartupInfo(false);
app.run(args);
}
private static final Logger LOG = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());
private static ObjectMapper Y_OBJECT_MAPPER, J_OBJECT_MAPPER;
private static javax.validation.Validator VALIDATOR;

private static Namespace parseArguments(String[] args) {
ArgumentParser parser = ArgumentParsers.newFor("validator").build()
.defaultHelp(true)
.description("Simple tool to validate blueprints and NSD for the 5G EVE platform.");
public static void main(String[] args) {
SpringApplication app = new SpringApplication(BlueprintValidatorApplication.class);
app.setLogStartupInfo(false);
app.run(args);
}

parser.addArgument("--debug").action(Arguments.storeTrue());
parser.addArgument("-t", "--type").type(TYPE.class).required(true)
.help("Specify the type of blueprint you want to validate.");
parser.addArgument("-s", "--schema").action(Arguments.storeTrue())
.help("Also generate JSON schema for the selected type");
parser.addArgument("file").help("YAML blueprint file path");
private static Namespace parseArguments(String[] args) {
ArgumentParser parser = ArgumentParsers.newFor("validator").build()
.defaultHelp(true)
.description("Simple tool to validate blueprints and NSD for the 5G EVE platform.");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
return ns;
parser.addArgument("--debug").action(Arguments.storeTrue());
parser.addArgument("-t", "--type").type(TYPE.class).required(true)
.help("Specify the type of blueprint you want to validate.");
parser.addArgument("-s", "--schema").action(Arguments.storeTrue())
.help("Also generate JSON schema for the selected type");
parser.addArgument("file").help("YAML blueprint file path");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
return ns;
}

/**
* @param s string containing the blueprint to be validated
* @param cls the specific blueprint class to use for validation (e.g. VsBlueprint.class)
* @param schema if true, generate JSON schema for cls
* @param <T> The type of the class to be validated
* @throws IOException if Jackson fails to deserialize the blueprint; it can be JsonParseException or JsonMappingException
* @throws ViolationException if javax.validation fails
* @throws MalformattedElementException if call to isValid() fails
*/
private static <T extends DescriptorInformationElement> void validate(String s, Class<T> cls, boolean schema)
throws IOException, ViolationException, MalformattedElementException {
if (schema) {
LOG.info("Schema:\n{}",
J_OBJECT_MAPPER.writeValueAsString(new JsonSchemaGenerator(J_OBJECT_MAPPER).generateSchema(cls)));
}
T b = Y_OBJECT_MAPPER.readValue(s, cls);
LOG.debug("Dump:\n{}", Y_OBJECT_MAPPER.writeValueAsString(b));
Set<ConstraintViolation<T>> violations = VALIDATOR.validate(b);
if (!violations.isEmpty()) {
throw new ViolationException(violations);
}
b.isValid();
/**
* @param s string containing the blueprint to be validated
* @param cls the specific blueprint class to use for validation (e.g. VsBlueprint.class)
* @param schema if true, generate JSON schema for cls
* @param <T> The type of the class to be validated
* @throws IOException if Jackson fails to deserialize the blueprint; it can be
* JsonParseException or JsonMappingException
* @throws ViolationException if javax.validation fails
* @throws MalformattedElementException if call to isValid() fails
*/
private static <T extends DescriptorInformationElement> void validate(String s, Class<T> cls,
boolean schema)
throws IOException, ViolationException, MalformattedElementException {
if (schema) {
LOG.info("Schema:\n{}",
J_OBJECT_MAPPER
.writeValueAsString(new JsonSchemaGenerator(J_OBJECT_MAPPER).generateSchema(cls)));
}
T b = Y_OBJECT_MAPPER.readValue(s, cls);
LOG.debug("Dump:\n{}", Y_OBJECT_MAPPER.writeValueAsString(b));
Set<ConstraintViolation<T>> violations = VALIDATOR.validate(b);
if (!violations.isEmpty()) {
throw new ViolationException(violations);
}
b.isValid();
}

@Override
public void run(String... args) {
Y_OBJECT_MAPPER = new ObjectMapper(new YAMLFactory().configure(Feature.SPLIT_LINES, false))
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
J_OBJECT_MAPPER = new ObjectMapper(new JsonFactory()).enable(SerializationFeature.INDENT_OUTPUT);
@Override
public void run(String... args) {
Y_OBJECT_MAPPER = new ObjectMapper(new YAMLFactory().configure(Feature.SPLIT_LINES, false))
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
J_OBJECT_MAPPER = new ObjectMapper(new JsonFactory())
.enable(SerializationFeature.INDENT_OUTPUT);

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
VALIDATOR = factory.getValidator();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
VALIDATOR = factory.getValidator();

Namespace ns = parseArguments(args);
LOG.info("Validating file {}", ns.getString("file"));
try (InputStream is = Files.newInputStream(Paths.get(ns.getString("file")))) {
JsonNode rootNode = Y_OBJECT_MAPPER.readTree(is);
switch ((TYPE) ns.get("type")) {
case vsb:
LOG.info("Selected type: Vertical Service Blueprint");
validate(rootNode.toString(), VsBlueprint.class, ns.getBoolean("schema"));
break;
case ctx:
LOG.info("Selected type: Context Blueprint");
validate(rootNode.toString(), CtxBlueprint.class, ns.getBoolean("schema"));
break;
case expb:
LOG.info("Selected type: Experiment Blueprint");
validate(rootNode.toString(), ExpBlueprint.class, ns.getBoolean("schema"));
break;
case tcb:
LOG.info("Selected type: Test Case Blueprint");
validate(rootNode.toString(), TestCaseBlueprint.class, ns.getBoolean("schema"));
break;
case nsd:
LOG.info("Selected type: Network Service Descriptor");
validate(rootNode.get(0).toString(), Nsd.class, ns.getBoolean("schema"));
break;
}
LOG.info("Validation success");
} catch (JsonParseException | JsonMappingException e) {
LOG.error(e.getOriginalMessage());
LOG.error("Error at line {}, column {}", e.getLocation().getLineNr(), e.getLocation().getColumnNr());
} catch (ViolationException e) {
for (String v : e.getViolationMessages()) {
LOG.error(v);
}
} catch (MalformattedElementException e) {
LOG.error(e.getMessage());
} catch (IOException e) {
LOG.error("Can't read input file {}", e.getMessage());
}
Namespace ns = parseArguments(args);
LOG.info("Validating file {}", ns.getString("file"));
try (InputStream is = Files.newInputStream(Paths.get(ns.getString("file")))) {
JsonNode rootNode = Y_OBJECT_MAPPER.readTree(is);
if (!rootNode.isObject()) {
throw new IOException("Not a JSON object. Please remove array (-) if present.");
}
switch ((TYPE) ns.get("type")) {
case vsb:
LOG.info("Selected type: Vertical Service Blueprint");
validate(rootNode.toString(), VsBlueprint.class, ns.getBoolean("schema"));
break;
case ctx:
LOG.info("Selected type: Context Blueprint");
validate(rootNode.toString(), CtxBlueprint.class, ns.getBoolean("schema"));
break;
case expb:
LOG.info("Selected type: Experiment Blueprint");
validate(rootNode.toString(), ExpBlueprint.class, ns.getBoolean("schema"));
break;
case tcb:
LOG.info("Selected type: Test Case Blueprint");
validate(rootNode.toString(), TestCaseBlueprint.class, ns.getBoolean("schema"));
break;
case nsd:
LOG.info("Selected type: Network Service Descriptor");
validate(rootNode.toString(), Nsd.class, ns.getBoolean("schema"));
break;
}
LOG.info("Validation success");
} catch (JsonParseException | JsonMappingException e) {
LOG.error(e.getOriginalMessage());
LOG.error("Error at line {}, column {}", e.getLocation().getLineNr(),
e.getLocation().getColumnNr());
} catch (ViolationException e) {
for (String v : e.getViolationMessages()) {
LOG.error(v);
}
} catch (MalformattedElementException e) {
LOG.error(e.getMessage());
} catch (IOException e) {
LOG.error("Can't read input file. {}", e.getMessage());
}
}
}

0 comments on commit 4798631

Please sign in to comment.