2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
26 import java.text.SimpleDateFormat;
30 import org.apache.commons.lang.ObjectUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.openecomp.appc.dg.common.JsonDgUtil;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.i18n.Msg;
35 import org.openecomp.appc.util.JsonUtil;
36 import org.openecomp.sdnc.sli.SvcLogicContext;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import com.att.eelf.i18n.EELFResourceManager;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.fasterxml.jackson.databind.node.ArrayNode;
43 import com.fasterxml.jackson.databind.node.ObjectNode;
46 public class JsonDgUtilImpl implements JsonDgUtil {
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(JsonDgUtilImpl.class);
49 private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
50 protected SimpleDateFormat initialValue() {
51 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
56 public void flatAndAddToContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58 if (logger.isTraceEnabled()) {
59 logger.trace("Entering to flatAndAddToContext with params = "+ ObjectUtils.toString(params)+", SvcLogicContext = "+ObjectUtils.toString(ctx));
62 String paramName = Constants.PAYLOAD;
63 String payload = params.get(paramName);
65 payload = ctx.getAttribute("input.payload");
66 if (!StringUtils.isEmpty(payload)) {
67 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(payload);
68 if (flatMap != null && flatMap.size() > 0) {
69 for (Map.Entry<String, String> entry : flatMap.entrySet()) {
70 ctx.setAttribute(entry.getKey(), entry.getValue());
74 logger.warn("input payload param value is empty (\"\") or null");
76 } catch (Exception e) {
77 logger.error(e.toString());
78 String msg = EELFResourceManager.format(Msg.INPUT_PAYLOAD_PARSING_FAILED,params.get(Constants.PAYLOAD));
79 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
80 throw new APPCException(e);
85 public void generateOutputPayloadFromContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException{
86 if (logger.isTraceEnabled()) {
87 logger.trace("Entering to generateOutputPayloadFromContext with SvcLogicContext = "+ObjectUtils.toString(ctx));
91 Set<String> keys = ctx.getAttributeKeySet();
92 ObjectMapper objectMapper = new ObjectMapper();
93 ObjectNode JsonNode = objectMapper.createObjectNode();
94 for (String key : keys) {
95 if(key.startsWith(Constants.OUTPUT_PAYLOAD+".")){
96 String objkey= key.replaceFirst(Constants.OUTPUT_PAYLOAD + ".", "");
97 if(objkey.contains("[") && objkey.contains("]")){
99 String arrayKey = objkey.substring(0,objkey.indexOf('['));
100 int arrayIndex = Integer.parseInt(objkey.substring(objkey.indexOf('[')+1,objkey.indexOf(']')));
101 if(JsonNode.has(arrayKey)){
102 arrayNode = (ArrayNode)JsonNode.get(arrayKey);
103 arrayNode.insert(arrayIndex,ctx.getAttribute(key));
105 arrayNode = objectMapper.createArrayNode();
106 arrayNode.insert(arrayIndex,ctx.getAttribute(key));
107 JsonNode.put(arrayKey,arrayNode);
110 JsonNode.put(objkey, ctx.getAttribute(key));
114 if(JsonNode.size()>0) {
115 ctx.setAttribute(Constants.OUTPUT_PAYLOAD, objectMapper.writeValueAsString(JsonNode));
117 } catch (Exception e) {
118 logger.error(e.toString());
119 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString());
120 throw new APPCException(e);
126 public void cvaasFileNameAndFileContentToContext(Map<String, String> params, SvcLogicContext ctx)
127 throws APPCException {
129 if (logger.isTraceEnabled()) {
130 logger.trace("Entering to caasFileNameAndFileContentToContext with SvcLogicContext = "
131 + ObjectUtils.toString(ctx));
136 String cvassDirectoryPath = params.get(Constants.CVAAS_DIRECTORY_PATH);
137 String appcInstanceId = params.get(Constants.APPC_INSTANCE_ID);
142 vnfId = params.get("vnf-id");
143 long timestampAsLongRepresentingFileCreationTime = System.currentTimeMillis();
145 ctx.setAttribute(Constants.CVAAS_FILE_NAME, cvassDirectoryPath + File.separator + vnfId + "_"
146 + timestampAsLongRepresentingFileCreationTime + "_" + appcInstanceId + ".json");
152 String uploadDate = ctx.getAttribute("running-config.upload-date");
153 long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(uploadDate).getTime();
155 ObjectMapper objectMapper = new ObjectMapper();
156 ObjectNode jsonNode = objectMapper.createObjectNode();
157 jsonNode.put("UPLOAD_CONFIG_ID", ctx.getAttribute("running-config.upload-config-id"));
158 jsonNode.put("REQUEST_ID", ctx.getAttribute("running-config.request-id"));
159 jsonNode.put("ORIGINATOR_ID", ctx.getAttribute("running-config.originator-id"));
160 jsonNode.put("SERVICE_DESCRIPTION", ctx.getAttribute("running-config.service-description"));
161 jsonNode.put("ACTION", ctx.getAttribute("running-config.action"));
162 jsonNode.put("UPLOAD_TIMESTAMP", epochUploadTimestamp);
163 jsonNode.put("UPLOAD_DATE", uploadDate);
164 jsonNode.put("VNF_ID", vnfId);
165 jsonNode.put("VNF_NAME", ctx.getAttribute("running-config.vnf-name"));
166 jsonNode.put("VM_NAME", ctx.getAttribute("running-config.vm-name"));
167 jsonNode.put("VNF_TYPE", ctx.getAttribute("running-config.vnf-type"));
168 jsonNode.put("VNFC_TYPE", ctx.getAttribute("running-config.vnfc-type"));
169 jsonNode.put("HOST_IP_ADDRESS", ctx.getAttribute("running-config.host-ip-address"));
170 jsonNode.put("CONFIG_INDICATOR", ctx.getAttribute("running-config.config-indicator"));
171 jsonNode.put("PENDING_DELETE", ctx.getAttribute("running-config.pending-delete"));
172 jsonNode.put("CONTENT", ctx.getAttribute("running-config.content"));
174 ctx.setAttribute(Constants.CVAAS_FILE_CONTENT,
175 objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
177 } catch (Exception e) {
178 String errorMessage = "Failed to parse create cvass file for vnf with id : " + vnfId;
179 logger.error(errorMessage, e);
180 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, errorMessage);
181 throw new APPCException(e);
186 public void checkFileCreated(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
187 String filePath = ctx.getAttribute(Constants.CVAAS_FILE_NAME);
188 File file = new File(filePath);
190 if (!file.exists()) {
191 String vnfId = params.get("vnf-id");
192 String errorMessage = "Cvass file could not be created for vnf with id : " + vnfId;
193 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, errorMessage);
194 throw new APPCException(errorMessage);