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;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Collection;
28 import java.util.HashMap;
30 import java.util.Map.Entry;
32 public class MbeParser {
34 public YangStoreSnapshotImpl parseYangFiles(
35 Collection<? extends InputStream> allInput)
36 throws YangStoreException {
37 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
39 Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
41 SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
45 Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
46 PackageTranslator packageTranslator = new PackageTranslator(
47 namespaceToPackageMapping);
49 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
51 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
52 // create SIE structure qNamesToSIEs
53 for (Module module : resolveSchemaContext.getModules()) {
54 String packageName = packageTranslator.getPackageName(module);
55 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
56 .create(module, packageName,knownSEITracker);
58 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
61 // merge value into qNamesToSIEs
62 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
63 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
65 throw new IllegalStateException(
66 "Cannot add two SIE with same qname "
67 + sieEntry.getValue());
72 Map<String, Map<String, ModuleMXBeanEntry>> retVal = Maps.newHashMap();
73 Map<String, Entry<Module, String>> modulesMap = new HashMap<>();
75 for (Entry<InputStream, Module> moduleEntry : allYangModules.entrySet()) {
76 String packageName = packageTranslator.getPackageName(moduleEntry
78 TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
79 new TypeProviderImpl(resolveSchemaContext));
82 moduleEntry.getKey().reset();
83 yangAsString = IOUtils.toString(moduleEntry.getKey());
84 } catch (IOException e) {
85 throw new IllegalStateException(e);
87 modulesMap.put(moduleEntry.getValue().getName(),
88 Maps.immutableEntry(moduleEntry.getValue(), yangAsString));
89 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
90 .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper,
92 retVal.put(moduleEntry.getValue().getNamespace().toString(),
96 return new YangStoreSnapshotImpl(retVal, modulesMap);
99 public Map<Module, String> parseYangFilesToString(
100 Collection<? extends InputStream> allYangs) {
101 YangParserImpl parser = YangParserWrapper.getYangParserInstance();
103 Map<InputStream, Module> allYangModules = parser
104 .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
105 Map<Module, String> retVal = new HashMap<>();
107 for (Entry<InputStream, Module> entry : allYangModules.entrySet()) {
109 retVal.put(entry.getValue(), IOUtils.toString(entry.getKey()));
110 } catch (IOException e) {
111 throw new IllegalStateException(
112 "Can not create string from yang file.");