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.util.Collections;
20 import java.util.HashMap;
22 import java.util.Map.Entry;
23 import java.util.Objects;
25 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
26 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
27 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
28 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
29 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
30 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
43 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
46 private final Map<String /* Namespace from yang file */,
47 Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
50 private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
52 private final BindingRuntimeContext bindingContextProvider;
53 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
55 public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
56 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
57 this.bindingContextProvider = bindingContextProvider;
58 this.sourceProvider = sourceProvider;
60 final SchemaContext schemaContext = bindingContextProvider.getSchemaContext();
61 LOG.trace("Resolved modules:{}", schemaContext.getModules());
64 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
65 PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
66 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
67 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
68 // create SIE structure qNamesToSIEs
69 for (Module module : schemaContext.getModules()) {
70 String packageName = packageTranslator.getPackageName(module);
71 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
72 .create(module, packageName, knownSEITracker);
73 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
74 // merge value into qNamesToSIEs
75 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
76 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
78 throw new IllegalStateException("Cannot add two SIE with same qname "
79 + sieEntry.getValue());
84 Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
86 Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
89 for (Module module : schemaContext.getModules()) {
90 String packageName = packageTranslator.getPackageName(module);
91 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
92 new TypeProviderImpl(schemaContext));
94 QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
96 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
97 Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, schemaContext,
98 typeProviderWrapper, packageName));
99 moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
101 qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
103 this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
104 this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
109 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
110 return moduleMXBeanEntryMap;
114 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
115 return qNamesToIdentitiesToModuleMXBeanEntries;
119 public Set<Module> getModules() {
120 final Set<Module> modules = Sets.newHashSet(bindingContextProvider.getSchemaContext().getModules());
121 for (final Module module : bindingContextProvider.getSchemaContext().getModules()) {
122 modules.addAll(module.getSubmodules());
128 public String getModuleSource(final org.opendaylight.yangtools.yang.model.api.ModuleIdentifier moduleIdentifier) {
129 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source = sourceProvider.getSource(
130 moduleIdentifier.getRevision() == null ?
131 new SourceIdentifier(moduleIdentifier.getName()) :
132 new SourceIdentifier(moduleIdentifier.getName(),
133 QName.formattedRevision(moduleIdentifier.getRevision())));
136 final YangTextSchemaSource yangTextSchemaSource = source.checkedGet();
137 return new String(ByteStreams.toByteArray(yangTextSchemaSource.openStream()), Charsets.UTF_8);
138 } catch (SchemaSourceException | IOException e) {
139 LOG.warn("Unable to provide source for {}", moduleIdentifier, e);
140 throw new IllegalArgumentException("Unable to provide source for " + moduleIdentifier, e);
145 public EnumResolver getEnumResolver() {
150 public boolean equals(final Object obj) {
154 if (!(obj instanceof YangStoreSnapshot)) {
158 final YangStoreSnapshot other = (YangStoreSnapshot) obj;
159 return Objects.equals(bindingContextProvider, other.bindingContextProvider);
163 public int hashCode() {
164 return Objects.hashCode(bindingContextProvider);
168 public String fromYang(final String enumClass, final String enumYangValue) {
169 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
170 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
171 final String javaName = enumMapping.get(enumYangValue);
172 return Preconditions.checkNotNull(javaName, "Unable to resolve enum value %s for enum class %s with assumed enum mapping: %s", enumYangValue, enumClass, enumMapping);
176 public String toYang(final String enumClass, final String enumJavaValue) {
177 Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
178 final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
179 final String javaName = enumMapping.inverse().get(enumJavaValue);
180 return Preconditions.checkNotNull(javaName,
181 "Unable to map enum value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass,
182 enumMapping.inverse());