Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / task / RetrieveOperationResultFromDisk.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.synchronizer.task;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.function.Supplier;
34
35 import org.openecomp.cl.api.Logger;
36 import org.openecomp.sparky.dal.rest.OperationResult;
37 import org.openecomp.sparky.logging.AaiUiMsgs;
38
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 /**
42  * The Class RetrieveOperationResultFromDisk.
43  */
44 public class RetrieveOperationResultFromDisk implements Supplier<OperationResult> {
45
46   private String fullPath;
47   private ObjectMapper mapper;
48   private Logger logger;
49
50   /**
51    * Instantiates a new retrieve operation result from disk.
52    *
53    * @param fullPath the full path
54    * @param mapper the mapper
55    * @param logger the logger
56    */
57   public RetrieveOperationResultFromDisk(String fullPath, ObjectMapper mapper, Logger logger) {
58
59     this.fullPath = fullPath;
60     this.mapper = mapper;
61     this.logger = logger;
62   }
63
64   /* (non-Javadoc)
65    * @see java.util.function.Supplier#get()
66    */
67   @Override
68   public OperationResult get() {
69
70     try {
71       File file = new File(fullPath);
72       if (file.exists()) {
73         if (logger.isDebugEnabled()) {
74           logger.debug(AaiUiMsgs.WILL_RETRIEVE_TXN, fullPath);
75         }
76
77         Path path = Paths.get(fullPath);
78         byte[] byteBuffer = Files.readAllBytes(path);
79
80         OperationResult opResult = mapper.readValue(byteBuffer, OperationResult.class);
81
82         return opResult;
83       } else {
84         logger.debug(AaiUiMsgs.FAILED_TO_RESTORE_TXN_FILE_MISSING, fullPath);
85       }
86     } catch (IOException exc) {
87       logger.error(AaiUiMsgs.DISK_CACHE_READ_IO_ERROR, exc.getLocalizedMessage());
88     }
89     return null;
90   }
91
92 }