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