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 * itaggregates validation exceptions and sends them back to the user.
21 public class ValidationException extends RuntimeException {
22 private static final long serialVersionUID = -6072893219820274247L;
24 private final Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations;
26 public ValidationException(
27 Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
28 super(failedValidations.toString());
29 this.failedValidations = Collections.unmodifiableMap(failedValidations);
32 public static ValidationException createFromCollectedValidationExceptions(
33 List<ValidationException> collectedExceptions) {
34 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
35 for (ValidationException ve : collectedExceptions) {
36 for (Entry<String, Map<String, ExceptionMessageWithStackTrace>> outerEntry : ve
37 .getFailedValidations().entrySet()) {
38 for (Entry<String, ExceptionMessageWithStackTrace> innerEntry : outerEntry
39 .getValue().entrySet()) {
40 String moduleName = outerEntry.getKey();
41 String instanceName = innerEntry.getKey();
42 ExceptionMessageWithStackTrace ex = innerEntry.getValue();
43 Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
45 if (instanceToExMap == null) {
46 instanceToExMap = new HashMap<>();
47 failedValidations.put(moduleName, instanceToExMap);
49 if (instanceToExMap.containsKey(instanceName)) {
50 throw new IllegalArgumentException(
51 "Cannot merge with same module name "
52 + moduleName + " and instance name "
55 instanceToExMap.put(instanceName, ex);
59 return new ValidationException(failedValidations);
62 public static ValidationException createForSingleException(
63 ModuleIdentifier moduleIdentifier, Exception e) {
64 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
65 Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
67 failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
68 innerMap.put(moduleIdentifier.getInstanceName(),
69 new ExceptionMessageWithStackTrace(e));
70 return new ValidationException(failedValidations);
73 public Map<String, Map<String, ExceptionMessageWithStackTrace>> getFailedValidations() {
74 return failedValidations;
77 public static class ExceptionMessageWithStackTrace {
78 private String message, stackTrace;
80 public ExceptionMessageWithStackTrace() {
83 public ExceptionMessageWithStackTrace(String message, String stackTrace) {
84 this.message = message;
85 this.stackTrace = stackTrace;
88 public ExceptionMessageWithStackTrace(Exception e) {
89 this(e.getMessage(), Arrays.toString(e.getStackTrace()));
92 public String getMessage() {
96 public String getTrace() {
100 public void setMessage(String message) {
101 this.message = message;
104 public void setTrace(String stackTrace) {
105 this.stackTrace = stackTrace;
109 public int hashCode() {
110 final int prime = 31;
112 result = prime * result
113 + ((message == null) ? 0 : message.hashCode());
114 result = prime * result
115 + ((stackTrace == null) ? 0 : stackTrace.hashCode());
120 public boolean equals(Object obj) {
125 if (getClass() != obj.getClass())
127 ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
128 if (message == null) {
129 if (other.message != null)
131 } else if (!message.equals(other.message))
133 if (stackTrace == null) {
134 if (other.stackTrace != null)
136 } else if (!stackTrace.equals(other.stackTrace))
142 public String toString() {
143 return "ExceptionMessageWithStackTrace [message=" + message
144 + ", stackTrace=" + stackTrace + "]";