2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.tree.leafref;
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;
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);
35 private static final QName NETWORK_INSTANCES =
36 QName.create("urn:opendaylight:params:xml:ns:yang:test:network:instance", "2018-08-14",
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",
46 private static final QName TEST_BGP = QName.create("urn:opendaylight:params:xml:ns:yang:bgp:test:extensions",
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");
61 private static final YangInstanceIdentifier NETWORK_INSTANCES_ID = YangInstanceIdentifier.of(NETWORK_INSTANCES);
63 private LeafRefContext leafRefContext;
64 private DataTree dataTree;
68 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yt892");
69 leafRefContext = LeafRefContext.create(schemaContext);
70 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
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"))
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"))
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"))
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"))
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(
139 ImmutableMap.of(AFI_SAFI_NAME, IPV4_UNICAST)))
140 .withChild(ImmutableNodes.leafNode(AFI_SAFI_NAME,
142 .withChild(Builders.containerBuilder()
144 new NodeIdentifier(TEST_CONFIG))
145 .withChild(ImmutableNodes.leafNode(
146 AFI_SAFI_NAME, IPV4_UNICAST))
148 .withChild(ImmutableNodes.leafNode(RECEIVE,
150 .withChild(ImmutableNodes.leafNode(SEND_MAX,
166 writeModification.ready();
167 final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
168 LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
169 dataTree.commit(writeContributorsCandidate);