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 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;
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;
33 import java.util.Map.Entry;
35 public class MbeParser {
36 private static final Logger logger = LoggerFactory.getLogger(MbeParser.class);
38 public YangStoreSnapshotImpl parseYangFiles(Collection<? extends InputStream> allInput) throws YangStoreException {
39 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
41 Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
43 SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
44 logger.trace("Resolved modules:{}", resolveSchemaContext.getModules());
47 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
48 PackageTranslator packageTranslator = new PackageTranslator(
49 namespaceToPackageMapping);
51 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
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);
60 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
63 // merge value into qNamesToSIEs
64 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
65 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
67 throw new IllegalStateException(
68 "Cannot add two SIE with same qname "
69 + sieEntry.getValue());
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<>();
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);
87 QName qName = new QName(module.getNamespace(), module.getRevision(), module.getName());
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);
97 return new YangStoreSnapshotImpl(moduleMXBeanEntryMap, modulesToSources, qNamesToIdentitiesToModuleMXBeanEntries);
100 private String reReadInputStream(Entry<InputStream, Module> moduleEntry) {
103 moduleEntry.getKey().reset();
104 yangAsString = IOUtils.toString(moduleEntry.getKey());
105 } catch (IOException e) {
106 throw new IllegalStateException("Cannot reread " + moduleEntry.getValue(), e);
112 public Map<Module, String> parseYangFilesToString(Collection<? extends InputStream> allYangs) {
114 logger.error("Using deprecated method that will be removed soon", new UnsupportedOperationException("Deprecated"));
115 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
117 Map<InputStream, Module> allYangModules = parser
118 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
119 Map<Module, String> retVal = new HashMap<>();
121 for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
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.");