0ab331ec3ee846871f88a2f6c5e5ba1dc2e25ff8
[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   /*
66    * (non-Javadoc)
67    * 
68    * @see java.util.function.Supplier#get()
69    */
70   @Override
71   public Void get() {
72     MDC.setContextMap(contextMap);
73     File file = new File(fullPath);
74     if (!file.exists()) {
75       try {
76         mapper.writeValue(new File(fullPath), dataToStore);
77       } catch (Exception exc) {
78         logger.error(AaiUiMsgs.DISK_DATA_WRITE_IO_ERROR, exc.toString());
79       }
80     }
81
82     return null;
83   }
84
85   /**
86    * @return the fullPath
87    */
88   public String getFullPath() {
89     return fullPath;
90   }
91
92   /**
93    * @param fullPath the fullPath to set
94    */
95   public void setFullPath(String fullPath) {
96     this.fullPath = fullPath;
97   }
98
99   /**
100    * @return the dataToStore
101    */
102   public OperationResult getDataToStore() {
103     return dataToStore;
104   }
105
106   /**
107    * @param dataToStore the dataToStore to set
108    */
109   public void setDataToStore(OperationResult dataToStore) {
110     this.dataToStore = dataToStore;
111   }
112
113   /**
114    * @return the mapper
115    */
116   public ObjectMapper getMapper() {
117     return mapper;
118   }
119
120   /**
121    * @param mapper the mapper to set
122    */
123   public void setMapper(ObjectMapper mapper) {
124     this.mapper = mapper;
125   }
126
127   /**
128    * @return the logger
129    */
130   public Logger getLogger() {
131     return logger;
132   }
133
134   /**
135    * @param logger the logger to set
136    */
137   public void setLogger(Logger logger) {
138     this.logger = logger;
139   }
140
141   /**
142    * @return the contextMap
143    */
144   public Map<String, String> getContextMap() {
145     return contextMap;
146   }
147
148   /**
149    * @param contextMap the contextMap to set
150    */
151   public void setContextMap(Map<String, String> contextMap) {
152     this.contextMap = contextMap;
153   }
154
155
156
157 }