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