2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.store.impl;
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;
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;
33 import java.util.Map.Entry;
35 public class MbeParser {
37 public YangStoreSnapshotImpl parseYangFiles(
38 Collection<? extends InputStream> allInput)
39 throws YangStoreException {
40 YangParserImpl parser = new YangParserImpl();
42 List<InputStream> bufferedInputStreams = new ArrayList<>();
43 for (InputStream is : allInput) {
46 content = IOUtils.toString(is);
47 } catch (IOException e) {
48 throw new YangStoreException("Can not get yang as String from "
51 InputStream buf = new ByteArrayInputStream(content.getBytes());
52 bufferedInputStreams.add(buf);
55 Map<InputStream, Module> allYangModules = parser
56 .parseYangModelsFromStreamsMapped(bufferedInputStreams);
58 SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets
59 .newHashSet(allYangModules.values()));
63 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
64 PackageTranslator packageTranslator = new PackageTranslator(
65 namespaceToPackageMapping);
67 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
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);
75 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
78 // merge value into qNamesToSIEs
79 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
80 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
82 throw new IllegalStateException(
83 "Cannot add two SIE with same qname "
84 + sieEntry.getValue());
89 Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
90 Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
92 for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
93 String packageName = packageTranslator.getPackageName(moduleEntry
95 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
96 new TypeProviderImpl(resolveSchemaContext));
99 moduleEntry.getKey().reset();
100 yangAsString = IOUtils.toString(moduleEntry.getKey());
101 } catch (IOException e) {
102 throw new IllegalStateException(e);
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,
109 retVal.put(moduleEntry.getValue().getNamespace().toString(),
113 return new YangStoreSnapshotImpl(retVal, modulesMap);
116 public Map<Module, String> parseYangFilesToString(
117 Collection<? extends InputStream> allYangs) {
118 YangParserImpl parser = new YangParserImpl();
120 Map<InputStream, Module> allYangModules = parser
121 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
122 Map<Module, String> retVal = new HashMap<>();
124 for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
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.");