DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_validator / checker / src / main / java / org / onap / sdc / dcae / checker / Report.java
1 package org.onap.sdc.dcae.checker;
2
3 import java.io.IOException;
4
5 import java.util.LinkedList;
6 import java.util.Collections;
7
8 import org.yaml.snakeyaml.error.MarkedYAMLException;
9 import kwalify.ValidationException;
10
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;
16
17 /**
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
20  */
21 /*
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 ..).  
27  */
28 @JsonSerialize(contentUsing=org.onap.sdc.dcae.checker.Report.ReportEntrySerializer.class)
29 public class Report<T extends Throwable> extends LinkedList<T> {
30
31         public Report() {
32         }
33
34         public Report(T[] theErrors) {
35                 Collections.addAll(this, theErrors);
36         }
37
38         public boolean hasErrors() {
39                 return !this.isEmpty();
40         }
41
42         public boolean addOnce(T theError) {
43                 for (T e: this) {
44                         if (e.getMessage().equals(theError.getMessage()))
45                                 return false;
46                 }
47                 return add(theError);
48         }
49
50         public String toString() {
51     StringBuilder sb = new StringBuilder(this.size() + " errors");
52     for (Throwable x: this) {
53       sb.append("\n")
54                                 .append("[")
55         .append(location(x))
56         .append("] ")
57                                 .append(x.getMessage());
58                                 if (x.getCause() != null) {
59                                         sb.append("\n\tCaused by:\n")
60                 .append(x.getCause());
61                                 }
62     }
63                 sb.append("\n");
64                 return sb.toString();
65         }
66
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();
71                 }
72                 if (theError instanceof ValidationException) {
73       ValidationException vx = (ValidationException)theError;
74       return vx.getPath();
75     }
76                 if (theError instanceof TargetError) {
77         TargetError tx = (TargetError)theError;
78       return tx.getLocation();
79                 }
80                 return "unknown"; 
81         }
82
83
84         public static class ReportEntrySerializer extends StdSerializer<Throwable> {
85                 
86                 public ReportEntrySerializer() {
87                         super(Throwable.class);
88                 }
89
90     @Override
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();
99     }
100         }
101 }
102