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
9 package org.opendaylight.netconf.sal.streams.listeners;
11 import static java.time.Instant.EPOCH;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
16 import java.nio.charset.StandardCharsets;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.util.Optional;
20 import org.json.JSONObject;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
32 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
33 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
34 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
35 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 import org.skyscreamer.jsonassert.JSONAssert;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
45 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
47 private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
48 private static final String JSON_NOTIF_LEAVES_UPDATE = "/listener-adapter-test/notif-leaves-update.json";
49 private static final String JSON_NOTIF_LEAVES_DEL = "/listener-adapter-test/notif-leaves-del.json";
50 private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
51 private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
52 private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
54 private static YangInstanceIdentifier PATCH_CONT_YIID =
55 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
57 private DataBroker dataBroker;
58 private DOMDataBroker domDataBroker;
61 public void setUp() throws Exception {
62 dataBroker = getDataBroker();
63 domDataBroker = getDomBroker();
64 SchemaContext sc = YangParserTestUtils.parseYangResource(
65 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
66 ControllerContext.getInstance().setGlobalSchema(sc);
69 class ListenerAdapterTester extends ListenerAdapter {
71 private String lastNotification = null;
73 ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
74 final NotificationOutputTypeGrouping.NotificationOutputType outputType,
75 final boolean leafNodesOnly) {
76 super(path, streamName, outputType);
77 setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly);
81 protected void post(final Event event) {
82 this.lastNotification = event.getData();
85 public void assertGot(final String json) throws Exception {
86 long start = System.currentTimeMillis();
88 if (lastNotification != null) {
91 if (System.currentTimeMillis() - start > 1000) {
92 throw new Exception("TIMED OUT waiting for notification with " + json);
94 Thread.currentThread();
97 LOG.debug("Comparing {} {}", json, lastNotification);
98 JSONAssert.assertEquals(json, withFakeDate(lastNotification), false);
99 this.lastNotification = null;
103 static String withFakeDate(final String in) {
104 JSONObject doc = new JSONObject(in);
105 JSONObject notification = doc.getJSONObject("notification");
106 if (notification == null) {
109 notification.put("eventTime", "someDate");
110 return doc.toString();
113 private String getNotifJson(final String path) throws IOException, URISyntaxException {
114 URL url = getClass().getResource(path);
115 byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
116 return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
120 public void testJsonNotifsLeaves() throws Exception {
121 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
122 NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
123 DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
124 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
125 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
126 changeService.registerDataTreeChangeListener(root, adapter);
128 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
129 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
130 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
131 .child(MyList1.class, new MyList1Key("Althea"));
132 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
133 writeTransaction.submit();
134 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
136 writeTransaction = dataBroker.newWriteOnlyTransaction();
137 builder = new MyList1Builder().setKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
138 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
139 writeTransaction.submit();
140 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
142 writeTransaction = dataBroker.newWriteOnlyTransaction();
143 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
144 writeTransaction.submit();
145 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
149 public void testJsonNotifs() throws Exception {
150 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
151 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
152 DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
153 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
154 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
155 changeService.registerDataTreeChangeListener(root, adapter);
157 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
158 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
159 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
160 .child(MyList1.class, new MyList1Key("Althea"));
161 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
162 writeTransaction.submit();
163 adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
165 writeTransaction = dataBroker.newWriteOnlyTransaction();
166 builder = new MyList1Builder().setKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
167 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
168 writeTransaction.submit();
169 adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
171 writeTransaction = dataBroker.newWriteOnlyTransaction();
172 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
173 writeTransaction.submit();
174 adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));