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