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