Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / 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.onap.aai.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.onap.aai.sparky.dal.rest.OperationResult;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
34 import org.onap.aai.cl.api.Logger;
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   /*
62    * (non-Javadoc)
63    * 
64    * @see java.util.function.Supplier#get()
65    */
66   @Override
67   public OperationResult get() {
68
69     try {
70       File file = new File(fullPath);
71       if (file.exists()) {
72         if (logger.isDebugEnabled()) {
73           logger.debug(AaiUiMsgs.WILL_RETRIEVE_TXN, fullPath);
74         }
75
76         Path path = Paths.get(fullPath);
77         byte[] byteBuffer = Files.readAllBytes(path);
78
79         OperationResult opResult = mapper.readValue(byteBuffer, OperationResult.class);
80
81         return opResult;
82       } else {
83         logger.debug(AaiUiMsgs.FAILED_TO_RESTORE_TXN_FILE_MISSING, fullPath);
84       }
85     } catch (IOException exc) {
86       logger.error(AaiUiMsgs.DISK_CACHE_READ_IO_ERROR, exc.getLocalizedMessage());
87     }
88     return null;
89   }
90
91   /**
92    * @return the fullPath
93    */
94   public String getFullPath() {
95     return fullPath;
96   }
97
98   /**
99    * @param fullPath the fullPath to set
100    */
101   public void setFullPath(String fullPath) {
102     this.fullPath = fullPath;
103   }
104
105   /**
106    * @return the mapper
107    */
108   public ObjectMapper getMapper() {
109     return mapper;
110   }
111
112   /**
113    * @param mapper the mapper to set
114    */
115   public void setMapper(ObjectMapper mapper) {
116     this.mapper = mapper;
117   }
118
119   /**
120    * @return the logger
121    */
122   public Logger getLogger() {
123     return logger;
124   }
125
126   /**
127    * @param logger the logger to set
128    */
129   public void setLogger(Logger logger) {
130     this.logger = logger;
131   }
132
133 }