Do not use schema-aware builders in RestconfMappingNodeUtil
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / mapping / RestconfMappingNodeUtilTest.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.restconf.nb.rfc8040.utils.mapping;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.net.URI;
15 import java.time.Instant;
16 import java.time.OffsetDateTime;
17 import java.time.ZoneId;
18 import java.time.format.DateTimeFormatter;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
27 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
28 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule.QueryParams;
29 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.RestconfModule;
30 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
31 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Unit tests for {@link RestconfMappingNodeUtil}.
51  */
52 public class RestconfMappingNodeUtilTest {
53     private static final Logger LOG = LoggerFactory.getLogger(RestconfMappingNodeUtilTest.class);
54
55     private static Collection<? extends Module> modules;
56     private static EffectiveModelContext schemaContext;
57     private static EffectiveModelContext schemaContextMonitoring;
58     private static Collection<? extends Module> modulesRest;
59
60     @BeforeClass
61     public static void loadTestSchemaContextAndModules() throws Exception {
62         // FIXME: assemble these from dependencies
63         schemaContext =
64                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules/restconf-module-testing"));
65         schemaContextMonitoring = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules"));
66         modules = schemaContextMonitoring.getModules();
67         modulesRest = YangParserTestUtils
68                 .parseYangFiles(TestRestconfUtils.loadFiles("/modules/restconf-module-testing")).getModules();
69     }
70
71     /**
72      * Test of writing modules into {@link RestconfModule#MODULE_LIST_SCHEMA_NODE} and checking if modules were
73      * correctly written.
74      */
75     @Test
76     public void restconfMappingNodeTest() {
77         // write modules into list module in Restconf
78         final ContainerNode mods = RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(
79             RestconfMappingNodeUtilTest.modules, schemaContext, "1");
80
81         // verify loaded modules
82         verifyLoadedModules(mods);
83         // verify deviations
84         verifyDeviations(mods);
85     }
86
87     @Test
88     public void restconfStateCapabilitesTest() {
89         final Module monitoringModule = schemaContextMonitoring.findModule(MonitoringModule.MODULE_QNAME).get();
90         final ContainerNode normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
91         assertNotNull(normNode);
92         final List<Object> listOfValues = new ArrayList<>();
93
94         for (final DataContainerChild<?, ?> child : normNode.getValue()) {
95             if (child.getNodeType().equals(MonitoringModule.CONT_CAPABILITES_QNAME)) {
96                 for (final DataContainerChild<?, ?> dataContainerChild : ((ContainerNode) child).getValue()) {
97                     for (final Object entry : ((LeafSetNode<?>) dataContainerChild).getValue()) {
98                         listOfValues.add(((LeafSetEntryNode<?>) entry).getValue());
99                     }
100                 }
101             }
102         }
103         assertTrue(listOfValues.contains(QueryParams.DEPTH));
104         assertTrue(listOfValues.contains(QueryParams.FIELDS));
105         assertTrue(listOfValues.contains(QueryParams.FILTER));
106         assertTrue(listOfValues.contains(QueryParams.REPLAY));
107         assertTrue(listOfValues.contains(QueryParams.WITH_DEFAULTS));
108     }
109
110     @Test
111     public void toStreamEntryNodeTest() throws Exception {
112         final YangInstanceIdentifier path = ParserIdentifier.toInstanceIdentifier(
113                 "nested-module:depth1-cont/depth2-leaf1", schemaContextMonitoring, null).getInstanceIdentifier();
114         final Instant start = Instant.now();
115         final String outputType = "XML";
116         final URI uri = new URI("uri");
117         final String streamName = "/nested-module:depth1-cont/depth2-leaf1";
118
119         final Map<QName, Object> map = prepareMap(streamName, uri, start, outputType);
120         final MapEntryNode mappedData = RestconfMappingNodeUtil.mapDataChangeNotificationStreamByIetfRestconfMonitoring(
121             path, start, outputType, uri, schemaContextMonitoring, streamName);
122         assertMappedData(map, mappedData);
123     }
124
125     @Test
126     public void toStreamEntryNodeNotifiTest() throws Exception {
127         final Instant start = Instant.now();
128         final String outputType = "JSON";
129         final URI uri = new URI("uri");
130
131         final Map<QName, Object> map = prepareMap("notifi", uri, start, outputType);
132         map.put(MonitoringModule.LEAF_DESCR_STREAM_QNAME, "Notifi");
133
134         final QName notifiQName = QName.create("urn:nested:module", "2014-06-03", "notifi");
135         final MapEntryNode mappedData = RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
136             notifiQName, schemaContextMonitoring.getNotifications(), start, outputType, uri);
137         assertMappedData(map, mappedData);
138     }
139
140     private static Map<QName, Object> prepareMap(final String name, final URI uri, final Instant start,
141             final String outputType) {
142         final Map<QName, Object> map = new HashMap<>();
143         map.put(MonitoringModule.LEAF_NAME_STREAM_QNAME, name);
144         map.put(MonitoringModule.LEAF_LOCATION_ACCESS_QNAME, uri.toString());
145         map.put(MonitoringModule.LEAF_REPLAY_SUPP_STREAM_QNAME, Boolean.TRUE);
146         map.put(MonitoringModule.LEAF_START_TIME_STREAM_QNAME, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(
147             OffsetDateTime.ofInstant(start, ZoneId.systemDefault())));
148         map.put(MonitoringModule.LEAF_ENCODING_ACCESS_QNAME, outputType);
149         return map;
150     }
151
152     private static void assertMappedData(final Map<QName, Object> map, final MapEntryNode mappedData) {
153         assertNotNull(mappedData);
154         for (final DataContainerChild<?, ?> child : mappedData.getValue()) {
155             if (child instanceof LeafNode) {
156                 final LeafNode<?> leaf = (LeafNode<?>) child;
157                 assertTrue(map.containsKey(leaf.getNodeType()));
158                 assertEquals(map.get(leaf.getNodeType()), leaf.getValue());
159             }
160         }
161     }
162
163     /**
164      * Verify whether the loaded modules contain any deviations.
165      *
166      * @param containerNode
167      *             modules
168      */
169     private static void verifyDeviations(final ContainerNode containerNode) {
170         int deviationsFound = 0;
171         for (final DataContainerChild<?, ?> child : containerNode.getValue()) {
172             if (child instanceof MapNode) {
173                 for (final MapEntryNode mapEntryNode : ((MapNode) child).getValue()) {
174                     for (final DataContainerChild<?, ?> dataContainerChild : mapEntryNode.getValue()) {
175                         if (dataContainerChild.getNodeType()
176                                 .equals(IetfYangLibrary.SPECIFIC_MODULE_DEVIATION_LIST_QNAME)) {
177                             deviationsFound++;
178                         }
179                     }
180                 }
181             }
182         }
183         assertTrue(deviationsFound > 0);
184     }
185
186     /**
187      * Verify loaded modules.
188      *
189      * @param containerNode
190      *             modules
191      */
192     private static void verifyLoadedModules(final ContainerNode containerNode) {
193
194         final Map<String, String> loadedModules = new HashMap<>();
195
196         for (final DataContainerChild<? extends PathArgument, ?> child : containerNode.getValue()) {
197             if (child instanceof LeafNode) {
198                 assertEquals(IetfYangLibrary.MODULE_SET_ID_LEAF_QNAME, child.getNodeType());
199             }
200             if (child instanceof MapNode) {
201                 assertEquals(IetfYangLibrary.MODULE_QNAME_LIST, child.getNodeType());
202                 for (final MapEntryNode mapEntryNode : ((MapNode) child).getValue()) {
203                     String name = "";
204                     String revision = "";
205                     for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : mapEntryNode
206                             .getValue()) {
207                         switch (dataContainerChild.getNodeType().getLocalName()) {
208                             case IetfYangLibrary.SPECIFIC_MODULE_NAME_LEAF:
209                                 name = String.valueOf(dataContainerChild.getValue());
210                                 break;
211                             case IetfYangLibrary.SPECIFIC_MODULE_REVISION_LEAF:
212                                 revision = String.valueOf(dataContainerChild.getValue());
213                                 break;
214                             default :
215                                 LOG.info("Unknown local name '{}' of node.",
216                                         dataContainerChild.getNodeType().getLocalName());
217                                 break;
218                         }
219                     }
220                     loadedModules.put(name, revision);
221                 }
222             }
223         }
224
225         verifyLoadedModules(RestconfMappingNodeUtilTest.modulesRest, loadedModules);
226     }
227
228     /**
229      * Verify if correct modules were loaded into Restconf module by comparison with modules from
230      * <code>SchemaContext</code>.
231      * @param expectedModules Modules from <code>SchemaContext</code>
232      * @param loadedModules Loaded modules into Restconf module
233      */
234     private static void verifyLoadedModules(final Collection<? extends Module> expectedModules,
235             final Map<String, String> loadedModules) {
236         assertEquals("Number of loaded modules is not as expected", expectedModules.size(), loadedModules.size());
237         for (final Module m : expectedModules) {
238             final String name = m.getName();
239
240             final String revision = loadedModules.get(name);
241             assertNotNull("Expected module not found", revision);
242             assertEquals("Incorrect revision of loaded module", Revision.ofNullable(revision), m.getRevision());
243
244             loadedModules.remove(name);
245         }
246     }
247 }