Enable usage of yanglib-mdsal-writer
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / monitoring / RestconfStateStreamsTest.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.monitoring;
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.HashMap;
20 import java.util.Map;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.Deviation;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Unit tests for {@link RestconfStateStreams}.
40  */
41 public class RestconfStateStreamsTest {
42     private static final Logger LOG = LoggerFactory.getLogger(RestconfStateStreamsTest.class);
43
44     private static EffectiveModelContext schemaContext;
45     private static EffectiveModelContext schemaContextMonitoring;
46
47     @BeforeClass
48     public static void loadTestSchemaContextAndModules() throws Exception {
49         // FIXME: assemble these from dependencies
50         schemaContext = YangParserTestUtils.parseYangResourceDirectory("/modules/restconf-module-testing");
51         schemaContextMonitoring = YangParserTestUtils.parseYangResourceDirectory("/modules");
52     }
53
54     @Test
55     public void toStreamEntryNodeTest() throws Exception {
56         final YangInstanceIdentifier path = ParserIdentifier.toInstanceIdentifier(
57                 "nested-module:depth1-cont/depth2-leaf1", schemaContextMonitoring, null).getInstanceIdentifier();
58         final Instant start = Instant.now();
59         final String outputType = "XML";
60         final URI uri = new URI("uri");
61         final String streamName = "/nested-module:depth1-cont/depth2-leaf1";
62
63         final Map<QName, Object> map = prepareMap(streamName, uri, start, outputType);
64         final MapEntryNode mappedData = RestconfStateStreams.dataChangeStreamEntry(path, start, outputType, uri,
65             schemaContextMonitoring, streamName);
66         assertMappedData(map, mappedData);
67     }
68
69     @Test
70     public void toStreamEntryNodeNotifiTest() throws Exception {
71         final Instant start = Instant.now();
72         final String outputType = "JSON";
73         final URI uri = new URI("uri");
74
75         final Map<QName, Object> map = prepareMap("notifi", uri, start, outputType);
76         map.put(RestconfStateStreams.DESCRIPTION_QNAME, "Notifi");
77
78         final MapEntryNode mappedData = RestconfStateStreams.notificationStreamEntry(schemaContextMonitoring,
79             QName.create("urn:nested:module", "2014-06-03", "notifi"), start, outputType, uri);
80         assertMappedData(map, mappedData);
81     }
82
83     private static Map<QName, Object> prepareMap(final String name, final URI uri, final Instant start,
84             final String outputType) {
85         final Map<QName, Object> map = new HashMap<>();
86         map.put(RestconfStateStreams.NAME_QNAME, name);
87         map.put(RestconfStateStreams.LOCATION_QNAME, uri.toString());
88         map.put(RestconfStateStreams.REPLAY_SUPPORT_QNAME, Boolean.TRUE);
89         map.put(RestconfStateStreams.REPLAY_LOG_CREATION_TIME, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(
90             OffsetDateTime.ofInstant(start, ZoneId.systemDefault())));
91         map.put(RestconfStateStreams.ENCODING_QNAME, outputType);
92         return map;
93     }
94
95     private static void assertMappedData(final Map<QName, Object> map, final MapEntryNode mappedData) {
96         assertNotNull(mappedData);
97         for (var child : mappedData.body()) {
98             if (child instanceof LeafNode<?> leaf) {
99                 assertTrue(map.containsKey(leaf.name().getNodeType()));
100                 assertEquals(map.get(leaf.name().getNodeType()), leaf.body());
101             }
102         }
103     }
104
105     /**
106      * Verify whether the loaded modules contain any deviations.
107      *
108      * @param containerNode
109      *             modules
110      */
111     private static void verifyDeviations(final ContainerNode containerNode) {
112         int deviationsFound = 0;
113         for (var child : containerNode.body()) {
114             if (child instanceof MapNode mapChild) {
115                 for (var mapEntryNode : mapChild.body()) {
116                     for (var dataContainerChild : mapEntryNode.body()) {
117                         if (dataContainerChild.name().getNodeType().equals(Deviation.QNAME)) {
118                             deviationsFound++;
119                         }
120                     }
121                 }
122             }
123         }
124         assertTrue(deviationsFound > 0);
125     }
126
127     /**
128      * Verify loaded modules.
129      *
130      * @param containerNode
131      *             modules
132      */
133     private static void verifyLoadedModules(final ContainerNode containerNode) {
134         final Map<String, String> loadedModules = new HashMap<>();
135
136         for (var child : containerNode.body()) {
137             if (child instanceof LeafNode) {
138                 assertEquals(QName.create(Module.QNAME, "module-set-id"), child.name().getNodeType());
139             }
140             if (child instanceof MapNode mapChild) {
141                 assertEquals(Module.QNAME, child.name().getNodeType());
142                 for (var mapEntryNode : mapChild.body()) {
143                     String name = "";
144                     String revision = "";
145                     for (var dataContainerChild : mapEntryNode.body()) {
146                         switch (dataContainerChild.name().getNodeType().getLocalName()) {
147                             case "name":
148                                 name = String.valueOf(dataContainerChild.body());
149                                 break;
150                             case "revision":
151                                 revision = String.valueOf(dataContainerChild.body());
152                                 break;
153                             default :
154                                 LOG.info("Unknown local name '{}' of node.",
155                                     dataContainerChild.name().getNodeType().getLocalName());
156                                 break;
157                         }
158                     }
159                     loadedModules.put(name, revision);
160                 }
161             }
162         }
163
164         final var expectedModules = schemaContext.getModules();
165         assertEquals("Number of loaded modules is not as expected", expectedModules.size(), loadedModules.size());
166         for (var m : expectedModules) {
167             final String name = m.getName();
168             final String revision = loadedModules.get(name);
169             assertNotNull("Expected module not found", revision);
170             assertEquals("Incorrect revision of loaded module", Revision.ofNullable(revision), m.getRevision());
171
172             loadedModules.remove(name);
173         }
174     }
175 }