From fe5d78724f723a451ddc0d7cc41d6fc60092b314 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 13 Jun 2019 12:34:17 -0400 Subject: More sonar fixes in policy/common Note: this does not increase code coverage, but should fix other code issues. Resolved cyclomatic complexity issue in ParameterValidationResult. Refactored duplicate code in GroupValidationResult. Removed IOException from NetworkUtil "throws". Replaced null/empty string tests with StringUtils.isBlank(). Added @FunctionalInterface where needed. Replaced anonymous classes with lambda expressions. Replaced duplicate strings with a constant. Added private constructors for utility classes. Removed sleep() from tests. Removed unused parameter from method call. Made some protected methods private. Compute integrity monitor's state-transition table once. Use for-loop instead of iterator. Moved constructors. Fixed some checkstyle issues (tabs => spaces, trailing spaces). Change-Id: I9a962ca45c4ff3f212c6014da799d06f07b232ef Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../org/onap/policy/common/im/StateTransition.java | 152 ++++++++++----------- .../policy/common/im/jmx/JmxAgentConnection.java | 26 ++-- .../common/im/jpa/StateManagementEntity.java | 16 +-- 3 files changed, 87 insertions(+), 107 deletions(-) (limited to 'integrity-monitor/src/main/java') 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 a921b3ea..79eeee49 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,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -20,11 +20,13 @@ package org.onap.policy.common.im; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; +import java.util.HashSet; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +41,7 @@ public class StateTransition { public static final String AVAILABLE_STATUS = "availStatus"; public static final String STANDBY_STATUS = "standbyStatus"; public static final String ACTOIN_NAME = "actionName"; - + /* * Common strings. */ @@ -49,33 +51,39 @@ public class StateTransition { private static final String STANDBY_STRING = "], standbyStatus=["; private static final String ACTION_STRING = "], actionName=["; - private HashMap stateTable = new HashMap<>(); + /* + * Valid values for each type. + */ + private static final Set VALID_ADMIN_STATE = Collections + .unmodifiableSet(new HashSet<>(Arrays.asList(StateManagement.LOCKED, StateManagement.UNLOCKED))); + + private static final Set VALID_OP_STATE = Collections + .unmodifiableSet(new HashSet<>(Arrays.asList(StateManagement.ENABLED, StateManagement.DISABLED))); + + private static final Set VALID_STANDBY_STATUS = Collections.unmodifiableSet( + new HashSet<>(Arrays.asList(StateManagement.NULL_VALUE, StateManagement.COLD_STANDBY, + StateManagement.HOT_STANDBY, StateManagement.PROVIDING_SERVICE))); + + private static final Set VALID_AVAIL_STATUS = Collections + .unmodifiableSet(new HashSet<>(Arrays.asList(StateManagement.NULL_VALUE, StateManagement.DEPENDENCY, + StateManagement.DEPENDENCY_FAILED, StateManagement.FAILED))); + + private static final Set VALID_ACTION_NAME = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList(StateManagement.DEMOTE, + StateManagement.DISABLE_DEPENDENCY, StateManagement.DISABLE_FAILED, + StateManagement.ENABLE_NO_DEPENDENCY, StateManagement.ENABLE_NOT_FAILED, + StateManagement.LOCK, StateManagement.PROMOTE, StateManagement.UNLOCK))); + /** - * StateTransition constructor. - * - * @throws StateTransitionException if an error occurs + * State-transition table. */ - public StateTransition() throws StateTransitionException { - if (logger.isDebugEnabled()) { - logger.debug("StateTransition constructor"); - } - - try { - if (logger.isDebugEnabled()) { - logger.debug("Load StateTable started"); - } + private static final Map STATE_TABLE = Collections.unmodifiableMap(makeStateTable()); - setupStateTable(); - } catch (Exception ex) { - logger.error("StateTransition threw exception.", ex); - throw new StateTransitionException("StateTransition Exception: " + ex.toString()); - } - } /** * Calculates the state transition and returns the end state. - * + * * @param adminState the administration state * @param opState the operational state * @param availStatus the availability status @@ -86,54 +94,30 @@ public class StateTransition { */ public StateElement getEndingState(String adminState, String opState, String availStatus, String standbyStatus, String actionName) throws StateTransitionException { - if (logger.isDebugEnabled()) { - logger.debug("getEndingState"); - } - if (logger.isDebugEnabled()) { - logger.debug("adminState=[{}], opState=[{}], availStatus=[{}], standbyStatus=[{}], actionName[{}]", - adminState, opState, availStatus, standbyStatus, actionName); - } + logger.debug("getEndingState"); + logger.debug("adminState=[{}], opState=[{}], availStatus=[{}], standbyStatus=[{}], actionName[{}]", + adminState, opState, availStatus, standbyStatus, actionName); if (availStatus == null) { - availStatus = "null"; + availStatus = StateManagement.NULL_VALUE; } if (standbyStatus == null) { - standbyStatus = "null"; + standbyStatus = StateManagement.NULL_VALUE; } - if (adminState == null || opState == null || actionName == null) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); - } else if (!(adminState.equals(StateManagement.LOCKED) || adminState.equals(StateManagement.UNLOCKED))) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); - } else if (!(opState.equals(StateManagement.ENABLED) || opState.equals(StateManagement.DISABLED))) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); - } else if (!(standbyStatus.equals(StateManagement.NULL_VALUE) - || standbyStatus.equals(StateManagement.COLD_STANDBY) - || standbyStatus.equals(StateManagement.HOT_STANDBY) - || standbyStatus.equals(StateManagement.PROVIDING_SERVICE))) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); - } else if (!(availStatus.equals(StateManagement.NULL_VALUE) || availStatus.equals(StateManagement.DEPENDENCY) - || availStatus.equals(StateManagement.DEPENDENCY_FAILED) - || availStatus.equals(StateManagement.FAILED))) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); - } else if (!(actionName.equals(StateManagement.DEMOTE) || actionName.equals(StateManagement.DISABLE_DEPENDENCY) - || actionName.equals(StateManagement.DISABLE_FAILED) - || actionName.equals(StateManagement.ENABLE_NO_DEPENDENCY) - || actionName.equals(StateManagement.ENABLE_NOT_FAILED) || actionName.equals(StateManagement.LOCK) - || actionName.equals(StateManagement.PROMOTE) || actionName.equals(StateManagement.UNLOCK))) { - throw new StateTransitionException(EXCEPTION_STRING - + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + STANDBY_STRING - + standbyStatus + ACTION_STRING + actionName + "]"); + + if (!VALID_ADMIN_STATE.contains(adminState) || !VALID_OP_STATE.contains(opState) + || !VALID_STANDBY_STATUS.contains(standbyStatus)) { + throw new StateTransitionException( + EXCEPTION_STRING + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + + STANDBY_STRING + standbyStatus + ACTION_STRING + actionName + "]"); } + if (!VALID_AVAIL_STATUS.contains(availStatus) || !VALID_ACTION_NAME.contains(actionName)) { + throw new StateTransitionException( + EXCEPTION_STRING + adminState + OPSTATE_STRING + opState + AVAILSTATUS_STRING + availStatus + + STANDBY_STRING + standbyStatus + ACTION_STRING + actionName + "]"); + } + + StateElement stateElement = new StateElement(); try { // dependency,failed is stored as dependency.failed in StateTable @@ -145,7 +129,7 @@ public class StateTransition { if (logger.isDebugEnabled()) { logger.debug("Ending State search key: {}", key); } - String value = stateTable.get(key); + String value = STATE_TABLE.get(key); if (value != null) { try { @@ -189,7 +173,7 @@ public class StateTransition { * endingAdminState,endingOpState,endingAvailStatus,endingStandbyStatus,exception Note : Use * period instead of comma as seperator when store multi-value endingStandbyStatus (convert to * comma during retrieval) - * + * *

