Merge "Startup arch - remove artifactId prefix from dir names."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / YangStoreSnapshot.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
11 import com.google.common.collect.Maps;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
19 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
20 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
21 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 final class YangStoreSnapshot implements YangStoreContext {
30     private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
31
32
33     private final Map<String /* Namespace from yang file */,
34     Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
35
36
37     private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
38
39     private final SchemaContext schemaContext;
40
41     public YangStoreSnapshot(final SchemaContext resolveSchemaContext) {
42         LOG.trace("Resolved modules:{}", resolveSchemaContext.getModules());
43         this.schemaContext = resolveSchemaContext;
44         // JMX generator
45
46         Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
47         PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
48         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
49         Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
50         // create SIE structure qNamesToSIEs
51         for (Module module : resolveSchemaContext.getModules()) {
52             String packageName = packageTranslator.getPackageName(module);
53             Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
54                     .create(module, packageName, knownSEITracker);
55             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
56                 // merge value into qNamesToSIEs
57                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
58                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
59                 } else {
60                     throw new IllegalStateException("Cannot add two SIE with same qname "
61                             + sieEntry.getValue());
62                 }
63             }
64         }
65
66         Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
67
68         Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
69
70
71         for (Module module : schemaContext.getModules()) {
72             String packageName = packageTranslator.getPackageName(module);
73             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
74                     new TypeProviderImpl(resolveSchemaContext));
75
76             QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
77
78             Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
79                     Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, resolveSchemaContext,
80                             typeProviderWrapper, packageName));
81             moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
82
83             qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
84         }
85         this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
86         this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
87
88     }
89
90     @Override
91     public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
92         return moduleMXBeanEntryMap;
93     }
94
95     @Override
96     public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
97         return qNamesToIdentitiesToModuleMXBeanEntries;
98     }
99
100     @Override
101     public Set<Module> getModules() {
102         return schemaContext.getModules();
103     }
104
105     @Override
106     public String getModuleSource(final org.opendaylight.yangtools.yang.model.api.ModuleIdentifier moduleIdentifier) {
107         return schemaContext.getModuleSource(moduleIdentifier).get();
108     }
109
110     @Override
111     public boolean equals(final Object o) {
112         if (this == o) return true;
113         if (o == null || getClass() != o.getClass()) return false;
114
115         final YangStoreSnapshot that = (YangStoreSnapshot) o;
116
117         if (schemaContext != null ? !schemaContext.equals(that.schemaContext) : that.schemaContext != null)
118             return false;
119
120         return true;
121     }
122
123     @Override
124     public int hashCode() {
125         return schemaContext != null ? schemaContext.hashCode() : 0;
126     }
127 }