From 4186aae5531a82c9f0227181866682bdd1d9cb93 Mon Sep 17 00:00:00 2001 From: Herbert Eiselt Date: Fri, 29 Mar 2019 13:35:13 +0100 Subject: SDN-R add historical alarm viewer Add function to devicemanager Change-Id: Idfac3926aad4e4d464763769ba16694cf15810f7 Issue-ID: SDNC-503 Signed-off-by: Herbert Eiselt --- .../test/TestDevMgrPropertiesFile.java | 4 +- .../test/TestDeviceManagerWithDatabase.java | 183 +++++++++++++++------ .../mock/ClusterSingletonServiceProviderMock.java | 19 +++ .../test/util/DBCleanServiceHelper.java | 91 ++++++++++ 4 files changed, 248 insertions(+), 49 deletions(-) create mode 100644 sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/mock/ClusterSingletonServiceProviderMock.java create mode 100644 sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/util/DBCleanServiceHelper.java (limited to 'sdnr/wt/devicemanager/provider/src/test/java/org/onap') diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevMgrPropertiesFile.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevMgrPropertiesFile.java index 74d2d2263..1f488b2e9 100644 --- a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevMgrPropertiesFile.java +++ b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDevMgrPropertiesFile.java @@ -163,14 +163,14 @@ public class TestDevMgrPropertiesFile { } - private void sleep(int milliseconds) { + private static void sleep(int milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) { } } - private void writeFile(File f, String content) { + public static void writeFile(File f, String content) { try { Files.asCharSink(f, StandardCharsets.UTF_8).write(content); } catch (IOException e) { diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDeviceManagerWithDatabase.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDeviceManagerWithDatabase.java index 661194c38..435e8e151 100644 --- a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDeviceManagerWithDatabase.java +++ b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDeviceManagerWithDatabase.java @@ -1,22 +1,17 @@ /******************************************************************************* - * ============LICENSE_START======================================================= - * ONAP : ccsdk feature sdnr wt - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * ============LICENSE_START======================================================= ONAP : ccsdk + * feature sdnr wt ================================================================================ + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================ Licensed under + * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. ============LICENSE_END========================================================= ******************************************************************************/ package org.onap.ccsdk.features.sdnr.wt.devicemanager.test; @@ -30,27 +25,35 @@ import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Scanner; +import java.util.concurrent.TimeUnit; import org.json.JSONException; import org.json.JSONObject; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanService; import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseWebAPIClient; import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.Resources; import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.Capabilities; import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl; import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerService.Action; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.ClusterSingletonServiceProviderMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.DataBrokerNetconfMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointServiceMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.NotificationPublishServiceMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.RpcProviderRegistryMock; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.DBCleanServiceHelper; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211Mock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211pMock; import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint12Mock; import org.opendaylight.controller.md.sal.binding.api.MountPointService; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.slf4j.Logger; @@ -63,7 +66,7 @@ public class TestDeviceManagerWithDatabase { private static Path KARAF_ETC = Paths.get("etc"); private static DeviceManagerImpl deviceManager; - private static MountPointMock mountPoint; + private static MountPointMock mountPoint; private static DataBrokerNetconfMock dataBrokerNetconf; private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class); @@ -73,25 +76,26 @@ public class TestDeviceManagerWithDatabase { @BeforeClass public static void before() throws InterruptedException, IOException { - System.out.println("Logger: "+LOG.getClass().getName() + " " + LOG.getName()); + System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName()); // Call System property to get the classpath value Path etc = KARAF_ETC; delete(etc); - System.out.println("Create empty:"+etc.toString()); + System.out.println("Create empty:" + etc.toString()); Files.createDirectories(etc); - //Create mocks + // Create mocks ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock(); dataBrokerNetconf = new DataBrokerNetconfMock(); dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction); mountPoint = new MountPointMock(); mountPoint.setReadOnlyTransaction(readOnlyTransaction); - MountPointService mountPointService = new MountPointServiceMock(mountPoint); + ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock(); + MountPointService mountPointService = new MountPointServiceMock(mountPoint); NotificationPublishService notificationPublishService = new NotificationPublishServiceMock(); RpcProviderRegistry rpcProviderRegistry = new RpcProviderRegistryMock(); - //start using blueprint interface + // start using blueprint interface String msg = ""; try { deviceManager = new DeviceManagerImpl(); @@ -100,7 +104,7 @@ public class TestDeviceManagerWithDatabase { deviceManager.setMountPointService(mountPointService); deviceManager.setNotificationPublishService(notificationPublishService); deviceManager.setRpcProviderRegistry(rpcProviderRegistry); - + deviceManager.setClusterSingletonService(clusterSingletonService); deviceManager.init(); } catch (Exception e) { StringWriter sw = new StringWriter(); @@ -110,17 +114,17 @@ public class TestDeviceManagerWithDatabase { e.printStackTrace(); } readOnlyTransaction.close(); - System.out.println("Initialization status: "+deviceManager.isDevicemanagerInitializationOk()); - assertTrue("Devicemanager not initialized: "+msg, deviceManager.isDevicemanagerInitializationOk()); + System.out.println("Initialization status: " + deviceManager.isDevicemanagerInitializationOk()); + assertTrue("Devicemanager not initialized: " + msg, deviceManager.isDevicemanagerInitializationOk()); System.out.println("Initialization done"); - + waitfordatabase(); } @AfterClass public static void after() throws InterruptedException, IOException { System.out.println("Start shutdown"); - //close using blueprint interface + // close using blueprint interface try { deviceManager.close(); } catch (Exception e) { @@ -142,18 +146,6 @@ public class TestDeviceManagerWithDatabase { } } - @Test - public void test1() throws InterruptedException { - - System.out.println("Test1: Wait for database"); - int timeout = DATABASETIMEOUTSECONDS; - while ( !deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) { - System.out.println("Test1: "+timeout); - Thread.sleep(1000); //On second - } - System.out.println("Test1: database initialized"); - } - @Test public void test2() { System.out.println("Test2: slave mountpoint"); @@ -190,7 +182,7 @@ public class TestDeviceManagerWithDatabase { NodeId nodeId = new NodeId("mountpointTest3"); Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode); - System.out.println("Node capabilites: "+capabilities); + System.out.println("Node capabilites: " + capabilities); try { deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode); @@ -223,7 +215,7 @@ public class TestDeviceManagerWithDatabase { NodeId nodeId = new NodeId("mountpointTest4"); Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode); - System.out.println("Node capabilites: "+capabilities); + System.out.println("Node capabilites: " + capabilities); try { deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode); @@ -257,7 +249,7 @@ public class TestDeviceManagerWithDatabase { NodeId nodeId = new NodeId("mountpointTest5"); Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode); - System.out.println("Node capabilites: "+capabilities); + System.out.println("Node capabilites: " + capabilities); try { deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode); @@ -283,9 +275,9 @@ public class TestDeviceManagerWithDatabase { System.out.println("Test6: Write zip data file file"); File testFile = new File("etc/elasticsearch_update.zip"); Resources.extractFileTo("elasticsearch_update.zip", testFile); - int wait=130; - while ( testFile.exists() && wait-- > 0) { - System.out.println("Waiting "+wait); + int wait = 130; + while (testFile.exists() && wait-- > 0) { + System.out.println("Waiting " + wait); try { Thread.sleep(1000); } catch (InterruptedException e) { @@ -298,11 +290,92 @@ public class TestDeviceManagerWithDatabase { } - //********************* Private + @Test + public void test7() throws Exception { + final int NUM = 5; + final int ARCHIVE_DAYS = 30; + final long ARCHIVE_LIMIT_SEC = TimeUnit.SECONDS.convert(ARCHIVE_DAYS, TimeUnit.DAYS); + final long ARCHIVE_INTERVAL_SEC = 10; + File propFile = KARAF_ETC.resolve("devicemanager.properties").toFile(); + + ArchiveCleanService service = deviceManager.getArchiveCleanService(); + DBCleanServiceHelper helper = new DBCleanServiceHelper(deviceManager); + + // setEsConfg + TestDevMgrPropertiesFile.writeFile(propFile, getContent(ARCHIVE_LIMIT_SEC, ARCHIVE_INTERVAL_SEC)); + //give time to read file + sleep(5); + System.out.println("Archive clean service configuration "+service); + System.out.println("To delete elements older: "+service.getDateForOldElements()); + System.out.println("Status of elements is: "+service.countOldEntries()); + + // create old data and check if the will be cleaned completely + int elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+5, 0 /*Hours*/); + System.out.println("Written elements are: "+elements); + + waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elements, "Entries are not cleared completely as expected"); + + // create partial old and newer data and check that only half of all data are cleaned + // New data are not counted as "old" .. + int elementsToRemove = elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+5, 0); + elements += helper.writeDataToLogs(NUM, ARCHIVE_DAYS-5, 0); + waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elementsToRemove, "Entries are not cleared exactly half as expected"); + + // create only newer data and check that nothing is cleaned + elements = helper.writeDataToLogs(NUM, ARCHIVE_DAYS+2, 0); + waitForDeletion(service, 2 * ARCHIVE_INTERVAL_SEC, elements, "Some entries were removed, but shouldn't."); + + service.close(); + } + + // ********************* Private + + private void waitForDeletion(ArchiveCleanService service, long timeout, long numberAtBeginning, String faultMessage) { + int numberEntries = 0; + while (timeout-- > 0) { + sleep(1000); + numberEntries = service.countOldEntries(); + if (numberEntries <= 0) { + break; + } + } + if (timeout == 0) { + fail(faultMessage + " Timeout at:" + timeout + " Entries at beginning " + numberAtBeginning + + " remaining" + numberEntries); + } + } + + + private static void waitfordatabase() throws InterruptedException { + + System.out.println("Test1: Wait for database"); + int timeout = DATABASETIMEOUTSECONDS; + while (!deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) { + System.out.println("Test1: " + timeout); + Thread.sleep(1000); // On second + } + System.out.println("Ddatabase initialized"); + } + + private static void sleep(int millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + LOG.warn(e.getMessage()); + Thread.interrupted(); + } + } + + private static void waitEnter() { + Scanner sc = new Scanner(System.in); + System.out.println("Enter"); + sc.next(); + sc.close(); + } private static void delete(Path etc) throws IOException { if (Files.exists(etc)) { - System.out.println("Found and remove:"+etc.toString()); + System.out.println("Found and remove:" + etc.toString()); delete(etc.toFile()); } } @@ -318,5 +391,21 @@ public class TestDeviceManagerWithDatabase { } } + private String getContent(long archiveLimitSeconds, long esArchiveCheckIntervalSeconds) { + return "[dcae]\n" + "dcaeUserCredentials=admin:admin\n" + "dcaeUrl=http://localhost:45/abc\n" + + "dcaeHeartbeatPeriodSeconds=120\n" + "dcaeTestCollector=no\n" + "\n" + "[aots]\n" + + "userPassword=passwd\n" + "soapurladd=off\n" + "soapaddtimeout=10\n" + "soapinqtimeout=20\n" + + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n" + "assignedto=userid\n" + + "addtemplate=addreq.tmpl.xml\n" + "severitypassthrough=critical,major,minor,warning\n" + + "systemuser=user\n" + "prt-offset=1200\n" + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n" + + "#smtpUsername=\n" + "#smtpPassword=\n" + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n" + + "esCluster=sendateodl5\n" + "esArchiveLifetimeSeconds=" + archiveLimitSeconds + "\n" + "esArchiveCheckIntervalSeconds=" + + esArchiveCheckIntervalSeconds + "\n" + "\n" + "[aai]\n" + "#keep comment\n" + + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=off\n" + "aaiUserCredentials=AAI:AAI\n" + + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n" + "aaiApiVersion=aai/v13\n" + + "aaiPropertiesFile=aaiclient.properties\n" + "\n" + "[pm]\n" + "pmCluster=sendateodl5\n" + + "pmEnabled=true\n" + "[toggleAlarmFilter]\n" + "taEnabled=false\n" + "taDelay=5555\n" + ""; + } + } diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/mock/ClusterSingletonServiceProviderMock.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/mock/ClusterSingletonServiceProviderMock.java new file mode 100644 index 000000000..30b64817a --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/mock/ClusterSingletonServiceProviderMock.java @@ -0,0 +1,19 @@ +package org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock; + +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration; + +public class ClusterSingletonServiceProviderMock implements ClusterSingletonServiceProvider { + + @Override + public void close() throws Exception { + + } + + @Override + public ClusterSingletonServiceRegistration registerClusterSingletonService(ClusterSingletonService service) { + return null; + } + +} diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/util/DBCleanServiceHelper.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/util/DBCleanServiceHelper.java new file mode 100644 index 000000000..4e46e1800 --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/util/DBCleanServiceHelper.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * ============LICENSE_END========================================================================== + ******************************************************************************/ +package org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util; + +import java.util.Date; +import java.util.concurrent.TimeUnit; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalDateAndTime; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.database.types.EsEventOdluxLog; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.impl.IndexMwtnService; + +public class DBCleanServiceHelper { + + private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter(); + + private final HtDatabaseEventsService databaseEventService; + private final IndexMwtnService mwtnService; + + /** + * Helper to fill data into the database + * @param deviceManager devicemanger to get services + */ + public DBCleanServiceHelper(DeviceManagerImpl deviceManager) { + this.databaseEventService = deviceManager.getDatabaseClientEvents(); + this.mwtnService = deviceManager.getMwtnService(); + } + + /** + * Write data into database with specific date and content profile. + * @param number of data to be written for each log + * @param days starting day, relative to actual date + * @param hours starting hour ... increased by one hour for each write + * @return integer with the amount of written data + */ + public int writeDataToLogs(int number, int days, int hours) { + int res = 0; + for (Integer t=0; t < number; t++) { //Test "sdnevents", "eventlog" + ObjectCreationNotificationXml notificationXml = new ObjectCreationNotificationXml( + "Testpoint"+t, t.toString(), getInternalDateAndTime(days, hours+t), "ObjectId"+t); + databaseEventService.writeEventLog(notificationXml); + res++; + } + + for (Integer t=0; t < number; t++) { //Test "sdnevents", "faultlog" + ProblemNotificationXml fault = new ProblemNotificationXml( + "ProblemNode"+t, "Problemuuid", "Problemname", InternalSeverity.Major, t.toString(), getInternalDateAndTime(days, hours+t)); + databaseEventService.writeFaultLog(fault); + res++; + } + + for (Integer t=0; t < number; t++) { //Test "mwtn", "log" + EsEventOdluxLog odluxEvent = new EsEventOdluxLog("Odluxevent"+t, "Problemuuid", "Message", getInternalDateAndTime(days, hours+t)); + mwtnService.writeOdluxEventForTestpurpose(odluxEvent); + res++; + } + + return res; + } + + /************************************************************** + * Private section + */ + + private InternalDateAndTime getInternalDateAndTime(int days, int hours) { + Date actual = new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(days, TimeUnit.DAYS) - TimeUnit.MILLISECONDS.convert(hours, TimeUnit.HOURS)); + InternalDateAndTime timeStamp = InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp(actual)); + return timeStamp; + } + + +} -- cgit 1.2.3-korg