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=========================================================
21 package org.onap.aai.failover;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Properties;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.stereotype.Component;
34 public class FailoverMonitor {
36 private static final String IS_PRIMARY = "is_primary";
37 private static final String TRUE = "true";
38 private static final String DEFAULT_FOR_PRIMARY = TRUE;
40 @Value("${failover.location:/opt/app/failover/failover.properties}")
41 private String failoverPropertiesPath;
43 public boolean shouldRun() throws IOException {
45 Path failoverPath = Paths.get(failoverPropertiesPath);
47 if (Files.exists(failoverPath)) {
48 Properties properties = new Properties();
49 try (InputStream is = Files.newInputStream(failoverPath)) {
51 // If the property is_primary is missing then it should proceed
52 return TRUE.equals(properties.getProperty(IS_PRIMARY, DEFAULT_FOR_PRIMARY));
55 // If the file doesn't exist, then scheduled task should execute