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