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
9 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
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;
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;
61 @SuppressWarnings("deprecation")
62 public class TestDeviceManagerWithDatabase {
64 private static int DATABASETIMEOUTSECONDS = 30;
66 private static Path KARAF_ETC = Paths.get("etc");
67 private static DeviceManagerImpl deviceManager;
68 private static MountPointMock mountPoint;
69 private static DataBrokerNetconfMock dataBrokerNetconf;
71 private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
76 public static void before() throws InterruptedException, IOException {
78 System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
79 // Call System property to get the classpath value
83 System.out.println("Create empty:" + etc.toString());
84 Files.createDirectories(etc);
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();
97 // start using blueprint interface
100 deviceManager = new DeviceManagerImpl();
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
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");
123 public static void after() throws InterruptedException, IOException {
125 System.out.println("Start shutdown");
126 // close using blueprint interface
128 deviceManager.close();
129 } catch (Exception e) {
130 System.out.println(e);
137 public void test0() throws InterruptedException {
138 HtDatabaseWebAPIClient client = new HtDatabaseWebAPIClient();
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) {
149 public void test2() {
150 System.out.println("Test2: slave mountpoint");
152 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
153 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
154 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
155 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
157 mountPoint.setDatabrokerAbsent(true);
158 NodeId nodeId = new NodeId("mountpointTest2");
160 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
161 } catch (Exception e) {
163 fail("Exception received.");
166 readOnlyTransaction.close();
167 System.out.println("Test2: Done");
172 public void test3() {
173 System.out.println("Test3: master mountpoint ONF Model 12");
175 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
176 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
177 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
178 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
180 mountPoint.setDatabrokerAbsent(false);
181 NodeId nodeId = new NodeId("mountpointTest3");
183 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
184 System.out.println("Node capabilites: " + capabilities);
187 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
188 } catch (Exception e) {
190 fail("Exception received.");
193 readOnlyTransaction.sendProblemNotification();
196 } catch (InterruptedException e) {
199 readOnlyTransaction.close();
200 System.out.println("Test3: Done");
205 public void test4() {
206 System.out.println("Test4: master mountpoint ONF Model 1211");
208 ReadOnlyTransactionMountpoint1211Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211Mock();
209 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
210 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
212 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
213 mountPoint.setDatabrokerAbsent(false);
214 NodeId nodeId = new NodeId("mountpointTest4");
216 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
217 System.out.println("Node capabilites: " + capabilities);
220 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
221 } catch (Exception e) {
223 fail("Exception received.");
226 readOnlyTransaction.sendProblemNotification();
229 } catch (InterruptedException e) {
230 Thread.interrupted();
233 readOnlyTransaction.close();
234 System.out.println("Test4: Done");
239 public void test5() {
240 System.out.println("Test5: master mountpoint ONF Model 1211p");
242 ReadOnlyTransactionMountpoint1211pMock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211pMock();
243 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
244 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
246 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
247 mountPoint.setDatabrokerAbsent(false);
248 NodeId nodeId = new NodeId("mountpointTest5");
250 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
251 System.out.println("Node capabilites: " + capabilities);
254 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
255 } catch (Exception e) {
257 fail("Exception received.");
259 readOnlyTransaction.sendProblemNotification();
262 } catch (InterruptedException e) {
263 Thread.interrupted();
266 readOnlyTransaction.close();
267 System.out.println("Test5: Done");
272 public void test6() {
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);
278 while (testFile.exists() && wait-- > 0) {
279 System.out.println("Waiting " + wait);
282 } catch (InterruptedException e) {
283 Thread.interrupted();
288 System.out.println("Test6: Done");
293 public void test7() throws Exception {
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();
300 ArchiveCleanService service = deviceManager.getArchiveCleanService();
301 DBCleanServiceHelper helper = new DBCleanServiceHelper(deviceManager);
304 TestDevMgrPropertiesFile.writeFile(propFile, getContent(ARCHIVE_LIMIT_SEC, ARCHIVE_INTERVAL_SEC));
305 //give time to read file
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());
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);
315 waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elements, "Entries are not cleared completely as expected");
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");
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.");
330 // ********************* Private
332 private void waitForDeletion(ArchiveCleanService service, long timeout, long numberAtBeginning, String faultMessage) {
333 int numberEntries = 0;
334 while (timeout-- > 0) {
336 numberEntries = service.countOldEntries();
337 if (numberEntries <= 0) {
342 fail(faultMessage + " Timeout at:" + timeout + " Entries at beginning " + numberAtBeginning
343 + " remaining" + numberEntries);
348 private static void waitfordatabase() throws InterruptedException {
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
356 System.out.println("Ddatabase initialized");
359 private static void sleep(int millis) {
361 Thread.sleep(millis);
362 } catch (InterruptedException e) {
363 LOG.warn(e.getMessage());
364 Thread.interrupted();
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());
375 private static void delete(File f) throws IOException {
376 if (f.isDirectory()) {
377 for (File c : f.listFiles()) {
382 throw new FileNotFoundException("Failed to delete file: " + f);
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" + "";