Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / api / ValidationException.java
1 /*
2  * Copyright (c) 2013, 2017 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, 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.
22  */
23 public class ValidationException extends Exception {
24     private static final long serialVersionUID = -6072893219820274247L;
25
26     private final Map<String/* module name */,
27         Map<String/* instance name */,
28         ExceptionMessageWithStackTrace>> failedValidations;
29
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);
36     }
37
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()
43                     .entrySet()) {
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);
53                     }
54                     instanceToExMap.put(instanceName, ex);
55                 }
56             }
57         }
58         return new ValidationException(failedValidations);
59     }
60
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<>();
65
66         failedValidations.put(moduleIdentifier.getFactoryName(), innerMap);
67         innerMap.put(moduleIdentifier.getInstanceName(), new ExceptionMessageWithStackTrace(exception));
68         return new ValidationException(failedValidations);
69     }
70
71     public Map<String/* module name */,
72         Map<String/* instance name */,
73         ExceptionMessageWithStackTrace>> getFailedValidations() {
74         return failedValidations;
75     }
76
77     public static class ExceptionMessageWithStackTrace {
78         private String message;
79         private String stackTrace;
80
81         public ExceptionMessageWithStackTrace() {
82         }
83
84         public ExceptionMessageWithStackTrace(final String message, final String stackTrace) {
85             this.message = message;
86             this.stackTrace = stackTrace;
87         }
88
89         public ExceptionMessageWithStackTrace(final Exception exception) {
90             this(exception.getMessage(), Arrays.toString(exception.getStackTrace()));
91         }
92
93         public String getMessage() {
94             return message;
95         }
96
97         public String getTrace() {
98             return stackTrace;
99         }
100
101         public void setMessage(final String message) {
102             this.message = message;
103         }
104
105         public void setStackTrace(final String trace) {
106             this.stackTrace = trace;
107         }
108
109         @Override
110         public int hashCode() {
111             final int prime = 31;
112             int result = 1;
113             result = prime * result + (message == null ? 0 : message.hashCode());
114             result = prime * result + (stackTrace == null ? 0 : stackTrace.hashCode());
115             return result;
116         }
117
118         @Override
119         public boolean equals(final Object obj) {
120             if (this == obj) {
121                 return true;
122             }
123             if (obj == null) {
124                 return false;
125             }
126             if (getClass() != obj.getClass()) {
127                 return false;
128             }
129             ExceptionMessageWithStackTrace other = (ExceptionMessageWithStackTrace) obj;
130             if (message == null) {
131                 if (other.message != null) {
132                     return false;
133                 }
134             } else if (!message.equals(other.message)) {
135                 return false;
136             }
137             if (stackTrace == null) {
138                 if (other.stackTrace != null) {
139                     return false;
140                 }
141             } else if (!stackTrace.equals(other.stackTrace)) {
142                 return false;
143             }
144             return true;
145         }
146
147         @Override
148         public String toString() {
149             return message;
150         }
151     }
152 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.