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 final 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 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 "
53 instanceToExMap.put(instanceName, ex);
57 return new ValidationException(failedValidations);
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<>();
65 failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
66 innerMap.put(moduleIdentifier.getInstanceName(),
67 new ExceptionMessageWithStackTrace(e));
68 return new ValidationException(failedValidations);
71 public Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> getFailedValidations() {
72 return failedValidations;
75 public static class ExceptionMessageWithStackTrace {
76 private String message, stackTrace;
78 public ExceptionMessageWithStackTrace() {
81 public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
82 this.message = message;
83 this.stackTrace = stackTrace;
86 public ExceptionMessageWithStackTrace(final Exception e) {
87 this(e.getMessage(), Arrays.toString(e.getStackTrace()));
90 public String getMessage() {
94 public String getTrace() {
98 public void setMessage(final String message) {
99 this.message = message;
102 public void setTrace(final String stackTrace) {
103 this.stackTrace = stackTrace;
107 public int hashCode() {
108 final int prime = 31;
110 result = prime * result
111 + ((message == null) ? 0 : message.hashCode());
112 result = prime * result
113 + ((stackTrace == null) ? 0 : stackTrace.hashCode());
118 public boolean equals(final Object obj) {
125 if (getClass() != obj.getClass()) {
128 ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
129 if (message == null) {
130 if (other.message != null) {
133 } else if (!message.equals(other.message)) {
136 if (stackTrace == null) {
137 if (other.stackTrace != null) {
140 } else if (!stackTrace.equals(other.stackTrace)) {
147 public String toString() {