f27d12399528c38c144fc17ea0354c243365a1a6
[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  * itaggregates validation exceptions and sends them back to the user.
20  */
21 public class ValidationException extends RuntimeException {
22     private static final long serialVersionUID = -6072893219820274247L;
23
24     private final Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> failedValidations;
25
26     public ValidationException(
27             Map<String /* module name */, Map<String /* instance name */, ExceptionMessageWithStackTrace>> failedValidations) {
28         super(failedValidations.toString());
29         this.failedValidations = Collections.unmodifiableMap(failedValidations);
30     }
31
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
44                             .get(moduleName);
45                     if (instanceToExMap == null) {
46                         instanceToExMap = new HashMap<>();
47                         failedValidations.put(moduleName, instanceToExMap);
48                     }
49                     if (instanceToExMap.containsKey(instanceName)) {
50                         throw new IllegalArgumentException(
51                                 "Cannot merge with same module name "
52                                         + moduleName + " and instance name "
53                                         + instanceName);
54                     }
55                     instanceToExMap.put(instanceName, ex);
56                 }
57             }
58         }
59         return new ValidationException(failedValidations);
60     }
61
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<>();
66
67         failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
68         innerMap.put(moduleIdentifier.getInstanceName(),
69                 new ExceptionMessageWithStackTrace(e));
70         return new ValidationException(failedValidations);
71     }
72
73     public Map<String/* module name */, Map<String/* instance name */, ExceptionMessageWithStackTrace>> getFailedValidations() {
74         return failedValidations;
75     }
76
77     public static class ExceptionMessageWithStackTrace {
78         private String message, stackTrace;
79
80         public ExceptionMessageWithStackTrace() {
81         }
82
83         public ExceptionMessageWithStackTrace(String message, String stackTrace) {
84             this.message = message;
85             this.stackTrace = stackTrace;
86         }
87
88         public ExceptionMessageWithStackTrace(Exception e) {
89             this(e.getMessage(), Arrays.toString(e.getStackTrace()));
90         }
91
92         public String getMessage() {
93             return message;
94         }
95
96         public String getTrace() {
97             return stackTrace;
98         }
99
100         public void setMessage(String message) {
101             this.message = message;
102         }
103
104         public void setTrace(String stackTrace) {
105             this.stackTrace = stackTrace;
106         }
107
108         @Override
109         public int hashCode() {
110             final int prime = 31;
111             int result = 1;
112             result = prime * result
113                     + ((message == null) ? 0 : message.hashCode());
114             result = prime * result
115                     + ((stackTrace == null) ? 0 : stackTrace.hashCode());
116             return result;
117         }
118
119         @Override
120         public boolean equals(Object obj) {
121             if (this == obj)
122                 return true;
123             if (obj == null)
124                 return false;
125             if (getClass() != obj.getClass())
126                 return false;
127             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
128             if (message == null) {
129                 if (other.message != null)
130                     return false;
131             } else if (!message.equals(other.message))
132                 return false;
133             if (stackTrace == null) {
134                 if (other.stackTrace != null)
135                     return false;
136             } else if (!stackTrace.equals(other.stackTrace))
137                 return false;
138             return true;
139         }
140
141         @Override
142         public String toString() {
143             return message;
144         }
145
146     }
147 }