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.netconf.sal.streams.listeners;
10 import static java.time.Instant.EPOCH;
12 import java.io.IOException;
13 import java.net.URISyntaxException;
15 import java.nio.charset.StandardCharsets;
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
18 import java.util.Optional;
19 import org.json.JSONObject;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.WriteTransaction;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
33 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
34 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
35 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
36 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 import org.skyscreamer.jsonassert.JSONAssert;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
46 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
48 private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
49 private static final String JSON_NOTIF_LEAVES_UPDATE = "/listener-adapter-test/notif-leaves-update.json";
50 private static final String JSON_NOTIF_LEAVES_DEL = "/listener-adapter-test/notif-leaves-del.json";
51 private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
52 private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
53 private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
55 private static final YangInstanceIdentifier PATCH_CONT_YIID =
56 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
58 private static EffectiveModelContext schemaContext;
60 private DataBroker dataBroker;
61 private DOMDataBroker domDataBroker;
62 private ControllerContext controllerContext;
65 public static void init() {
66 schemaContext = YangParserTestUtils.parseYangResource(
67 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
71 public void setUp() throws Exception {
72 dataBroker = getDataBroker();
73 domDataBroker = getDomBroker();
74 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
77 class ListenerAdapterTester extends ListenerAdapter {
79 private String lastNotification = null;
81 ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
82 final NotificationOutputTypeGrouping.NotificationOutputType outputType,
83 final boolean leafNodesOnly) {
84 super(path, streamName, outputType, controllerContext);
85 setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly);
89 protected void post(final Event event) {
90 this.lastNotification = event.getData();
93 public void assertGot(final String json) throws Exception {
94 long start = System.currentTimeMillis();
96 if (lastNotification != null) {
99 if (System.currentTimeMillis() - start > 1000) {
100 throw new Exception("TIMED OUT waiting for notification with " + json);
102 Thread.currentThread();
105 LOG.debug("Comparing {} {}", json, lastNotification);
106 JSONAssert.assertEquals(json, withFakeDate(lastNotification), false);
107 this.lastNotification = null;
111 static String withFakeDate(final String in) {
112 JSONObject doc = new JSONObject(in);
113 JSONObject notification = doc.getJSONObject("notification");
114 if (notification == null) {
117 notification.put("eventTime", "someDate");
118 return doc.toString();
121 private String getNotifJson(final String path) throws IOException, URISyntaxException {
122 URL url = getClass().getResource(path);
123 byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
124 return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
128 public void testJsonNotifsLeaves() throws Exception {
129 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
130 NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
131 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
132 .getInstance(DOMDataTreeChangeService.class);
133 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
134 changeService.registerDataTreeChangeListener(root, adapter);
136 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
137 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
138 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
139 .child(MyList1.class, new MyList1Key("Althea"));
140 writeTransaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
141 writeTransaction.commit();
142 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
144 writeTransaction = dataBroker.newWriteOnlyTransaction();
145 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
146 writeTransaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
147 writeTransaction.commit();
148 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
150 writeTransaction = dataBroker.newWriteOnlyTransaction();
151 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
152 writeTransaction.commit();
153 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
157 public void testJsonNotifs() throws Exception {
158 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
159 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
160 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
161 .getInstance(DOMDataTreeChangeService.class);
162 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
163 changeService.registerDataTreeChangeListener(root, adapter);
165 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
166 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
167 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
168 .child(MyList1.class, new MyList1Key("Althea"));
169 writeTransaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
170 writeTransaction.commit();
171 adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
173 writeTransaction = dataBroker.newWriteOnlyTransaction();
174 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
175 writeTransaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
176 writeTransaction.commit();
177 adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
179 writeTransaction = dataBroker.newWriteOnlyTransaction();
180 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
181 writeTransaction.commit();
182 adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));