2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.md.cluster.datastore.model;
11 import com.google.common.io.ByteSource;
12 import com.google.common.io.Resources;
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;
25 public class SchemaContextHelper {
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";
31 public static InputStream getInputStream(final String yangFileName) {
32 return SchemaContextHelper.class.getResourceAsStream(yangFileName);
35 public static SchemaContext full(){
36 return select(ODL_DATASTORE_TEST_YANG, PEOPLE_YANG, CARS_YANG);
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<>();
44 for(String schemaFile : schemaFiles){
45 streams.add(getInputStream(schemaFile));
49 schemaContext = reactor.buildEffective(streams);
50 } catch (ReactorException e) {
51 throw new RuntimeException("Unable to build schema context from " + streams, e);
57 public static SchemaContext entityOwners() {
58 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59 final SchemaContext schemaContext;
63 file = new File("src/main/yang/entity-owners.yang");
64 final List<ByteSource> sources = Arrays.asList(Resources.asByteSource(file.toURI().toURL()));
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);
73 } catch (MalformedURLException e3) {
74 throw new RuntimeException("Malformed URL detected in " + file, e3);