Note on illegal state/status combinations: This table has many state/status combinations * that should never occur. However, they *may* occur due to corruption or manual manipulation * of the DB. So, in each case of an illegal combination, the state/status is first corrected @@ -199,18 +183,18 @@ public class StateTransition { * availability status is left null until a disabledfailed or disableddependency action is * received. Or, if a enableNotFailed or enableNoDependency is received while the availability * status is null, it will remain null, but the Operational state will change to enabled. - * + * *

If the standby status is not in agreement with the administrative and/or operational * states, it is brought into agreement. For example, if the administrative state is locked and * the standby status is providingservice, the standby status is changed to coldstandby. - * + * *

After bringing the states/status attributes into agreement, *then* the action is applied * to them. For example, if the administrative state is locked, the operational state is * enabled, the availability status is null, the standby status is providingservice and the * action is unlock, the standby status is changed to coldstandby and then the unlock action is * applied. This will change the final state/status to administrative state = unlocked, * operational state = disabled, availability status = null and standby status = hotstandby. - * + * *

Note on standby status: If the starting state of standby status is null and either a * promote or demote action is made, the assumption is that standbystatus is supported and * therefore, the standby status will be changed to providingservice, hotstandby or coldstandby @@ -221,9 +205,13 @@ public class StateTransition { * operational state such that they are unlocked and enabled, the standby status is * automatically transitioned to hotstandby since it is only those two states that can hold the * statndby status in the coldstandby value. + * + * @return a new state-transaction table */ - private void setupStateTable() { + private static Map makeStateTable() { + Map stateTable = new HashMap<>(); + stateTable.put("unlocked,enabled,null,null,lock", "locked,enabled,null,null,"); stateTable.put("unlocked,enabled,null,null,unlock", "unlocked,enabled,null,null,"); stateTable.put("unlocked,enabled,null,null,disableFailed", "unlocked,disabled,failed,null,"); @@ -934,21 +922,21 @@ public class StateTransition { "locked,disabled,dependency.failed,coldstandby,StandbyStatusException"); stateTable.put("locked,disabled,dependency.failed,providingservice,demote", "locked,disabled,dependency.failed,coldstandby,"); + + return stateTable; } /** * Display the state table. */ public void displayStateTable() { - Set set = stateTable.entrySet(); - Iterator iter = set.iterator(); + if (!logger.isDebugEnabled()) { + return; + } - while (iter.hasNext()) { - Map.Entry me = (Map.Entry) iter.next(); - String key = (String) me.getKey() + ((String) me.getValue()).replace(".", ","); - if (logger.isDebugEnabled()) { - logger.debug("{}", key); - } + for (Entry me : STATE_TABLE.entrySet()) { + String key = me.getKey() + me.getValue().replace(".", ","); + logger.debug("{}", key); } } } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java index b71751f0..a2d8c091 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -23,15 +23,11 @@ package org.onap.policy.common.im.jmx; import java.io.IOException; import java.util.HashMap; import java.util.Map; - import javax.management.MBeanServerConnection; -import javax.management.Notification; -import javax.management.NotificationListener; import javax.management.remote.JMXConnectionNotification; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; - import org.onap.policy.common.im.IntegrityMonitorException; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -66,7 +62,7 @@ public final class JmxAgentConnection { /** * Generate jmxAgent url. service:jmx:rmi:///jndi/rmi://host.domain:9999/jmxAgent - * + * * @param host host.domain * @param port 9999 * @return jmxAgent url. @@ -78,7 +74,7 @@ public final class JmxAgentConnection { /** * Get a connection to the jmxAgent MBeanServer. - * + * * @return the connection * @throws IntegrityMonitorException on error */ @@ -95,14 +91,10 @@ public final class JmxAgentConnection { connector = JMXConnectorFactory.newJMXConnector(url, env); connector.connect(); - connector.addConnectionNotificationListener(new NotificationListener() { - - @Override - public void handleNotification(Notification notification, Object handback) { - if (notification.getType().equals(JMXConnectionNotification.FAILED)) { - // handle disconnect - disconnect(); - } + connector.addConnectionNotificationListener((notification, handback) -> { + if (notification.getType().equals(JMXConnectionNotification.FAILED)) { + // handle disconnect + disconnect(); } }, null, null); 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 78b90abd..79f843e4 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,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -73,6 +73,10 @@ public class StateManagementEntity implements Serializable { @Column(name = "modifiedDate", nullable = false) private Date modifiedDate; + public StateManagementEntity() { + // default constructor + } + @PrePersist public void prePersist() { this.createdDate = MonitorTime.getInstance().getDate(); @@ -84,10 +88,6 @@ public class StateManagementEntity implements Serializable { this.modifiedDate = MonitorTime.getInstance().getDate(); } - public StateManagementEntity() { - // default constructor - } - public String getResourceName() { return this.resourceName; } @@ -135,7 +135,7 @@ public class StateManagementEntity implements Serializable { /** * Clone a StateManagementEntity. - * + * * @param sm the StateManagementEntity to clone * @return a new StateManagementEntity */ -- cgit 1.2.3-korg