94298c1bb1bae829a7640a8899e3207d7baf22f3
[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.api.source.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
23 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
25
26 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
27     private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
28     private final ImmutableMap<String, ClassLoader> classLoaders;
29     private final @NonNull EffectiveModelContext modelContext;
30
31     DefaultModuleInfoSnapshot(final EffectiveModelContext modelContext,
32             final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
33         this.modelContext = requireNonNull(modelContext);
34         this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
35         this.classLoaders = ImmutableMap.copyOf(classLoaders);
36     }
37
38     @Override
39     public EffectiveModelContext modelContext() {
40         return modelContext;
41     }
42
43     @Override
44     public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceId) {
45         final var info = moduleInfos.get(sourceId);
46         return info == null
47             ? Futures.immediateFailedFuture(new MissingSchemaSourceException(sourceId, "No source registered"))
48                 : Futures.immediateFuture(new DelegatedYangTextSource(sourceId, info.getYangTextCharSource()));
49     }
50
51     @Override
52     public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
53         final var packageName = Naming.getModelRootPackageName(fullyQualifiedName);
54         final var loader = classLoaders.get(packageName);
55         if (loader == null) {
56             throw new ClassNotFoundException("Package " + packageName + " not found");
57         }
58         @SuppressWarnings("unchecked")
59         final var loaded = (Class<T>) loader.loadClass(fullyQualifiedName);
60         return loaded;
61     }
62 }