2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.common.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResourceManager;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.node.ArrayNode;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
34 import java.text.SimpleDateFormat;
37 import org.apache.commons.lang.ObjectUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.appc.dg.common.JsonDgUtil;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.i18n.Msg;
42 import org.onap.appc.util.JsonUtil;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 public class JsonDgUtilImpl implements JsonDgUtil {
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(JsonDgUtilImpl.class);
50 private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
51 protected SimpleDateFormat initialValue() {
52 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57 public void flatAndAddToContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59 if (logger.isTraceEnabled()) {
61 "Entering to flatAndAddToContext with params = " + ObjectUtils.toString(params) + ", SvcLogicContext = "
62 + ObjectUtils.toString(ctx));
65 String paramName = Constants.PAYLOAD;
66 String payload = params.get(paramName);
67 if (payload == null || payload.isEmpty()) {
68 payload = ctx.getAttribute("input.payload");
70 if (!StringUtils.isEmpty(payload)) {
71 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(payload);
72 if (flatMap != null && flatMap.size() > 0) {
73 for (Map.Entry<String, String> entry : flatMap.entrySet()) {
74 ctx.setAttribute(entry.getKey(), entry.getValue());
78 logger.warn("input payload param value is empty (\"\") or null");
80 } catch (Exception e) {
81 logger.error(e.toString());
82 String msg = EELFResourceManager.format(Msg.INPUT_PAYLOAD_PARSING_FAILED, params.get(Constants.PAYLOAD));
83 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
84 throw new APPCException(e);
89 public void generateOutputPayloadFromContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
90 if (logger.isTraceEnabled()) {
92 "Entering to generateOutputPayloadFromContext with SvcLogicContext = " + ObjectUtils.toString(ctx));
96 Set<String> keys = ctx.getAttributeKeySet();
97 ObjectMapper objectMapper = new ObjectMapper();
98 ObjectNode JsonNode = objectMapper.createObjectNode();
99 for (String key : keys) {
100 if (key.startsWith(Constants.OUTPUT_PAYLOAD + ".")) {
101 String objkey = key.replaceFirst(Constants.OUTPUT_PAYLOAD + ".", "");
102 if (objkey.contains("[") && objkey.contains("]")) {
104 String arrayKey = objkey.substring(0, objkey.indexOf('['));
105 int arrayIndex = Integer
106 .parseInt(objkey.substring(objkey.indexOf('[') + 1, objkey.indexOf(']')));
107 if (JsonNode.has(arrayKey)) {
108 arrayNode = (ArrayNode) JsonNode.get(arrayKey);
109 arrayNode.insert(arrayIndex, ctx.getAttribute(key));
111 arrayNode = objectMapper.createArrayNode();
112 arrayNode.insert(arrayIndex, ctx.getAttribute(key));
113 JsonNode.put(arrayKey, arrayNode);
116 JsonNode.put(objkey, ctx.getAttribute(key));
120 if (JsonNode.size() > 0) {
121 ctx.setAttribute(Constants.OUTPUT_PAYLOAD, objectMapper.writeValueAsString(JsonNode));
123 } catch (Exception e) {
124 logger.error(e.toString());
125 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString());
126 throw new APPCException(e);
132 public void cvaasFileNameAndFileContentToContext(Map<String, String> params, SvcLogicContext ctx)
133 throws APPCException {
135 if (logger.isTraceEnabled()) {
136 logger.trace("Entering to caasFileNameAndFileContentToContext with SvcLogicContext = "
137 + ObjectUtils.toString(ctx));
142 String cvassDirectoryPath = params.get(Constants.CVAAS_DIRECTORY_PATH);
143 String appcInstanceId = params.get(Constants.APPC_INSTANCE_ID);
148 vnfId = params.get("vnf-id");
149 long timestampAsLongRepresentingFileCreationTime = System.currentTimeMillis();
151 ctx.setAttribute(Constants.CVAAS_FILE_NAME, cvassDirectoryPath + File.separator + vnfId + "_"
152 + timestampAsLongRepresentingFileCreationTime + "_" + appcInstanceId + ".json");
158 String uploadDate = ctx.getAttribute("running-config.upload-date");
159 long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(uploadDate).getTime();
161 ObjectMapper objectMapper = new ObjectMapper();
162 ObjectNode jsonNode = objectMapper.createObjectNode();
163 jsonNode.put("UPLOAD_CONFIG_ID", ctx.getAttribute("running-config.upload-config-id"));
164 jsonNode.put("REQUEST_ID", ctx.getAttribute("running-config.request-id"));
165 jsonNode.put("ORIGINATOR_ID", ctx.getAttribute("running-config.originator-id"));
166 jsonNode.put("SERVICE_DESCRIPTION", ctx.getAttribute("running-config.service-description"));
167 jsonNode.put("ACTION", ctx.getAttribute("running-config.action"));
168 jsonNode.put("UPLOAD_TIMESTAMP", epochUploadTimestamp);
169 jsonNode.put("UPLOAD_DATE", uploadDate);
170 jsonNode.put("VNF_ID", vnfId);
171 jsonNode.put("VNF_NAME", ctx.getAttribute("running-config.vnf-name"));
172 jsonNode.put("VM_NAME", ctx.getAttribute("running-config.vm-name"));
173 jsonNode.put("VNF_TYPE", ctx.getAttribute("running-config.vnf-type"));
174 jsonNode.put("VNFC_TYPE", ctx.getAttribute("running-config.vnfc-type"));
175 jsonNode.put("HOST_IP_ADDRESS", ctx.getAttribute("running-config.host-ip-address"));
176 jsonNode.put("CONFIG_INDICATOR", ctx.getAttribute("running-config.config-indicator"));
177 jsonNode.put("PENDING_DELETE", ctx.getAttribute("running-config.pending-delete"));
178 jsonNode.put("CONTENT", ctx.getAttribute("running-config.content"));
180 ctx.setAttribute(Constants.CVAAS_FILE_CONTENT,
181 objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
183 } catch (Exception e) {
184 String errorMessage = "Failed to parse create cvass file for vnf with id : " + vnfId;
185 logger.error(errorMessage, e);
186 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, errorMessage);
187 throw new APPCException(e);
192 public void checkFileCreated(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
193 String filePath = ctx.getAttribute(Constants.CVAAS_FILE_NAME);
194 File file = new File(filePath);
196 if (!file.exists()) {
197 String vnfId = params.get("vnf-id");
198 String errorMessage = "Cvass file could not be created for vnf with id : " + vnfId;
199 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, errorMessage);
200 throw new APPCException(errorMessage);