34116da78c9a5aac3f91056091f00db67b001afc
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / ListenerAdapterTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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.streams.listeners;
9
10 import static java.time.Instant.EPOCH;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.nio.charset.StandardCharsets;
18 import java.nio.file.Files;
19 import java.nio.file.Paths;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22 import org.json.JSONObject;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.mdsal.binding.api.WriteTransaction;
28 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
33 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
34 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
36 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
37 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
38 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
39 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
40 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 import org.skyscreamer.jsonassert.JSONAssert;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
50     private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
51
52     private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
53     private static final String JSON_NOTIF_LEAVES_UPDATE =  "/listener-adapter-test/notif-leaves-update.json";
54     private static final String JSON_NOTIF_LEAVES_DEL =  "/listener-adapter-test/notif-leaves-del.json";
55     private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
56     private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
57     private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
58
59     private static YangInstanceIdentifier PATCH_CONT_YIID =
60             YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
61
62     private DataBroker dataBroker;
63     private DOMDataBroker domDataBroker;
64     private TransactionChainHandler transactionChainHandler;
65     private SchemaContextHandler schemaContextHandler;
66
67     @Before
68     public void setUp() throws Exception {
69         dataBroker = getDataBroker();
70         domDataBroker = getDomBroker();
71         SchemaContext sc = YangParserTestUtils.parseYangResource(
72                 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
73
74         transactionChainHandler = new TransactionChainHandler(domDataBroker);
75         schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
76                 Mockito.mock(DOMSchemaService.class));
77         schemaContextHandler.onGlobalContextUpdated(sc);
78     }
79
80     class ListenerAdapterTester extends ListenerAdapter {
81
82         private volatile String lastNotification;
83         private CountDownLatch notificationLatch = new CountDownLatch(1);
84
85         ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
86                               final NotificationOutputTypeGrouping.NotificationOutputType outputType,
87                               final boolean leafNodesOnly) {
88             super(path, streamName, outputType);
89             setQueryParams(EPOCH, null, null, leafNodesOnly);
90         }
91
92         @Override
93         protected void post(final String data) {
94             this.lastNotification = data;
95             notificationLatch.countDown();
96         }
97
98         public void assertGot(final String json) {
99             if (!Uninterruptibles.awaitUninterruptibly(notificationLatch, 5, TimeUnit.SECONDS)) {
100                 fail("Timed out waiting for notification for: " + json);
101             }
102
103             LOG.info("lastNotification: {}", lastNotification);
104             String withFakeDate = withFakeDate(lastNotification);
105             LOG.info("Comparing: \n{}\n{}", json, withFakeDate);
106
107             JSONAssert.assertEquals(json, withFakeDate, false);
108             this.lastNotification = null;
109             notificationLatch = new CountDownLatch(1);
110         }
111     }
112
113     static String withFakeDate(final String in) {
114         JSONObject doc = new JSONObject(in);
115         JSONObject notification = doc.getJSONObject("notification");
116         if (notification == null) {
117             return in;
118         }
119         notification.put("eventTime", "someDate");
120         return doc.toString();
121     }
122
123     private String getNotifJson(final String path) throws IOException, URISyntaxException {
124         URL url = getClass().getResource(path);
125         byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
126         return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
127     }
128
129     @Test
130     public void testJsonNotifsLeaves() throws Exception {
131         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
132                                         NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
133         adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
134
135         DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
136                 .getInstance(DOMDataTreeChangeService.class);
137         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
138         changeService.registerDataTreeChangeListener(root, adapter);
139
140         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
141         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
142         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
143                 .child(MyList1.class, new MyList1Key("Althea"));
144         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
145         writeTransaction.commit();
146         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
147
148         writeTransaction = dataBroker.newWriteOnlyTransaction();
149         builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
150         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
151         writeTransaction.commit();
152         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
153
154         writeTransaction = dataBroker.newWriteOnlyTransaction();
155         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
156         writeTransaction.commit();
157         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
158     }
159
160     @Test
161     public void testJsonNotifs() throws Exception {
162         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
163                 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
164         adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
165
166         DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
167                 .getInstance(DOMDataTreeChangeService.class);
168         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
169         changeService.registerDataTreeChangeListener(root, adapter);
170
171         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
172         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
173         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
174                 .child(MyList1.class, new MyList1Key("Althea"));
175         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
176         writeTransaction.commit();
177         adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
178
179         writeTransaction = dataBroker.newWriteOnlyTransaction();
180         builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
181         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
182         writeTransaction.commit();
183         adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
184
185         writeTransaction = dataBroker.newWriteOnlyTransaction();
186         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
187         writeTransaction.commit();
188         adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));
189     }
190 }