eaca037a51fd0201f0f8c742d09abffbd1191250
[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 java.util.Collections;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
20
21 /**
22  * Exception thrown when a Schema Source fails to resolve.
23  */
24 @Beta
25 public class SchemaResolutionException extends SchemaSourceException {
26
27     private static final long serialVersionUID = 1L;
28     private final SourceIdentifier failedSource;
29     private final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
30     private final Collection<SourceIdentifier> resolvedSources;
31
32     public SchemaResolutionException(@Nonnull final String message) {
33         this(message, (Throwable)null);
34     }
35
36     public SchemaResolutionException(@Nonnull final String message, final Throwable cause) {
37         this(message, null, cause, Collections.emptySet(), ImmutableMultimap.of());
38     }
39
40     public SchemaResolutionException(@Nonnull final String message, final SourceIdentifier failedSource,
41             final Throwable cause) {
42         this(message, failedSource, cause, Collections.emptySet(), ImmutableMultimap.of());
43     }
44
45     public SchemaResolutionException(@Nonnull final String message, final Collection<SourceIdentifier> resolvedSources,
46             final @Nonnull Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
47         this(message, null, null, resolvedSources, unsatisfiedImports);
48     }
49
50     public SchemaResolutionException(@Nonnull final String message, final SourceIdentifier failedSource,
51             final Throwable cause, @Nonnull final Collection<SourceIdentifier> resolvedSources,
52             @Nonnull final 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
77      * why they were not resolved.
78      *
79      * @return Source/reason map.
80      */
81     public final Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
82         return unsatisfiedImports;
83     }
84
85
86     // FIXME: should be leak actual mapping?
87     public final Collection<SourceIdentifier> getResolvedSources() {
88         return resolvedSources;
89     }
90
91     @Override
92     public final String toString() {
93         return addToStringAttributes(MoreObjects.toStringHelper(this).add("unsatisfiedImports", unsatisfiedImports)).toString();
94     }
95
96     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
97         return toStringHelper;
98     }
99 }