2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.generator.util;
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.BEGIN_TIMESTAMP;
24 import static org.openecomp.sdc.generator.data.GeneratorConstants.BE_FQDN;
25 import static org.openecomp.sdc.generator.data.GeneratorConstants.CATEGORY_LOG_LEVEL;
26 import static org.openecomp.sdc.generator.data.GeneratorConstants.CLIENT_IP;
27 import static org.openecomp.sdc.generator.data.GeneratorConstants.ELAPSED_TIME;
28 import static org.openecomp.sdc.generator.data.GeneratorConstants.END_TIMESTAMP;
29 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CATEGORY;
30 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CODE;
31 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_DESCRIPTION;
32 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGFILE_NOT_FOUND;
33 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND;
34 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND;
35 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING;
36 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_MISSING;
37 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID;
38 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_RESOURCE_VERSION_IN_SERVICE_TOSCA;
39 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_TOSCA_MSG;
40 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_MANDATORY_METADATA_DEFINITION_MSG;
41 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_RESOURCE_TOSCA;
42 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA_MSG;
43 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION;
44 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_ERROR_NULL_RESOURCE_VERSION_IN_SERVICE_TOSCA;
45 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION;
46 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED_MSG;
47 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION_MSG;
48 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_PARTNER_NAME;
49 import static org.openecomp.sdc.generator.data.GeneratorConstants.INSTANCE_UUID;
50 import static org.openecomp.sdc.generator.data.GeneratorConstants.LOCAL_ADDR;
51 import static org.openecomp.sdc.generator.data.GeneratorConstants.MDC_SDC_INSTANCE_UUID;
52 import static org.openecomp.sdc.generator.data.GeneratorConstants.PARTNER_NAME;
53 import static org.openecomp.sdc.generator.data.GeneratorConstants.REMOTE_HOST;
54 import static org.openecomp.sdc.generator.data.GeneratorConstants.REQUEST_ID;
55 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_CODE;
56 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_DESCRIPTION;
57 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_INSTANCE_ID;
58 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_METRIC_BEGIN_TIMESTAMP;
59 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_NAME;
60 import static org.openecomp.sdc.generator.data.GeneratorConstants.STATUS_CODE;
61 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_ENTITY;
62 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_SERVICE_NAME;
63 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INTERNAL_SERVER_ERROR;
64 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INVALID_CLIENT_CONFIGURATION;
65 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INVALID_ID_VALUE;
66 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INVALID_RESOURCE_VERSION;
67 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INVALID_SERVICE_VERSION;
68 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.INVALID_TOSCA_YAML;
69 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MANDATORY_ATTRIBUTE_MISSING;
70 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_CONFIG_PROPERTIES_FILE;
71 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_PRO_SERVICE;
72 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_PRO_SERVICE_METADATA;
73 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_RESOURCE_VERSION;
74 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_SERVICE_VERSION;
75 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_SYSTME_PROPERY_CONFIGURATION;
76 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.MISSING_WIDGET_CONFIGURATION;
77 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.RESOURCE_TOSCA_MISSING;
78 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.SERVICE_TOSCA_MISSING;
79 import static org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode.UNABLE_TO_GENERATE_ARTIFACT;
81 import org.openecomp.sdc.logging.api.Logger;
82 import org.openecomp.sdc.logging.api.LoggerFactory;
83 import org.openecomp.sdc.generator.data.Artifact;
84 import org.openecomp.sdc.generator.logging.ArtifactGeneratorLogResponseCode;
85 import org.openecomp.sdc.generator.logging.CategoryLogLevel;
86 import org.openecomp.sdc.generator.logging.StatusCode;
89 import java.io.PrintWriter;
90 import java.io.StringWriter;
91 import java.net.InetAddress;
92 import java.net.UnknownHostException;
93 import java.text.DateFormat;
94 import java.text.SimpleDateFormat;
95 import java.util.Date;
96 import java.util.TimeZone;
99 public class ArtifactGeneratorUtil {
101 private static Logger log = LoggerFactory.getLogger(ArtifactGeneratorUtil.class.getName());
102 private static final String LOG_UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
105 * Artifact Generator Error logging Helper.
106 * @param errorDescription Description of the error
108 public static void logError(String errorDescription) {
109 logError(errorDescription, "");
113 * Artifact Generator Error logging Helper.
114 * @param errorDescription Description of the error
115 * @param ex Exception object for stackstrace
117 public static void logError(String errorDescription, Exception ex) {
118 StringWriter sw = new StringWriter();
119 ex.printStackTrace(new PrintWriter(sw));
120 String detailMessage = sw.toString();
121 logError(CategoryLogLevel.ERROR, errorDescription, detailMessage);
125 * Artifact Generator Error logging Helper.
126 * @param errorDescription Description of the error
127 * @param detailMessage Detailed Error message
129 public static void logError(String errorDescription, String detailMessage) {
130 logError(CategoryLogLevel.ERROR, errorDescription, detailMessage);
134 * Artifact Generator Error logging Helper.
135 * @param errorCategory ERROR
136 * @param errorDescription Description of the error
137 * @param detailMessage Detailed Error message
139 public static void logError(CategoryLogLevel errorCategory,
140 String errorDescription, String detailMessage) {
141 MDC.put(ERROR_CATEGORY, errorCategory.name());
142 MDC.put(STATUS_CODE, StatusCode.ERROR.name());
143 artifactGeneratorErrorLogProcessor(errorCategory,errorDescription);
144 log.error(detailMessage);
145 resetLoggingContext();
149 * Initialize generic MDC attributes for logging the current request.
152 public static void initializeLoggingContext() {
153 log.debug("Initializing generic logging context ");
154 MDC.put(PARTNER_NAME, GENERATOR_PARTNER_NAME);
155 MDC.put(SERVICE_METRIC_BEGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
156 MDC.put(INSTANCE_UUID, MDC_SDC_INSTANCE_UUID);
157 MDC.put(STATUS_CODE, StatusCode.COMPLETE.name());
158 MDC.put(CLIENT_IP, MDC.get(REMOTE_HOST));
161 InetAddress ip = InetAddress.getLocalHost();
162 MDC.put(LOCAL_ADDR, ip.getHostAddress());
163 String hostname = ip.getHostName();
164 MDC.put(BE_FQDN, hostname);
165 } catch (UnknownHostException uhe) {
166 log.error("Failed to get server FQDN", uhe);
169 if (log.isDebugEnabled()) {
170 MDC.put(CATEGORY_LOG_LEVEL, CategoryLogLevel.DEBUG.name());
171 } else if (log.isInfoEnabled()) {
172 MDC.put(CATEGORY_LOG_LEVEL, CategoryLogLevel.INFO.name());
173 } else if (log.isWarnEnabled()) {
174 MDC.put(CATEGORY_LOG_LEVEL, CategoryLogLevel.WARN.name());
175 } else if (log.isErrorEnabled()) {
176 MDC.put(CATEGORY_LOG_LEVEL, CategoryLogLevel.ERROR.name());
181 * Initialize MDC for logging the current artifact request.
183 * @param artifact Current artifact
185 public static void initializeArtifactLoggingContext(Artifact artifact) {
186 log.debug("Initializing logging context for " + artifact.getLabel());
187 MDC.put(REQUEST_ID, artifact.getLabel());
188 MDC.put(SERVICE_NAME, artifact.getType());
189 MDC.put(SERVICE_INSTANCE_ID, artifact.getName());
193 * Reset the logging context after a Audit/Metrics logging operation.
195 public static void resetLoggingContext() {
196 MDC.remove(ERROR_CATEGORY);
197 MDC.remove(ERROR_CODE);
198 MDC.remove(STATUS_CODE);
199 MDC.remove(ERROR_DESCRIPTION);
200 MDC.remove(BEGIN_TIMESTAMP);
201 MDC.remove(END_TIMESTAMP);
202 MDC.remove(ELAPSED_TIME);
203 MDC.put(STATUS_CODE, StatusCode.COMPLETE.name());
204 MDC.remove(RESPONSE_CODE);
205 MDC.remove(RESPONSE_DESCRIPTION);
206 MDC.remove(TARGET_ENTITY);
207 MDC.remove(TARGET_SERVICE_NAME);
211 * Convert timestamp to UTC format date string.
213 * @param timeStamp UTC timestamp to be converted to the UTC Date format.
214 * @return UTC formatted Date string from timestamp.
216 public static String getLogUtcDateStringFromTimestamp(Date timeStamp) {
217 DateFormat df = new SimpleDateFormat(LOG_UTC_DATE_FORMAT);
218 df.setTimeZone(TimeZone.getTimeZone("GMT"));
219 return df.format(timeStamp);
223 * Artifact Gnenerator Error logging Helper.
225 * @param errorCategory WARN or ERROR.
226 * @param errorDescription Description of the error.
228 public static void artifactGeneratorErrorLogProcessor(CategoryLogLevel errorCategory,
229 String errorDescription) {
230 MDC.put(ERROR_CATEGORY, errorCategory.name());
231 if (errorDescription != null) {
232 String errorType = "";
233 switch (errorCategory) {
246 MDC.put(ERROR_CODE, getLogResponseCode(errorDescription) + errorType);
248 MDC.put(ERROR_DESCRIPTION, errorDescription);
254 * @return Audit log code corresponding to the Artifact Generator exception.
256 public static int getLogResponseCode(String errorDescription) {
257 ArtifactGeneratorLogResponseCode responseCode = INTERNAL_SERVER_ERROR;
258 if (errorDescription.contains(GENERATOR_AAI_ERROR_MANDATORY_METADATA_DEFINITION_MSG)) {
259 responseCode = MANDATORY_ATTRIBUTE_MISSING;
260 } else if (errorDescription.contains(GENERATOR_AAI_ERROR_INVALID_TOSCA_MSG)) {
261 responseCode = INVALID_TOSCA_YAML;
262 } else if (errorDescription.contains(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA_MSG)) {
263 responseCode = SERVICE_TOSCA_MISSING;
264 } else if (errorDescription.contains(GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION_MSG)) {
265 responseCode = INVALID_CLIENT_CONFIGURATION;
266 } else if (errorDescription.contains(GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED_MSG)) {
267 responseCode = UNABLE_TO_GENERATE_ARTIFACT;
268 } else if (errorDescription.contains(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND.split("%s")[0])) {
269 responseCode = MISSING_SYSTME_PROPERY_CONFIGURATION;
270 } else if (errorDescription.contains(GENERATOR_AAI_CONFIGFILE_NOT_FOUND.split("%s")[0])) {
271 responseCode = MISSING_CONFIG_PROPERTIES_FILE;
272 } else if (errorDescription.contains(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND.split("%s")[0])) {
273 responseCode = MISSING_WIDGET_CONFIGURATION;
274 } else if (errorDescription.contains(GENERATOR_AAI_ERROR_INVALID_ID.split("%s")[0])) {
275 responseCode = INVALID_ID_VALUE;
276 } else if (errorDescription.contains(GENERATOR_AAI_ERROR_MISSING_RESOURCE_TOSCA.split("%s")[0]))
278 responseCode = RESOURCE_TOSCA_MISSING;
279 } else if(errorDescription.contains(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION)) {
280 responseCode = MISSING_SERVICE_VERSION;
281 } else if(errorDescription.contains(GENERATOR_AAI_INVALID_SERVICE_VERSION))
283 responseCode = INVALID_SERVICE_VERSION;
284 } else if(errorDescription.contains(GENERATOR_AAI_ERROR_NULL_RESOURCE_VERSION_IN_SERVICE_TOSCA.
286 responseCode = MISSING_RESOURCE_VERSION;
287 } else if(errorDescription.contains(
288 GENERATOR_AAI_ERROR_INVALID_RESOURCE_VERSION_IN_SERVICE_TOSCA.split("%s")[0])) {
289 responseCode = INVALID_RESOURCE_VERSION;
290 } else if(errorDescription.contains(GENERATOR_AAI_PROVIDING_SERVICE_MISSING.split("%s")[0])) {
291 responseCode = MISSING_PRO_SERVICE;
292 } else if(errorDescription.contains(
293 GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING.split("%s")[0])) {
294 responseCode = MISSING_PRO_SERVICE_METADATA;
296 return responseCode.getValue();