2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.failover;
22 import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.stereotype.Component;
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;
33 public class FailoverMonitor {
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;
39 @Value("${failover.location:/opt/app/failover/failover.properties}")
40 private String failoverPropertiesPath;
42 public boolean shouldRun() throws IOException {
44 Path failoverPath = Paths.get(failoverPropertiesPath);
46 if(Files.exists(failoverPath)){
47 Properties properties = new Properties();
48 try (InputStream is = Files.newInputStream(failoverPath)){
50 // If the property is_primary is missing then it should proceed
51 return TRUE.equals(properties.getProperty(IS_PRIMARY, DEFAULT_FOR_PRIMARY));
54 // If the file doesn't exist, then scheduled task should execute