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