911603d294e8e1de2b850a9039ab6dfc1ef2d20d
[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
21 package org.onap.aai.tasks;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25
26 import java.io.File;
27 import java.util.Arrays;
28 import java.util.Date;
29 import java.util.UUID;
30
31 import org.apache.commons.io.FilenameUtils;
32 import org.apache.commons.io.comparator.LastModifiedFileComparator;
33 import org.onap.aai.logging.LoggingContext;
34 import org.onap.aai.logging.LoggingContext.StatusCode;
35 import org.onap.aai.util.AAIConfig;
36 import org.onap.aai.util.AAIConstants;
37 import org.springframework.scheduling.annotation.Scheduled;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class ScheduledTasks {
42
43     private static EELFLogger LOGGER = EELFManager.getInstance().getLogger(ScheduledTasks.class);
44
45     private static final String COMPONENT = "Scheduler";
46     private static final String FROM_APP_ID = "CronApp";
47     private static final long PROPERTY_READ_INTERVAL = 60000; // every minute
48
49     private String GlobalPropFileName = AAIConstants.AAI_CONFIG_FILENAME;
50
51     // for read and possibly reloading aaiconfig.properties and other
52     /**
53      * Load AAI properties.
54      */
55     // configuration properties files
56     @Scheduled(fixedRate = PROPERTY_READ_INTERVAL)
57     public void loadAAIProperties() {
58         final UUID transId = UUID.randomUUID();
59
60         // LoggingContext.init();
61         LoggingContext.save();
62         LoggingContext.requestId(transId);
63         LoggingContext.partnerName(FROM_APP_ID);
64         LoggingContext.component(COMPONENT);
65         LoggingContext.targetEntity("AAI");
66         LoggingContext.targetServiceName("loadAAIProperties");
67         LoggingContext.serviceName("AAI");
68         LoggingContext.statusCode(StatusCode.COMPLETE);
69         LoggingContext.responseCode(LoggingContext.SUCCESS);
70
71         String dir = FilenameUtils.getFullPathNoEndSeparator(GlobalPropFileName);
72         if (dir == null || dir.length() < 3) {
73             dir = "/opt/aai/etc";
74         }
75
76         File pdir = new File(dir);
77         File[] files = pdir.listFiles();
78         Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
79         String fn;
80
81         // leave this loop here since we may want to check other configurable
82         // property files in the SAME directory
83         for (File file : files) {
84             fn = file.getName();
85             if (fn.equals("aaiconfig.properties")) {
86                 Date lastMod = new Date(file.lastModified());
87                 long lastModTm = lastMod.getTime();
88                 Date curTS = new Date();
89                 long curTSTm = curTS.getTime();
90                 if (curTSTm - lastModTm < PROPERTY_READ_INTERVAL + 1000) {
91                     AAIConfig.reloadConfig();
92                     LOGGER.debug("reloaded from aaiconfig.properties");
93                 }
94                 break;
95             }
96         }
97         LoggingContext.restoreIfPossible();
98     }
99 }