ffe1d99d9f03f2b6636eda0efb30802b92172275
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / ContextHolder.java
1 /*
2  * Copyright (c) 2013 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.yang2sources.plugin;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Optional;
15 import java.util.Set;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver;
24
25 final class ContextHolder implements Immutable, ModuleResourceResolver {
26     private final EffectiveModelContext context;
27     private final Set<Module> modules;
28     private final Set<SourceIdentifier> sources;
29
30     ContextHolder(final EffectiveModelContext context, final Set<Module> modules, final Set<SourceIdentifier> sources) {
31         this.context = requireNonNull(context);
32         this.modules = ImmutableSet.copyOf(modules);
33         this.sources = ImmutableSet.copyOf(sources);
34     }
35
36     @Override
37     public Optional<String> findModuleResourcePath(final ModuleLike module,
38             final Class<? extends SchemaSourceRepresentation> representation) {
39         checkArgument(YangTextSchemaSource.class.equals(requireNonNull(representation)),
40             "Unsupported representation %s", representation);
41         final SourceIdentifier id = Util.moduleToIdentifier(module);
42         return sources.contains(id)
43                 ? Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/" + id.toYangFilename())
44                         : Optional.empty();
45     }
46
47     EffectiveModelContext getContext() {
48         return context;
49     }
50
51     Set<Module> getYangModules() {
52         return modules;
53     }
54 }