81c81f55d660e62fd67476e14791b055125a9acc
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / tasks / ScheduledTasks.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.tasks;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.UUID;
26
27 import org.apache.commons.io.FilenameUtils;
28 import org.apache.commons.io.comparator.LastModifiedFileComparator;
29 import org.springframework.scheduling.annotation.Scheduled;
30 import org.springframework.stereotype.Component;
31
32 import org.onap.aai.logging.LoggingContext;
33 import org.onap.aai.logging.LoggingContext.StatusCode;
34 import org.onap.aai.util.AAIConfig;
35 import org.onap.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.init();
60                 LoggingContext.save();
61                 LoggingContext.requestId(transId);
62                 LoggingContext.partnerName(FROM_APP_ID);
63                 LoggingContext.component(COMPONENT);
64                 LoggingContext.targetEntity("AAI");
65                 LoggingContext.targetServiceName("loadAAIProperties");
66                 LoggingContext.serviceName("AAI");
67                 LoggingContext.statusCode(StatusCode.COMPLETE);
68                 LoggingContext.responseCode(LoggingContext.SUCCESS);
69
70                 String dir = FilenameUtils.getFullPathNoEndSeparator(GlobalPropFileName);
71                 if (dir == null || dir.length() < 3) {
72                         dir = "/opt/aai/etc";
73                 }
74
75                 File pdir = new File(dir);
76                 File[] files = pdir.listFiles();
77                 Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
78                 String fn;
79
80                 // leave this loop here since we may want to check other configurable
81                 // property files in the SAME directory
82                 for (File file : files) {
83                         fn = file.getName();
84                         if (fn.equals("aaiconfig.properties")) {
85                                 Date lastMod = new Date(file.lastModified());
86                                 long lastModTm = lastMod.getTime();
87                                 Date curTS = new Date();
88                                 long curTSTm = curTS.getTime();
89                                 if (curTSTm - lastModTm < PROPERTY_READ_INTERVAL + 1000) {
90                                         AAIConfig.reloadConfig();
91                                         LOGGER.debug("reloaded from aaiconfig.properties");
92                                 }
93                                 break;
94                         }
95                 }
96                 LoggingContext.restoreIfPossible();
97         }
98 }