From 1d0aaaa5b31719c1718700bb0d1a99c413fd513c Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 6 May 2021 11:28:53 -0400 Subject: Fix sonars in policy-common Fixed sonars: - use "var" instead of actual type name - re-interrupt threads - use rej2 split() instead of String split() Issue-ID: POLICY-3285 Change-Id: I82261e0b8a53ee5c5264556fbf5cec37454f014e Signed-off-by: Jim Hahn --- .../onap/policy/common/im/IntegrityMonitor.java | 64 ++++++++++++---------- .../org/onap/policy/common/im/StateManagement.java | 29 +++++----- .../org/onap/policy/common/im/StateTransition.java | 9 ++- .../onap/policy/common/im/jmx/ComponentAdmin.java | 6 +- .../common/im/jpa/ForwardProgressEntity.java | 2 +- .../onap/policy/common/im/jpa/ImTestEntity.java | 2 +- .../common/im/jpa/ResourceRegistrationEntity.java | 2 +- .../common/im/jpa/StateManagementEntity.java | 4 +- 8 files changed, 64 insertions(+), 54 deletions(-) (limited to 'integrity-monitor') diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java index 62103925..9a6616e3 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -34,6 +34,7 @@ import java.util.function.Consumer; import java.util.function.IntConsumer; import java.util.function.LongConsumer; import java.util.function.Supplier; +import java.util.regex.Pattern; import javax.management.JMX; import javax.management.MBeanServerConnection; import javax.persistence.EntityManager; @@ -42,7 +43,6 @@ import javax.persistence.EntityTransaction; import javax.persistence.FlushModeType; import javax.persistence.LockModeType; import javax.persistence.Persistence; -import javax.persistence.Query; import javax.persistence.TypedQuery; import org.apache.commons.lang3.StringUtils; import org.eclipse.persistence.config.PersistenceUnitProperties; @@ -63,6 +63,9 @@ public class IntegrityMonitor { private static final Logger logger = LoggerFactory.getLogger(IntegrityMonitor.class.getName()); + private static final Pattern COMMA_PAT = Pattern.compile(","); + private static final Pattern SEMICOLON_PAT = Pattern.compile(";"); + // only allow one instance of IntegrityMonitor private static IntegrityMonitor instance = null; @@ -212,7 +215,7 @@ public class IntegrityMonitor { // singleton check since this constructor can be called from a child or // sub-class if (instance != null) { - String msg = "IM object exists and only one instance allowed"; + var msg = "IM object exists and only one instance allowed"; logger.error("{}", msg); throw new IntegrityMonitorException("IntegrityMonitor constructor exception: " + msg); } @@ -466,7 +469,7 @@ public class IntegrityMonitor { private static String getJmxUrlFromProps() throws IntegrityMonitorException { // get the jmx remote port and construct the JMX URL - Properties systemProps = System.getProperties(); + var systemProps = System.getProperties(); String jmxPort = systemProps.getProperty("com.sun.management.jmxremote.port"); String jmxErrMsg; if (jmxPort == null) { @@ -475,7 +478,7 @@ public class IntegrityMonitor { throw new IntegrityMonitorException("getJmxUrl exception: " + jmxErrMsg); } - int port = 0; + var port = 0; try { port = Integer.parseInt(jmxPort); } catch (NumberFormatException e) { @@ -490,12 +493,12 @@ public class IntegrityMonitor { jmxFqdn = InetAddress.getLocalHost().getCanonicalHostName(); } } catch (Exception e) { - String msg = "getJmxUrl could not get hostname"; + var msg = "getJmxUrl could not get hostname"; logger.error("{}", msg, e); throw new IntegrityMonitorException("getJmxUrl Exception: " + msg); } if (jmxFqdn == null) { - String msg = "getJmxUrl encountered null hostname"; + var msg = "getJmxUrl encountered null hostname"; logger.error("{}", msg); throw new IntegrityMonitorException("getJmxUrl error: " + msg); } @@ -663,7 +666,7 @@ public class IntegrityMonitor { private void checkForwardProgress(String dep, ForwardProgressEntity forwardProgressEntity, StateManagementEntity stateManagementEntity) { if (forwardProgressEntity != null && stateManagementEntity != null) { - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); long diffMs = date.getTime() - forwardProgressEntity.getLastUpdated().getTime(); logger.debug("IntegrityMonitor.stateCheck(): diffMs = {}", diffMs); @@ -762,7 +765,7 @@ public class IntegrityMonitor { ArrayList fpList = new ArrayList<>(); withinTransaction("getAllForwardProgessEntity DB read failed with exception: ", () -> { - Query fquery = em.createQuery("Select e from ForwardProgressEntity e"); + var fquery = em.createQuery("Select e from ForwardProgressEntity e"); fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList() .forEach(obj -> fpList.add((ForwardProgressEntity) obj)); return null; @@ -773,7 +776,7 @@ public class IntegrityMonitor { } logger.debug("getAllForwardProgressEntity: fpList.size(): {}", fpList.size()); - int index = 0; + var index = 0; for (ForwardProgressEntity fpe : fpList) { logger.debug("getAllForwardProgressEntity: fpList.get({}).getResourceName(): {}", index++, fpe.getResourceName()); @@ -861,7 +864,7 @@ public class IntegrityMonitor { synchronized (dependencyCheckLock) { // Start with the error message empty - StringBuilder errorMsg = new StringBuilder(); + var errorMsg = new StringBuilder(); /* * Before we check dependency groups we need to check subsystemTest. @@ -948,7 +951,7 @@ public class IntegrityMonitor { * @return {@code true} if the dependencies are OK, {@code false} otherwise */ private boolean checkDependencies(StringBuilder errorMsg) { - boolean dependencyOk = true; + var dependencyOk = true; // check state of resources in dependency groups for (String group : depGroups) { @@ -978,12 +981,12 @@ public class IntegrityMonitor { // ignore empty group return false; } - String[] dependencies = group.split(","); + String[] dependencies = COMMA_PAT.split(group); if (logger.isDebugEnabled()) { logger.debug("group dependencies = {}", Arrays.toString(dependencies)); } - int realDepCount = 0; - int failDepCount = 0; + var realDepCount = 0; + var failDepCount = 0; for (String dep : dependencies) { dep = dep.trim(); if (dep.isEmpty()) { @@ -1170,20 +1173,16 @@ public class IntegrityMonitor { * An entity has reported that it is not well. We must not allow the the forward progress counter to * advance. */ - String msg = "allNotWellMap:"; - for (Entry entry : allNotWellMap.entrySet()) { - msg = msg.concat("\nkey = " + entry.getKey() + " msg = " + entry.getValue()); - } + var msg = new StringBuilder("allNotWellMap:"); + buildMapString(msg, allNotWellMap); logger.error("endTransaction: allNotWellMap is NOT EMPTY. Not advancing forward" + "progress counter. \n{}\n", msg); return; } if (logger.isDebugEnabled() && getAllSeemsWellMap() != null && !(getAllSeemsWellMap().isEmpty())) { - String msg = "allSeemsWellMap:"; - for (Entry entry : allSeemsWellMap.entrySet()) { - msg = msg.concat("\nkey = " + entry.getKey() + " msg = " + entry.getValue()); - } + var msg = new StringBuilder("allSeemsWellMap:"); + buildMapString(msg, allSeemsWellMap); logger.debug("endTransaction: allNotWellMap IS EMPTY and allSeemsWellMap is NOT EMPTY. " + "Advancing forward progress counter. \n{}\n", msg); } @@ -1193,6 +1192,15 @@ public class IntegrityMonitor { } } + private void buildMapString(StringBuilder msg, Map map) { + for (Entry entry : map.entrySet()) { + msg.append("\nkey = "); + msg.append(entry.getKey()); + msg.append(" msg = "); + msg.append(entry.getValue()); + } + } + // update FP count in DB with local FP count private void writeFpc() throws IntegrityMonitorException { @@ -1280,7 +1288,7 @@ public class IntegrityMonitor { // site_1.pdp_2 String depGroupsValue = prop.getProperty(IntegrityMonitorProperties.DEPENDENCY_GROUPS); if (!StringUtils.isBlank(depGroupsValue)) { - depGroups = depGroupsValue.split(";"); + depGroups = SEMICOLON_PAT.split(depGroupsValue); if (logger.isDebugEnabled()) { logger.debug("dependency groups property = {}", Arrays.toString(depGroups)); } @@ -1478,7 +1486,7 @@ public class IntegrityMonitor { return; } - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); long timeSinceLastStateAudit = date.getTime() - lastStateAuditTime.getTime(); if (timeSinceLastStateAudit < stateAuditIntervalMs) { logger.debug("IntegrityMonitor.stateAudit(): Not time to run. returning"); @@ -1497,7 +1505,7 @@ public class IntegrityMonitor { */ public void executeStateAudit() { logger.debug("IntegrityMonitor.executeStateAudit(): entry"); - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); // Get all entries in the forwardprogressentity table List fpList = getAllForwardProgressEntity(); @@ -1606,7 +1614,7 @@ public class IntegrityMonitor { private void disableEntity(String dep) { try { // create instance of StateMangement class for dependent - StateManagement depStateManager = new StateManagement(emf, dep); + var depStateManager = new StateManagement(emf, dep); if (!depStateManager.getOpState().equals(StateManagement.DISABLED)) { logger.debug("Forward progress not detected for dependent resource {}. Setting dependent's " + "state to disable failed.", dep); @@ -1742,7 +1750,7 @@ public class IntegrityMonitor { logger.debug("executeRefreshStateAudit(): entry"); synchronized (refreshStateAuditLock) { logger.debug("refreshStateAudit: entry"); - Date now = MonitorTime.getInstance().getDate(); + var now = MonitorTime.getInstance().getDate(); long nowMs = now.getTime(); long lastTimeMs = refreshStateAuditLastRunDate.getTime(); logger.debug("refreshStateAudit: ms since last run = {}", nowMs - lastTimeMs); diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java index feca72f9..9ee095fb 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -155,13 +155,13 @@ public class StateManagement { resourceName); logger.debug("StateManagement: {}() operation started, resourceName = {}", methodName, resourceName); - final EntityManager em = emf.createEntityManager(); + final var em = emf.createEntityManager(); - try (EntityMgrCloser emc = new EntityMgrCloser(em); MyTransaction et = new MyTransaction(em)) { + try (var emc = new EntityMgrCloser(em); MyTransaction et = new MyTransaction(em)) { logger.debug(FIND_MESSAGE, resourceName); - final StateManagementEntity sm = findStateManagementEntity(em, resourceName); + final var sm = findStateManagementEntity(em, resourceName); String changed = updateState.update(sm); em.persist(sm); @@ -197,7 +197,7 @@ public class StateManagement { throws StateManagementException { setState(actionName, resourceName, sm -> { - final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(), + final var stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(), sm.getAvailStatus(), sm.getStandbyStatus(), actionName); sm.setAdminState(stateElement.getEndingAdminState()); @@ -292,7 +292,7 @@ public class StateManagement { AtomicReference newStatus = new AtomicReference<>(); setState(PROMOTE_ACTION, resourceName, sm -> { - final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(), + final var stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(), sm.getAvailStatus(), sm.getStandbyStatus(), PROMOTE_ACTION); sm.setAdminState(stateElement.getEndingAdminState()); @@ -357,8 +357,8 @@ public class StateManagement { logger.debug("StateManagement(6/1/16): {} for resourceName {}", methodName, resourceName); - final EntityManager em = emf.createEntityManager(); - try (final EntityMgrCloser emc = new EntityMgrCloser(em)) { + final var em = emf.createEntityManager(); + try (final var emc = new EntityMgrCloser(em)) { final TypedQuery query = em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class); @@ -369,7 +369,7 @@ public class StateManagement { query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); if (!resourceList.isEmpty()) { // exist - final StateManagementEntity stateManagementEntity = resourceList.get(0); + final var stateManagementEntity = resourceList.get(0); // refresh the object from DB in case cached data was returned em.refresh(stateManagementEntity); function.accept(stateManagementEntity); @@ -458,14 +458,14 @@ public class StateManagement { query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); if (!resourceList.isEmpty()) { // exist - final StateManagementEntity stateManagementEntity = resourceList.get(0); + final var stateManagementEntity = resourceList.get(0); // refresh the object from DB in case cached data was returned em.refresh(stateManagementEntity); stateManagementEntity.setModifiedDate(MonitorTime.getInstance().getDate()); return stateManagementEntity; } else { // not exist - create one - final StateManagementEntity stateManagementEntity = new StateManagementEntity(); + final var stateManagementEntity = new StateManagementEntity(); stateManagementEntity.setResourceName(otherResourceName); stateManagementEntity.setAdminState(UNLOCKED); stateManagementEntity.setOpState(ENABLED); @@ -474,8 +474,7 @@ public class StateManagement { return stateManagementEntity; } } catch (final Exception ex) { - final String message = "findStateManagementEntity exception"; - throw new EntityRetrievalException(message, ex); + throw new EntityRetrievalException("findStateManagementEntity exception", ex); } } @@ -489,9 +488,9 @@ public class StateManagement { /* * Start transaction */ - final EntityManager em = emf.createEntityManager(); + final var em = emf.createEntityManager(); - try (EntityMgrCloser emc = new EntityMgrCloser(em); MyTransaction et = new MyTransaction(em)) { + try (var emc = new EntityMgrCloser(em); MyTransaction et = new MyTransaction(em)) { final TypedQuery stateManagementEntityListQuery = em.createQuery("SELECT p FROM StateManagementEntity p", StateManagementEntity.class); final List stateManagementEntityList = stateManagementEntityListQuery diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java index 3542aa2c..17897d89 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.regex.Pattern; import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +38,8 @@ import org.slf4j.LoggerFactory; * The StateTransition class coordinates all state transitions. */ public class StateTransition { + private static final Pattern COMMA_PAT = Pattern.compile(","); + private static final String DEPENDENCY_FAILED = "dependency.failed"; private static final String ANY_DISABLED_ANY_COLDSTANDBY = "${1},disabled,${3},coldstandby,"; @@ -165,7 +168,7 @@ public class StateTransition { } - StateElement stateElement = new StateElement(); + var stateElement = new StateElement(); // dependency,failed is stored as dependency.failed in StateTable String availStatus2 = availStatus; @@ -177,7 +180,7 @@ public class StateTransition { String value = STATE_TABLE.get(key); if (value != null) { - String[] parts = value.split(",", 5); + String[] parts = COMMA_PAT.split(value, 5); stateElement.setEndingAdminState(parts[0].trim()); stateElement.setEndingOpState(parts[1].trim()); stateElement.setEndingAvailStatus(parts[2].trim().replace(".", ",")); diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java index cf7a968f..8cbdaa7d 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2018, 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018, 2020-2021 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. @@ -87,13 +87,13 @@ public class ComponentAdmin implements ComponentAdminMBean { try { logger.debug("Registering {} MBean", name); - MBeanServer mbeanServer = findMBeanServer(); + var mbeanServer = findMBeanServer(); if (mbeanServer == null) { return; } - ObjectName objectName = new ObjectName(name); + var objectName = new ObjectName(name); if (mbeanServer.isRegistered(objectName)) { logger.debug("Unregistering a previously registered {} MBean", name); diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ForwardProgressEntity.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ForwardProgressEntity.java index 88159311..2f9544e9 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ForwardProgressEntity.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ForwardProgressEntity.java @@ -76,7 +76,7 @@ public class ForwardProgressEntity implements Serializable { */ @PrePersist public void prePersist() { - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); this.createdDate = date; this.lastUpdated = date; this.fpcCount = 0; diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ImTestEntity.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ImTestEntity.java index 14dce068..858a3f77 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ImTestEntity.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ImTestEntity.java @@ -73,7 +73,7 @@ public class ImTestEntity implements Serializable { */ @PrePersist public void prePersist() { - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); this.createdDate = date; this.modifiedDate = date; } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ResourceRegistrationEntity.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ResourceRegistrationEntity.java index 20237afc..0e654d36 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ResourceRegistrationEntity.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/ResourceRegistrationEntity.java @@ -82,7 +82,7 @@ public class ResourceRegistrationEntity implements Serializable { */ @PrePersist public void prePersist() { - Date date = MonitorTime.getInstance().getDate(); + var date = MonitorTime.getInstance().getDate(); this.createdDate = date; this.lastUpdated = date; } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/StateManagementEntity.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/StateManagementEntity.java index 30677814..b13d3a76 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/StateManagementEntity.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jpa/StateManagementEntity.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -103,7 +103,7 @@ public class StateManagementEntity implements Serializable { * @return a new StateManagementEntity */ public static StateManagementEntity clone(StateManagementEntity sm) { - StateManagementEntity newStateManagementEntity = new StateManagementEntity(); + var newStateManagementEntity = new StateManagementEntity(); newStateManagementEntity.setResourceName(sm.getResourceName()); newStateManagementEntity.setAdminState(sm.getResourceName()); newStateManagementEntity.setOpState(sm.getOpState()); -- cgit 1.2.3-korg