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