aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/datacleanup/DataCleanupTasks.java
blob: 99b1619795f49fbf0e4f2694b7e60500acc38768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T 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.aai.datacleanup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.onap.aai.aailog.logs.AaiScheduledTaskAuditLog;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.logging.LogFormatTools;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.onap.logging.filter.base.ONAPComponents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@PropertySource("file:${server.local.startpath}/etc/appprops/datatoolscrons.properties")
public class DataCleanupTasks {
    
	@Autowired
    private AaiScheduledTaskAuditLog auditLog;
	
	private static final Logger logger = LoggerFactory.getLogger(DataCleanupTasks.class);
	private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

	/**The function archives/deletes files that end in .out (Ie. dataGrooming.201511111305.out) that sit in our log/data directory structure.
		logDir is the {project_home}/logs
		archiveDir is the ARCHIVE directory where the files will be stored after 5 days.
		ageZip is the number of days after which the file will be moved to the ARCHIVE folder.
		ageDelete is the number of days after which the data files will be deleted i.e after 30 days.
	*/
	@Scheduled(cron = "${datagroomingcleanup.cron}" )
	public void dataGroomingCleanup() {
		auditLog.logBefore("dataGroomingCleanup", ONAPComponents.AAI.toString() );
		
		logger.debug("Started cron job dataGroomingCleanup @ " + simpleDateFormat.format(new Date()));
		
		try {
			String logDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs";
			String dataGroomingDir = logDir + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "dataGrooming";
			String archiveDir = dataGroomingDir + AAIConstants.AAI_FILESEP + "ARCHIVE";
			String dataGroomingArcDir = archiveDir + AAIConstants.AAI_FILESEP + "dataGrooming";		
			File path = new File(dataGroomingDir);
			File archivepath = new File(archiveDir);
			File dataGroomingPath = new File(dataGroomingArcDir);
		
			logger.debug("The logDir is " + logDir);
			logger.debug("The dataGroomingDir is " + dataGroomingDir);
			logger.debug("The archiveDir is " + archiveDir );
			logger.debug("The dataGroomingArcDir is " + dataGroomingArcDir );
		
			boolean exists = directoryExists(logDir);
			logger.debug("Directory" + logDir + "exists: " + exists);
			if(!exists)
				logger.debug("The directory" + logDir +"does not exists");
		
			Integer ageZip = AAIConfig.getInt("aai.datagrooming.agezip");
			Integer ageDelete = AAIConfig.getInt("aai.datagrooming.agedelete");
							
			Date newAgeZip = getZipDate(ageZip);
						         	
			//Iterate through the dataGroomingDir
			File[] listFiles = path.listFiles();  
			if(listFiles != null) {
				for(File listFile : listFiles) {
					if (listFile.toString().contains("ARCHIVE")){
						continue;
					}
					if(listFile.isFile()){
						logger.debug("The file name in dataGrooming: " +listFile.getName()); 
						Date fileCreateDate = fileCreationMonthDate(listFile);
						logger.debug("The fileCreateDate in dataGrooming is " + fileCreateDate);
						if( fileCreateDate.compareTo(newAgeZip) < 0) {
						archive(listFile,archiveDir,dataGroomingArcDir);						
						}
					}
				}
			}
		
			Date newAgeDelete = getZipDate(ageDelete);
			//Iterate through the archive/dataGrooming dir
			File[] listFilesArchive = dataGroomingPath.listFiles(); 
			if(listFilesArchive != null) {
				for(File listFileArchive : listFilesArchive) { 
					if(listFileArchive.isFile()) {
				logger.debug("The file name in ARCHIVE/dataGrooming: " +listFileArchive.getName()); 
				Date fileCreateDate = fileCreationMonthDate(listFileArchive);
				logger.debug("The fileCreateDate in ARCHIVE/dataGrooming is " + fileCreateDate);
				if(fileCreateDate.compareTo(newAgeDelete) < 0) {
					delete(listFileArchive);
					}
				}	
			}
			}
		}
		catch (Exception e) {
			ErrorLogHelper.logError("AAI_4000", "Exception running cron job for DataCleanup"+LogFormatTools.getStackTop(e));
			logger.debug("AAI_4000", "Exception running cron job for DataCleanup"+LogFormatTools.getStackTop(e));
		}
		logger.debug("Ended cron job dataGroomingCleanup @ " + simpleDateFormat.format(new Date()));
		auditLog.logAfter();
	}
	
    /**
     * This method checks if the directory exists
     * @param dir the Directory
     * 
     */
    public boolean directoryExists(String dir) {
    	File path = new File(dir);
		boolean exists = path.exists();
		return exists;	
    }
    
    public Date getZipDate(Integer days) {
    	return getZipDate(days, new Date());
    }
    
