From f17e395186c8d6866977ff3210f62b79f4f61e37 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 8 Nov 2019 08:54:40 -0500 Subject: Fix more sonar issues in drools-applications Addressed the following sonar issues: - unused imports - unused method parameters - superfluous "throws" declaration - fields within a serializable class must also be serializable; this was/will be fixed with a change to AaiCqResponse in policy-models - use logger instead of System.out; turns out that the code that used System.out is no longer needed. In fact, deleted several classes that are no longer needed: ControlLoopLogger and ControlLoopPublisher - cyclomatic complexity and switch/case statements too big; used eclipse to extract chunks of code into separate methods - duplicate code Note: extracted common code and used lambdas to eliminate duplicate code in PolicyGuardYamlToXacml. However, a better approach would be to use object-oriented programming, using mini/nested objects to do the generation. The lambdas would then become abstract methods. Nevertheless, that would entail significantly more re-write of this class than desired at this time Issue-ID: POLICY-2225 Change-Id: Ie503ffd7accbad3e410af602d32b29c0095c3a33 Signed-off-by: Jim Hahn --- .../onap/policy/controlloop/ControlLoopLogger.java | 56 --------------------- .../policy/controlloop/ControlLoopPublisher.java | 53 -------------------- .../eventmanager/ControlLoopEventManager.java | 58 ++++++++++++++-------- .../eventmanager/ControlLoopOperationManager.java | 51 ++++++++++--------- .../impl/ControlLoopLoggerStdOutImpl.java | 46 ----------------- .../impl/ControlLoopPublisherJUnitImpl.java | 31 ------------ 6 files changed, 65 insertions(+), 230 deletions(-) delete mode 100644 controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopLogger.java delete mode 100644 controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopPublisher.java delete mode 100644 controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopLoggerStdOutImpl.java delete mode 100644 controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopPublisherJUnitImpl.java (limited to 'controlloop/common/eventmanager/src/main') diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopLogger.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopLogger.java deleted file mode 100644 index 4bf500704..000000000 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopLogger.java +++ /dev/null @@ -1,56 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * controlloop - * ================================================================================ - * Copyright (C) 2017-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.policy.controlloop; - -import java.lang.reflect.Constructor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public interface ControlLoopLogger { - - public void info(String... parameters); - - public void metrics(String... msgs); - - public void metrics(Object obj); - - public static class Factory { - private static final Logger logger = LoggerFactory.getLogger(Factory.class); - - /** - * Construct an instance. - * - * @param className name of the class - * @return the instance - * @throws ControlLoopException if an error occurs - */ - public ControlLoopLogger buildLogger(String className) throws ControlLoopException { - try { - Constructor constr = Class.forName(className).getConstructor(); - return (ControlLoopLogger) constr.newInstance(); - } catch (Exception e) { - logger.error("buildLogger threw: ", e); - throw new ControlLoopException("Cannot load class " + className + " as a control loop logger"); - } - } - } -} diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopPublisher.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopPublisher.java deleted file mode 100644 index 086e1173d..000000000 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ControlLoopPublisher.java +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * controlloop - * ================================================================================ - * Copyright (C) 2017-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.policy.controlloop; - -import java.lang.reflect.Constructor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@FunctionalInterface -public interface ControlLoopPublisher { - - public void publish(Object object); - - public static class Factory { - private static final Logger logger = LoggerFactory.getLogger(Factory.class); - - /** - * Construct an instance. - * - * @param className name of the class - * @return the instance - * @throws ControlLoopException if an error occurs - */ - public ControlLoopPublisher buildLogger(String className) throws ControlLoopException { - try { - Constructor constr = Class.forName(className).getConstructor(); - return (ControlLoopPublisher) constr.newInstance(); - } catch (Exception e) { - logger.error("ControlLoopPublisher.buildLogger threw: ", e); - throw new ControlLoopException("Cannot load class " + className + " as a control loop publisher"); - } - } - } -} diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java index c01bec443..8b06a9681 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java @@ -746,22 +746,30 @@ public class ControlLoopEventManager implements Serializable { switch (event.getTargetType()) { case VM: case VNF: - if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null - && eventAai.get(GENERIC_VNF_VNF_NAME) == null) { - throw new ControlLoopException( - "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing"); - } + validateAaiVmVnfData(eventAai); return; case PNF: - if (eventAai.get(PNF_NAME) == null) { - throw new ControlLoopException("AAI PNF object key pnf-name is missing"); - } + validateAaiPnfData(eventAai); return; default: throw new ControlLoopException("The target type is not supported"); } } + private void validateAaiVmVnfData(Map eventAai) throws ControlLoopException { + if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null + && eventAai.get(GENERIC_VNF_VNF_NAME) == null) { + throw new ControlLoopException( + "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing"); + } + } + + private void validateAaiPnfData(Map eventAai) throws ControlLoopException { + if (eventAai.get(PNF_NAME) == null) { + throw new ControlLoopException("AAI PNF object key pnf-name is missing"); + } + } + /** * Query A&AI for an event. * @@ -772,19 +780,7 @@ public class ControlLoopEventManager implements Serializable { Map aai = event.getAai(); - if (aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED) - || aai.containsKey(PNF_IS_IN_MAINT)) { - - if (isClosedLoopDisabled(event)) { - throw new AaiException( - "is-closed-loop-disabled is set to true on VServer or VNF or in-maint is set to true for PNF"); - } - - if (isProvStatusInactive(event)) { - throw new AaiException("prov-status is not ACTIVE on VServer or VNF or PNF"); - } - - // no need to query, as we already have the data + if (alreadyHaveData(event, aai)) { return; } @@ -810,6 +806,26 @@ public class ControlLoopEventManager implements Serializable { } } + private boolean alreadyHaveData(VirtualControlLoopEvent event, Map aai) throws AaiException { + if (aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED) + || aai.containsKey(PNF_IS_IN_MAINT)) { + + if (isClosedLoopDisabled(event)) { + throw new AaiException( + "is-closed-loop-disabled is set to true on VServer or VNF or in-maint is set to true for PNF"); + } + + if (isProvStatusInactive(event)) { + throw new AaiException("prov-status is not ACTIVE on VServer or VNF or PNF"); + } + + // no need to query, as we already have the data + return true; + } + + return false; + } + /** * Process a response from A&AI for a VNF. * diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java index 570274149..9353ac2d6 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java @@ -136,26 +136,7 @@ public class ControlLoopOperationManager implements Serializable { this.targetEntity = getTarget(policy); - // - // Let's make a sanity check - // - switch (policy.getActor()) { - case "APPC": - initAppc(onset, policy); - break; - case "SO": - break; - case "SDNR": - break; - case "VFC": - break; - case "SDNC": - break; - case "CDS": - break; - default: - throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor."); - } + initActor(policy); } catch (AaiException e) { throw new ControlLoopException(e.getMessage(), e); @@ -163,7 +144,31 @@ public class ControlLoopOperationManager implements Serializable { } - private void initAppc(ControlLoopEvent onset, Policy policy) throws AaiException { + private void initActor(Policy policy) throws AaiException, ControlLoopException { + // + // Let's make a sanity check + // + switch (policy.getActor()) { + case "APPC": + initAppc(policy); + break; + case "SO": + break; + case "SDNR": + break; + case "VFC": + break; + case "SDNC": + break; + case "CDS": + break; + default: + throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor."); + } + } + + + private void initAppc(Policy policy) throws AaiException { if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) { /* * The target vnf-id may not be the same as the source vnf-id specified in the yaml, the target @@ -383,7 +388,7 @@ public class ControlLoopOperationManager implements Serializable { } - private Object startSoOperation(ControlLoopEvent onset, Operation operation) throws AaiException { + private Object startSoOperation(ControlLoopEvent onset, Operation operation) { SoActorServiceProvider soActorSp = new SoActorServiceProvider(); if (Boolean.valueOf(PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_CUSTOM_QUERY))) { this.operationRequest = @@ -405,7 +410,7 @@ public class ControlLoopOperationManager implements Serializable { } - private Object startVfcOperation(ControlLoopEvent onset, Operation operation) throws AaiException { + private Object startVfcOperation(ControlLoopEvent onset, Operation operation) { if (Boolean.valueOf(PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_CUSTOM_QUERY))) { this.operationRequest = VfcActorServiceProvider.constructRequestCq((VirtualControlLoopEvent) onset, operation.clOperation, this.policy, this.aaiCqResponse); diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopLoggerStdOutImpl.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopLoggerStdOutImpl.java deleted file mode 100644 index fbff87fdd..000000000 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopLoggerStdOutImpl.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * controlloop - * ================================================================================ - * Copyright (C) 2017-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.policy.controlloop.impl; - -import org.onap.policy.controlloop.ControlLoopLogger; - -public class ControlLoopLoggerStdOutImpl implements ControlLoopLogger { - @Override - public void info(String... parameters) { - StringBuilder builder = new StringBuilder(); - for (String param : parameters) { - builder.append(param); - builder.append(" "); - } - System.out.println(builder.toString().trim()); - } - - @Override - public void metrics(String... msgs) { - this.info(msgs); - } - - @Override - public void metrics(Object obj) { - this.info(obj.toString()); - } -} diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopPublisherJUnitImpl.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopPublisherJUnitImpl.java deleted file mode 100644 index 067663700..000000000 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/impl/ControlLoopPublisherJUnitImpl.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * controlloop - * ================================================================================ - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop.impl; - -import org.onap.policy.controlloop.ControlLoopPublisher; - -public class ControlLoopPublisherJUnitImpl implements ControlLoopPublisher { - @Override - public void publish(Object object) { - throw new UnsupportedOperationException( - "publish() method is not implemented on " + this.getClass().getName()); - } -} -- cgit 1.2.3-korg