From 8ff4faad57fceb3c62ad04da0cf9b6bae5f6bc4f Mon Sep 17 00:00:00 2001 From: Kai Date: Sat, 22 Feb 2020 15:54:23 +0800 Subject: fix debug log Issue-ID: DCAEGEN2-2077 Signed-off-by: Kai Lu Change-Id: Ice1b21ae478a6311f6202cac48ba10dc1b859bc3 --- .../core/exception/AnalyticsParsingException.java | 33 +++++++ .../onap/dcae/analytics/tca/core/util/LogSpec.java | 103 +++++++++++++++++++++ .../dcae/analytics/tca/core/util/TcaUtils.java | 92 ++++++++++++++++++ .../calculation/TcaAbatementCalculator.java | 28 +++--- .../calculation/TcaCalculationFunction.java | 35 +++++-- 5 files changed, 271 insertions(+), 20 deletions(-) create mode 100644 dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/exception/AnalyticsParsingException.java create mode 100644 dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/LogSpec.java create mode 100644 dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/TcaUtils.java (limited to 'dcae-analytics/dcae-analytics-tca-core') diff --git a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/exception/AnalyticsParsingException.java b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/exception/AnalyticsParsingException.java new file mode 100644 index 0000000..b06f2fb --- /dev/null +++ b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/exception/AnalyticsParsingException.java @@ -0,0 +1,33 @@ +/* + * ================================================================================ + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ + +package org.onap.dcae.analytics.tca.core.exception; + +/** + * @author Rajiv Singla + */ +public class AnalyticsParsingException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public AnalyticsParsingException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/LogSpec.java b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/LogSpec.java new file mode 100644 index 0000000..002cf07 --- /dev/null +++ b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/LogSpec.java @@ -0,0 +1,103 @@ +/* + * ================================================================================ + * Copyright (c) 2018 China Mobile. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.dcae.analytics.tca.core.util; + +import java.util.Date; + +import org.onap.dcae.analytics.model.TcaModelConstants; +import org.onap.dcae.analytics.model.ecomplogger.AnalyticsErrorType; +import org.onap.dcae.utils.eelf.logger.api.info.ErrorLogInfo; +import org.onap.dcae.utils.eelf.logger.api.info.ResponseLogInfo; +import org.onap.dcae.utils.eelf.logger.api.info.TargetServiceLogInfo; +import org.onap.dcae.utils.eelf.logger.api.spec.AuditLogSpec; +import org.onap.dcae.utils.eelf.logger.api.spec.DebugLogSpec; +import org.onap.dcae.utils.eelf.logger.api.spec.ErrorLogSpec; +import org.onap.dcae.utils.eelf.logger.model.info.ErrorLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.RequestTimingLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.ResponseLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.TargetServiceLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.AuditLogSpecImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.DebugLogSpecImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.ErrorLogSpecImpl; + +/** + * @author Kai Lu + */ +public final class LogSpec { + + private LogSpec( ) { + // private constructor + } + + /** + * create ErrorLogSpec + * + * @param requestId requestId + * + * @return ErrorLogSpecImpl object + * + */ + public static ErrorLogSpec createErrorLogSpec(final String requestId) { + final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId); + final TargetServiceLogInfo targetServiceLogInfo = new TargetServiceLogInfoImpl( + "DCAE-TCA", TcaModelConstants.TCA_SERVICE_NAME, ""); + final ErrorLogInfo errorLogInfo = + new ErrorLogInfoImpl(AnalyticsErrorType.SCHEMA_ERROR.getErrorCode(), + AnalyticsErrorType.SCHEMA_ERROR.getErrorDescription()); + return new ErrorLogSpecImpl(requestIdLogInfo, + TcaUtils.TCA_SERVICE_LOG_INFO, targetServiceLogInfo, errorLogInfo); + } + + /** + * create DebugLogSpec + * + * @param requestId requestId + * + * @return DebugLogSpecImpl object + * + */ + public static DebugLogSpec createDebugLogSpec(final String requestId) { + final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId); + return new DebugLogSpecImpl(requestIdLogInfo); + } + + /** + * create AuditLogSpec + * + * @param requestId requestId + * @param requestBeginTimestamp requestBeginTimestamp + * + * @return AuditLogSpec object + * + */ + public static AuditLogSpec createAuditLogSpec(final String requestId, + final Date requestBeginTimestamp) { + final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId); + final Date endTimestamp = new Date(); + final RequestTimingLogInfoImpl requestTimingLogInfo = new RequestTimingLogInfoImpl(requestBeginTimestamp, + endTimestamp, endTimestamp.getTime() - requestBeginTimestamp.getTime()); + final ResponseLogInfo responseLogInfo = + new ResponseLogInfoImpl(AnalyticsErrorType.SUCCESSFUL.getErrorCode(), + AnalyticsErrorType.SUCCESSFUL.getErrorDescription()); + return new AuditLogSpecImpl(requestIdLogInfo, TcaUtils.TCA_SERVICE_LOG_INFO, + requestTimingLogInfo, responseLogInfo); + } + +} diff --git a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/TcaUtils.java b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/TcaUtils.java new file mode 100644 index 0000000..bbadcac --- /dev/null +++ b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/TcaUtils.java @@ -0,0 +1,92 @@ +/* + * ================================================================================ + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ + +package org.onap.dcae.analytics.tca.core.util; + +import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_OBJECT_MAPPER; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.onap.dcae.analytics.model.TcaModelConstants; +import org.onap.dcae.analytics.model.cef.EventListener; +import org.onap.dcae.analytics.tca.core.exception.AnalyticsParsingException; +import org.onap.dcae.analytics.tca.model.policy.TcaPolicy; +import org.onap.dcae.utils.eelf.logger.model.info.ServiceLogInfoImpl; + +/** + * @author Rajiv Singla + */ +public abstract class TcaUtils { + + /** + * TCA Service Log Info for ECOMP Logging + */ + public static final ServiceLogInfoImpl TCA_SERVICE_LOG_INFO = + new ServiceLogInfoImpl(TcaModelConstants.TCA_SERVICE_NAME, System.getProperty("user.name"), ""); + + /** + * Creates a deep copy of Tca Policy + * + * @param tcaPolicy source tca policy object + * + * @return deep copy of provided tca policy + */ + public static TcaPolicy getTcaPolicyDeepCopy(final TcaPolicy tcaPolicy) { + if (tcaPolicy != null) { + try { + return TCA_OBJECT_MAPPER.treeToValue(TCA_OBJECT_MAPPER.valueToTree(tcaPolicy), TcaPolicy.class); + } catch (JsonProcessingException e) { + throw new AnalyticsParsingException("Unable to create deep copy of TCA Policy: " + tcaPolicy, e); + } + } else { + final String errorMessage = "Invalid application state. TCA Policy must not be null"; + throw new AnalyticsParsingException(errorMessage, new IllegalStateException(errorMessage)); + } + } + + + /** + * Converts given event Listeners to list of CEF Message String + * + * @param eventListeners event listeners object + * + * @return cef messages as string + */ + public static List getCefMessagesFromEventListeners(final List eventListeners) { + if (!Optional.ofNullable(eventListeners).isPresent()) { + return Collections.emptyList(); + } + return eventListeners.stream().map(eventListener -> { + try { + return TCA_OBJECT_MAPPER.writeValueAsString(eventListener); + } catch (JsonProcessingException e) { + throw new AnalyticsParsingException("Unable to parse EventLister to String: " + eventListener, e); + } + }).collect(Collectors.toList()); + } + + private TcaUtils() { + // private constructor + } +} diff --git a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaAbatementCalculator.java b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaAbatementCalculator.java index 0322456..3dff203 100644 --- a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaAbatementCalculator.java +++ b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaAbatementCalculator.java @@ -36,15 +36,17 @@ import org.onap.dcae.analytics.tca.core.service.TcaResultContext; import org.onap.dcae.analytics.tca.model.policy.ClosedLoopEventStatus; import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName; import org.onap.dcae.analytics.tca.model.policy.Threshold; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.dcae.utils.eelf.logger.api.log.EELFLogFactory; +import org.onap.dcae.utils.eelf.logger.api.log.EELFLogger; +import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.DebugLogSpecImpl; /** * @author Rajiv Singla */ public class TcaAbatementCalculator implements TcaCalculationFunction { - private static final Logger logger = LoggerFactory.getLogger(TcaAbatementCalculator.class); + private static final EELFLogger logger = EELFLogFactory.getLogger(TcaAbatementCalculator.class); @Override public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) { @@ -70,8 +72,8 @@ public class TcaAbatementCalculator implements TcaCalculationFunction { final ClosedLoopEventStatus closedLoopEventStatus = violatedThreshold.getClosedLoopEventStatus(); final String requestId = tcaExecutionContext.getRequestId(); final String lookupKey = createLookupKey(eventListener, violatedMetricsPerEventName); - - + final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId); + final DebugLogSpecImpl debugLogSpec = new DebugLogSpecImpl(requestIdLogInfo); switch (closedLoopEventStatus) { // ONSET - save alert info in database so that next abated event can fetch its request id for abated @@ -80,9 +82,9 @@ public class TcaAbatementCalculator implements TcaCalculationFunction { final TcaAbatementEntity tcaAbatementEntity = abatementContext.create(lookupKey, requestId, false); - logger.debug("Request Id: {}. Alert ClosedLoop Status is ONSET. " + + logger.debugLog().debug("Request Id: {}. Alert ClosedLoop Status is ONSET. " + "Saving abatement Entity to repository with lookupKey: {}", - requestId, tcaAbatementEntity.getLookupKey()); + debugLogSpec, requestId, tcaAbatementEntity.getLookupKey()); abatementPersistenceContext.save(tcaAbatementEntity); return tcaExecutionContext; @@ -99,8 +101,8 @@ public class TcaAbatementCalculator implements TcaCalculationFunction { final TcaAbatementEntity previousTcaAbatementEntity = previousTcaAbatementEntities.get(previousTcaAbatementEntities.size() - 1); - logger.debug("Request Id: {}. Found previous Abatement Entity: {}", requestId, - previousTcaAbatementEntity); + logger.debugLog().debug("Request Id: {}. Found previous Abatement Entity with lookupKey: {}", + debugLogSpec, requestId, previousTcaAbatementEntity.getLookupKey()); // previous abatement entity was found - but it was already sent before - so ignore alert creation if (previousTcaAbatementEntity.isAbatementAlertSent()) { @@ -113,14 +115,14 @@ public class TcaAbatementCalculator implements TcaCalculationFunction { // no previous abatement was sent final String previousRequestId = previousTcaAbatementEntity.getRequestId(); // set abated alert request id to previous ONSET alert request id - logger.debug("Request Id: {}. No previous abated alert was sent. Setting previous request id: {}", - requestId, previousRequestId); + logger.debugLog().debug("Request Id: {}. No previous abated alert was sent. Setting previous request id: {}", + debugLogSpec, requestId, previousRequestId); resultContext.setPreviousRequestId(previousRequestId); // save new entity with alert as sent final TcaAbatementEntity newTcaAbatementEntity = abatementContext.create(lookupKey, previousRequestId, true); - logger.debug("Request Id: {}. Saving new entity with alert as sent: {}", - requestId, newTcaAbatementEntity); + logger.debugLog().debug("Request Id: {}. Saving new entity with alert as sent with lookupKey: {}", + debugLogSpec, requestId, newTcaAbatementEntity.getLookupKey()); abatementPersistenceContext.save(newTcaAbatementEntity); return tcaExecutionContext; diff --git a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaCalculationFunction.java b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaCalculationFunction.java index ec9f7bd..d24bfe1 100644 --- a/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaCalculationFunction.java +++ b/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaCalculationFunction.java @@ -21,10 +21,20 @@ package org.onap.dcae.analytics.tca.core.util.function.calculation; import java.util.function.Function; +import org.onap.dcae.analytics.model.TcaModelConstants; +import org.onap.dcae.analytics.model.ecomplogger.AnalyticsErrorType; import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext; import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.dcae.analytics.tca.core.util.TcaUtils; +import org.onap.dcae.utils.eelf.logger.api.info.ErrorLogInfo; +import org.onap.dcae.utils.eelf.logger.api.info.TargetServiceLogInfo; +import org.onap.dcae.utils.eelf.logger.api.log.EELFLogFactory; +import org.onap.dcae.utils.eelf.logger.api.log.EELFLogger; +import org.onap.dcae.utils.eelf.logger.model.info.ErrorLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.info.TargetServiceLogInfoImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.DebugLogSpecImpl; +import org.onap.dcae.utils.eelf.logger.model.spec.ErrorLogSpecImpl; /** * Functional interface which all TCA calculation functions should implement @@ -34,11 +44,10 @@ import org.slf4j.LoggerFactory; @FunctionalInterface public interface TcaCalculationFunction extends Function { - Logger logger = LoggerFactory.getLogger(TcaCalculationFunction.class); + EELFLogger logger = EELFLogFactory.getLogger(TcaCalculationFunction.class); TcaExecutionContext calculate(TcaExecutionContext tcaExecutionContext); - default TcaExecutionContext preCalculation(TcaExecutionContext tcaExecutionContext) { // do nothing return tcaExecutionContext; @@ -69,12 +78,24 @@ public interface TcaCalculationFunction extends Function