Fixup Augmentable and Identifiable methods changing
[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 import org.json.JSONObject;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
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.controller.md.sal.rest.common.TestRestconfUtils;
32 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
33 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
34 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
35 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
36 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
37 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 import org.skyscreamer.jsonassert.JSONAssert;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
47     private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
48
49     private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
50     private static final String JSON_NOTIF_LEAVES_UPDATE =  "/listener-adapter-test/notif-leaves-update.json";
51     private static final String JSON_NOTIF_LEAVES_DEL =  "/listener-adapter-test/notif-leaves-del.json";
52     private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
53     private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
54     private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
55
56     private static YangInstanceIdentifier PATCH_CONT_YIID =
57             YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
58
59     private static SchemaContext schemaContext;
60
61     private DataBroker dataBroker;
62     private DOMDataBroker domDataBroker;
63     private ControllerContext controllerContext;
64
65     @BeforeClass
66     public static void init() {
67         schemaContext = YangParserTestUtils.parseYangResource(
68                 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
69     }
70
71     @Before
72     public void setUp() throws Exception {
73         dataBroker = getDataBroker();
74         domDataBroker = getDomBroker();
75         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
76     }
77
78     class ListenerAdapterTester extends ListenerAdapter {
79
80         private String lastNotification = null;
81
82         ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
83                               final NotificationOutputTypeGrouping.NotificationOutputType outputType,
84                               final boolean leafNodesOnly) {
85             super(path, streamName, outputType, controllerContext);
86             setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly);
87         }
88
89         @Override
90         protected void post(final Event event) {
91             this.lastNotification = event.getData();
92         }
93
94         public void assertGot(final String json) throws Exception {
95             long start = System.currentTimeMillis();
96             while (true) {
97                 if (lastNotification != null) {
98                     break;
99                 }
100                 if (System.currentTimeMillis() - start > 1000) {
101                     throw new Exception("TIMED OUT waiting for notification with " + json);
102                 }
103                 Thread.currentThread();
104                 Thread.sleep(200);
105             }
106             LOG.debug("Comparing {} {}", json, lastNotification);
107             JSONAssert.assertEquals(json, withFakeDate(lastNotification), false);
108             this.lastNotification = null;
109         }
110     }
111
112     static String withFakeDate(final String in) {
113         JSONObject doc = new JSONObject(in);
114         JSONObject notification = doc.getJSONObject("notification");
115         if (notification == null) {
116             return in;
117         }
118         notification.put("eventTime", "someDate");
119         return doc.toString();
120     }
121
122     private String getNotifJson(final String path) throws IOException, URISyntaxException {
123         URL url = getClass().getResource(path);
124         byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
125         return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
126     }
127
128     @Test
129     public void testJsonNotifsLeaves() throws Exception {
130         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
131                                         NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
132         DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
133                 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
134         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
135         changeService.registerDataTreeChangeListener(root, adapter);
136
137         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
138         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
139         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
140                 .child(MyList1.class, new MyList1Key("Althea"));
141         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
142         writeTransaction.submit();
143         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
144
145         writeTransaction = dataBroker.newWriteOnlyTransaction();
146         builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
147         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
148         writeTransaction.submit();
149         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
150
151         writeTransaction = dataBroker.newWriteOnlyTransaction();
152         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
153         writeTransaction.submit();
154         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
155     }
156
157     @Test
158     public void testJsonNotifs() throws Exception {
159         ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
160                 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
161         DOMDataTreeChangeService changeService = (DOMDataTreeChangeService)
162                 domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
163         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
164         changeService.registerDataTreeChangeListener(root, adapter);
165
166         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
167         MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
168         InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
169                 .child(MyList1.class, new MyList1Key("Althea"));
170         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
171         writeTransaction.submit();
172         adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
173
174         writeTransaction = dataBroker.newWriteOnlyTransaction();
175         builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
176         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
177         writeTransaction.submit();
178         adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
179
180         writeTransaction = dataBroker.newWriteOnlyTransaction();
181         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
182         writeTransaction.submit();
183         adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));
184     }
185 }