682e41a9a870b0e577e9819a3cdfe8f5c0b995af
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.workflow.activator;
25
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;
32
33 import java.io.*;
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;
39
40 public class TransactionAbortedMarker implements Runnable {
41
42     private ScheduledExecutorService executor = null;
43
44     public static final String PREFIX = "APPC";
45     public static final String SUFFIX = "VM";
46     private final EELFLogger logger = EELFManager.getInstance().getLogger(TransactionAbortedMarker.class);
47
48     public TransactionAbortedMarker(ScheduledExecutorService executor){
49         this.executor = executor;
50     }
51
52     @Override
53     public void run() {
54         try {
55             TransactionRecorder recorder = lookupTransactionRecorder();
56
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());
61             }
62
63             File[] allInstanceIdFiles = getAllInstanceIdFiles(parentDirectory);
64
65             if (allInstanceIdFiles.length > 0) {
66                 File lastModifiedFile = getLastModifiedFile(allInstanceIdFiles);
67                 if (logger.isDebugEnabled()) {
68                     logger.debug("Last Modified File" + lastModifiedFile.getName());
69                 }
70                 String prevAppcInstanceId = readInstanceId(lastModifiedFile);
71                 recorder.markTransactionsAborted(prevAppcInstanceId);
72                 boolean isFileDeleted = lastModifiedFile.delete();
73                 logger.debug("Previous file deleted  " + isFileDeleted);
74             }
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);
83         }
84     }
85
86
87
88     protected TransactionRecorder lookupTransactionRecorder() throws TransactionRecorderServiceNotFoundException {
89         String message = null;
90         BundleContext bctx = FrameworkUtil.getBundle(TransactionRecorder.class).getBundleContext();
91         if(bctx!=null){
92             ServiceReference sref= bctx.getServiceReference(TransactionRecorder.class.getName());
93             TransactionRecorder transactionRecorder;
94             if (sref != null) {
95                 transactionRecorder = (TransactionRecorder) bctx.getService(sref);
96                 if (transactionRecorder != null) {
97                     return transactionRecorder;
98                 }
99             }
100         }
101         message = "Cannot find service org.onap.appc.transactionrecorder.TransactionRecorder";
102         logger.warn(message);
103         throw new TransactionRecorderServiceNotFoundException(message);
104     }
105
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);
110         }
111         catch (IOException e){
112             String message = "Error writing appc-instance-id";
113             logger.error(message,e);
114             throw new RuntimeException(message);
115         }
116         logger.debug("new appc-instance-id = " + newAppcInstanceId);
117         return newAppcInstanceId;
118     }
119
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();
126         }
127         catch (IOException e){
128             String message ="Error reading previous appc-instance-id";
129             logger.error(message,e);
130             throw new RuntimeException(message);
131         }
132         logger.debug("previous appc-instance-id " + prevAppcInstanceId);
133         return prevAppcInstanceId;
134     }
135
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)
141                 return true;
142             return false;
143         });
144     }
145
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;
153             }
154         }
155         return lastModifiedFile;
156     }
157 }