Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / DataTreeCandidateValidatorTest3.java
1 /*
2  * Copyright (c) 2016 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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12
13 import java.util.Map;
14 import org.junit.AfterClass;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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.tree.api.DataTree;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
31 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class DataTreeCandidateValidatorTest3 {
39
40     private static EffectiveModelContext context;
41     private static Module mainModule;
42     private static QNameModule rootModuleQname;
43     private static LeafRefContext rootLeafRefContext;
44     public static DataTree inMemoryDataTree;
45
46     private static QName chips;
47     private static QName chip;
48     private static QName devType;
49     private static QName chipDesc;
50
51     private static QName devices;
52     private static QName device;
53     private static QName typeText1;
54     private static QName typeText2;
55     private static QName typeText3;
56     private static QName devDesc;
57     private static QName sn;
58     private static QName defaultIp;
59
60     private static QName deviceTypeStr;
61     private static QName deviceType;
62     private static QName type1;
63     private static QName type2;
64     private static QName type3;
65     private static QName desc;
66
67     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
68     private static final String NEW_LINE = System.getProperty("line.separator");
69
70     @BeforeClass
71     public static void init() throws DataValidationFailedException {
72         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
73
74         for (final Module module : context.getModules()) {
75             if (module.getName().equals("leafref-validation3")) {
76                 mainModule = module;
77             }
78         }
79
80         rootModuleQname = mainModule.getQNameModule();
81         rootLeafRefContext = LeafRefContext.create(context);
82
83
84         chips = QName.create(rootModuleQname, "chips");
85         chip = QName.create(rootModuleQname, "chip");
86         devType = QName.create(rootModuleQname, "dev_type");
87         chipDesc = QName.create(rootModuleQname, "chip_desc");
88
89         devices = QName.create(rootModuleQname, "devices");
90         device = QName.create(rootModuleQname, "device");
91         typeText1 = QName.create(rootModuleQname, "type_text1");
92         typeText2 = QName.create(rootModuleQname, "type_text2");
93         typeText3 = QName.create(rootModuleQname, "type_text3");
94         devDesc = QName.create(rootModuleQname, "dev_desc");
95         sn = QName.create(rootModuleQname, "sn");
96         defaultIp = QName.create(rootModuleQname, "default_ip");
97
98         deviceTypeStr = QName.create(rootModuleQname, "device_types");
99         deviceType = QName.create(rootModuleQname, "device_type");
100         type1 = QName.create(rootModuleQname, "type1");
101         type2 = QName.create(rootModuleQname, "type2");
102         type3 = QName.create(rootModuleQname, "type3");
103         desc = QName.create(rootModuleQname, "desc");
104
105         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
106
107         final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
108
109         initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
110             .withNodeIdentifier(new NodeIdentifier(chips))
111             .addChild(Builders.mapBuilder()
112                 .withNodeIdentifier(new NodeIdentifier(chip))
113                 .addChild(createChipsListEntry("dev_type_1", "desc1"))
114                 .addChild(createChipsListEntry("dev_type_2", "desc2"))
115                 .build())
116             .build());
117
118         initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
119             .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
120             .addChild(Builders.mapBuilder()
121                 .withNodeIdentifier(new NodeIdentifier(deviceType))
122                 .addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1"))
123                 .addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2"))
124                 .addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3"))
125                 .build())
126             .build());
127
128         initialDataTreeModification.ready();
129         final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
130
131         inMemoryDataTree.commit(writeChipsCandidate);
132
133         LOG.debug("{}", inMemoryDataTree);
134     }
135
136     @AfterClass
137     public static void cleanup() {
138         inMemoryDataTree = null;
139         rootLeafRefContext = null;
140         mainModule = null;
141         context = null;
142     }
143
144     @Test
145     public void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
146         writeDevices();
147         mergeDevices();
148     }
149
150     private static void writeDevices() throws DataValidationFailedException {
151         final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
152         writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
153             .withNodeIdentifier(new NodeIdentifier(devices))
154             .addChild(Builders.mapBuilder()
155                 .withNodeIdentifier(new NodeIdentifier(device))
156                 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
157                     "192.168.0.1"))
158                 .addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
159                     "192.168.0.1"))
160                 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
161                     "192.168.0.1"))
162                 .addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1"))
163                 .build())
164             .build());
165
166         writeModification.ready();
167         final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
168
169         LOG.debug("*************************");
170         LOG.debug("Before writeDevices: ");
171         LOG.debug("*************************");
172         LOG.debug("{}", inMemoryDataTree);
173
174         final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
175             () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
176         assertEquals(6, ex.getValidationsErrorsCount());
177
178         inMemoryDataTree.commit(writeDevicesCandidate);
179
180         LOG.debug("*************************");
181         LOG.debug("After writeDevices: ");
182         LOG.debug("*************************");
183         LOG.debug("{}", inMemoryDataTree);
184     }
185
186     private static void mergeDevices() throws DataValidationFailedException {
187         final ContainerNode devicesContainer = Builders.containerBuilder()
188             .withNodeIdentifier(new NodeIdentifier(devices))
189             .addChild(Builders.mapBuilder()
190                 .withNodeIdentifier(new NodeIdentifier(device))
191                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
192                     "192.168.0.1"))
193                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
194                     "192.168.0.1"))
195                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
196                     "192.168.0.1"))
197                 .addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1"))
198                 .build())
199             .build();
200
201         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
202         final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
203         mergeModification.write(devicesPath, devicesContainer);
204         mergeModification.merge(devicesPath, devicesContainer);
205
206         mergeModification.ready();
207         final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
208
209         LOG.debug("*************************");
210         LOG.debug("Before mergeDevices: ");
211         LOG.debug("*************************");
212         LOG.debug("{}", inMemoryDataTree);
213
214         final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
215             () -> LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext));
216         // :TODO verify errors count gz
217         assertEquals(6, ex.getValidationsErrorsCount());
218
219         inMemoryDataTree.commit(mergeDevicesCandidate);
220
221         LOG.debug("*************************");
222         LOG.debug("After mergeDevices: ");
223         LOG.debug("*************************");
224         LOG.debug("{}", inMemoryDataTree);
225     }
226
227     private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
228             final String type3Val, final String descVal) {
229         return Builders.mapEntryBuilder()
230             .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType,
231                 Map.of(type1, type1Val, type2, type2Val, type3, type3Val)))
232             .addChild(ImmutableNodes.leafNode(type1, type1Val))
233             .addChild(ImmutableNodes.leafNode(type2, type2Val))
234             .addChild(ImmutableNodes.leafNode(type3, type3Val))
235             .addChild(ImmutableNodes.leafNode(desc, descVal))
236             .build();
237     }
238
239     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
240         return Builders.mapEntryBuilder()
241             .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
242             .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
243             .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
244             .build();
245     }
246
247     private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
248             final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal) {
249         return Builders.mapEntryBuilder()
250             .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText1, type1TextVal, sn, snVal)))
251             .addChild(ImmutableNodes.leafNode(typeText1, type1TextVal))
252             .addChild(ImmutableNodes.leafNode(typeText2, type2TextVal))
253             .addChild(ImmutableNodes.leafNode(typeText3, type3TextVal))
254             .addChild(ImmutableNodes.leafNode(devDesc, descVal))
255             .addChild(ImmutableNodes.leafNode(sn, snVal))
256             .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))
257             .build();
258     }
259 }