2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.api;
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
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.
23 public class ValidationException extends Exception {
24 private static final long serialVersionUID = -6072893219820274247L;
26 private final Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> failedValidations;
28 public ValidationException(
29 Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
30 super(failedValidations.toString());
31 this.failedValidations = Collections.unmodifiableMap(failedValidations);
34 public static ValidationException createFromCollectedValidationExceptions(
35 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
47 if (instanceToExMap == null) {
48 instanceToExMap = new HashMap<>();
49 failedValidations.put(moduleName, instanceToExMap);
51 if (instanceToExMap.containsKey(instanceName)) {
52 throw new IllegalArgumentException(
53 "Cannot merge with same module name "
54 + moduleName + " and instance name "
57 instanceToExMap.put(instanceName, ex);
61 return new ValidationException(failedValidations);
64 public static ValidationException createForSingleException(
65 ModuleIdentifier moduleIdentifier, Exception e) {
66 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
67 Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
69 failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
70 innerMap.put(moduleIdentifier.getInstanceName(),
71 new ExceptionMessageWithStackTrace(e));
72 return new ValidationException(failedValidations);
75 public Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> getFailedValidations() {
76 return failedValidations;
79 public static class ExceptionMessageWithStackTrace {
80 private String message, stackTrace;
82 public ExceptionMessageWithStackTrace() {
85 public ExceptionMessageWithStackTrace(String message, String stackTrace) {
86 this.message = message;
87 this.stackTrace = stackTrace;
90 public ExceptionMessageWithStackTrace(Exception e) {
91 this(e.getMessage(), Arrays.toString(e.getStackTrace()));
94 public String getMessage() {
98 public String getTrace() {
102 public void setMessage(String message) {
103 this.message = message;
106 public void setTrace(String stackTrace) {
107 this.stackTrace = stackTrace;
111 public int hashCode() {
112 final int prime = 31;
114 result = prime * result
115 + ((message == null) ? 0 : message.hashCode());
116 result = prime * result
117 + ((stackTrace == null) ? 0 : stackTrace.hashCode());
122 public boolean equals(Object obj) {
129 if (getClass() != obj.getClass()) {
132 ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
133 if (message == null) {
134 if (other.message != null) {
137 } else if (!message.equals(other.message)) {
140 if (stackTrace == null) {
141 if (other.stackTrace != null) {
144 } else if (!stackTrace.equals(other.stackTrace)) {
151 public String toString() {