2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
10 import static java.time.Instant.EPOCH;
11 import static org.junit.Assert.fail;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
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.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
35 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
36 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
37 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
38 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
39 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
40 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
41 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
42 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 import org.skyscreamer.jsonassert.JSONAssert;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
52 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
54 private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
55 private static final String JSON_NOTIF_LEAVES_UPDATE = "/listener-adapter-test/notif-leaves-update.json";
56 private static final String JSON_NOTIF_LEAVES_DEL = "/listener-adapter-test/notif-leaves-del.json";
57 private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
58 private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
59 private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
61 private static final YangInstanceIdentifier PATCH_CONT_YIID =
62 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
64 private static EffectiveModelContext SCHEMA_CONTEXT;
66 private DataBroker dataBroker;
67 private DOMDataBroker domDataBroker;
68 private TransactionChainHandler transactionChainHandler;
69 private SchemaContextHandler schemaContextHandler;
72 public static void beforeClass() {
73 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource(
74 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
78 public static void afterClass() {
79 SCHEMA_CONTEXT = null;
83 public void setUp() throws Exception {
84 dataBroker = getDataBroker();
85 domDataBroker = getDomBroker();
87 transactionChainHandler = new TransactionChainHandler(domDataBroker);
88 schemaContextHandler = new SchemaContextHandler(transactionChainHandler, Mockito.mock(DOMSchemaService.class));
89 schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
92 class ListenerAdapterTester extends ListenerAdapter {
94 private volatile String lastNotification;
95 private CountDownLatch notificationLatch = new CountDownLatch(1);
97 ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
98 final NotificationOutputTypeGrouping.NotificationOutputType outputType,
99 final boolean leafNodesOnly) {
100 super(path, streamName, outputType);
101 setQueryParams(EPOCH, null, null, leafNodesOnly);
105 protected void post(final String data) {
106 this.lastNotification = data;
107 notificationLatch.countDown();
110 public void assertGot(final String json) {
111 if (!Uninterruptibles.awaitUninterruptibly(notificationLatch, 5, TimeUnit.SECONDS)) {
112 fail("Timed out waiting for notification for: " + json);
115 LOG.info("lastNotification: {}", lastNotification);
116 String withFakeDate = withFakeDate(lastNotification);
117 LOG.info("Comparing: \n{}\n{}", json, withFakeDate);
119 JSONAssert.assertEquals(json, withFakeDate, false);
120 this.lastNotification = null;
121 notificationLatch = new CountDownLatch(1);
125 static String withFakeDate(final String in) {
126 JSONObject doc = new JSONObject(in);
127 JSONObject notification = doc.getJSONObject("notification");
128 if (notification == null) {
131 notification.put("eventTime", "someDate");
132 return doc.toString();
135 private String getNotifJson(final String path) throws IOException, URISyntaxException {
136 URL url = getClass().getResource(path);
137 byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
138 return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
142 public void testJsonNotifsLeaves() throws Exception {
143 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
144 NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
145 adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
147 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
148 .getInstance(DOMDataTreeChangeService.class);
149 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
150 changeService.registerDataTreeChangeListener(root, adapter);
152 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
153 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
154 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
155 .child(MyList1.class, new MyList1Key("Althea"));
156 writeTransaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
157 writeTransaction.commit();
158 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
160 writeTransaction = dataBroker.newWriteOnlyTransaction();
161 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
162 writeTransaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
163 writeTransaction.commit();
164 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
166 writeTransaction = dataBroker.newWriteOnlyTransaction();
167 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
168 writeTransaction.commit();
169 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
173 public void testJsonNotifs() throws Exception {
174 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
175 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
176 adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
178 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
179 .getInstance(DOMDataTreeChangeService.class);
180 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
181 changeService.registerDataTreeChangeListener(root, adapter);
183 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
184 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
185 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
186 .child(MyList1.class, new MyList1Key("Althea"));
187 writeTransaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
188 writeTransaction.commit();
189 adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
191 writeTransaction = dataBroker.newWriteOnlyTransaction();
192 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
193 writeTransaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
194 writeTransaction.commit();
195 adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
197 writeTransaction = dataBroker.newWriteOnlyTransaction();
198 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
199 writeTransaction.commit();
200 adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));