f1c90920be8c5bcaa3dbceb44226b057547560df
[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                             .get(moduleName);
47                     if (instanceToExMap == null) {
48                         instanceToExMap = new HashMap<>();
49                         failedValidations.put(moduleName, instanceToExMap);
50                     }
51                     if (instanceToExMap.containsKey(instanceName)) {
52                         throw new IllegalArgumentException(
53                                 "Cannot merge with same module name "
54                                         + moduleName + " and instance name "
55                                         + instanceName);
56                     }
57                     instanceToExMap.put(instanceName, ex);
58                 }
59             }
60         }
61         return new ValidationException(failedValidations);
62     }
63
64     public static ValidationException createForSingleException(
65             final ModuleIdentifier moduleIdentifier, final Exception e) {
66         Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = new HashMap<>();
67         Map<String, ExceptionMessageWithStackTrace> innerMap = new HashMap<>();
68
69         failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
70         innerMap.put(moduleIdentifier.getInstanceName(),
71                 new ExceptionMessageWithStackTrace(e));
72         return new ValidationException(failedValidations);
73     }
74
75     public Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> getFailedValidations() {
76         return failedValidations;
77     }
78
79     public static class ExceptionMessageWithStackTrace {
80         private String message, stackTrace;
81
82         public ExceptionMessageWithStackTrace() {
83         }
84
85         public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
86             this.message = message;
87             this.stackTrace = stackTrace;
88         }
89
90         public ExceptionMessageWithStackTrace(final Exception e) {
91             this(e.getMessage(), Arrays.toString(e.getStackTrace()));
92         }
93
94         public String getMessage() {
95             return message;
96         }
97
98         public String getTrace() {
99             return stackTrace;
100         }
101
102         public void setMessage(final String message) {
103             this.message = message;
104         }
105
106         public void setTrace(final String stackTrace) {
107             this.stackTrace = stackTrace;
108         }
109
110         @Override
111         public int hashCode() {
112             final int prime = 31;
113             int result = 1;
114             result = prime * result
115                     + ((message == null) ? 0 : message.hashCode());
116             result = prime * result
117                     + ((stackTrace == null) ? 0 : stackTrace.hashCode());
118             return result;
119         }
120
121         @Override
122         public boolean equals(final Object obj) {
123             if (this == obj) {
124                 return true;
125             }
126             if (obj == null) {
127                 return false;
128             }
129             if (getClass() != obj.getClass()) {
130                 return false;
131             }
132             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
133             if (message == null) {
134                 if (other.message != null) {
135                     return false;
136                 }
137             } else if (!message.equals(other.message)) {
138                 return false;
139             }
140             if (stackTrace == null) {
141                 if (other.stackTrace != null) {
142                     return false;
143                 }
144             } else if (!stackTrace.equals(other.stackTrace)) {
145                 return false;
146             }
147             return true;
148         }
149
150         @Override
151         public String toString() {
152             return message;
153         }
154
155     }
156 }