Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SchemaResolutionException.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.model.repo.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMultimap;
15 import com.google.common.collect.Multimap;
16 import java.util.Collection;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
19
20 /**
21  * Exception thrown when a Schema Source fails to resolve.
22  */
23 @Beta
24 public class SchemaResolutionException extends SchemaSourceException {
25     private static final long serialVersionUID = 1L;
26
27     private final SourceIdentifier failedSource;
28     private final @NonNull ImmutableMultimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
29     private final @NonNull ImmutableList<SourceIdentifier> resolvedSources;
30
31     public SchemaResolutionException(final @NonNull String message) {
32         this(message, null);
33     }
34
35     public SchemaResolutionException(final @NonNull String message, final Throwable cause) {
36         this(message, null, cause, ImmutableList.of(), ImmutableMultimap.of());
37     }
38
39     public SchemaResolutionException(final @NonNull String message, final SourceIdentifier failedSource,
40             final Throwable cause) {
41         this(message, failedSource, cause, ImmutableList.of(), ImmutableMultimap.of());
42     }
43
44     public SchemaResolutionException(final @NonNull String message,
45             final @NonNull Collection<SourceIdentifier> resolvedSources,
46             final @NonNull Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
47         this(message, null, null, resolvedSources, unsatisfiedImports);
48     }
49
50     public SchemaResolutionException(final @NonNull String message, final SourceIdentifier failedSource,
51             final Throwable cause, final @NonNull Collection<SourceIdentifier> resolvedSources,
52             final @NonNull Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
53         super(formatMessage(message, failedSource, resolvedSources, unsatisfiedImports), cause);
54         this.failedSource = failedSource;
55         this.unsatisfiedImports = ImmutableMultimap.copyOf(unsatisfiedImports);
56         this.resolvedSources = ImmutableList.copyOf(resolvedSources);
57     }
58
59     private static String formatMessage(final String message, final SourceIdentifier failedSource,
60             final Collection<SourceIdentifier> resolvedSources,
61             final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
62         return String.format("%s, failed source: %s, resolved sources: %s, unsatisfied imports: %s", message,
63                 failedSource, resolvedSources, unsatisfiedImports);
64     }
65
66     /**
67      * Return YANG schema source identifier consisting of name and revision of the module which caused this exception.
68      *
69      * @return YANG schema source identifier
70      */
71     public final SourceIdentifier getFailedSource() {
72         return this.failedSource;
73     }
74
75     /**
76      * Return the list of sources which failed to resolve along with reasons why they were not resolved.
77      *
78      * @return Source/reason map.
79      */
80     public final @NonNull Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
81         return unsatisfiedImports;
82     }
83
84     // FIXME: should be leak actual mapping?
85     public final @NonNull Collection<SourceIdentifier> getResolvedSources() {
86         return resolvedSources;
87     }
88
89     @Override
90     public final String toString() {
91         return addToStringAttributes(MoreObjects.toStringHelper(this).add("unsatisfiedImports", unsatisfiedImports))
92             .toString();
93     }
94
95     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
96         return toStringHelper;
97     }
98 }