Merge "Adding another constructor to ContainerFlowConfig to take dlVlan. Being an...
[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 com.google.common.collect.Sets;
13 import org.apache.commons.io.IOUtils;
14 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
15 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
16 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
17 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
19 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
20 import org.opendaylight.yangtools.yang.common.QName;
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.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 public class MbeParser {
36
37     public YangStoreSnapshotImpl parseYangFiles(
38             Collection<? extends InputStream> allInput)
39             throws YangStoreException {
40         YangParserImpl parser = new YangParserImpl();
41
42         List<InputStream> bufferedInputStreams = new ArrayList<>();
43         for (InputStream is : allInput) {
44             String content;
45             try {
46                 content = IOUtils.toString(is);
47             } catch (IOException e) {
48                 throw new YangStoreException("Can not get yang as String from "
49                         + is, e);
50             }
51             InputStream buf = new ByteArrayInputStream(content.getBytes());
52             bufferedInputStreams.add(buf);
53         }
54
55         Map<InputStream, Module> allYangModules = parser
56                 .parseYangModelsFromStreamsMapped(bufferedInputStreams);
57
58         SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets
59                 .newHashSet(allYangModules.values()));
60
61         // JMX generator
62
63         Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
64         PackageTranslator packageTranslator = new PackageTranslator(
65                 namespaceToPackageMapping);
66
67         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
68
69         // create SIE structure qNamesToSIEs
70         for (Module module : resolveSchemaContext.getModules()) {
71             String packageName = packageTranslator.getPackageName(module);
72             Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
73                     .create(module, packageName);
74
75             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
76                     .entrySet()) {
77
78                 // merge value into qNamesToSIEs
79                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
80                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
81                 } else {
82                     throw new IllegalStateException(
83                             "Cannot add two SIE with same qname "
84                                     + sieEntry.getValue());
85                 }
86             }
87         }
88
89         Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
90         Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
91
92         for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
93             String packageName = packageTranslator.getPackageName(moduleEntry
94                     .getValue());
95             TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
96                     new TypeProviderImpl(resolveSchemaContext));
97             String yangAsString;
98             try {
99                 moduleEntry.getKey().reset();
100                 yangAsString = IOUtils.toString(moduleEntry.getKey());
101             } catch (IOException e) {
102                 throw new IllegalStateException(e);
103             }
104             modulesMap.put(moduleEntry.getValue().getName(),
105                     Maps.immutableEntry(moduleEntry.getValue(), yangAsString));
106             Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
107                     .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper,
108                             packageName);
109             retVal.put(moduleEntry.getValue().getNamespace().toString(),
110                     namesToMBEs);
111         }
112
113         return new YangStoreSnapshotImpl(retVal, modulesMap);
114     }
115
116     public Map<Module, String> parseYangFilesToString(
117             Collection<? extends InputStream> allYangs) {
118         YangParserImpl parser = new YangParserImpl();
119
120         Map<InputStream, Module> allYangModules = parser
121                 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
122         Map<Module, String> retVal = new HashMap<>();
123
124         for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
125             try {
126                 retVal.put(entry.getValue(), IOUtils.toString(entry.getKey()));
127             } catch (IOException e) {
128                 throw new IllegalStateException(
129                         "Can not create string from yang file.");
130             }
131         }
132         return retVal;
133     }
134
135 }