2 * Copyright (c) 2013, 2017 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, it
19 * aggregates validation exceptions and sends them back to the user. Use
20 * {@link org.opendaylight.controller.config.api.JmxAttributeValidationException}
21 * for validating modules instead.
23 public class ValidationException extends Exception {
24 private static final long serialVersionUID = -6072893219820274247L;
26 private final Map<String/* module name */,
27 Map<String/* instance name */,
28 ExceptionMessageWithStackTrace>> failedValidations;
30 public ValidationException(
31 final Map<String /* module name */,
32 Map<String /* instance name */,
33 ExceptionMessageWithStackTrace>> failedValidations) {
34 super(failedValidations.toString());
35 this.failedValidations = Collections.unmodifiableMap(failedValidations);
38 public static ValidationException createFromCollectedValidationExceptions(
39 final List<ValidationException> collectedExceptions) {
40 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
41 for (ValidationException ve : collectedExceptions) {
42 for (Entry<String, Map<String, ExceptionMessageWithStackTrace>> outerEntry : ve.getFailedValidations()
44 for (Entry<String, ExceptionMessageWithStackTrace> innerEntry : outerEntry.getValue().entrySet()) {
45 String moduleName = outerEntry.getKey();
46 String instanceName = innerEntry.getKey();
47 ExceptionMessageWithStackTrace ex = innerEntry.getValue();
48 Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
49 .computeIfAbsent(moduleName, k -> new HashMap<>());
50 if (instanceToExMap.containsKey(instanceName)) {
51 throw new IllegalArgumentException("Cannot merge with same module name " + moduleName
52 + " and instance name " + instanceName);
54 instanceToExMap.put(instanceName, ex);
58 return new ValidationException(failedValidations);
61 public static ValidationException createForSingleException(final ModuleIdentifier moduleIdentifier,
62 final Exception exception) {
63 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
64 Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
66 failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
67 innerMap.put(moduleIdentifier.getInstanceName(), new ExceptionMessageWithStackTrace(exception));
68 return new ValidationException(failedValidations);
71 public Map<String/* module name */,
72 Map<String/* instance name */,
73 ExceptionMessageWithStackTrace>> getFailedValidations() {
74 return failedValidations;
77 public static class ExceptionMessageWithStackTrace {
78 private String message;
79 private String stackTrace;
81 public ExceptionMessageWithStackTrace() {
84 public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
85 this.message = message;
86 this.stackTrace = stackTrace;
89 public ExceptionMessageWithStackTrace(final Exception exception) {
90 this(exception.getMessage(), Arrays.toString(exception.getStackTrace()));
93 public String getMessage() {
97 public String getTrace() {
101 public void setMessage(final String message) {
102 this.message = message;
105 public void setTrace(final String stackTrace) {
106 this.stackTrace = stackTrace;
110 public int hashCode() {
111 final int prime = 31;
113 result = prime * result + (message == null ? 0 : message.hashCode());
114 result = prime * result + (stackTrace == null ? 0 : stackTrace.hashCode());
119 public boolean equals(final Object obj) {
126 if (getClass() != obj.getClass()) {
129 ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
130 if (message == null) {
131 if (other.message != null) {
134 } else if (!message.equals(other.message)) {
137 if (stackTrace == null) {
138 if (other.stackTrace != null) {
141 } else if (!stackTrace.equals(other.stackTrace)) {
148 public String toString() {