Eliminate YangModelDependencyInfo
[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.source.SourceDependency;
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     @java.io.Serial
27     private static final long serialVersionUID = 2L;
28
29     private final @NonNull ImmutableMultimap<SourceIdentifier, SourceDependency> unsatisfiedImports;
30     private final @NonNull ImmutableList<SourceIdentifier> resolvedSources;
31
32     public SchemaResolutionException(final @NonNull String message, final SourceIdentifier failedSource,
33             final Throwable cause) {
34         this(message, failedSource, cause, ImmutableList.of(), ImmutableMultimap.of());
35     }
36
37     public SchemaResolutionException(final @NonNull String message, final SourceIdentifier failedSource,
38             final @NonNull Collection<SourceIdentifier> resolvedSources,
39             final @NonNull Multimap<SourceIdentifier, SourceDependency> unsatisfiedImports) {
40         this(message, failedSource, null, resolvedSources, unsatisfiedImports);
41     }
42
43     public SchemaResolutionException(final @NonNull String message, final SourceIdentifier failedSource,
44             final Throwable cause, final @NonNull Collection<SourceIdentifier> resolvedSources,
45             final @NonNull Multimap<SourceIdentifier, SourceDependency> unsatisfiedImports) {
46         super(failedSource, formatMessage(message, failedSource, resolvedSources, unsatisfiedImports), cause);
47         this.unsatisfiedImports = ImmutableMultimap.copyOf(unsatisfiedImports);
48         this.resolvedSources = ImmutableList.copyOf(resolvedSources);
49     }
50
51     private static String formatMessage(final String message, final SourceIdentifier failedSource,
52             final Collection<?> resolvedSources, final Multimap<?, ?> unsatisfiedImports) {
53         return String.format("%s, failed source: %s, resolved sources: %s, unsatisfied imports: %s", message,
54                 failedSource, resolvedSources, unsatisfiedImports);
55     }
56
57     /**
58      * Return the list of sources which failed to resolve along with reasons why they were not resolved.
59      *
60      * @return Source/reason map.
61      */
62     public final @NonNull Multimap<SourceIdentifier, SourceDependency> getUnsatisfiedImports() {
63         return unsatisfiedImports;
64     }
65
66     // FIXME: should be leak actual mapping?
67     public final @NonNull Collection<SourceIdentifier> getResolvedSources() {
68         return resolvedSources;
69     }
70
71     @Override
72     public final String toString() {
73         return addToStringAttributes(MoreObjects.toStringHelper(this).add("unsatisfiedImports", unsatisfiedImports))
74             .toString();
75     }
76
77     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
78         return toStringHelper;
79     }
80 }