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.Charsets;
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.util.Collections;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import java.util.Objects;
27 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
28 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
29 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
30 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
31 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
32 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
38 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
45 private static final class MXBeans {
46 private final Map<String /* Namespace from yang file */,
47 Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
48 private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
50 MXBeans(final SchemaContext schemaContext) {
51 LOG.trace("Resolved modules:{}", schemaContext.getModules());
54 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
55 PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
56 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
57 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
58 // create SIE structure qNamesToSIEs
59 for (Module module : schemaContext.getModules()) {
60 String packageName = packageTranslator.getPackageName(module);
61 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
62 .create(module, packageName, knownSEITracker);
63 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
64 // merge value into qNamesToSIEs
65 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
66 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
68 throw new IllegalStateException("Cannot add two SIE with same qname "
69 + sieEntry.getValue());
74 Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
76 Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
79 for (Module module : schemaContext.getModules()) {
80 String packageName = packageTranslator.getPackageName(module);
81 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
82 new TypeProviderImpl(schemaContext));
84 QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
86 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
87 Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, schemaContext,
88 typeProviderWrapper, packageName));
89 moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
91 qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
93 this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
94 this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
98 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
99 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
100 private final BindingRuntimeContext bindingContextProvider;
103 * We want to lazily compute the context of the MXBean class and have it only softly-attached to this instance,
104 * so it can be garbage collected when the memory gets tight. If the schema context changes as we are computing
105 * things, YangStoreService will detect that and retry, so we do not have to worry about that.
107 private volatile SoftReference<MXBeans> ref = new SoftReference<>(null);
109 public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
110 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
111 this.bindingContextProvider = Preconditions.checkNotNull(bindingContextProvider);
112 this.sourceProvider = Preconditions.checkNotNull(sourceProvider);
115 private MXBeans getMXBeans() {
116 MXBeans mxBean = ref.get();
118 if (mxBean == null) {
119 synchronized (this) {
121 if (mxBean == null) {
122 mxBean = new MXBeans(bindingContextProvider.getSchemaContext());
123 ref = new SoftReference<>(mxBean);
132 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
133 return getMXBeans().moduleMXBeanEntryMap;
137 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
138 return getMXBeans().qNamesToIdentitiesToModuleMXBeanEntries;
142 public Set<Module> getModules() {
143 final Set<Module> modules = Sets.newHashSet(bindingContextProvider.getSchemaContext().getModules());
144 for (final Module module : bindingContextProvider.getSchemaContext().getModules()) {
145 modules.addAll(module.getSubmodules());
151 public String getModuleSource(final org.opendaylight.yangtools.yang.model.api.ModuleIdentifier moduleIdentifier) {
152 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source = sourceProvider.getSource(
153 moduleIdentifier.getRevision() == null ?
154 new SourceIdentifier(moduleIdentifier.getName()) :
155 new SourceIdentifier(moduleIdentifier.getName(),
156 QName.formattedRevision(moduleIdentifier.getRevision())));
159 final YangTextSchemaSource yangTextSchemaSource = source.checkedGet();
160 try(InputStream inStream = yangTextSchemaSource.openStream()) {
161 return new String(ByteStreams.toByteArray(inStream), Charsets.UTF_8);
163 } catch (SchemaSourceException | IOException e) {
164 LOG.warn("Unable to provide source for {}", moduleIdentifier, e);
165 throw new IllegalArgumentException("Unable to provide source for " + moduleIdentifier, e);
170 public EnumResolver getEnumResolver() {
175 public boolean equals(final Object obj) {
179 if (!(obj instanceof YangStoreSnapshot)) {
183 final YangStoreSnapshot other = (YangStoreSnapshot) obj;
184 return Objects.equals(bindingContextProvider, other.bindingContextProvider);
188 public int hashCode() {
189 return Objects.hashCode(bindingContextProvider);
193 public String fromYang(final String enumClass, final String enumYangValue) {
194 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
195 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
196 final String javaName = enumMapping.get(enumYangValue);
197 return Preconditions.checkNotNull(javaName, "Unable to resolve enum value %s for enum class %s with assumed enum mapping: %s", enumYangValue, enumClass, enumMapping);
201 public String toYang(final String enumClass, final String enumJavaValue) {
202 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
203 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
204 final String javaName = enumMapping.inverse().get(enumJavaValue);
205 return Preconditions.checkNotNull(javaName,
206 "Unable to map enum value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass,
207 enumMapping.inverse());