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;
24 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
25 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
26 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
27 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
28 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
29 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
42 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
45 private final Map<String /* Namespace from yang file */,
46 Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
49 private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
51 private final SchemaContext schemaContext;
52 private final BindingRuntimeContext bindingContextProvider;
53 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
55 public YangStoreSnapshot(final SchemaContext resolveSchemaContext,
56 final BindingRuntimeContext bindingContextProvider,
57 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
58 this.bindingContextProvider = bindingContextProvider;
59 this.sourceProvider = sourceProvider;
60 LOG.trace("Resolved modules:{}", resolveSchemaContext.getModules());
61 this.schemaContext = resolveSchemaContext;
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 : resolveSchemaContext.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(resolveSchemaContext));
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, resolveSchemaContext,
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(schemaContext.getModules());
121 for (final Module module : schemaContext.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 o) {
151 if (this == o) return true;
152 if (o == null || getClass() != o.getClass()) return false;
154 final YangStoreSnapshot that = (YangStoreSnapshot) o;
156 if (schemaContext != null ? !schemaContext.equals(that.schemaContext) : that.schemaContext != null)
163 public int hashCode() {
164 return schemaContext != null ? schemaContext.hashCode() : 0;
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, "Unable to map enumcd .." +
181 "cd value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass, enumMapping.inverse());