2 * Copyright (c) 2013 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.netconf.confignetconfconnector.osgi;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import java.util.Collections;
17 import java.util.HashMap;
19 import java.util.Map.Entry;
20 import java.util.NoSuchElementException;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
23 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
24 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
25 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
26 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 final class YangStoreSnapshot implements YangStoreContext {
36 private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
39 private final Map<String /* Namespace from yang file */,
40 Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
43 private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
45 private final SchemaContext schemaContext;
47 public YangStoreSnapshot(final SchemaContext resolveSchemaContext) {
48 LOG.trace("Resolved modules:{}", resolveSchemaContext.getModules());
49 this.schemaContext = resolveSchemaContext;
52 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
53 PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
54 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
55 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
56 // create SIE structure qNamesToSIEs
57 for (Module module : resolveSchemaContext.getModules()) {
58 String packageName = packageTranslator.getPackageName(module);
59 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
60 .create(module, packageName, knownSEITracker);
61 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
62 // merge value into qNamesToSIEs
63 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
64 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
66 throw new IllegalStateException("Cannot add two SIE with same qname "
67 + sieEntry.getValue());
72 Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
74 Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
77 for (Module module : schemaContext.getModules()) {
78 String packageName = packageTranslator.getPackageName(module);
79 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
80 new TypeProviderImpl(resolveSchemaContext));
82 QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
84 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
85 Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, resolveSchemaContext,
86 typeProviderWrapper, packageName));
87 moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
89 qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
91 this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
92 this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
97 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
98 return moduleMXBeanEntryMap;
102 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
103 return qNamesToIdentitiesToModuleMXBeanEntries;
107 public Set<Module> getModules() {
108 final Set<Module> modules = Sets.newHashSet(schemaContext.getModules());
109 for (final Module module : schemaContext.getModules()) {
110 modules.addAll(module.getSubmodules());
116 public String getModuleSource(final org.opendaylight.yangtools.yang.model.api.ModuleIdentifier moduleIdentifier) {
117 final Optional<String> moduleSource = schemaContext.getModuleSource(moduleIdentifier);
118 if(moduleSource.isPresent()) {
119 return moduleSource.get();
122 return Iterables.find(getModules(), new Predicate<Module>() {
124 public boolean apply(final Module input) {
125 final ModuleIdentifierImpl id = new ModuleIdentifierImpl(input.getName(), Optional.fromNullable(input.getNamespace()), Optional.fromNullable(input.getRevision()));
126 return id.equals(moduleIdentifier);
129 } catch (final NoSuchElementException e) {
130 throw new IllegalArgumentException("Source for yang module " + moduleIdentifier + " not found", e);
136 public boolean equals(final Object o) {
137 if (this == o) return true;
138 if (o == null || getClass() != o.getClass()) return false;
140 final YangStoreSnapshot that = (YangStoreSnapshot) o;
142 if (schemaContext != null ? !schemaContext.equals(that.schemaContext) : that.schemaContext != null)
149 public int hashCode() {
150 return schemaContext != null ? schemaContext.hashCode() : 0;