Fix malformed time stamp
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / test / TestDeviceManagerWithDatabase.java
1 /*******************************************************************************
2  * ============LICENSE_START======================================================= ONAP : ccsdk
3  * feature sdnr wt ================================================================================
4  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
5  * ================================================================================ Licensed under
6  * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
7  * with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License. ============LICENSE_END=========================================================
15  ******************************************************************************/
16 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
17
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.concurrent.TimeUnit;
30
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanService;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseWebAPIClient;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.Resources;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.Capabilities;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerService.Action;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.ClusterSingletonServiceProviderMock;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.DataBrokerNetconfMock;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointMock;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointServiceMock;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.NotificationPublishServiceMock;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.RpcProviderRegistryMock;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.DBCleanServiceHelper;
49 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211Mock;
50 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211pMock;
51 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint12Mock;
52 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
53 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
54 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
55 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 @SuppressWarnings("deprecation")
62 public class TestDeviceManagerWithDatabase {
63
64     private static int DATABASETIMEOUTSECONDS = 30;
65
66     private static Path KARAF_ETC = Paths.get("etc");
67     private static DeviceManagerImpl deviceManager;
68     private static MountPointMock mountPoint;
69     private static DataBrokerNetconfMock dataBrokerNetconf;
70
71     private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
72
73
74
75     @BeforeClass
76     public static void before() throws InterruptedException, IOException {
77
78         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
79         // Call System property to get the classpath value
80         Path etc = KARAF_ETC;
81         delete(etc);
82
83         System.out.println("Create empty:" + etc.toString());
84         Files.createDirectories(etc);
85
86         // Create mocks
87         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
88         dataBrokerNetconf = new DataBrokerNetconfMock();
89         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
90         mountPoint = new MountPointMock();
91         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
92         ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock();
93                 MountPointService mountPointService = new MountPointServiceMock(mountPoint);
94         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
95         RpcProviderRegistry rpcProviderRegistry = new RpcProviderRegistryMock();
96
97         // start using blueprint interface
98         String msg = "";
99         try {
100             deviceManager = new DeviceManagerImpl();
101
102             deviceManager.setDataBroker(dataBrokerNetconf);
103             deviceManager.setMountPointService(mountPointService);
104             deviceManager.setNotificationPublishService(notificationPublishService);
105             deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
106             deviceManager.setClusterSingletonService(clusterSingletonService);
107             deviceManager.init();
108         } catch (Exception e) {
109             StringWriter sw = new StringWriter();
110             PrintWriter pw = new PrintWriter(sw);
111             e.printStackTrace(pw);
112             msg = sw.toString(); // stack trace as a string
113             e.printStackTrace();
114         }
115         readOnlyTransaction.close();
116         System.out.println("Initialization status: " + deviceManager.isDevicemanagerInitializationOk());
117         assertTrue("Devicemanager not initialized: " + msg, deviceManager.isDevicemanagerInitializationOk());
118         System.out.println("Initialization done");
119         waitfordatabase();
120     }
121
122     @AfterClass
123     public static void after() throws InterruptedException, IOException {
124
125         System.out.println("Start shutdown");
126         // close using blueprint interface
127         try {
128             deviceManager.close();
129         } catch (Exception e) {
130             System.out.println(e);
131         }
132         delete(KARAF_ETC);
133
134     }
135
136     @Test
137     public void test0() throws InterruptedException {
138         HtDatabaseWebAPIClient client = new HtDatabaseWebAPIClient();
139         try {
140             String response = client.sendRequest("/mwtn/mediator-server/_search", "GET",
141                     new JSONObject("{\"match\":{\"id\":id}}"));
142             System.out.println(response);
143         } catch (JSONException | IOException e) {
144             e.printStackTrace();
145         }
146     }
147
148     @Test
149     public void test2() {
150         System.out.println("Test2: slave mountpoint");
151
152         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
153         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
154         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
155         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
156
157         mountPoint.setDatabrokerAbsent(true);
158         NodeId nodeId = new NodeId("mountpointTest2");
159         try {
160             deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
161         } catch (Exception e) {
162             e.printStackTrace();
163             fail("Exception received.");
164         }
165
166         readOnlyTransaction.close();
167         System.out.println("Test2: Done");
168
169     }
170
171     @Test
172     public void test3() {
173         System.out.println("Test3: master mountpoint ONF Model 12");
174
175         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
176         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
177         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
178         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
179
180         mountPoint.setDatabrokerAbsent(false);
181         NodeId nodeId = new NodeId("mountpointTest3");
182
183         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
184         System.out.println("Node capabilites: " + capabilities);
185
186         try {
187             deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
188         } catch (Exception e) {
189             e.printStackTrace();
190             fail("Exception received.");
191         }
192
193         readOnlyTransaction.sendProblemNotification();
194         try {
195             Thread.sleep(500);
196         } catch (InterruptedException e) {
197         }
198
199         readOnlyTransaction.close();
200         System.out.println("Test3: Done");
201
202     }
203
204     @Test
205     public void test4() {
206         System.out.println("Test4: master mountpoint ONF Model 1211");
207
208         ReadOnlyTransactionMountpoint1211Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211Mock();
209         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
210         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
211
212         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
213         mountPoint.setDatabrokerAbsent(false);
214         NodeId nodeId = new NodeId("mountpointTest4");
215
216         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
217         System.out.println("Node capabilites: " + capabilities);
218
219         try {
220             deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
221         } catch (Exception e) {
222             e.printStackTrace();
223             fail("Exception received.");
224         }
225
226         readOnlyTransaction.sendProblemNotification();
227         try {
228             Thread.sleep(500);
229         } catch (InterruptedException e) {
230             Thread.interrupted();
231         }
232
233         readOnlyTransaction.close();
234         System.out.println("Test4: Done");
235
236     }
237
238     @Test
239     public void test5() {
240         System.out.println("Test5: master mountpoint ONF Model 1211p");
241
242         ReadOnlyTransactionMountpoint1211pMock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211pMock();
243         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
244         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
245
246         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
247         mountPoint.setDatabrokerAbsent(false);
248         NodeId nodeId = new NodeId("mountpointTest5");
249
250         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
251         System.out.println("Node capabilites: " + capabilities);
252
253         try {
254             deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
255         } catch (Exception e) {
256             e.printStackTrace();
257             fail("Exception received.");
258         }
259         readOnlyTransaction.sendProblemNotification();
260         try {
261             Thread.sleep(500);
262         } catch (InterruptedException e) {
263             Thread.interrupted();
264         }
265
266         readOnlyTransaction.close();
267         System.out.println("Test5: Done");
268
269     }
270
271     @Test
272     public void test6() {
273
274         System.out.println("Test6: Write zip data file file");
275         File testFile = new File("etc/elasticsearch_update.zip");
276         Resources.extractFileTo("elasticsearch_update.zip", testFile);
277         int wait = 130;
278         while (testFile.exists() && wait-- > 0) {
279             System.out.println("Waiting " + wait);
280             try {
281                 Thread.sleep(1000);
282             } catch (InterruptedException e) {
283                 Thread.interrupted();
284             }
285         }
286
287
288         System.out.println("Test6: Done");
289
290     }
291
292     @Test
293     public void test7() throws Exception {
294         final int NUM = 5;
295         final int ARCHIVE_DAYS = 30;
296         final long ARCHIVE_LIMIT_SEC = TimeUnit.SECONDS.convert(ARCHIVE_DAYS, TimeUnit.DAYS);
297         final long ARCHIVE_INTERVAL_SEC = 10;
298         File propFile = KARAF_ETC.resolve("devicemanager.properties").toFile();
299
300         ArchiveCleanService service = deviceManager.getArchiveCleanService();
301         DBCleanServiceHelper helper = new DBCleanServiceHelper(deviceManager);
302
303         // setEsConfg
304         TestDevMgrPropertiesFile.writeFile(propFile, getContent(ARCHIVE_LIMIT_SEC, ARCHIVE_INTERVAL_SEC));
305         //give time to read file
306         sleep(5);
307         System.out.println("Archive clean service configuration "+service);
308         System.out.println("To delete elements older: "+service.getDateForOldElements());
309         System.out.println("Status of elements is: "+service.countOldEntries());
310
311         // create old data and check if the will be cleaned completely
312         int elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+5, 0 /*Hours*/);
313         System.out.println("Written elements are: "+elements);
314
315         waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elements, "Entries are not cleared completely as expected");
316
317         // create partial old and newer data and check that only half of all data are cleaned
318         // New data are not counted as "old" ..
319         int elementsToRemove = elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+5, 0);
320         elements += helper.writeDataToLogs(NUM, ARCHIVE_DAYS-5, 0);
321         waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elementsToRemove, "Entries are not cleared exactly half as expected");
322
323         // create only newer data and check that nothing is cleaned
324         elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+2, 0);
325         waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elements, "Some entries were removed, but shouldn't.");
326
327         service.close();
328     }
329
330     // ********************* Private
331
332     private void waitForDeletion(ArchiveCleanService service, long timeout, long numberAtBeginning, String faultMessage) {
333         int numberEntries = 0;
334         while (timeout-- > 0) {
335             sleep(1000);
336             numberEntries = service.countOldEntries();
337             if (numberEntries <= 0) {
338                 break;
339             }
340         }
341         if (timeout == 0) {
342             fail(faultMessage + " Timeout at:" + timeout + " Entries at beginning " + numberAtBeginning
343                     + " remaining" + numberEntries);
344         }
345     }
346
347
348     private static void waitfordatabase() throws InterruptedException {
349
350         System.out.println("Test1: Wait for database");
351         int timeout = DATABASETIMEOUTSECONDS;
352         while (!deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) {
353             System.out.println("Test1: " + timeout);
354             Thread.sleep(1000); // On second
355         }
356         System.out.println("Ddatabase initialized");
357     }
358
359     private static void sleep(int millis) {
360         try {
361             Thread.sleep(millis);
362         } catch (InterruptedException e) {
363             LOG.warn(e.getMessage());
364             Thread.interrupted();
365         }
366     }
367
368     private static void delete(Path etc) throws IOException {
369         if (Files.exists(etc)) {
370             System.out.println("Found and remove:" + etc.toString());
371             delete(etc.toFile());
372         }
373     }
374
375     private static void delete(File f) throws IOException {
376         if (f.isDirectory()) {
377             for (File c : f.listFiles()) {
378                 delete(c);
379             }
380         }
381         if (!f.delete()) {
382             throw new FileNotFoundException("Failed to delete file: " + f);
383         }
384     }
385
386     private String getContent(long archiveLimitSeconds, long esArchiveCheckIntervalSeconds) {
387         return "[dcae]\n" + "dcaeUserCredentials=admin:admin\n" + "dcaeUrl=http://localhost:45/abc\n"
388                 + "dcaeHeartbeatPeriodSeconds=120\n" + "dcaeTestCollector=no\n" + "\n" + "[aots]\n"
389                 + "userPassword=passwd\n" + "soapurladd=off\n" + "soapaddtimeout=10\n" + "soapinqtimeout=20\n"
390                 + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n" + "assignedto=userid\n"
391                 + "addtemplate=addreq.tmpl.xml\n" + "severitypassthrough=critical,major,minor,warning\n"
392                 + "systemuser=user\n" + "prt-offset=1200\n" + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n"
393                 + "#smtpUsername=\n" + "#smtpPassword=\n" + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n"
394                 + "esCluster=sendateodl5\n" + "esArchiveLifetimeSeconds=" + archiveLimitSeconds + "\n" + "esArchiveCheckIntervalSeconds="
395                 + esArchiveCheckIntervalSeconds + "\n" + "\n" + "[aai]\n" + "#keep comment\n"
396                 + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=off\n" + "aaiUserCredentials=AAI:AAI\n"
397                 + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n" + "aaiApiVersion=aai/v13\n"
398                 + "aaiPropertiesFile=aaiclient.properties\n" + "\n" + "[pm]\n" + "pmCluster=sendateodl5\n"
399                 + "pmEnabled=true\n" + "[toggleAlarmFilter]\n" + "taEnabled=false\n" + "taDelay=5555\n" + "";
400     }
401
402
403 }