Merge "YANG Model update for A1 Adapter"
[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 org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.common.ResourcesFromDeviceManager;
33 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
35 import org.onap.ccsdk.features.sdnr.wt.database.config.EsConfig;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEntityDataProvider;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanService;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.NetconfNodeService.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.DataProviderMock;
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.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.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
52 import org.opendaylight.mdsal.binding.api.MountPointService;
53 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
54 import org.opendaylight.mdsal.binding.api.RpcProviderService;
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 public class TestDeviceManagerWithDatabase {
62
63     private static Path KARAF_ETC = Paths.get("etc");
64     private static DeviceManagerImpl deviceManager;
65     private static MountPointMock mountPoint;
66     private static DataBrokerNetconfMock dataBrokerNetconf;
67     private static IEntityDataProvider dataProvider = new IEntityDataProvider() {
68
69         @Override
70         public void setReadyStatus(boolean status) {
71
72         }
73
74         @Override
75         public DataProvider getDataProvider() {
76             return  new DataProviderMock();
77         }
78     };
79     private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
80
81     @BeforeClass
82     public static void before() throws InterruptedException, IOException {
83
84         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
85         // Call System property to get the classpath value
86         Path etc = KARAF_ETC;
87         delete(etc);
88
89         System.out.println("Create empty:" + etc.toString());
90         Files.createDirectories(etc);
91         //write db config for testing
92         ConfigurationFileRepresentation configfile = new ConfigurationFileRepresentation(etc.resolve("devicemanager.properties").toFile());
93         EsConfig dbConfig = new EsConfig(configfile);
94            dbConfig.setHosts(new HostInfo[] {new HostInfo("localhost",Integer.valueOf(System.getProperty("databaseport")!=null?System.getProperty("databaseport"):"49200")) });
95
96         configfile.save();
97         // Create mocks
98         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
99         dataBrokerNetconf = new DataBrokerNetconfMock();
100         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
101         mountPoint = new MountPointMock();
102         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
103         ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock();
104         MountPointService mountPointService = new MountPointServiceMock(mountPoint);
105         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
106         RpcProviderService rpcProviderRegistry = new RpcProviderRegistryMock();
107
108         // start using blueprint interface
109         String msg = "";
110         try {
111             deviceManager = new DeviceManagerImpl();
112
113             deviceManager.setDataBroker(dataBrokerNetconf);
114             deviceManager.setMountPointService(mountPointService);
115             deviceManager.setNotificationPublishService(notificationPublishService);
116             deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
117             deviceManager.setClusterSingletonService(clusterSingletonService);
118             deviceManager.setEntityDataProvider(dataProvider);
119             deviceManager.init();
120         } catch (Exception e) {
121             StringWriter sw = new StringWriter();
122             PrintWriter pw = new PrintWriter(sw);
123             e.printStackTrace(pw);
124             msg = sw.toString(); // stack trace as a string
125             e.printStackTrace();
126         }
127         readOnlyTransaction.close();
128         System.out.println("Initialization status: " + deviceManager.isDevicemanagerInitializationOk());
129         assertTrue("Devicemanager not initialized: " + msg, deviceManager.isDevicemanagerInitializationOk());
130         System.out.println("Initialization done");
131         waitfordatabase();
132     }
133
134     @AfterClass
135     public static void after() throws InterruptedException, IOException {
136
137         System.out.println("Start shutdown");
138         // close using blueprint interface
139         try {
140             deviceManager.close();
141         } catch (Exception e) {
142             System.out.println(e);
143         }
144         delete(KARAF_ETC);
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         System.out.println("Devicemanager started ----");
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         ResourcesFromDeviceManager.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     /*
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     @SuppressWarnings("unused")
334     private void waitForDeletion(ArchiveCleanService service, long timeout, long numberAtBeginning, String faultMessage) {
335         int numberEntries = 0;
336         while (timeout-- > 0) {
337             sleep(1000);
338             numberEntries = service.countOldEntries();
339             if (numberEntries <= 0) {
340                 break;
341             }
342         }
343         if (timeout == 0) {
344             fail(faultMessage + " Timeout at:" + timeout + " Entries at beginning " + numberAtBeginning
345                     + " remaining" + numberEntries);
346         }
347     }
348
349
350     private static void waitfordatabase() throws InterruptedException {
351 /*
352         System.out.println("Test1: Wait for database");
353         int timeout = DATABASETIMEOUTSECONDS;
354         while (!deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) {
355             System.out.println("Test1: " + timeout);
356             Thread.sleep(1000); // On second
357         }
358         System.out.println("Ddatabase initialized");
359 */
360     }
361
362     private static void sleep(int millis) {
363         try {
364             Thread.sleep(millis);
365         } catch (InterruptedException e) {
366             LOG.warn(e.getMessage());
367             Thread.interrupted();
368         }
369     }
370
371     private static void delete(Path etc) throws IOException {
372         if (Files.exists(etc)) {
373             System.out.println("Found and remove:" + etc.toString());
374             delete(etc.toFile());
375         }
376     }
377
378     private static void delete(File f) throws IOException {
379         if (f.isDirectory()) {
380             for (File c : f.listFiles()) {
381                 delete(c);
382             }
383         }
384         if (!f.delete()) {
385             throw new FileNotFoundException("Failed to delete file: " + f);
386         }
387     }
388
389     @SuppressWarnings("unused")
390     private String getContent(long archiveLimitSeconds, long esArchiveCheckIntervalSeconds) {
391         return "[dcae]\n" + "dcaeUserCredentials=admin:admin\n" + "dcaeUrl=http://localhost:45/abc\n"
392                 + "dcaeHeartbeatPeriodSeconds=120\n" + "dcaeTestCollector=no\n" + "\n" + "[aots]\n"
393                 + "userPassword=passwd\n" + "soapurladd=off\n" + "soapaddtimeout=10\n" + "soapinqtimeout=20\n"
394                 + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n" + "assignedto=userid\n"
395                 + "addtemplate=addreq.tmpl.xml\n" + "severitypassthrough=critical,major,minor,warning\n"
396                 + "systemuser=user\n" + "prt-offset=1200\n" + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n"
397                 + "#smtpUsername=\n" + "#smtpPassword=\n" + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n"
398                 + "esCluster=sendateodl5\n" + "esArchiveLifetimeSeconds=" + archiveLimitSeconds + "\n" + "esArchiveCheckIntervalSeconds="
399                 + esArchiveCheckIntervalSeconds + "\n" + "\n" + "[aai]\n" + "#keep comment\n"
400                 + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=off\n" + "aaiUserCredentials=AAI:AAI\n"
401                 + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n" + "aaiApiVersion=aai/v13\n"
402                 + "aaiPropertiesFile=aaiclient.properties\n" + "\n" + "[pm]\n" + "pmCluster=sendateodl5\n"
403                 + "pmEnabled=true\n" + "[toggleAlarmFilter]\n" + "taEnabled=false\n" + "taDelay=5555\n" + "";
404     }
405
406
407 }