4e906a4d157850b54e42cafeb2f5d181a3fda9bc
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / md / cluster / datastore / model / SchemaContextHelper.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.md.cluster.datastore.model;
10
11 import com.google.common.base.Throwables;
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class SchemaContextHelper {
24
25     public static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
26     public static final String PEOPLE_YANG = "/people.yang";
27     public static final String CARS_YANG = "/cars.yang";
28
29     public static InputStream getInputStream(final String yangFileName) {
30         return SchemaContextHelper.class.getResourceAsStream(yangFileName);
31     }
32
33     public static SchemaContext full() {
34         return select(ODL_DATASTORE_TEST_YANG, PEOPLE_YANG, CARS_YANG);
35     }
36
37     public static SchemaContext select(final String... schemaFiles) {
38         List<InputStream> streams = new ArrayList<>(schemaFiles.length);
39
40         for (String schemaFile : schemaFiles) {
41             streams.add(getInputStream(schemaFile));
42         }
43
44         try {
45             return YangParserTestUtils.parseYangStreams(streams);
46         } catch (ReactorException e) {
47             throw new RuntimeException("Unable to build schema context from " + streams, e);
48         }
49     }
50
51     public static SchemaContext distributedShardedDOMDataTreeSchemaContext() {
52         final List<InputStream> streams = new ArrayList<>();
53         try {
54             // we need prefix-shard-configuration and odl-datastore-test models
55             // for DistributedShardedDOMDataTree tests
56             streams.add(getInputStream(ODL_DATASTORE_TEST_YANG));
57             streams.add(new FileInputStream("src/main/yang/prefix-shard-configuration.yang"));
58             return YangParserTestUtils.parseYangStreams(streams);
59         } catch (FileNotFoundException | ReactorException e) {
60             throw new RuntimeException(e);
61         }
62     }
63
64     public static SchemaContext entityOwners() {
65         try {
66             return YangParserTestUtils.parseYangSources(new File("src/main/yang/entity-owners.yang"));
67         } catch (IOException | ReactorException e) {
68             throw Throwables.propagate(e);
69         }
70     }
71 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.