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