Changed to unmaintained
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / writer / FileWriterNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.generator.writer;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import java.io.File;
31 import java.util.Map;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
37 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
38
39 public class FileWriterNode implements SvcLogicJavaPlugin {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(FileWriterNode.class);
42
43     public void writeFile(Map<String, String> inParams, SvcLogicContext ctx)
44         throws SvcLogicException {
45         if (log.isTraceEnabled()) {
46             log.trace("Received writeFile call with params : " + inParams);
47         }
48         else {
49             String mapString = inParams.toString();
50             if (mapString.length() > 255) {
51                 log.info("Received writeFile call with params : " + mapString.substring(0, 255));
52                 log.info("\n...\n" + mapString.length() +
53                         " characters in parameters map, turn on TRACE logging to log entire parameter map");
54             }
55             else {
56                 log.info("Received writeFile call with params : " + mapString);
57             }
58         }
59         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
60         try {
61             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
62             String fileName = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_FILE_NAME);
63             String fileContents = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_REQUEST_DATA);
64
65             File file = new File(fileName);
66             File filePath = file.getParentFile();
67             FileUtils.forceMkdir(filePath);
68
69             FileUtils.writeStringToFile(file, fileContents,
70                 ConfigGeneratorConstant.STRING_ENCODING);
71
72             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
73                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
74         } catch (Exception e) {
75             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
76                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
77             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
78                 e.getMessage());
79             log.error("Failed in writeFile", e);
80             throw new SvcLogicException(e.getMessage());
81         }
82     }
83 }