170a9c229ee1db493cb8a318b2516eeeba5e4a7f
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.generator.aspect;
22
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.BEGIN_TIMESTAMP;
24 import static org.openecomp.sdc.generator.data.GeneratorConstants.ELAPSED_TIME;
25 import static org.openecomp.sdc.generator.data.GeneratorConstants.END_TIMESTAMP;
26 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CODE;
27 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC;
28 import static org.openecomp.sdc.generator.data.GeneratorConstants
29     .GENERATOR_METRICS_FAILURE_RESPONSE_CODE;
30 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_FAILURE_RESPONSE_DESC;
31 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_DESC;
32
33 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_CODE;
34 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_DESCRIPTION;
35 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_NAME;
36 import static org.openecomp.sdc.generator.data.GeneratorConstants.STATUS_CODE;
37 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_ENTITY;
38 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_SERVICE_NAME;
39 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.artifactGeneratorErrorLogProcessor;
40 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.getLogUtcDateStringFromTimestamp;
41 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.logError;
42 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.resetLoggingContext;
43
44 import org.aspectj.lang.ProceedingJoinPoint;
45 import org.aspectj.lang.annotation.Around;
46 import org.aspectj.lang.annotation.Aspect;
47 import org.openecomp.sdc.logging.api.Logger;
48 import org.openecomp.sdc.logging.api.LoggerFactory;
49 import org.openecomp.sdc.generator.data.GenerationData;
50 import org.openecomp.sdc.generator.logging.CategoryLogLevel;
51 import org.openecomp.sdc.generator.logging.StatusCode;
52 import org.openecomp.sdc.generator.util.ArtifactGeneratorUtil;
53 import org.slf4j.MDC;
54
55 import java.util.Date;
56
57 /**
58  * Wraps around any method annotated with
59  * {@link org.openecomp.sdc.generator.logging.annotations.Audit} for auditing information
60  * In order for the aspect to be used, AspectJ annotation processing must be
61  * turned on and this particular aspect enabled.
62  *
63  * @see org.openecomp.sdc.generator.logging.annotations.Audit
64  */
65 @Aspect
66 public class AuditAspect {
67
68   /**
69    * Log Audit information for the method.
70    *
71    * @param pjp the pjp
72    * @return the object
73    * @throws Throwable the throwable
74    */
75   @Around("@annotation(org.openecomp.sdc.generator.logging.annotations.Audit)")
76   public Object logAuditInfo(ProceedingJoinPoint pjp) throws Throwable {
77
78     final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
79     // check if audit aspect is enabled
80     if (logger.isAuditEnabled()) {
81       final String method = pjp.getSignature().getName();
82       final long beginTimestamp = System.currentTimeMillis();
83       try {
84         Object obj = pjp.proceed();
85         MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_SUCCESS_RESPONSE_DESC, MDC
86             .get(SERVICE_NAME)));
87
88         GenerationData data = (GenerationData)obj;
89         if (data.getErrorData() != null && !data.getErrorData().isEmpty()) {
90           MDC.put(STATUS_CODE, StatusCode.ERROR.name());
91           artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR,data.getErrorData().get("AAI")
92               .get(0));
93           MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
94           // MDC.put(RESPONSE_CODE, GENERATOR_METRICS_FAILURE_RESPONSE_CODE);
95           // If not service name  found log no artifact type found error.
96           if (MDC.get(SERVICE_NAME) == null) {
97             MDC.put(RESPONSE_DESCRIPTION, GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC);
98           } else {
99             MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
100                 .get(SERVICE_NAME)));
101           }
102         }
103         return obj;
104
105       } catch (Exception ex) {
106         artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR,ex.getMessage());
107         MDC.put(STATUS_CODE, StatusCode.ERROR.name());
108         MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
109         MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
110               .get(SERVICE_NAME)));
111         throw ex;
112       } finally {
113         long endTimestamp = System.currentTimeMillis();
114         MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
115         MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
116         MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
117
118         logger.audit("");
119         resetLoggingContext();
120         MDC.remove(SERVICE_NAME);
121       }
122     } else {
123       return pjp.proceed();
124     }
125   }
126 }