2 * Copyright (c) 2015 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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.BiMap;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import com.google.common.io.ByteStreams;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.lang.ref.SoftReference;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.Objects;
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.yangtools.sal.binding.generator.util.BindingRuntimeContext;
33 import org.opendaylight.yangtools.sal.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.SchemaSourceException;
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 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>> qNamesToIdentitiesToModuleMXBeanEntries;
51 MXBeans(final SchemaContext schemaContext) {
52 LOG.trace("Resolved modules:{}", schemaContext.getModules());
55 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
56 PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
57 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
58 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
59 // create SIE structure qNamesToSIEs
60 for (Module module : schemaContext.getModules()) {
61 String packageName = packageTranslator.getPackageName(module);
62 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
63 .create(module, packageName, knownSEITracker);
64 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
65 // merge value into qNamesToSIEs
66 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
67 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
69 throw new IllegalStateException("Cannot add two SIE with same qname "
70 + sieEntry.getValue());
75 Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
77 Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
80 for (Module module : schemaContext.getModules()) {
81 String packageName = packageTranslator.getPackageName(module);
82 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
83 new TypeProviderImpl(schemaContext));
85 QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
87 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
88 Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, schemaContext,
89 typeProviderWrapper, packageName));
90 moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
92 qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
94 this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
95 this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
99 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
100 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
101 private final BindingRuntimeContext bindingContextProvider;
104 * We want to lazily compute the context of the MXBean class and have it only softly-attached to this instance,
105 * so it can be garbage collected when the memory gets tight. If the schema context changes as we are computing
106 * things, YangStoreService will detect that and retry, so we do not have to worry about that.
108 private volatile SoftReference<MXBeans> ref = new SoftReference<>(null);
110 public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
111 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
112 this.bindingContextProvider = Preconditions.checkNotNull(bindingContextProvider);
113 this.sourceProvider = Preconditions.checkNotNull(sourceProvider);
116 private MXBeans getMXBeans() {
117 MXBeans mxBean = ref.get();
119 if (mxBean == null) {
120 synchronized (this) {
122 if (mxBean == null) {
123 mxBean = new MXBeans(bindingContextProvider.getSchemaContext());
124 ref = new SoftReference<>(mxBean);
133 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
134 return getMXBeans().moduleMXBeanEntryMap;
138 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
139 return getMXBeans().qNamesToIdentitiesToModuleMXBeanEntries;
143 public Set<Module> getModules() {
144 final Set<Module> modules = Sets.newHashSet(bindingContextProvider.getSchemaContext().getModules());
145 for (final Module module : bindingContextProvider.getSchemaContext().getModules()) {
146 modules.addAll(module.getSubmodules());
152 public String getModuleSource(final org.opendaylight.yangtools.yang.model.api.ModuleIdentifier moduleIdentifier) {
153 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source = sourceProvider.getSource(
154 SourceIdentifier.create(moduleIdentifier.getName(), Optional.fromNullable(
155 QName.formattedRevision(moduleIdentifier.getRevision()))));
158 final YangTextSchemaSource yangTextSchemaSource = source.checkedGet();
159 try(InputStream inStream = yangTextSchemaSource.openStream()) {
160 return new String(ByteStreams.toByteArray(inStream), StandardCharsets.UTF_8);
162 } catch (SchemaSourceException | 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(bindingContextProvider, other.bindingContextProvider);
187 public int hashCode() {
188 return Objects.hashCode(bindingContextProvider);
192 public String fromYang(final String enumClass, final String enumYangValue) {
193 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
194 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
195 final String javaName = enumMapping.get(enumYangValue);
196 return Preconditions.checkNotNull(javaName, "Unable to resolve enum value %s for enum class %s with assumed enum mapping: %s", enumYangValue, enumClass, enumMapping);
200 public String toYang(final String enumClass, final String enumJavaValue) {
201 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
202 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
203 final String javaName = enumMapping.inverse().get(enumJavaValue);
204 return Preconditions.checkNotNull(javaName,
205 "Unable to map enum value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass,
206 enumMapping.inverse());