    public Date getZipDate(Integer days, Date date) {
    	
    	Calendar cal = Calendar.getInstance();
    	logger.debug("The current date is " + date );
    	cal.setTime(date);	
    	cal.add(Calendar.DATE, -days);
    	Date newAgeZip = cal.getTime();
		logger.debug("The newAgeDate is " +newAgeZip);
		return newAgeZip;		
    }
    
    
    public Date fileCreationMonthDate (File file) throws Exception {

        BasicFileAttributes attr = Files.readAttributes(file.toPath(),
                                                        BasicFileAttributes.class);
        FileTime time = attr.creationTime();
	    String formatted = simpleDateFormat.format( new Date( time.toMillis() ) );
	    return simpleDateFormat.parse(formatted);
    }
    
    /**
     * This method will zip the files and add it to the archive folder
     * Checks if the archive folder exists, if not then creates one
     * After adding the file to archive folder it deletes the file from the filepath
     * @throws Exception
     */
    public void archive(File file, String archiveDir, String afterArchiveDir) throws Exception {
		
    	logger.debug("Inside the archive folder");  
    	String filename = file.getName();
    	logger.debug("file name is " +filename);
		
		String zipFile = afterArchiveDir + AAIConstants.AAI_FILESEP + filename;
		
		File dataGroomingPath = new File(afterArchiveDir);
	
		boolean exists = directoryExists(archiveDir);
		logger.debug("Directory" + archiveDir + "exists: " + exists);		
		if(!exists) {
			logger.debug("The directory" + archiveDir +"does not exists so will create a new archive folder");
			//Create an archive folder if does not exists		
			boolean flag = dataGroomingPath.mkdirs();
			if(!flag)
				logger.debug("Failed to create ARCHIVE folder");		
		}
		try(FileOutputStream outputstream = new FileOutputStream(zipFile + ".gz");
				ZipOutputStream zoutputstream = new ZipOutputStream(outputstream);
				FileInputStream inputstream = new FileInputStream(file)) {
			ZipEntry ze = new ZipEntry(file.getName());
			zoutputstream.putNextEntry(ze);
			byte[] buffer = new byte[1024];
			int len;
			while ((len = inputstream.read(buffer)) > 0) {
				zoutputstream.write(buffer,0,len);
			}			
			//close all the sources
			zoutputstream.closeEntry();
			//Delete the file after been added to archive folder
			delete(file);
			logger.debug("The file archived is " + file + " at " + afterArchiveDir );
		}	
    }
    
    /**
     * This method will delete all the files from the archive folder that are older than 60 days
     * @param file
     */
    public static void delete(File file) {
    	
    	logger.debug("Deleting the file " + file);
    	boolean deleteStatus = file.delete();
		if(!deleteStatus){
			logger.debug("Failed to delete the file" +file);			
		}
    }
    
