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.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.HashMap;
29 import java.util.Map.Entry;
31 public class MbeParser {
33 public YangStoreSnapshotImpl parseYangFiles(
34 Collection<? extends InputStream> allInput)
35 throws YangStoreException {
36 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
38 Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
40 SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
44 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
45 PackageTranslator packageTranslator = new PackageTranslator(
46 namespaceToPackageMapping);
48 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
50 // create SIE structure qNamesToSIEs
51 for (Module module : resolveSchemaContext.getModules()) {
52 String packageName = packageTranslator.getPackageName(module);
53 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
54 .create(module, packageName);
56 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
59 // merge value into qNamesToSIEs
60 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
61 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
63 throw new IllegalStateException(
64 "Cannot add two SIE with same qname "
65 + sieEntry.getValue());
70 Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
71 Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
73 for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
74 String packageName = packageTranslator.getPackageName(moduleEntry
76 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
77 new TypeProviderImpl(resolveSchemaContext));
80 moduleEntry.getKey().reset();
81 yangAsString = IOUtils.toString(moduleEntry.getKey());
82 } catch (IOException e) {
83 throw new IllegalStateException(e);
85 modulesMap.put(moduleEntry.getValue().getName(),
86 Maps.immutableEntry(moduleEntry.getValue(), yangAsString));
87 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
88 .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper,
90 retVal.put(moduleEntry.getValue().getNamespace().toString(),
94 return new YangStoreSnapshotImpl(retVal, modulesMap);
97 public Map<Module, String> parseYangFilesToString(
98 Collection<? extends InputStream> allYangs) {
99 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
101 Map<InputStream, Module> allYangModules = parser
102 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
103 Map<Module, String> retVal = new HashMap<>();
105 for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
107 retVal.put(entry.getValue(), IOUtils.toString(entry.getKey()));
108 } catch (IOException e) {
109 throw new IllegalStateException(
110 "Can not create string from yang file.");