BUG-7159: eliminate use of CrossStatementSourceReactor
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / md / cluster / datastore / model / TestModel.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 package org.opendaylight.controller.md.cluster.datastore.model;
9
10 import com.google.common.io.Resources;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.Collections;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class TestModel {
30
31     public static final QName TEST_QNAME =
32             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13", "test");
33
34     public static final QName TEST2_QNAME =
35             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13", "test2");
36
37     public static final QName JUNK_QNAME =
38             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:junk", "2014-03-13", "junk");
39
40
41     public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
42     public static final QName OUTER_CONTAINER_QNAME = QName.create(TEST_QNAME, "outer-container");
43     public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
44     public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
45     public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
46     public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
47     public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
48     private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
49
50     public static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_QNAME);
51     public static final YangInstanceIdentifier TEST2_PATH = YangInstanceIdentifier.of(TEST2_QNAME);
52     public static final YangInstanceIdentifier JUNK_PATH = YangInstanceIdentifier.of(JUNK_QNAME);
53     public static final YangInstanceIdentifier OUTER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH)
54             .node(OUTER_LIST_QNAME).build();
55     public static final YangInstanceIdentifier INNER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH)
56             .node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).build();
57     public static final YangInstanceIdentifier OUTER_CONTAINER_PATH = TEST_PATH.node(OUTER_CONTAINER_QNAME);
58     public static final QName TWO_QNAME = QName.create(TEST_QNAME,"two");
59     public static final QName THREE_QNAME = QName.create(TEST_QNAME,"three");
60
61     public static SchemaContext createTestContext() {
62         final List<InputStream> sources;
63
64         try {
65             sources = Collections.singletonList(
66                 Resources.asByteSource(TestModel.class.getResource(DATASTORE_TEST_YANG)).openStream());
67         } catch (IOException e1) {
68             throw new ExceptionInInitializerError(e1);
69         }
70
71         try {
72             return YangParserTestUtils.parseYangStreams(sources);
73         }  catch (ReactorException e) {
74             throw new RuntimeException("Unable to build schema context from " + sources, e);
75         }
76     }
77
78     public static DataContainerChild<?, ?> outerNode(final int... ids) {
79         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME);
80         for (int id: ids) {
81             outer.addChild(ImmutableNodes.mapEntry(OUTER_LIST_QNAME, ID_QNAME, id));
82         }
83
84         return outer.build();
85     }
86
87     public static DataContainerChild<?, ?> outerNode(final MapEntryNode... entries) {
88         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME);
89         for (MapEntryNode e: entries) {
90             outer.addChild(e);
91         }
92
93         return outer.build();
94     }
95
96     public static DataContainerChild<?, ?> innerNode(final String... names) {
97         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(INNER_LIST_QNAME);
98         for (String name: names) {
99             outer.addChild(ImmutableNodes.mapEntry(INNER_LIST_QNAME, NAME_QNAME, name));
100         }
101
102         return outer.build();
103     }
104
105     public static MapEntryNode outerNodeEntry(final int id, final DataContainerChild<?, ?> inner) {
106         return ImmutableNodes.mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, id).addChild(inner).build();
107     }
108
109     public static NormalizedNode<?, ?> testNodeWithOuter(final int... ids) {
110         return testNodeWithOuter(outerNode(ids));
111     }
112
113     public static NormalizedNode<?, ?> testNodeWithOuter(final DataContainerChild<?, ?> outer) {
114         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
115                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outer).build();
116     }
117
118     public static NodeIdentifierWithPredicates outerEntryKey(final int id) {
119         return new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, id);
120     }
121
122     public static YangInstanceIdentifier outerEntryPath(final int id) {
123         return OUTER_LIST_PATH.node(outerEntryKey(id));
124     }
125
126     public static NodeIdentifierWithPredicates innerEntryKey(final String name) {
127         return new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, name);
128     }
129
130     public static YangInstanceIdentifier innerEntryPath(final int id, final String name) {
131         return OUTER_LIST_PATH.node(outerEntryKey(id)).node(INNER_LIST_QNAME).node(innerEntryKey(name));
132     }
133 }

©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.