    /**The function archives/deletes files that end in .out (Ie. dataGrooming.201511111305.out) that sit in our log/data directory structure.
	logDir is the {project_home}/logs
	archiveDir is the ARCHIVE directory where the files will be stored after 5 days.
	ageZip is the number of days after which the file will be moved to the ARCHIVE folder.
	ageDelete is the number of days after which the data files will be deleted i.e after 30 days.
*/
    @Scheduled(cron = "${datasnapshotcleanup.cron}" )
    public void dataSnapshotCleanup() {
		
		auditLog.logBefore("dataSnapshotCleanup", ONAPComponents.AAI.toString() );
    	
		logger.debug("Started cron job dataSnapshotCleanup @ " + simpleDateFormat.format(new Date()));
	
    	try {
    		String logDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs";
    		String dataSnapshotDir = logDir + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "dataSnapshots";
    		String archiveDir = dataSnapshotDir + AAIConstants.AAI_FILESEP + "ARCHIVE";
    		String dataSnapshotArcDir = archiveDir + AAIConstants.AAI_FILESEP + "dataSnapshots";		
    		File path = new File(dataSnapshotDir);
    		File dataSnapshotPath = new File(dataSnapshotArcDir);
	
    		logger.debug("The logDir is " + logDir);
    		logger.debug("The dataSnapshotDir is " + dataSnapshotDir);
    		logger.debug("The archiveDir is " + archiveDir );
    		logger.debug("The dataSnapshotArcDir is " + dataSnapshotArcDir );
	
    		boolean exists = directoryExists(logDir);
    		logger.debug("Directory" + logDir + "exists: " + exists);
    		if(!exists)
    			logger.debug("The directory" + logDir +"does not exists");
	
    		Integer ageZipSnapshot = AAIConfig.getInt("aai.datasnapshot.agezip");
    		Integer ageDeleteSnapshot = AAIConfig.getInt("aai.datasnapshot.agedelete");
    		
    		Date newAgeZip = getZipDate(ageZipSnapshot);
			         	
    		//Iterate through the dataGroomingDir
    		File[] listFiles = path.listFiles();  
    		if(listFiles != null) {
    			for(File listFile : listFiles) {
    				if (listFile.toString().contains("ARCHIVE")){
					continue;
    				}
    				if(listFile.isFile()){
    					logger.debug("The file name in dataSnapshot: " +listFile.getName()); 
    					Date fileCreateDate = fileCreationMonthDate(listFile);
    					logger.debug("The fileCreateDate in dataSnapshot is " + fileCreateDate);
    					if( fileCreateDate.compareTo(newAgeZip) < 0) {
    						archive(listFile,archiveDir,dataSnapshotArcDir);						
    					}
    				}
    			}
    		}
	
    		Date newAgeDelete = getZipDate(ageDeleteSnapshot);
    		//Iterate through the archive/dataSnapshots dir
    		File[] listFilesArchive = dataSnapshotPath.listFiles(); 
    		if(listFilesArchive != null) {
    			for(File listFileArchive : listFilesArchive) { 
    				if(listFileArchive.isFile()) {
    					logger.debug("The file name in ARCHIVE/dataSnapshot: " +listFileArchive.getName()); 
    					Date fileCreateDate = fileCreationMonthDate(listFileArchive);
    					logger.debug("The fileCreateDate in ARCHIVE/dataSnapshot is " + fileCreateDate);
    					if(fileCreateDate.compareTo(newAgeDelete) < 0) {
    						delete(listFileArchive);
    					}
    				}	
    			}
    		}
    		dmaapEventsDataCleanup(newAgeDelete);
    		dataMigrationCleanup();
	}
	catch (Exception e) {
		ErrorLogHelper.logError("AAI_4000", "Exception running cron job for DataCleanup"+LogFormatTools.getStackTop(e));
		logger.debug("AAI_4000", "Exception running cron job for DataCleanup"+LogFormatTools.getStackTop(e));
	}
    logger.debug("Ended cron job dataSnapshotCleanup @ " + simpleDateFormat.format(new Date()));
	auditLog.logAfter();
  }
	public void dmaapEventsDataCleanup(Date deleteAge) {

		logger.debug("Started dmaapEventsDataCleanup @ " + simpleDateFormat.format(new Date()));

		try {
			String logDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs";
			String dmaapEventsDataDir = logDir + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "dmaapEvents";
			File path = new File(dmaapEventsDataDir);

			logger.debug("The logDir is " + logDir);
			logger.debug("The dmaapEventsDataDir is " + dmaapEventsDataDir);

			//Iterate through the files
			File[] listFiles = path.listFiles();
			if(listFiles != null) {
				for(File listFile : listFiles) {
					if(listFile.isFile()){
						logger.debug("The file name in dmaapEvents is: " +listFile.getName());
						Date fileCreateDate = fileCreationMonthDate(listFile);
						logger.debug("The fileCreateDate in dmaapEvents is " + fileCreateDate);
						if( fileCreateDate.compareTo(deleteAge) < 0) {
							delete(listFile);
							logger.debug("Deleted " + listFile.getName());
						}
					}
				}
			}

		}
		catch (Exception e) {
			ErrorLogHelper.logError("AAI_4000", "Exception in dmaapEventsDataCleanup");
			logger.debug("AAI_4000", "Exception in dmaapEventsDataCleanup "+LogFormatTools.getStackTop(e));
		}
		logger.debug("Ended cron dmaapEventsDataCleanup @ " + simpleDateFormat.format(new Date()));
	}
	
    public void dataMigrationCleanup() throws AAIException {
		Integer ageDeleteSnapshot = AAIConfig.getInt("aai.datamigration.agedelete");
		
		Date deleteAge = getZipDate(ageDeleteSnapshot);
    	
		logger.debug("Started dataMigrationCleanup @ " + simpleDateFormat.format(new Date()));
	
    	try {
    		String logDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs";
    		String dataMigrationCleanupDir = logDir + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "migration-input-files";
    		File path = new File(dataMigrationCleanupDir);
    		
    		logger.debug("The logDir is " + logDir);
			logger.debug("The migrationInputFilesDir is " + dataMigrationCleanupDir);

			//Iterate through the files
			File[] listFiles = path.listFiles();
			if(listFiles != null) {
				for(File listFile : listFiles) {
					if(listFile.isFile()){
						logger.debug("The file name in migration-input-files is: " +listFile.getName());
						Date fileCreateDate = fileCreationMonthDate(listFile);
						logger.debug("The fileCreateDate in migration-input-files is " + fileCreateDate);
						if( fileCreateDate.compareTo(deleteAge) < 0) {
							delete(listFile);
							logger.debug("Deleted " + listFile.getName());
						}
					}
				}
			}

		}
		catch (Exception e) {
			ErrorLogHelper.logError("AAI_4000", "Exception in dataMigrationCleanup");
			logger.debug("AAI_4000", "Exception in dataMigrationCleanup "+LogFormatTools.getStackTop(e));
		}
		logger.debug("Ended cron dataMigrationCleanup @ " + simpleDateFormat.format(new Date()));
	}
}