Set odlparent-lite as parent for benchmark/pom.xml
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / osgi / YangStoreSnapshot.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.osgi;
10
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;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Objects;
24 import java.util.Set;
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;
41
42 final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
43     private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
44
45
46     private final Map<String /* Namespace from yang file */,
47         Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
48
49
50     private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
51
52     private final BindingRuntimeContext bindingContextProvider;
53     private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
54
55     public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
56         final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
57         this.bindingContextProvider = bindingContextProvider;
58         this.sourceProvider = sourceProvider;
59
60         final SchemaContext schemaContext = bindingContextProvider.getSchemaContext();
61         LOG.trace("Resolved modules:{}", schemaContext.getModules());
62
63         // JMX generator
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());
77                 } else {
78                     throw new IllegalStateException("Cannot add two SIE with same qname "
79                             + sieEntry.getValue());
80                 }
81             }
82         }
83
84         Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
85
86         Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
87
88
89         for (Module module : schemaContext.getModules()) {
90             String packageName = packageTranslator.getPackageName(module);
91             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
92                     new TypeProviderImpl(schemaContext));
93
94             QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
95
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);
100
101             qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
102         }
103         this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
104         this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
105
106     }
107
108     @Override
109     public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
110         return moduleMXBeanEntryMap;
111     }
112
113     @Override
114     public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
115         return qNamesToIdentitiesToModuleMXBeanEntries;
116     }
117
118     @Override
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());
123         }
124         return modules;
125     }
126
127     @Override
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())));
134
135         try {
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);
141         }
142     }
143
144     @Override
145     public EnumResolver getEnumResolver() {
146         return this;
147     }
148
149     @Override
150     public boolean equals(final Object obj) {
151         if (this == obj) {
152             return true;
153         }
154         if (!(obj instanceof YangStoreSnapshot)) {
155             return false;
156         }
157
158         final YangStoreSnapshot other = (YangStoreSnapshot) obj;
159         return Objects.equals(bindingContextProvider, other.bindingContextProvider);
160     }
161
162     @Override
163     public int hashCode() {
164         return Objects.hashCode(bindingContextProvider);
165     }
166
167     @Override
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);
173     }
174
175     @Override
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());
183     }
184 }