[AAI] Fix doc config files
[aai/aai-common.git] / aai-failover / src / main / java / org / onap / aai / failover / FailoverAspect.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.failover;
21
22 import org.aspectj.lang.ProceedingJoinPoint;
23 import org.aspectj.lang.annotation.Around;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.aspectj.lang.reflect.MethodSignature;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Component;
29
30 import java.util.Date;
31 import java.util.concurrent.atomic.AtomicLong;
32
33 @Aspect
34 @Component
35 public class FailoverAspect {
36
37     private final Logger LOGGER = LoggerFactory.getLogger(FailoverAspect.class);
38
39     private final FailoverMonitor failoverMonitor;
40     private final AtomicLong atomicLong;
41
42     public FailoverAspect(FailoverMonitor failoverMonitor){
43         this.failoverMonitor = failoverMonitor;
44         this.atomicLong = new AtomicLong(1l);
45     }
46
47     /*
48      * By default, check for the existence of the following file: /opt/app/failover/failover.properties
49      *
50      * If the file exists, open the file as properties
51      * and find the following property: is_primary
52      * Check if the following value is set to true
53      * If it is set to true, then proceed with running the scheduled task
54      * and store the current value into an thread safe variable
55      *
56      * If the file doesn't exist, then proceed with the execution of scheduled task
57      * as if it is the primary site since there is nothing helping identify if its primary
58      *
59      * If the application is not in an kubernetes environment, in order to emulate the behavior
60      * search for the file in the classpath of application
61      * If the file can be found then it will behavior similar to above in kubernetes env
62      *
63      * Since some tasks such as ones in history is constantly getting data
64      * with little time in between each runs of the task to get latest data
65      * we don't want to log too much when the failover properties isn't being changed
66      * So it will check the last time this got executed and see if its more than two minutes have passed
67      * then if it did, then it will log current status
68      */
69     @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
70     public void preSchedule(ProceedingJoinPoint pointcut) throws Throwable {
71
72         Object target             = pointcut.getTarget();
73         MethodSignature signature = (MethodSignature) pointcut.getSignature();
74         String method             = signature.getMethod().getName();
75
76         if(failoverMonitor.shouldRun()){
77             atomicLong.set(1l);
78             pointcut.proceed();
79         } else {
80             long currentTime = new Date().getTime();
81             long lastMessageTime = atomicLong.get();
82
83             if((currentTime - lastMessageTime) > 120000){
84                 atomicLong.compareAndSet(lastMessageTime, new Date().getTime());
85                 LOGGER.debug("Not proceeding the task {}#{} due to is_primary set to false in failover.properties",
86                     target.getClass(),
87                     method
88                 );
89             }
90         }
91     }
92 }
93