Fix checkstyle violations caused by checkstyle 6.1.1
[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.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;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.NoSuchElementException;
21 import java.util.Set;
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;
34
35 final class YangStoreSnapshot implements YangStoreContext {
36     private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
37
38
39     private final Map<String /* Namespace from yang file */,
40         Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
41
42
43     private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
44
45     private final SchemaContext schemaContext;
46
47     public YangStoreSnapshot(final SchemaContext resolveSchemaContext) {
48         LOG.trace("Resolved modules:{}", resolveSchemaContext.getModules());
49         this.schemaContext = resolveSchemaContext;
50         // JMX generator
51
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());
65                 } else {
66                     throw new IllegalStateException("Cannot add two SIE with same qname "
67                             + sieEntry.getValue());
68                 }
69             }
70         }
71
72         Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
73
74         Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
75
76
77         for (Module module : schemaContext.getModules()) {
78             String packageName = packageTranslator.getPackageName(module);
79             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
80                     new TypeProviderImpl(resolveSchemaContext));
81
82             QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
83
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);
88
89             qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
90         }
91         this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
92         this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
93
94     }
95
96     @Override
97     public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
98         return moduleMXBeanEntryMap;
99     }
100
101     @Override
102     public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
103         return qNamesToIdentitiesToModuleMXBeanEntries;
104     }
105
106     @Override
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());
111         }
112         return modules;
113     }
114
115     @Override
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();
120         } else {
121             try {
122                 return Iterables.find(getModules(), new Predicate<Module>() {
123                     @Override
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);
127                     }
128                 }).getSource();
129             } catch (final NoSuchElementException e) {
130                 throw new IllegalArgumentException("Source for yang module " + moduleIdentifier + " not found", e);
131             }
132         }
133     }
134
135     @Override
136     public boolean equals(final Object o) {
137         if (this == o) return true;
138         if (o == null || getClass() != o.getClass()) return false;
139
140         final YangStoreSnapshot that = (YangStoreSnapshot) o;
141
142         if (schemaContext != null ? !schemaContext.equals(that.schemaContext) : that.schemaContext != null)
143             return false;
144
145         return true;
146     }
147
148     @Override
149     public int hashCode() {
150         return schemaContext != null ? schemaContext.hashCode() : 0;
151     }
152 }