1e7c3d3a1e5a2d0776f89f9020b90a3bbe7c90d9
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / YT892Test.java
1 /*
2  * Copyright (c) 2018 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.yangtools.yang.data.impl.leafref;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.Uint8;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 public class YT892Test {
31     private static final QName BGP = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp", "2018-08-14", "bgp");
32     private static final QName PEER_GROUPS = QName.create(BGP, "peer-groups");
33     private static final QName PEER_GROUP = QName.create(BGP, "peer-group");
34     private static final QName PEER_GROUP_NAME = QName.create(BGP, "peer-group-name");
35     private static final YangInstanceIdentifier BGP_ID = YangInstanceIdentifier.of(BGP);
36
37     private static final QName NETWORK_INSTANCES =
38             QName.create("urn:opendaylight:params:xml:ns:yang:test:network:instance", "2018-08-14",
39                 "network-instances");
40     private static final QName NETWORK_INSTANCE = QName.create(NETWORK_INSTANCES, "network-instance");
41     private static final QName NAME = QName.create(NETWORK_INSTANCES, "name");
42     private static final QName CONFIG = QName.create(NETWORK_INSTANCES, "config");
43     private static final QName PROTOCOLS = QName.create(NETWORK_INSTANCES, "protocols");
44     private static final QName PROTOCOL = QName.create(NETWORK_INSTANCES, "protocol");
45     private static final QName IDENTIFIER = QName.create(NETWORK_INSTANCES, "identifier");
46     private static final QName BGP_POLICY = QName.create("urn:opendaylight:params:xml:ns:yang:test:policy:types",
47         "2018-08-14", "BGP");
48     private static final QName TEST_BGP = QName.create("urn:opendaylight:params:xml:ns:yang:bgp:test:extensions",
49         "2018-08-14", "bgp");
50     private static final QName NEIGHBORS = QName.create(TEST_BGP, "neighbors");
51     private static final QName NEIGHBOR = QName.create(TEST_BGP, "neighbor");
52     private static final QName NEIGHBOR_ADDRESS = QName.create(TEST_BGP, "neighbor-address");
53     private static final QName TEST_CONFIG = QName.create(TEST_BGP, "config");
54     private static final QName TEST_PEER_GROUP = QName.create(TEST_BGP, "peer-group");
55     private static final QName AFI_SAFIS = QName.create(TEST_BGP, "afi-safis");
56     private static final QName AFI_SAFI = QName.create(TEST_BGP, "afi-safi");
57     private static final QName AFI_SAFI_NAME = QName.create(TEST_BGP, "afi-safi-name");
58     private static final QName IPV4_UNICAST = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp:types",
59         "2018-08-14", "IPV4-UNICAST");
60     private static final QName RECEIVE = QName.create(TEST_BGP, "receive");
61     private static final QName SEND_MAX = QName.create(TEST_BGP, "send-max");
62
63     private static final YangInstanceIdentifier NETWORK_INSTANCES_ID = YangInstanceIdentifier.of(NETWORK_INSTANCES);
64
65     private LeafRefContext leafRefContext;
66     private DataTree dataTree;
67
68     @Before
69     public void setup() {
70         final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yt892");
71         leafRefContext = LeafRefContext.create(schemaContext);
72         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
73     }
74
75     @Test
76     public void testWriteBgpNeighbour() throws Exception {
77         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
78         writeModification.write(BGP_ID, Builders.containerBuilder()
79             .withNodeIdentifier(new NodeIdentifier(BGP))
80             .withChild(Builders.containerBuilder()
81                 .withNodeIdentifier(new NodeIdentifier(PEER_GROUPS))
82                 .withChild(Builders.mapBuilder()
83                     .withNodeIdentifier(new NodeIdentifier(PEER_GROUP))
84                     .withChild(Builders.mapEntryBuilder()
85                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(PEER_GROUP,
86                             PEER_GROUP_NAME, "application-peers"))
87                         .withChild(ImmutableNodes.leafNode(PEER_GROUP_NAME, "application-peers"))
88                         .build())
89                     .build())
90                 .build())
91             .build());
92
93         writeModification.write(NETWORK_INSTANCES_ID, Builders.containerBuilder()
94             .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCES))
95             .withChild(Builders.mapBuilder()
96                 .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCE))
97                 .withChild(Builders.mapEntryBuilder()
98                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(NETWORK_INSTANCE, NAME, "global-bgp"))
99                     .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
100                     .withChild(Builders.containerBuilder()
101                         .withNodeIdentifier(new NodeIdentifier(CONFIG))
102                         .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
103                         .build())
104                     .withChild(Builders.containerBuilder()
105                         .withNodeIdentifier(new NodeIdentifier(PROTOCOLS))
106                         .withChild(Builders.mapBuilder()
107                             .withNodeIdentifier(new NodeIdentifier(PROTOCOL))
108                             .withChild(Builders.mapEntryBuilder()
109                                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(PROTOCOL, ImmutableMap.of(
110                                     IDENTIFIER, BGP_POLICY,
111                                     NAME, "test-bgp-instance")))
112                                 .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
113                                 .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
114                                 .withChild(Builders.containerBuilder()
115                                     .withNodeIdentifier(new NodeIdentifier(CONFIG))
116                                     .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
117                                     .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
118                                     .build())
119                                 .withChild(Builders.augmentationBuilder()
120                                     .withNodeIdentifier(
121                                         AugmentationIdentifier.create(ImmutableSet.of(TEST_BGP)))
122                                     .withChild(Builders.containerBuilder()
123                                         .withNodeIdentifier(new NodeIdentifier(TEST_BGP))
124                                         .withChild(Builders.containerBuilder()
125                                             .withNodeIdentifier(new NodeIdentifier(NEIGHBORS))
126                                             .withChild(Builders.mapBuilder()
127                                                 .withNodeIdentifier(new NodeIdentifier(NEIGHBOR))
128                                                 .withChild(Builders.mapEntryBuilder()
129                                                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(NEIGHBOR,
130                                                         NEIGHBOR_ADDRESS, "10.25.1.9"))
131                                                     .withChild(ImmutableNodes.leafNode(NEIGHBOR_ADDRESS,
132                                                             "10.25.1.9"))
133                                                     .withChild(Builders.containerBuilder()
134                                                         .withNodeIdentifier(new NodeIdentifier(TEST_CONFIG))
135                                                         .withChild(Builders.augmentationBuilder()
136                                                             .withNodeIdentifier(AugmentationIdentifier.create(
137                                                                 ImmutableSet.of(TEST_PEER_GROUP)))
138                                                             .withChild(ImmutableNodes.leafNode(TEST_PEER_GROUP,
139                                                                     "application-peers"))
140                                                             .build())
141                                                         .build())
142                                                     .withChild(Builders.containerBuilder()
143                                                         .withNodeIdentifier(new NodeIdentifier(AFI_SAFIS))
144                                                         .withChild(Builders.mapBuilder()
145                                                             .withNodeIdentifier(new NodeIdentifier(AFI_SAFI))
146                                                             .withChild(Builders.mapEntryBuilder()
147                                                                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(
148                                                                     AFI_SAFI,
149                                                                     ImmutableMap.of(AFI_SAFI_NAME, IPV4_UNICAST)))
150                                                                 .withChild(ImmutableNodes.leafNode(AFI_SAFI_NAME,
151                                                                     IPV4_UNICAST))
152                                                                 .withChild(Builders.containerBuilder()
153                                                                     .withNodeIdentifier(
154                                                                         new NodeIdentifier(TEST_CONFIG))
155                                                                     .withChild(ImmutableNodes.leafNode(
156                                                                         AFI_SAFI_NAME, IPV4_UNICAST))
157                                                                     .build())
158                                                                 .withChild(Builders.augmentationBuilder()
159                                                                     .withNodeIdentifier(
160                                                                         AugmentationIdentifier.create(
161                                                                             ImmutableSet.of(RECEIVE, SEND_MAX)))
162                                                                     .withChild(ImmutableNodes.leafNode(RECEIVE,
163                                                                         Boolean.TRUE))
164                                                                     .withChild(ImmutableNodes.leafNode(SEND_MAX,
165                                                                         Uint8.ZERO))
166                                                                     .build())
167                                                                 .build())
168                                                             .build())
169                                                         .build())
170                                                     .build())
171                                                 .build())
172                                             .build())
173                                         .build())
174                                     .build())
175                                 .build())
176                             .build())
177                         .build())
178                     .build())
179                 .build())
180             .build());
181
182         writeModification.ready();
183         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
184         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
185         dataTree.commit(writeContributorsCandidate);
186     }
187 }