Merge "Add support for identity-ref config attributes to config/netconf subsystem"
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / MbeParser.java
index 211da6bfefdc62df10906eff27b30655e90eb790..dd07c5a84fa81976fe5dd97937d0a2c2afedaf0b 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.controller.config.yang.store.impl;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
 import org.apache.commons.io.IOUtils;
 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
@@ -18,46 +17,31 @@ import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry
 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
 public class MbeParser {
+    private static final Logger logger = LoggerFactory.getLogger(MbeParser.class);
 
-    public YangStoreSnapshotImpl parseYangFiles(
-            Collection<? extends InputStream> allInput)
-            throws YangStoreException {
-        YangParserImpl parser = new YangParserImpl();
+    public YangStoreSnapshotImpl parseYangFiles(Collection<? extends InputStream> allInput) throws YangStoreException {
+        YangParserImpl parser = YangParserWrapper.getYangParserInstance();
 
-        List<InputStream> bufferedInputStreams = new ArrayList<>();
-        for (InputStream is : allInput) {
-            String content;
-            try {
-                content = IOUtils.toString(is);
-            } catch (IOException e) {
-                throw new YangStoreException("Can not get yang as String from "
-                        + is, e);
-            }
-            InputStream buf = new ByteArrayInputStream(content.getBytes());
-            bufferedInputStreams.add(buf);
-        }
-
-        Map<InputStream, Module> allYangModules = parser
-                .parseYangModelsFromStreamsMapped(bufferedInputStreams);
-
-        SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets
-                .newHashSet(allYangModules.values()));
+        Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
 
+        SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
+        logger.trace("Resolved modules:{}", resolveSchemaContext.getModules());
         // JMX generator
 
         Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
@@ -66,11 +50,12 @@ public class MbeParser {
 
         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
 
+        Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
         // create SIE structure qNamesToSIEs
         for (Module module : resolveSchemaContext.getModules()) {
             String packageName = packageTranslator.getPackageName(module);
             Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
-                    .create(module, packageName);
+                    .create(module, packageName,knownSEITracker);
 
             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
                     .entrySet()) {
@@ -86,36 +71,48 @@ public class MbeParser {
             }
         }
 
-        Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
-        Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
+        Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
+        Map<Module, String> modulesToSources = new HashMap<>();
+        Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>>
+                qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
+
 
         for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
-            String packageName = packageTranslator.getPackageName(moduleEntry
-                    .getValue());
+            Module module = moduleEntry.getValue();
+            String packageName = packageTranslator.getPackageName(module);
             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
                     new TypeProviderImpl(resolveSchemaContext));
-            String yangAsString;
-            try {
-                moduleEntry.getKey().reset();
-                yangAsString = IOUtils.toString(moduleEntry.getKey());
-            } catch (IOException e) {
-                throw new IllegalStateException(e);
-            }
-            modulesMap.put(moduleEntry.getValue().getName(),
-                    Maps.immutableEntry(moduleEntry.getValue(), yangAsString));
-            Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
-                    .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper,
-                            packageName);
-            retVal.put(moduleEntry.getValue().getNamespace().toString(),
-                    namesToMBEs);
+            String yangAsString = reReadInputStream(moduleEntry);
+
+            QName qName = new QName(module.getNamespace(), module.getRevision(), module.getName());
+
+            Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
+                    Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, resolveSchemaContext,
+                            typeProviderWrapper, packageName));
+            moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
+            modulesToSources.put(module, yangAsString);
+            qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
         }
 
-        return new YangStoreSnapshotImpl(retVal, modulesMap);
+        return new YangStoreSnapshotImpl(moduleMXBeanEntryMap, modulesToSources, qNamesToIdentitiesToModuleMXBeanEntries);
     }
 
-    public Map<Module, String> parseYangFilesToString(
-            Collection<? extends InputStream> allYangs) {
-        YangParserImpl parser = new YangParserImpl();
+    private String reReadInputStream(Entry<InputStream, Module> moduleEntry) {
+        String yangAsString;
+        try {
+            moduleEntry.getKey().reset();
+            yangAsString = IOUtils.toString(moduleEntry.getKey());
+        } catch (IOException e) {
+            throw new IllegalStateException("Cannot reread " + moduleEntry.getValue(), e);
+        }
+        return yangAsString;
+    }
+
+    @Deprecated
+    public Map<Module, String> parseYangFilesToString(Collection<? extends InputStream> allYangs) {
+
+        logger.error("Using deprecated method that will be removed soon", new UnsupportedOperationException("Deprecated"));
+        YangParserImpl parser = YangParserWrapper.getYangParserInstance();
 
         Map<InputStream, Module> allYangModules = parser
                 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));