Get rid of old Yang Parser in Controller
[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.io.ByteSource;
12 import com.google.common.io.Resources;
13 import java.io.File;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.MalformedURLException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
24
25 public class SchemaContextHelper {
26
27     public static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
28     public static final String PEOPLE_YANG = "/people.yang";
29     public static final String CARS_YANG = "/cars.yang";
30
31     public static InputStream getInputStream(final String yangFileName) {
32         return SchemaContextHelper.class.getResourceAsStream(yangFileName);
33     }
34
35     public static SchemaContext full(){
36         return select(ODL_DATASTORE_TEST_YANG, PEOPLE_YANG, CARS_YANG);
37     }
38
39     public static SchemaContext select(String... schemaFiles){
40         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41         final SchemaContext schemaContext;
42         List<InputStream> streams = new ArrayList<>();
43
44         for(String schemaFile : schemaFiles){
45             streams.add(getInputStream(schemaFile));
46         }
47
48         try {
49             schemaContext = reactor.buildEffective(streams);
50         } catch (ReactorException e) {
51             throw new RuntimeException("Unable to build schema context from " + streams, e);
52         }
53
54         return schemaContext;
55     }
56
57     public static SchemaContext entityOwners() {
58         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59         final SchemaContext schemaContext;
60         File file = null;
61
62         try {
63             file = new File("src/main/yang/entity-owners.yang");
64             final List<ByteSource> sources = Arrays.asList(Resources.asByteSource(file.toURI().toURL()));
65             try {
66                 schemaContext = reactor.buildEffective(sources);
67             } catch (IOException e1) {
68                 throw new ExceptionInInitializerError(e1);
69             } catch (ReactorException e2) {
70                 throw new RuntimeException("Unable to build schema context from " + sources, e2);
71             }
72             return schemaContext;
73         } catch (MalformedURLException e3) {
74             throw new RuntimeException("Malformed URL detected in " + file, e3);
75         }
76     }
77 }