1d528e5b001c5c5335fff5abbba5c41e2b7ca87c
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / api / ValidationException.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.api;
9
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16
17 /**
18  * This exception is not intended to be used while implementing modules,
19  * it aggregates validation exceptions and sends them back to the user.
20  * Use {@link org.opendaylight.controller.config.api.JmxAttributeValidationException} for
21  * validating modules instead.
22  */
23 public class ValidationException extends Exception {
24     private static final long serialVersionUID = -6072893219820274247L;
25
26     private final Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> failedValidations;
27
28     public ValidationException(
29             final Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
30         super(failedValidations.toString());
31         this.failedValidations = Collections.unmodifiableMap(failedValidations);
32     }
33
34     public static ValidationException createFromCollectedValidationExceptions(
35             final List<ValidationException> collectedExceptions) {
36         Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
37         for (ValidationException ve : collectedExceptions) {
38             for (Entry<String, Map<String, ExceptionMessageWithStackTrace>> outerEntry : ve
39                     .getFailedValidations().entrySet()) {
40                 for (Entry<String, ExceptionMessageWithStackTrace> innerEntry : outerEntry
41                         .getValue().entrySet()) {
42                     String moduleName = outerEntry.getKey();
43                     String instanceName = innerEntry.getKey();
44                     ExceptionMessageWithStackTrace ex = innerEntry.getValue();
45                     Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
46                             .computeIfAbsent(moduleName, k -> new HashMap<>());
47                     if (instanceToExMap.containsKey(instanceName)) {
48                         throw new IllegalArgumentException(
49                                 "Cannot merge with same module name "
50                                         + moduleName + " and instance name "
51                                         + instanceName);
52                     }
53                     instanceToExMap.put(instanceName, ex);
54                 }
55             }
56         }
57         return new ValidationException(failedValidations);
58     }
59
60     public static ValidationException createForSingleException(
61             final ModuleIdentifier moduleIdentifier, final Exception e) {
62         Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
63         Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
64
65         failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
66         innerMap.put(moduleIdentifier.getInstanceName(),
67                 new ExceptionMessageWithStackTrace(e));
68         return new ValidationException(failedValidations);
69     }
70
71     public Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> getFailedValidations() {
72         return failedValidations;
73     }
74
75     public static class ExceptionMessageWithStackTrace {
76         private String message, stackTrace;
77
78         public ExceptionMessageWithStackTrace() {
79         }
80
81         public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
82             this.message = message;
83             this.stackTrace = stackTrace;
84         }
85
86         public ExceptionMessageWithStackTrace(final Exception e) {
87             this(e.getMessage(), Arrays.toString(e.getStackTrace()));
88         }
89
90         public String getMessage() {
91             return message;
92         }
93
94         public String getTrace() {
95             return stackTrace;
96         }
97
98         public void setMessage(final String message) {
99             this.message = message;
100         }
101
102         public void setTrace(final String stackTrace) {
103             this.stackTrace = stackTrace;
104         }
105
106         @Override
107         public int hashCode() {
108             final int prime = 31;
109             int result = 1;
110             result = prime * result
111                     + ((message == null) ? 0 : message.hashCode());
112             result = prime * result
113                     + ((stackTrace == null) ? 0 : stackTrace.hashCode());
114             return result;
115         }
116
117         @Override
118         public boolean equals(final Object obj) {
119             if (this == obj) {
120                 return true;
121             }
122             if (obj == null) {
123                 return false;
124             }
125             if (getClass() != obj.getClass()) {
126                 return false;
127             }
128             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
129             if (message == null) {
130                 if (other.message != null) {
131                     return false;
132                 }
133             } else if (!message.equals(other.message)) {
134                 return false;
135             }
136             if (stackTrace == null) {
137                 if (other.stackTrace != null) {
138                     return false;
139                 }
140             } else if (!stackTrace.equals(other.stackTrace)) {
141                 return false;
142             }
143             return true;
144         }
145
146         @Override
147         public String toString() {
148             return message;
149         }
150
151     }
152 }