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 / 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.ByteSource;
11 import com.google.common.io.Resources;
12 import java.io.IOException;
13 import java.util.Collections;
14 import java.util.Set;
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.parser.stmt.reactor.CrossSourceStatementReactor;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
29
30 public class TestModel {
31
32     public static final QName TEST_QNAME =
33             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13", "test");
34
35     public static final QName TEST2_QNAME =
36             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13", "test2");
37
38     public static final QName JUNK_QNAME =
39             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:junk", "2014-03-13", "junk");
40
41
42     public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
43     public static final QName OUTER_CONTAINER_QNAME = QName.create(TEST_QNAME, "outer-container");
44     public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
45     public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
46     public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
47     public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
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 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
64         final SchemaContext schemaContext;
65         final Set<ByteSource> sources = Collections.singleton(Resources.asByteSource(TestModel.class.getResource
66                 (DATASTORE_TEST_YANG)));
67
68         try {
69             schemaContext = reactor.buildEffective(sources);
70         } catch (IOException e1) {
71             throw new ExceptionInInitializerError(e1);
72         }  catch (ReactorException e2) {
73             throw new RuntimeException("Unable to build schema context from " + sources, e2);
74         }
75         return schemaContext;
76     }
77
78     public static DataContainerChild<?, ?> outerNode(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(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(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(int id, DataContainerChild<?, ?> inner) {
106         return ImmutableNodes.mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, id).addChild(inner).build();
107     }
108
109     public static NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
110         return testNodeWithOuter(outerNode(ids));
111     }
112
113     public static NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
114         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
115                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outer).build();
116     }
117
118     public static NodeIdentifierWithPredicates outerEntryKey(int id) {
119         return new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, id);
120     }
121
122     public static YangInstanceIdentifier outerEntryPath(int id) {
123         return OUTER_LIST_PATH.node(outerEntryKey(id));
124     }
125
126     public static NodeIdentifierWithPredicates innerEntryKey(String name) {
127         return new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, name);
128     }
129
130     public static YangInstanceIdentifier innerEntryPath(int id, 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.