Add filtering capability to config.ini in order to reference logging bridge version.
[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 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 public class MbeParser {
36     private static final Logger logger = LoggerFactory.getLogger(MbeParser.class);
37
38     public YangStoreSnapshotImpl parseYangFiles(Collection<? extends InputStream> allInput) throws YangStoreException {
39         YangParserImpl parser = YangParserWrapper.getYangParserInstance();
40
41         Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
42
43         SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
44         logger.trace("Resolved modules:{}", resolveSchemaContext.getModules());
45         // JMX generator
46
47         Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
48         PackageTranslator packageTranslator = new PackageTranslator(
49                 namespaceToPackageMapping);
50
51         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
52
53         Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
54         // create SIE structure qNamesToSIEs
55         for (Module module : resolveSchemaContext.getModules()) {
56             String packageName = packageTranslator.getPackageName(module);
57             Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
58                     .create(module, packageName,knownSEITracker);
59
60             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
61                     .entrySet()) {
62
63                 // merge value into qNamesToSIEs
64                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
65                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
66                 } else {
67                     throw new IllegalStateException(
68                             "Cannot add two SIE with same qname "
69                                     + sieEntry.getValue());
70                 }
71             }
72         }
73
74         Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
75         Map<Module, String> modulesToSources = new HashMap<>();
76         Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>>
77                 qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
78
79
80         for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
81             Module module = moduleEntry.getValue();
82             String packageName = packageTranslator.getPackageName(module);
83             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
84                     new TypeProviderImpl(resolveSchemaContext));
85             String yangAsString = reReadInputStream(moduleEntry);
86
87             QName qName = new QName(module.getNamespace(), module.getRevision(), module.getName());
88
89             Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
90                     Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, resolveSchemaContext,
91                             typeProviderWrapper, packageName));
92             moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
93             modulesToSources.put(module, yangAsString);
94             qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
95         }
96
97         return new YangStoreSnapshotImpl(moduleMXBeanEntryMap, modulesToSources, qNamesToIdentitiesToModuleMXBeanEntries);
98     }
99
100     private String reReadInputStream(Entry<InputStream, Module> moduleEntry) {
101         String yangAsString;
102         try {
103             moduleEntry.getKey().reset();
104             yangAsString = IOUtils.toString(moduleEntry.getKey());
105         } catch (IOException e) {
106             throw new IllegalStateException("Cannot reread " + moduleEntry.getValue(), e);
107         }
108         return yangAsString;
109     }
110
111     @Deprecated
112     public Map<Module, String> parseYangFilesToString(Collection<? extends InputStream> allYangs) {
113
114         logger.error("Using deprecated method that will be removed soon", new UnsupportedOperationException("Deprecated"));
115         YangParserImpl parser = YangParserWrapper.getYangParserInstance();
116
117         Map<InputStream, Module> allYangModules = parser
118                 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
119         Map<Module, String> retVal = new HashMap<>();
120
121         for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
122             try {
123                 retVal.put(entry.getValue(), IOUtils.toString(entry.getKey()));
124             } catch (IOException e) {
125                 throw new IllegalStateException(
126                         "Can not create string from yang file.");
127             }
128         }
129         return retVal;
130     }
131
132 }