7c428181aa5e63bacf1fcabf7875b98f739ce587
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / YangParserWrapper.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.store.impl;
9
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;
17
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;
24 import java.util.Map;
25
26 public class YangParserWrapper {
27
28     /**
29      * throw IllegalStateException if it is unable to parse yang files
30      */
31     public static SchemaContext parseYangFiles(Collection<? extends InputStream> yangFilesAsInputStreams) {
32         YangParserImpl parser = getYangParserInstance();
33         Map<InputStream, Module> mappedYangModules = null;
34         try {
35             mappedYangModules = parseYangFiles(parser, yangFilesAsInputStreams);
36         } catch (YangStoreException e) {
37             throw new IllegalStateException("Unable to parse yang files", e);
38         }
39         return getSchemaContextFromModules(parser, mappedYangModules);
40     }
41
42     static YangParserImpl getYangParserInstance() {
43         return new YangParserImpl();
44     }
45
46     static SchemaContext getSchemaContextFromModules(YangModelParser parser, Map<InputStream, Module> allYangModules) {
47         return parser.resolveSchemaContext(Sets
48                 .newHashSet(allYangModules.values()));
49     }
50
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) {
54             String content;
55             try {
56                 content = IOUtils.toString(is);
57             } catch (IOException e) {
58                 throw new YangStoreException("Can not get yang as String from "
59                         + is, e);
60             }
61             InputStream buf = new ByteArrayInputStream(content.getBytes());
62             bufferedInputStreams.add(buf);
63         }
64
65         return parser
66                 .parseYangModelsFromStreamsMapped(bufferedInputStreams);
67     }
68 }