aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/datacleanup/DataCleanupTasks.java
blob: 193149123d7a70122d4e0ae52a884f9c14812bfd (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
/**
 * ============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.io.IOException;
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.exceptions.AAIException;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

@Component
@PropertySource("file:${server.local.startpath}/etc/appprops/datatoolscrons.properties")
public class DataCleanupTasks {

	private static final EELFLogger logger = EELFManager.getInstance().getLogger(DataCleanupTasks.class);
	private static 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() throws AAIException, Exception {
		
		logger.info("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.info("The logDir is " + logDir);
			logger.info("The dataGroomingDir is " + dataGroomingDir);
			logger.info("The archiveDir is " + archiveDir );
			logger.info("The dataGroomingArcDir is " + dataGroomingArcDir );
		
			boolean exists = directoryExists(logDir);
			logger.info("Directory" + logDir + "exists: " + exists);
			if(!exists)
				logger.error("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.info("The file name in dataGrooming: " +listFile.getName()); 
						Date fileCreateDate = fileCreationMonthDate(listFile);
						logger.info("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.info("The file name in ARCHIVE/dataGrooming: " +listFileArchive.getName()); 
				Date fileCreateDate = fileCreationMonthDate(listFileArchive);
				logger.info("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"+e.toString());
			logger.info("AAI_4000", "Exception running cron job for DataCleanup"+e.toString());
			throw e;
		}
	}
	
    /**
     * This method checks if the directory exists
     * @param DIR
     * 
     */
    public boolean directoryExists(String dir) {
    	File path = new File(dir);
		boolean exists = path.exists();
		return exists;	
    }
    
    public Date getZipDate(Integer days) throws Exception {
    	return getZipDate(days, new Date());
    }
    
    public Date getZipDate(Integer days, Date date) throws Exception{
    	
    	Calendar cal = Calendar.getInstance();
    	logger.info("The current date is " + date );
    	cal.setTime(date);	
    	cal.add(Calendar.DATE, -days);
    	Date newAgeZip = cal.getTime();
		logger.info("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() ) );
	    Date d = simpleDateFormat.parse(formatted);
	    return d;
    }
    
    /**
     * 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 AAIException
     * @throws Exception
     */
    public void archive(File file, String archiveDir, String afterArchiveDir) throws AAIException, Exception {
		
    	logger.info("Inside the archive folder");  
    	String filename = file.getName();
    	logger.info("file name is " +filename);
		File archivepath = new File(archiveDir);
		
		String zipFile = afterArchiveDir + AAIConstants.AAI_FILESEP + filename;
		
		File dataGroomingPath = new File(afterArchiveDir);
	
		boolean exists = directoryExists(archiveDir);
		logger.info("Directory" + archiveDir + "exists: " + exists);		
		if(!exists) {
			logger.error("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.error("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.info("The file archived is " + file + " at " + afterArchiveDir );
		}	
	 catch (IOException e) {
		 ErrorLogHelper.logError("AAI_4000", "Exception running cron job for DataCleanup " + e.getStackTrace());
		 logger.info("AAI_4000", "Exception running cron job for DataCleanup", e);
		 throw e;
	 	}
    }
    
    /**
     * 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.info("Deleting the file " + file);
    	boolean deleteStatus = file.delete();
		if(!deleteStatus){
			logger.error("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() throws AAIException, Exception {
	
    	logger.info("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 archivepath = new File(archiveDir);
    		File dataSnapshotPath = new File(dataSnapshotArcDir);
	
    		logger.info("The logDir is " + logDir);
    		logger.info("The dataSnapshotDir is " + dataSnapshotDir);
    		logger.info("The archiveDir is " + archiveDir );
    		logger.info("The dataSnapshotArcDir is " + dataSnapshotArcDir );
	
    		boolean exists = directoryExists(logDir);
    		logger.info("Directory" + logDir + "exists: " + exists);
    		if(!exists)
    			logger.error("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.info("The file name in dataSnapshot: " +listFile.getName()); 
    					Date fileCreateDate = fileCreationMonthDate(listFile);
    					logger.info("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.info("The file name in ARCHIVE/dataSnapshot: " +listFileArchive.getName()); 
    					Date fileCreateDate = fileCreationMonthDate(listFileArchive);
    					logger.info("The fileCreateDate in ARCHIVE/dataSnapshot is " + fileCreateDate);
    					if(fileCreateDate.compareTo(newAgeDelete) < 0) {
    						delete(listFileArchive);
    					}
    				}	
    			}
    		}
	}
	catch (Exception e) {
		ErrorLogHelper.logError("AAI_4000", "Exception running cron job for DataCleanup"+e.toString());
		logger.info("AAI_4000", "Exception running cron job for DataCleanup"+e.toString());
		throw e;
	}
  }   
}