From eb06c3532c133a9126bd45c5485087edf5e3ad54 Mon Sep 17 00:00:00 2001 From: huaxing Date: Fri, 10 Apr 2020 13:16:12 +0800 Subject: Fix sonar issues Issue-ID: POLICY-1913 Signed-off-by: huaxing Change-Id: Id1a9a6b6f7a9c8c74dd69bd0d30af05162a92cd1 --- ...alyDetectionPolicyDecideTaskSelectionLogic.java | 41 +++++++++------------- .../apex/model/basicmodel/concepts/AxKeyInfo.java | 9 ++--- .../event/carrier/grpc/ApexGrpcProducer.java | 2 +- .../common/model/java/DefaultTaskLogic.java | 2 +- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java index ffbaf5905..99dd28dd8 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +38,6 @@ import org.slf4j.Logger; * The Class AnomalyDetectionPolicyDecideTaskSelectionLogic. */ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { - private Logger logger; // Recurring string constants private static final String ANOMALY_DETECTION_ALBUM = "AnomalyDetectionAlbum"; @@ -71,8 +71,6 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { } // CHECKSTYLE:ON: checkstyle:magicNumber - private volatile TaskSelectionExecutionContext executionContext; - /** * Gets the task. * @@ -80,16 +78,14 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { * @return the task */ public boolean getTask(final TaskSelectionExecutionContext executor) { - executionContext = executor; - logger = executionContext.logger; String id = executor.subject.getId(); - logger.debug(id); + executor.logger.debug(id); String inFields = executor.inFields.toString(); - logger.debug(inFields); + executor.logger.debug(inFields); final double now = (Double) (executor.inFields.get("MonitoredValue")); final Integer iteration = (Integer) (executor.inFields.get("Iteration")); // get the double[forecastedValue, AnomalyScore, AnomalyProbability] - final double[] vals = this.forecastingAndAnomaly(now); + final double[] vals = forecastingAndAnomaly(executor, now); final double anomalyness = vals[2]; String task = null; for (final Map.Entry i : TASK_INTERVALS.entrySet()) { @@ -99,17 +95,14 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { } } if (task == null) { - executionContext.subject.getDefaultTaskKey().copyTo(executionContext.selectedTask); + executor.subject.getDefaultTaskKey().copyTo(executor.selectedTask); } else { - executionContext.subject.getTaskKey(task).copyTo(executionContext.selectedTask); - } - if (logger.isDebugEnabled()) { - logger.debug( - "TestAnomalyDetectionTSLPolicy0000DecideStateTaskSelectionLogic.getTask():\t************\t\t\t\t" - + "Iteration:\t" + iteration + "\tValue:\t" + now + "\tForecast:\t" + vals[0] - + "\tAnomalyScore:\t" + vals[1] + "\tAnomalyProbability:\t" + vals[2] + "\tInvoking Task:\t" - + executionContext.selectedTask); + executor.subject.getTaskKey(task).copyTo(executor.selectedTask); } + executor.logger.debug( + "TestAnomalyDetectionTSLPolicy0000DecideStateTaskSelectionLogic.getTask():\t************\t\t\t\t" + + "Iteration:\t{}\tValue:\t{}\tForecast:\t{}\tAnomalyScore:\t{}\tAnomalyProbability:\t{}\t" + + "Invoking Task:\t{}", iteration, now, vals[0], vals[1], vals[2], executor.selectedTask); return true; } @@ -120,20 +113,20 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { * @return Null if the function can not be executed correctly, otherwise double[forecastedValue, * AnomalyScore, AnomalyProbability] */ - public double[] forecastingAndAnomaly(final double value) { + private double[] forecastingAndAnomaly(final TaskSelectionExecutionContext executor, final double value) { try { - executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).lockForWriting(ANOMALY_DETECTION); + executor.getContextAlbum(ANOMALY_DETECTION_ALBUM).lockForWriting(ANOMALY_DETECTION); } catch (final ApexException e) { - logger.error("Failed to acquire write lock on \"AnomalyDetection\" context", e); + executor.logger.error("Failed to acquire write lock on \"AnomalyDetection\" context", e); return new double[0]; } // Get the context object AnomalyDetection anomalyDetection = - (AnomalyDetection) executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).get(ANOMALY_DETECTION); + (AnomalyDetection) executor.getContextAlbum(ANOMALY_DETECTION_ALBUM).get(ANOMALY_DETECTION); if (anomalyDetection == null) { anomalyDetection = new AnomalyDetection(); - executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).put(ANOMALY_DETECTION, anomalyDetection); + executor.getContextAlbum(ANOMALY_DETECTION_ALBUM).put(ANOMALY_DETECTION, anomalyDetection); } // Check the lists are initialized @@ -186,9 +179,9 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { // CHECKSTYLE:ON: checkstyle:magicNumber try { - executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).unlockForWriting(ANOMALY_DETECTION); + executor.getContextAlbum(ANOMALY_DETECTION_ALBUM).unlockForWriting(ANOMALY_DETECTION); } catch (final ApexException e) { - logger.error("Failed to release write lock on \"AnomalyDetection\" context", e); + executor.logger.error("Failed to release write lock on \"AnomalyDetection\" context", e); return new double[0]; } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java index a60c609e2..35010bf4b 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java @@ -37,6 +37,7 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner; import org.onap.policy.apex.model.basicmodel.dao.converters.Uuid2String; @@ -64,6 +65,8 @@ public class AxKeyInfo extends AxConcept { private static final int MAX_DESCRIPTION_LENGTH_8192 = 8192; private static final int UUID_BYTE_LENGTH_16 = 16; + private static final Random sharedRandom = new Random(); + @EmbeddedId @XmlElement(name = "key", required = true) private AxArtifactKey key; @@ -331,11 +334,9 @@ public class AxKeyInfo extends AxConcept { * @return the uuid */ public static UUID generateReproducibleUuid(final String seed) { - final Random random; - if (seed != null && seed.length() > 0) { + Random random = sharedRandom; + if (!StringUtils.isEmpty(seed)) { random = new Random(seed.hashCode()); - } else { - random = new Random(); } final byte[] array = new byte[UUID_BYTE_LENGTH_16]; random.nextBytes(array); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/main/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/main/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducer.java index c98fa41ea..2e4736208 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/main/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/main/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducer.java @@ -176,6 +176,6 @@ public class ApexGrpcProducer extends ApexPluginsEventProducer implements CdsPro cdsResponse.set(ExecutionServiceOutput.newBuilder() .setStatus(Status.newBuilder().setErrorMessage(errorMsg).setEventType(EventType.EVENT_COMPONENT_FAILURE)) .build()); - LOGGER.error("Failed processing blueprint {} {}", errorMsg, throwable); + LOGGER.error("Failed processing blueprint {}", errorMsg, throwable); } } diff --git a/testsuites/integration/integration-common/src/main/java/org/onap/policy/apex/testsuites/integration/common/model/java/DefaultTaskLogic.java b/testsuites/integration/integration-common/src/main/java/org/onap/policy/apex/testsuites/integration/common/model/java/DefaultTaskLogic.java index 3730bba47..33f88bf2d 100644 --- a/testsuites/integration/integration-common/src/main/java/org/onap/policy/apex/testsuites/integration/common/model/java/DefaultTaskLogic.java +++ b/testsuites/integration/integration-common/src/main/java/org/onap/policy/apex/testsuites/integration/common/model/java/DefaultTaskLogic.java @@ -30,6 +30,7 @@ import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext; */ public class DefaultTaskLogic { private static final int BOUND_FOR_RANDOM_INT = 4; + private static final Random rand = new Random(); /** * Gets the event. @@ -46,7 +47,6 @@ public class DefaultTaskLogic { String inFieldsString = executor.inFields.toString(); executor.logger.debug(inFieldsString); - final Random rand = new Random(); if (executor.inFields.containsKey("TestDecideCaseSelected")) { executor.outFields.put("TestActCaseSelected", (byte) rand.nextInt(BOUND_FOR_RANDOM_INT)); executor.outFields.put("TestActStateTime", System.nanoTime()); -- cgit 1.2.3-korg