Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / tasks / ScheduledTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11      http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.tasks;
22
23 import java.io.File;
24 import java.util.Arrays;
25 import java.util.Date;
26 import java.util.UUID;
27
28 import org.apache.commons.io.FilenameUtils;
29 import org.apache.commons.io.comparator.LastModifiedFileComparator;
30 import org.springframework.scheduling.annotation.Scheduled;
31 import org.springframework.stereotype.Component;
32
33 import org.openecomp.aai.logging.LoggingContext;
34 import org.openecomp.aai.util.AAIConfig;
35 import org.openecomp.aai.util.AAIConstants;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39 @Component
40 public class ScheduledTasks {
41
42         private static EELFLogger LOGGER = EELFManager.getInstance().getLogger(ScheduledTasks.class);
43
44         private static final String COMPONENT = "Scheduler";
45         private static final String FROM_APP_ID = "CronApp";
46         private static final long PROPERTY_READ_INTERVAL = 60000; // every minute
47
48         private String GlobalPropFileName = AAIConstants.AAI_CONFIG_FILENAME;
49
50         // for read and possibly reloading aaiconfig.properties and other
51         /**
52          * Load AAI properties.
53          */
54         // configuration properties files
55         @Scheduled(fixedRate = PROPERTY_READ_INTERVAL)
56         public void loadAAIProperties() {
57                 final UUID transId = UUID.randomUUID();
58
59                 LoggingContext.requestId(transId);
60                 LoggingContext.partnerName(FROM_APP_ID);
61                 LoggingContext.component(COMPONENT);
62
63                 String dir = FilenameUtils.getFullPathNoEndSeparator(GlobalPropFileName);
64                 if (dir == null || dir.length() < 3) {
65                         dir = "/opt/aai/etc";
66                 }
67
68                 File pdir = new File(dir);
69                 File[] files = pdir.listFiles();
70                 Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
71                 String fn;
72
73                 // leave this loop here since we may want to check other configurable
74                 // property files in the SAME directory
75                 for (File file : files) {
76                         fn = file.getName();
77                         if (fn.equals("aaiconfig.properties")) {
78                                 Date lastMod = new Date(file.lastModified());
79                                 long lastModTm = lastMod.getTime();
80                                 Date curTS = new Date();
81                                 long curTSTm = curTS.getTime();
82                                 if (curTSTm - lastModTm < PROPERTY_READ_INTERVAL + 1000) {
83                                         AAIConfig.reloadConfig();
84                                         LOGGER.info("reloaded from aaiconfig.properties");
85                                 }
86                                 break;
87                         }
88                 }
89         }
90 }