a26b0fec1d06acf943f792b6cb21e9118eaf2a30
[yangtools.git] / yang / 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.repo.api.SchemaSourceRepresentation;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
22 import org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver;
23
24 final class ContextHolder implements Immutable, ModuleResourceResolver {
25     private final EffectiveModelContext context;
26     private final Set<Module> modules;
27     private final Set<SourceIdentifier> sources;
28
29     ContextHolder(final EffectiveModelContext context, final Set<Module> modules, final Set<SourceIdentifier> sources) {
30         this.context = requireNonNull(context);
31         this.modules = ImmutableSet.copyOf(modules);
32         this.sources = ImmutableSet.copyOf(sources);
33     }
34
35     @Override
36     public Optional<String> findModuleResourcePath(final Module module,
37             final Class<? extends SchemaSourceRepresentation> representation) {
38         checkArgument(YangTextSchemaSource.class.equals(requireNonNull(representation)),
39             "Unsupported representation %s", representation);
40         final SourceIdentifier id = Util.moduleToIdentifier(module);
41         return sources.contains(id)
42                 ? Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/" + id.toYangFilename())
43                         : Optional.empty();
44     }
45
46     EffectiveModelContext getContext() {
47         return context;
48     }
49
50     Set<Module> getYangModules() {
51         return modules;
52     }
53 }