BUG-2138: Create DistributedShardFrontend
[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 VALUE_QNAME = QName.create(TEST_QNAME, "value");
48     public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
49     private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
50
51     public static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_QNAME);
52     public static final YangInstanceIdentifier TEST2_PATH = YangInstanceIdentifier.of(TEST2_QNAME);
53     public static final YangInstanceIdentifier JUNK_PATH = YangInstanceIdentifier.of(JUNK_QNAME);
54     public static final YangInstanceIdentifier OUTER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH)
55             .node(OUTER_LIST_QNAME).build();
56     public static final YangInstanceIdentifier INNER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH)
57             .node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).build();
58     public static final YangInstanceIdentifier OUTER_CONTAINER_PATH = TEST_PATH.node(OUTER_CONTAINER_QNAME);
59     public static final QName TWO_QNAME = QName.create(TEST_QNAME,"two");
60     public static final QName THREE_QNAME = QName.create(TEST_QNAME,"three");
61
62     public static SchemaContext createTestContext() {
63         final List<InputStream> sources;
64
65         try {
66             sources = Collections.singletonList(
67                 Resources.asByteSource(TestModel.class.getResource(DATASTORE_TEST_YANG)).openStream());
68         } catch (IOException e1) {
69             throw new ExceptionInInitializerError(e1);
70         }
71
72         try {
73             return YangParserTestUtils.parseYangStreams(sources);
74         }  catch (ReactorException e) {
75             throw new RuntimeException("Unable to build schema context from " + sources, e);
76         }
77     }
78
79     public static DataContainerChild<?, ?> outerNode(final int... ids) {
80         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME);
81         for (int id: ids) {
82             outer.addChild(ImmutableNodes.mapEntry(OUTER_LIST_QNAME, ID_QNAME, id));
83         }
84
85         return outer.build();
86     }
87
88     public static DataContainerChild<?, ?> outerNode(final MapEntryNode... entries) {
89         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME);
90         for (MapEntryNode e: entries) {
91             outer.addChild(e);
92         }
93
94         return outer.build();
95     }
96
97     public static DataContainerChild<?, ?> innerNode(final String... names) {
98         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(INNER_LIST_QNAME);
99         for (String name: names) {
100             outer.addChild(ImmutableNodes.mapEntry(INNER_LIST_QNAME, NAME_QNAME, name));
101         }
102
103         return outer.build();
104     }
105
106     public static MapEntryNode outerNodeEntry(final int id, final DataContainerChild<?, ?> inner) {
107         return ImmutableNodes.mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, id).addChild(inner).build();
108     }
109
110     public static NormalizedNode<?, ?> testNodeWithOuter(final int... ids) {
111         return testNodeWithOuter(outerNode(ids));
112     }
113
114     public static NormalizedNode<?, ?> testNodeWithOuter(final DataContainerChild<?, ?> outer) {
115         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
116                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outer).build();
117     }
118
119     public static NodeIdentifierWithPredicates outerEntryKey(final int id) {
120         return new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, id);
121     }
122
123     public static YangInstanceIdentifier outerEntryPath(final int id) {
124         return OUTER_LIST_PATH.node(outerEntryKey(id));
125     }
126
127     public static NodeIdentifierWithPredicates innerEntryKey(final String name) {
128         return new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, name);
129     }
130
131     public static YangInstanceIdentifier innerEntryPath(final int id, final String name) {
132         return OUTER_LIST_PATH.node(outerEntryKey(id)).node(INNER_LIST_QNAME).node(innerEntryKey(name));
133     }
134 }

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