[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-core / src / main / java / org / openecomp / sdc / generator / aspect / MetricsAspect.java
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.ARTIFACT_MODEL_INFO;
24 import static org.openecomp.sdc.generator.data.GeneratorConstants.BEGIN_TIMESTAMP;
25 import static org.openecomp.sdc.generator.data.GeneratorConstants.ELAPSED_TIME;
26 import static org.openecomp.sdc.generator.data.GeneratorConstants.END_TIMESTAMP;
27 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CODE;
28 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC;
29 import static org.openecomp.sdc.generator.data.GeneratorConstants.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_CODE;
32 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_DESC;
33 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_TARGET_ENTITY;
34 import static org.openecomp.sdc.generator.data.GeneratorConstants.REQUEST_ID;
35 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_CODE;
36 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_DESCRIPTION;
37 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_INSTANCE_ID;
38 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_NAME;
39 import static org.openecomp.sdc.generator.data.GeneratorConstants.STATUS_CODE;
40 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_ENTITY;
41 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_SERVICE_NAME;
42 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.artifactGeneratorErrorLogProcessor;
43 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.getLogUtcDateStringFromTimestamp;
44 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.resetLoggingContext;
45
46 import org.aspectj.lang.ProceedingJoinPoint;
47 import org.aspectj.lang.annotation.Around;
48 import org.aspectj.lang.annotation.Aspect;
49 import org.openecomp.core.logging.api.Logger;
50 import org.openecomp.core.logging.api.LoggerFactory;
51 import org.openecomp.sdc.generator.data.GenerationData;
52 import org.openecomp.sdc.generator.logging.CategoryLogLevel;
53 import org.openecomp.sdc.generator.logging.StatusCode;
54 import org.slf4j.MDC;
55
56 import java.util.Date;
57
58 /**
59  * Wraps around any method annotated with
60  * {@link org.openecomp.sdc.generator.logging.annotations.Metrics} for logging metrics information
61  * In order for the aspect to be used, AspectJ annotation processing must be
62  * turned on and this particular aspect enabled.
63  *
64  * @see org.openecomp.sdc.generator.logging.annotations.Metrics
65  */
66 @Aspect
67 public class MetricsAspect {
68
69   /**
70    * Log Audit information for the method.
71    *
72    * @param pjp the pjp
73    * @return the object
74    * @throws Throwable the throwable
75    */
76   @Around("@annotation(org.openecomp.sdc.generator.logging.annotations.Metrics)")
77   public Object logMetrics(ProceedingJoinPoint pjp) throws Throwable {
78
79     final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
80     String [] modelInfo = MDC.get(ARTIFACT_MODEL_INFO).split(",");
81
82     if (modelInfo.length == 2) {
83       //Since ARTIFACT_MODEL_INFO is passed as String from caller "null" value is populated.
84       //So resetting "null" to "" in logs
85       String serviceInstanceId =  modelInfo[0].equals("null") ? "" : modelInfo[0];
86       String requestId =  modelInfo[1].equals("null") ? "" : modelInfo[1];
87       MDC.put(SERVICE_INSTANCE_ID, serviceInstanceId);
88       MDC.put(REQUEST_ID, requestId);
89     }
90
91     MDC.put(TARGET_ENTITY, GENERATOR_METRICS_TARGET_ENTITY);
92     MDC.put(TARGET_SERVICE_NAME, MDC.get(SERVICE_NAME) + " artifact generation");
93     // check if metrics aspect is enabled
94     if (logger.isMetricsEnabled()) {
95       final long beginTimestamp = System.currentTimeMillis();
96       try {
97         MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_SUCCESS_RESPONSE_DESC, MDC
98             .get(SERVICE_NAME)));
99         Object obj = pjp.proceed();
100         if (obj instanceof  GenerationData) {
101           GenerationData data = (GenerationData) obj;
102           if (data.getErrorData() != null && !data.getErrorData().isEmpty()) {
103             MDC.put(STATUS_CODE, StatusCode.ERROR.name());
104             artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR, data.getErrorData().get(
105                 "AAI").get(0));
106             MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
107             // MDC.put(RESPONSE_CODE, GENERATOR_METRICS_FAILURE_RESPONSE_CODE);
108             // If not service name  found log no artifact type found error.
109             if (MDC.get(SERVICE_NAME) == null) {
110               MDC.put(RESPONSE_DESCRIPTION, GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC);
111             } else {
112               MDC.put(RESPONSE_DESCRIPTION,
113                   String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
114                       .get(SERVICE_NAME)));
115             }
116           }
117         }
118         return obj;
119
120
121       } catch (Exception ex) {
122         artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR,ex.getMessage());
123         MDC.put(STATUS_CODE, StatusCode.ERROR.name());
124         MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
125         MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
126             .get(SERVICE_NAME)));
127         throw ex;
128       } finally {
129         long endTimestamp = System.currentTimeMillis();
130         MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
131         MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
132         MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
133         logger.metrics("");
134         resetLoggingContext();
135       }
136     } else {
137       return pjp.proceed();
138     }
139   }
140 }