2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.workflow.activator;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.onap.appc.transactionrecorder.TransactionRecorder;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.FrameworkUtil;
31 import org.osgi.framework.ServiceReference;
34 import java.util.UUID;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.Future;
37 import java.util.concurrent.ScheduledExecutorService;
38 import java.util.concurrent.TimeUnit;
40 public class TransactionAbortedMarker implements Runnable {
42 private ScheduledExecutorService executor = null;
44 public static final String PREFIX = "APPC";
45 public static final String SUFFIX = "VM";
46 private final EELFLogger logger = EELFManager.getInstance().getLogger(TransactionAbortedMarker.class);
48 public TransactionAbortedMarker(ScheduledExecutorService executor){
49 this.executor = executor;
55 TransactionRecorder recorder = lookupTransactionRecorder();
57 File newAppcInstanceIdFile = File.createTempFile(PREFIX, SUFFIX);
58 File parentDirectory = newAppcInstanceIdFile.getParentFile();
59 if (logger.isDebugEnabled()) {
60 logger.debug("New instance id file path" + newAppcInstanceIdFile.getAbsolutePath());
63 File[] allInstanceIdFiles = getAllInstanceIdFiles(parentDirectory);
65 if (allInstanceIdFiles.length > 0) {
66 File lastModifiedFile = getLastModifiedFile(allInstanceIdFiles);
67 if (logger.isDebugEnabled()) {
68 logger.debug("Last Modified File" + lastModifiedFile.getName());
70 String prevAppcInstanceId = readInstanceId(lastModifiedFile);
71 recorder.markTransactionsAborted(prevAppcInstanceId);
72 boolean isFileDeleted = lastModifiedFile.delete();
73 logger.debug("Previous file deleted " + isFileDeleted);
75 String newAppcInstanceId = writeNewInstanceId(newAppcInstanceIdFile);
76 recorder.setAppcInstanceId(newAppcInstanceId);
77 } catch (TransactionRecorderServiceNotFoundException e) {
78 logger.warn("Transaction Recorder Service Not Found, Next attempt after 30 seconds");
79 executor.schedule(this,30, TimeUnit.SECONDS);
80 } catch (Exception e) {
81 logger.error("Error on workflow manager bundle start-up" + e.getMessage(), e);
82 throw new RuntimeException(e);
88 protected TransactionRecorder lookupTransactionRecorder() throws TransactionRecorderServiceNotFoundException {
89 String message = null;
90 BundleContext bctx = FrameworkUtil.getBundle(TransactionRecorder.class).getBundleContext();
92 ServiceReference sref= bctx.getServiceReference(TransactionRecorder.class.getName());
93 TransactionRecorder transactionRecorder;
95 transactionRecorder = (TransactionRecorder) bctx.getService(sref);
96 if (transactionRecorder != null) {
97 return transactionRecorder;
101 message = "Cannot find service org.onap.appc.transactionrecorder.TransactionRecorder";
102 logger.warn(message);
103 throw new TransactionRecorderServiceNotFoundException(message);
106 private String writeNewInstanceId(File newInstanceIdFile) throws IOException {
107 String newAppcInstanceId = UUID.randomUUID().toString();
108 try (FileWriter writer = new FileWriter(newInstanceIdFile)) {
109 writer.write(newAppcInstanceId);
111 catch (IOException e){
112 String message = "Error writing appc-instance-id";
113 logger.error(message,e);
114 throw new RuntimeException(message);
116 logger.debug("new appc-instance-id = " + newAppcInstanceId);
117 return newAppcInstanceId;
120 private String readInstanceId(File lastModifiedFile) {
121 String prevAppcInstanceId = null;
122 BufferedReader buffReader;
123 try (FileReader reader = new FileReader(lastModifiedFile)) {
124 buffReader = new BufferedReader(reader);
125 prevAppcInstanceId = buffReader.readLine();
127 catch (IOException e){
128 String message ="Error reading previous appc-instance-id";
129 logger.error(message,e);
130 throw new RuntimeException(message);
132 logger.debug("previous appc-instance-id " + prevAppcInstanceId);
133 return prevAppcInstanceId;
136 private File[] getAllInstanceIdFiles(File directory) {
137 return directory.listFiles(pathname -> {
138 if (pathname.getName().startsWith(PREFIX)
139 && pathname.getName().endsWith(SUFFIX)
140 && pathname.length()>0)
146 private File getLastModifiedFile(File[] allInstanceIdFiles) {
147 File lastModifiedFile = allInstanceIdFiles[0];
148 long lastModified = allInstanceIdFiles[0].lastModified();
149 for(File file:allInstanceIdFiles){
150 if(file.lastModified() > lastModified){
151 lastModified = file.lastModified();
152 lastModifiedFile = file;
155 return lastModifiedFile;