Introduce yangtools.binding.meta
[yangtools.git] / binding / binding-runtime-spi / src / main / java / org / opendaylight / yangtools / binding / runtime / spi / DefaultModuleInfoSnapshot.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.binding.runtime.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.binding.contract.Naming;
16 import org.opendaylight.yangtools.binding.meta.YangModuleInfo;
17 import org.opendaylight.yangtools.binding.runtime.api.ModuleInfoSnapshot;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
21 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
23
24 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
25     private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
26     private final ImmutableMap<String, ClassLoader> classLoaders;
27     private final @NonNull EffectiveModelContext modelContext;
28
29     DefaultModuleInfoSnapshot(final EffectiveModelContext modelContext,
30             final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
31         this.modelContext = requireNonNull(modelContext);
32         this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
33         this.classLoaders = ImmutableMap.copyOf(classLoaders);
34     }
35
36     @Override
37     public EffectiveModelContext modelContext() {
38         return modelContext;
39     }
40
41     @Override
42     public YangTextSource yangTextSource(final SourceIdentifier sourceId) {
43         final var info = moduleInfos.get(sourceId);
44         return info == null ? null : new DelegatedYangTextSource(sourceId, info.getYangTextCharSource());
45     }
46
47     @Override
48     public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException {
49         final var source = yangTextSource(sourceId);
50         if (source == null) {
51             throw new MissingSchemaSourceException(sourceId, "No source registered");
52         }
53         return source;
54     }
55
56     @Override
57     public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
58         final var packageName = Naming.getModelRootPackageName(fullyQualifiedName);
59         final var loader = classLoaders.get(packageName);
60         if (loader == null) {
61             throw new ClassNotFoundException("Package " + packageName + " not found");
62         }
63         @SuppressWarnings("unchecked")
64         final var loaded = (Class<T>) loader.loadClass(fullyQualifiedName);
65         return loaded;
66     }
67 }