2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.sdnc.config.generator.writer;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
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;
39 public class FileWriterNode implements SvcLogicJavaPlugin {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(FileWriterNode.class);
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);
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");
56 log.info("Received writeFile call with params : " + mapString);
59 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
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);
65 File file = new File(fileName);
66 File filePath = file.getParentFile();
67 FileUtils.forceMkdir(filePath);
69 FileUtils.writeStringToFile(file, fileContents,
70 ConfigGeneratorConstant.STRING_ENCODING);
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,
79 log.error("Failed in writeFile", e);
80 throw new SvcLogicException(e.getMessage());