Renaming openecomp to onap
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / task / PersistOperationResultToDisk.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.util.Map;
27 import java.util.function.Supplier;
28
29 import org.onap.aai.sparky.dal.rest.OperationResult;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.cl.api.Logger;
32 import org.slf4j.MDC;
33
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 /**
37  * The Class PersistOperationResultToDisk.
38  */
39 public class PersistOperationResultToDisk implements Supplier<Void> {
40
41   private String fullPath;
42   private OperationResult dataToStore;
43   private ObjectMapper mapper;
44   private Logger logger;
45   private Map<String, String> contextMap;
46
47   /**
48    * Instantiates a new persist operation result to disk.
49    *
50    * @param fullPath the full path
51    * @param dataToStore the data to store
52    * @param mapper the mapper
53    * @param logger the logger
54    */
55   public PersistOperationResultToDisk(String fullPath, OperationResult dataToStore,
56       ObjectMapper mapper, Logger logger) {
57
58     this.fullPath = fullPath;
59     this.mapper = mapper;
60     this.dataToStore = dataToStore;
61     this.logger = logger;
62     this.contextMap = MDC.getCopyOfContextMap();
63   }
64
65   /* (non-Javadoc)
66    * @see java.util.function.Supplier#get()
67    */
68   @Override
69   public Void get() {
70         MDC.setContextMap(contextMap);
71     File file = new File(fullPath);
72     if (!file.exists()) {
73       try {
74         mapper.writeValue(new File(fullPath), dataToStore);
75       } catch (Exception exc) {
76         logger.error(AaiUiMsgs.DISK_DATA_WRITE_IO_ERROR, exc.toString());
77       }
78     }
79
80     return null;
81   }
82
83
84
85 }