Modify config-api exceptions, bump config and netconf to 0.2.5-SNAPSHOT.
[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             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             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             ModuleIdentifier moduleIdentifier, 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(String message, String stackTrace) {
86             this.message = message;
87             this.stackTrace = stackTrace;
88         }
89
90         public ExceptionMessageWithStackTrace(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(String message) {
103             this.message = message;
104         }
105
106         public void setTrace(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(Object obj) {
123             if (this == obj)
124                 return true;
125             if (obj == null)
126                 return false;
127             if (getClass() != obj.getClass())
128                 return false;
129             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
130             if (message == null) {
131                 if (other.message != null)
132                     return false;
133             } else if (!message.equals(other.message))
134                 return false;
135             if (stackTrace == null) {
136                 if (other.stackTrace != null)
137                     return false;
138             } else if (!stackTrace.equals(other.stackTrace))
139                 return false;
140             return true;
141         }
142
143         @Override
144         public String toString() {
145             return message;
146         }
147
148     }
149 }