Merge "Fix for Bug 3"
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / MbeParser.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 package org.opendaylight.controller.config.yang.store.impl;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Maps;
12 import org.apache.commons.io.IOUtils;
13 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
14 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
15 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
16 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
17 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
18 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 public class MbeParser {
33
34     public YangStoreSnapshotImpl parseYangFiles(
35             Collection<? extends InputStream> allInput)
36             throws YangStoreException {
37         YangParserImpl parser = YangParserWrapper.getYangParserInstance();
38
39         Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
40
41         SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
42
43         // JMX generator
44
45         Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
46         PackageTranslator packageTranslator = new PackageTranslator(
47                 namespaceToPackageMapping);
48
49         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
50
51         Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
52         // create SIE structure qNamesToSIEs
53         for (Module module : resolveSchemaContext.getModules()) {
54             String packageName = packageTranslator.getPackageName(module);
55             Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
56                     .create(module, packageName,knownSEITracker);
57
58             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
59                     .entrySet()) {
60
61                 // merge value into qNamesToSIEs
62                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
63                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
64                 } else {
65                     throw new IllegalStateException(
66                             "Cannot add two SIE with same qname "
67                                     + sieEntry.getValue());
68                 }
69             }
70         }
71
72         Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
73         Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
74
75         for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
76             String packageName = packageTranslator.getPackageName(moduleEntry
77                     .getValue());
78             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
79                     new TypeProviderImpl(resolveSchemaContext));
80             String yangAsString;
81             try {
82                 moduleEntry.getKey().reset();
83                 yangAsString = IOUtils.toString(moduleEntry.getKey());
84             } catch (IOException e) {
85                 throw new IllegalStateException(e);
86             }
87             modulesMap.put(moduleEntry.getValue().getName(),
88                     Maps.immutableEntry(moduleEntry.getValue(), yangAsString));
89             Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
90                     .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper,
91                             packageName);
92             retVal.put(moduleEntry.getValue().getNamespace().toString(),
93                     namesToMBEs);
94         }
95
96         return new YangStoreSnapshotImpl(retVal, modulesMap);
97     }
98
99     public Map<Module, String> parseYangFilesToString(
100             Collection<? extends InputStream> allYangs) {
101         YangParserImpl parser = YangParserWrapper.getYangParserInstance();
102
103         Map<InputStream, Module> allYangModules = parser
104                 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
105         Map<Module, String> retVal = new HashMap<>();
106
107         for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
108             try {
109                 retVal.put(entry.getValue(), IOUtils.toString(entry.getKey()));
110             } catch (IOException e) {
111                 throw new IllegalStateException(
112                         "Can not create string from yang file.");
113             }
114         }
115         return retVal;
116     }
117
118 }