Transition ListenerAdapter to ClusteredDOMDataTreeListener
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / netconf / sal / 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
9 package org.opendaylight.netconf.sal.streams.listeners;
10
11 import static java.time.Instant.EPOCH;
12
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.nio.charset.StandardCharsets;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.util.Optional;
20
21 import org.json.JSONObject;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
30 import org.opendaylight.controller.md.sal.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.SchemaContext;
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;
44
45 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
46     private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
47
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";
54
55     private static YangInstanceIdentifier PATCH_CONT_YIID =
56             YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
57
58     private DataBroker dataBroker;
59     private DOMDataBroker domDataBroker;
60
61     @Before
62     public void setUp() throws Exception {
63         dataBroker = getDataBroker();
64         domDataBroker = getDomBroker();
65         SchemaContext sc = YangParserTestUtils.parseYangSource(
66                 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
67         ControllerContext.getInstance().setGlobalSchema(sc);
68     }
69
70     class ListenerAdapterTester extends ListenerAdapter {
71
72         private String lastNotification = null;
73
74         ListenerAdapterTester(YangInstanceIdentifier path, String streamName,
75                               NotificationOutputTypeGrouping.NotificationOutputType outputType, boolean leafNodesOnly) {
76             super(path, streamName, outputType);
77             setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly);
78         }
79
80         @Override
81         protected void post(final Event event) {
82             this.lastNotification = event.getData();
83         }
84
85         public void assertGot(String json) throws Exception {
86             long start = System.currentTimeMillis();
87             while (true) {
88                 if (lastNotification != null) {
89                     break;
90                 }
91                 if (System.currentTimeMillis() - start > 1000) {
92                     throw new Exception("TIMED OUT waiting for notification with " + json);
93                 }
94                 Thread.currentThread().sleep(200);
95             }
96             LOG.debug("Comparing {} {}", json, lastNotification);
97             JSONAssert.assertEquals(json, withFakeDate(lastNotification), false);
98             this.lastNotification = null;
99         }
100     }
101
102     static String withFakeDate(String in) {
103         JSONObject doc = new JSONObject(in);
104         JSONObject notification = doc.getJSONObject("notification");
105         if (notification == null) {
106             return in;
107         }
108         notification.put("eventTime", "someDate");
109         return doc.toString();
110     }
111
112     private String getNotifJson(String path) throws IOException, URISyntaxException {
113         URL url = getClass().getResource(path);
114         byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
115         return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
116     }
117
118     @Test
119     public void testJsonNotifsLeaves() throws Exception {
120         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
121                                         NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
122         DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
123                 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
124         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
125         changeService.registerDataTreeChangeListener(root, adapter);
126
127         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
128         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
129         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
130                 .child(MyList1.class, new MyList1Key("Althea"));
131         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
132         writeTransaction.submit();
133         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
134
135         writeTransaction = dataBroker.newWriteOnlyTransaction();
136         builder = new MyList1Builder().setKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
137         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
138         writeTransaction.submit();
139         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
140
141         writeTransaction = dataBroker.newWriteOnlyTransaction();
142         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
143         writeTransaction.submit();
144         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
145     }
146
147     @Test
148     public void testJsonNotifs() throws Exception {
149         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
150                 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
151         DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
152                 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
153         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
154         changeService.registerDataTreeChangeListener(root, adapter);
155
156         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
157         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
158         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
159                 .child(MyList1.class, new MyList1Key("Althea"));
160         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
161         writeTransaction.submit();
162         adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
163
164         writeTransaction = dataBroker.newWriteOnlyTransaction();
165         builder = new MyList1Builder().setKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
166         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
167         writeTransaction.submit();
168         adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
169
170         writeTransaction = dataBroker.newWriteOnlyTransaction();
171         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
172         writeTransaction.submit();
173         adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));
174     }
175 }