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 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;
18 import java.util.Map.Entry;
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;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Maps;
35 import com.google.common.collect.Sets;
37 public class MbeParser {
39 public YangStoreSnapshot parseYangFiles(
40 Collection<? extends InputStream> allInput)
41 throws YangStoreException {
42 YangParserImpl parser = new YangParserImpl();
44 List<InputStream> bufferedInputStreams = new ArrayList<>();
45 for (InputStream is : allInput) {
48 content = IOUtils.toString(is);
49 } catch (IOException e) {
50 throw new YangStoreException("Can not get yang as String from "
53 InputStream buf = new ByteArrayInputStream(content.getBytes());
54 bufferedInputStreams.add(buf);
57 Map<InputStream, Module> allYangModules = parser
58 .parseYangModelsFromStreamsMapped(bufferedInputStreams);
60 SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets
61 .newHashSet(allYangModules.values()));
65 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
66 PackageTranslator packageTranslator = new PackageTranslator(
67 namespaceToPackageMapping);
69 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
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);
77 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
80 // merge value into qNamesToSIEs
81 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
82 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
84 throw new IllegalStateException(
85 "Cannot add two SIE with same qname "
86 + sieEntry.getValue());
91 Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
92 Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
94 for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
95 String packageName = packageTranslator.getPackageName(moduleEntry
97 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
98 new TypeProviderImpl(resolveSchemaContext));
101 moduleEntry.getKey().reset();
102 yangAsString = IOUtils.toString(moduleEntry.getKey());
103 } catch (IOException e) {
104 throw new IllegalStateException(e);
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,
111 retVal.put(moduleEntry.getValue().getNamespace().toString(),
115 return new YangStoreSnapshotImpl(retVal, modulesMap);
118 public Map<Module, String> parseYangFilesToString(
119 Collection<? extends InputStream> allYangs) {
120 YangParserImpl parser = new YangParserImpl();
122 Map<InputStream, Module> allYangModules = parser
123 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
124 Map<Module, String> retVal = new HashMap<>();
126 for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
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.");