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