1 package org.onap.sdc.dcae.checker;
3 import java.io.IOException;
5 import java.util.LinkedList;
6 import java.util.Collections;
8 import org.yaml.snakeyaml.error.MarkedYAMLException;
9 import kwalify.ValidationException;
11 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
12 import com.fasterxml.jackson.databind.SerializerProvider;
13 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
14 import com.fasterxml.jackson.core.JsonGenerator;
15 import com.fasterxml.jackson.core.JsonProcessingException;
18 * Represents a collection of errors that occured during one of the stages
19 * of the checker: yaml parsing, yaml validation (tosca syntax), tosca checking
22 * This needs some re-thinking: while it is useful to have all original errors introducing
23 * the custom json conversion (just to help the service) is not great either.
24 * I was torn between this approach or creating a custom deserializer and object mapper (which
25 * would have kept all the customized serialization in the service but then the error analysis
26 * would be duplicated there too ..).
28 @JsonSerialize(contentUsing=org.onap.sdc.dcae.checker.Report.ReportEntrySerializer.class)
29 public class Report<T extends Throwable> extends LinkedList<T> {
34 public Report(T[] theErrors) {
35 Collections.addAll(this, theErrors);
38 public boolean hasErrors() {
39 return !this.isEmpty();
42 public boolean addOnce(T theError) {
44 if (e.getMessage().equals(theError.getMessage()))
50 public String toString() {
51 StringBuilder sb = new StringBuilder(this.size() + " errors");
52 for (Throwable x: this) {
57 .append(x.getMessage());
58 if (x.getCause() != null) {
59 sb.append("\n\tCaused by:\n")
60 .append(x.getCause());
67 private static String location(Throwable theError) {
68 if (theError instanceof MarkedYAMLException) {
69 MarkedYAMLException mx = (MarkedYAMLException)theError;
70 return "line " + mx.getProblemMark().getLine() + ", column " + mx.getProblemMark().getColumn();
72 if (theError instanceof ValidationException) {
73 ValidationException vx = (ValidationException)theError;
76 if (theError instanceof TargetError) {
77 TargetError tx = (TargetError)theError;
78 return tx.getLocation();
84 public static class ReportEntrySerializer extends StdSerializer<Throwable> {
86 public ReportEntrySerializer() {
87 super(Throwable.class);
91 public void serialize(Throwable theError, JsonGenerator theGenerator, SerializerProvider theProvider)
92 throws IOException, JsonProcessingException {
93 theGenerator.writeStartObject();
94 theGenerator.writeStringField("location", location(theError));
95 theGenerator.writeStringField("message", theError.getMessage());
96 if (theError.getCause() != null)
97 theGenerator.writeStringField("cause", theError.getCause().toString());
98 theGenerator.writeEndObject();