37018798026d6a466e4ffe3e6123c50aa9cf5997
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / task / RetrieveOperationResultFromDisk.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.synchronizer.task;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.function.Supplier;
31
32 import org.openecomp.cl.api.Logger;
33 import org.openecomp.sparky.dal.rest.OperationResult;
34 import org.openecomp.sparky.logging.AaiUiMsgs;
35
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 /**
39  * The Class RetrieveOperationResultFromDisk.
40  */
41 public class RetrieveOperationResultFromDisk implements Supplier<OperationResult> {
42
43   private String fullPath;
44   private ObjectMapper mapper;
45   private Logger logger;
46
47   /**
48    * Instantiates a new retrieve operation result from disk.
49    *
50    * @param fullPath the full path
51    * @param mapper the mapper
52    * @param logger the logger
53    */
54   public RetrieveOperationResultFromDisk(String fullPath, ObjectMapper mapper, Logger logger) {
55
56     this.fullPath = fullPath;
57     this.mapper = mapper;
58     this.logger = logger;
59   }
60
61   /* (non-Javadoc)
62    * @see java.util.function.Supplier#get()
63    */
64   @Override
65   public OperationResult get() {
66
67     try {
68       File file = new File(fullPath);
69       if (file.exists()) {
70         if (logger.isDebugEnabled()) {
71           logger.debug(AaiUiMsgs.WILL_RETRIEVE_TXN, fullPath);
72         }
73
74         Path path = Paths.get(fullPath);
75         byte[] byteBuffer = Files.readAllBytes(path);
76
77         OperationResult opResult = mapper.readValue(byteBuffer, OperationResult.class);
78
79         return opResult;
80       } else {
81         logger.debug(AaiUiMsgs.FAILED_TO_RESTORE_TXN_FILE_MISSING, fullPath);
82       }
83     } catch (IOException exc) {
84       logger.error(AaiUiMsgs.DISK_CACHE_READ_IO_ERROR, exc.getLocalizedMessage());
85     }
86     return null;
87   }
88
89 }