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.Sets;
11 import org.apache.commons.io.IOUtils;
12 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
15 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
16 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
26 public class YangParserWrapper {
29 * throw IllegalStateException if it is unable to parse yang files
31 public static SchemaContext parseYangFiles(Collection<? extends InputStream> yangFilesAsInputStreams) {
32 YangParserImpl parser = getYangParserInstance();
33 Map<InputStream, Module> mappedYangModules = null;
35 mappedYangModules = parseYangFiles(parser, yangFilesAsInputStreams);
36 } catch (YangStoreException e) {
37 throw new IllegalStateException("Unable to parse yang files", e);
39 return getSchemaContextFromModules(parser, mappedYangModules);
42 static YangParserImpl getYangParserInstance() {
43 return new YangParserImpl();
46 static SchemaContext getSchemaContextFromModules(YangModelParser parser, Map<InputStream, Module> allYangModules) {
47 return parser.resolveSchemaContext(Sets
48 .newHashSet(allYangModules.values()));
51 static Map<InputStream, Module> parseYangFiles(YangModelParser parser, Collection<? extends InputStream> allInput) throws YangStoreException {
52 List<InputStream> bufferedInputStreams = new ArrayList<>();
53 for (InputStream is : allInput) {
56 content = IOUtils.toString(is);
57 } catch (IOException e) {
58 throw new YangStoreException("Can not get yang as String from "
61 InputStream buf = new ByteArrayInputStream(content.getBytes());
62 bufferedInputStreams.add(buf);
66 .parseYangModelsFromStreamsMapped(bufferedInputStreams);