Update odlux
[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.dataprovider.model.DataProvider;
23 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
24 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SourceType;
31
32 public class DBCleanServiceHelper {
33
34     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
35
36     private final DataProvider databaseEventService;
37
38     /**
39      * Helper to fill data into the database
40      * @param deviceManager devicemanger to get services
41      */
42     public DBCleanServiceHelper(DeviceManagerImpl deviceManager) {
43         this.databaseEventService = deviceManager.getDatabaseClientEvents();
44     }
45
46     /**
47      * Write data into database with specific date and content profile.
48      * @param number of data to be written for each log
49      * @param days starting day, relative to actual date
50      * @param hours starting hour ... increased by one hour for each write
51      * @return integer with the amount of written data
52      */
53     public int writeDataToLogs(int number, int days, int hours) {
54         int res = 0;
55         for (Integer t=0; t < number; t++) { //Test "sdnevents", "eventlog"
56             ObjectCreationNotificationXml notificationXml = new ObjectCreationNotificationXml(
57                     "Testpoint"+t, t, getInternalDateAndTime(days, hours+t), "ObjectId"+t);
58             databaseEventService.writeConnectionLog(notificationXml.getConnectionlogEntity());
59             res++;
60         }
61
62         for (Integer t=0; t < number; t++) { //Test "sdnevents", "faultlog"
63             ProblemNotificationXml fault = new ProblemNotificationXml(
64                     "ProblemNode"+t, "Problemuuid", "Problemname", InternalSeverity.Major, t, getInternalDateAndTime(days, hours+t));
65             databaseEventService.writeFaultLog(fault.getFaultlog(SourceType.Unknown));
66             res++;
67         }
68
69         return res;
70     }
71
72     /**************************************************************
73      * Private section
74      */
75
76     private InternalDateAndTime getInternalDateAndTime(int days, int hours) {
77         Date actual = new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(days, TimeUnit.DAYS) - TimeUnit.MILLISECONDS.convert(hours, TimeUnit.HOURS));
78         InternalDateAndTime timeStamp = InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp(actual));
79         return timeStamp;
80     }
81
82
83 }