[AAI] Fix doc config files
[aai/aai-common.git] / aai-failover / src / main / java / org / onap / aai / failover / FailoverMonitor.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.springframework.beans.factory.annotation.Value;
23 import org.springframework.stereotype.Component;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Properties;
31
32 @Component
33 public class FailoverMonitor {
34
35     private static final String IS_PRIMARY = "is_primary";
36     private static final String TRUE = "true";
37     private static final String DEFAULT_FOR_PRIMARY = TRUE;
38
39     @Value("${failover.location:/opt/app/failover/failover.properties}")
40     private String failoverPropertiesPath;
41
42     public boolean shouldRun() throws IOException {
43
44         Path failoverPath = Paths.get(failoverPropertiesPath);
45
46         if(Files.exists(failoverPath)){
47             Properties properties = new Properties();
48             try (InputStream is = Files.newInputStream(failoverPath)){
49                 properties.load(is);
50                 // If the property is_primary is missing then it should proceed
51                 return TRUE.equals(properties.getProperty(IS_PRIMARY, DEFAULT_FOR_PRIMARY));
52             }
53         } else {
54             // If the file doesn't exist, then scheduled task should execute
55             return true;
56         }
57     }
58 }