2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.osgi;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Sets;
15 import com.google.common.io.ByteStreams;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.lang.ref.SoftReference;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import java.util.Objects;
27 import java.util.concurrent.ExecutionException;
28 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
29 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
30 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
31 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
32 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
33 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
46 private static final class MXBeans {
47 private final Map<String /* Namespace from yang file */,
48 Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
49 private final Map<QName, Map<String, ModuleMXBeanEntry>> namesToIdentitiesToModuleMXBeanEntries;
51 MXBeans(final SchemaContext schemaContext) {
52 LOG.trace("Resolved modules:{}", schemaContext.getModules());
55 final Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
56 final PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
57 final Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
58 final Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
59 // create SIE structure qNamesToSIEs
60 for (final Module module : schemaContext.getModules()) {
61 final String packageName = packageTranslator.getPackageName(module);
62 final Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
63 .create(module, packageName, knownSEITracker);
64 for (final Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
65 // merge value into qNamesToSIEs
66 if (!qNamesToSIEs.containsKey(sieEntry.getKey())) {
67 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
69 throw new IllegalStateException("Cannot add two SIE with same qname "
70 + sieEntry.getValue());
75 final Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntries = Maps.newHashMap();
77 final Map<QName, Map<String /* identity local name */,
78 ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
81 for (final Module module : schemaContext.getModules()) {
82 final String packageName = packageTranslator.getPackageName(module);
83 final TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
84 new TypeProviderImpl(schemaContext));
86 final QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
88 final Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
89 Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, schemaContext,
90 typeProviderWrapper, packageName));
91 moduleMXBeanEntries.put(module.getNamespace().toString(), namesToMBEs);
93 qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
95 this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntries);
96 this.namesToIdentitiesToModuleMXBeanEntries =
97 Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
101 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
102 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
103 private final BindingRuntimeContext bindingContextProvider;
106 * We want to lazily compute the context of the MXBean class and have it only softly-attached to this instance,
107 * so it can be garbage collected when the memory gets tight. If the schema context changes as we are computing
108 * things, YangStoreService will detect that and retry, so we do not have to worry about that.
110 private volatile SoftReference<MXBeans> ref = new SoftReference<>(null);
112 public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
113 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
114 this.bindingContextProvider = Preconditions.checkNotNull(bindingContextProvider);
115 this.sourceProvider = Preconditions.checkNotNull(sourceProvider);
118 private MXBeans getMXBeans() {
119 MXBeans mxBean = this.ref.get();
121 if (mxBean == null) {
122 synchronized (this) {
123 mxBean = this.ref.get();
124 if (mxBean == null) {
125 mxBean = new MXBeans(this.bindingContextProvider.getSchemaContext());
126 this.ref = new SoftReference<>(mxBean);
135 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
136 return getMXBeans().moduleMXBeanEntryMap;
140 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
141 return getMXBeans().namesToIdentitiesToModuleMXBeanEntries;
145 public Set<Module> getModules() {
146 final Set<Module> modules = Sets.newHashSet(this.bindingContextProvider.getSchemaContext().getModules());
147 for (final Module module : this.bindingContextProvider.getSchemaContext().getModules()) {
148 modules.addAll(module.getSubmodules());
154 public String getModuleSource(final SourceIdentifier moduleIdentifier) {
155 final ListenableFuture<? extends YangTextSchemaSource> source = this.sourceProvider.getSource(
156 RevisionSourceIdentifier.create(moduleIdentifier.getName(), moduleIdentifier.getRevision()));
158 final YangTextSchemaSource yangTextSchemaSource = source.get();
159 try (InputStream inStream = yangTextSchemaSource.openStream()) {
160 return new String(ByteStreams.toByteArray(inStream), StandardCharsets.UTF_8);
162 } catch (ExecutionException | InterruptedException | IOException e) {
163 LOG.warn("Unable to provide source for {}", moduleIdentifier, e);
164 throw new IllegalArgumentException("Unable to provide source for " + moduleIdentifier, e);
169 public EnumResolver getEnumResolver() {
174 public boolean equals(final Object obj) {
178 if (!(obj instanceof YangStoreSnapshot)) {
182 final YangStoreSnapshot other = (YangStoreSnapshot) obj;
183 return Objects.equals(this.bindingContextProvider, other.bindingContextProvider);
187 public int hashCode() {
188 return Objects.hashCode(this.bindingContextProvider);
192 public String fromYang(final String enumClass, final String enumYangValue) {
193 Preconditions.checkState(this.bindingContextProvider != null, "Binding context provider was not set yet");
194 final BiMap<String, String> enumMapping = this.bindingContextProvider.getEnumMapping(enumClass);
195 final String javaName = enumMapping.get(enumYangValue);
196 return Preconditions.checkNotNull(javaName,
197 "Unable to resolve enum value %s for enum class %s with assumed enum mapping: %s", enumYangValue,
198 enumClass, enumMapping);
202 public String toYang(final String enumClass, final String enumJavaValue) {
203 Preconditions.checkState(this.bindingContextProvider != null, "Binding context provider was not set yet");
204 final BiMap<String, String> enumMapping = this.bindingContextProvider.getEnumMapping(enumClass);
205 final String javaName = enumMapping.inverse().get(enumJavaValue);
206 return Preconditions.checkNotNull(javaName,
207 "Unable to map enum value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass,
208 enumMapping.inverse());