SDN-R add historical alarm viewer
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / test / util / DBCleanServiceHelper.java
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util;
19
20 import java.util.Date;
21 import java.util.concurrent.TimeUnit;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalDateAndTime;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.database.types.EsEventOdluxLog;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.impl.IndexMwtnService;
31
32 public class DBCleanServiceHelper {
33
34     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
35
36     private final HtDatabaseEventsService databaseEventService;
37     private final IndexMwtnService mwtnService;
38
39     /**
40      * Helper to fill data into the database
41      * @param deviceManager devicemanger to get services
42      */
43     public DBCleanServiceHelper(DeviceManagerImpl deviceManager) {
44         this.databaseEventService = deviceManager.getDatabaseClientEvents();
45         this.mwtnService = deviceManager.getMwtnService();
46     }
47
48     /**
49      * Write data into database with specific date and content profile.
50      * @param number of data to be written for each log
51      * @param days starting day, relative to actual date
52      * @param hours starting hour ... increased by one hour for each write
53      * @return integer with the amount of written data
54      */
55     public int writeDataToLogs(int number, int days, int hours) {
56         int res = 0;
57         for (Integer t=0; t < number; t++) { //Test "sdnevents", "eventlog"
58             ObjectCreationNotificationXml notificationXml = new ObjectCreationNotificationXml(
59                     "Testpoint"+t, t.toString(), getInternalDateAndTime(days, hours+t), "ObjectId"+t);
60             databaseEventService.writeEventLog(notificationXml);
61             res++;
62         }
63
64         for (Integer t=0; t < number; t++) { //Test "sdnevents", "faultlog"
65             ProblemNotificationXml fault = new ProblemNotificationXml(
66                     "ProblemNode"+t, "Problemuuid", "Problemname", InternalSeverity.Major, t.toString(), getInternalDateAndTime(days, hours+t));
67             databaseEventService.writeFaultLog(fault);
68             res++;
69         }
70
71         for (Integer t=0; t < number; t++) { //Test "mwtn", "log"
72             EsEventOdluxLog odluxEvent = new EsEventOdluxLog("Odluxevent"+t, "Problemuuid", "Message", getInternalDateAndTime(days, hours+t));
73             mwtnService.writeOdluxEventForTestpurpose(odluxEvent);
74             res++;
75         }
76
77         return res;
78     }
79
80     /**************************************************************
81      * Private section
82      */
83
84     private InternalDateAndTime getInternalDateAndTime(int days, int hours) {
85         Date actual = new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(days, TimeUnit.DAYS) - TimeUnit.MILLISECONDS.convert(hours, TimeUnit.HOURS));
86         InternalDateAndTime timeStamp = InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp(actual));
87         return timeStamp;
88     }
89
90
91 }