From 329da0d034a4645685ac0fe7f8e61d24e435d5dc Mon Sep 17 00:00:00 2001 From: rameshiyer27 Date: Wed, 19 Aug 2020 10:22:20 +0100 Subject: Picking workflow names dynamically for schema update procedure Issue-ID: SO-3147 Signed-off-by: zrrmmua Change-Id: I4c9de93ea7b51657c2bc6ee24b4bb52e2068cf5a --- .../level/AbstractServiceLevelPreparable.java | 72 ---------------- .../service/level/ServiceLevelPreparable.java | 98 ++++++++++++++++++++++ .../service/level/impl/ServiceLevelConstants.java | 48 +++++++++-- .../level/impl/ServiceLevelPreparation.java | 55 ++++++------ .../service/level/impl/ServiceLevelUpgrade.java | 69 ++++++++------- 5 files changed, 207 insertions(+), 135 deletions(-) delete mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java (limited to 'bpmn/so-bpmn-tasks/src/main') diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java deleted file mode 100644 index 0671354bd6..0000000000 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * 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. - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.so.bpmn.infrastructure.service.level; - -import org.camunda.bpm.engine.delegate.DelegateExecution; -import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants; -import org.onap.so.client.exception.ExceptionBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import java.util.ArrayList; -import java.util.List; - -/** - * Abstract class for Service level upgrade Execution, it should be extended for service level upgrade tasks. - */ -public abstract class AbstractServiceLevelPreparable { - - protected static final Logger LOG = LoggerFactory.getLogger(AbstractServiceLevelPreparable.class); - - @Autowired - protected ExceptionBuilder exceptionBuilder; - - /** - * This method fetches workflow names to be invoked based on the controller scope . - * - * @param scope Controller scope - * @return String value of Workflow name - */ - protected abstract String fetchWorkflowUsingScope(DelegateExecution execution, final String scope); - - /** - * This method validates the execution parameters to be passed for health check workflow. - * - * @param execution Delegate execution obj - * @param scope Controller scope * Throws workflow exception if validation fails - */ - protected void validateParamsWithScope(DelegateExecution execution, final String scope, List params) { - List invalidVariables = new ArrayList<>(); - for (String param : params) { - if (!execution.hasVariable(param) || execution.getVariable(param) == null - || String.valueOf(execution.getVariable(param)).isEmpty()) { - invalidVariables.add(param); - } - } - if (invalidVariables.size() > 0) { - LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables); - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Validation of health check workflow parameters failed for the scope: " + scope); - } - - } - -} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java new file mode 100644 index 0000000000..e26195158d --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * 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. + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.service.level; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.db.catalog.beans.Workflow; +import org.onap.so.db.catalog.client.CatalogDbClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Parent class for Service level upgrade Execution, it should be extended for service level upgrade tasks. + */ +public class ServiceLevelPreparable { + + protected static final Logger LOG = LoggerFactory.getLogger(ServiceLevelPreparable.class); + + @Autowired + protected ExceptionBuilder exceptionBuilder; + + @Autowired + protected CatalogDbClient catalogDbClient; + + /** + * Fetches workflow names based on the controller scope and operation name. + * + * @param scope Controller scope + * @param operationName healthcheck/softwareUpgrade + * @return String value of Workflow name + */ + protected String fetchWorkflowUsingScope(final String scope, String operationName) { + Optional wflName = Optional.empty(); + try { + List workflows = catalogDbClient.findWorkflowByOperationName(operationName); + if (!workflows.isEmpty()) { + wflName = Optional.ofNullable( + workflows.stream().filter(workflow -> workflow.getResourceTarget().equalsIgnoreCase(scope)) + .findFirst().get().getName()); + } + } catch (Exception e) { + // do nothing and assign the default workflow in finally + LOG.error("Error occurred while fetching workflow name from CatalogDb {}", e); + } finally { + if (wflName.isEmpty()) { + wflName = Optional.of(ServiceLevelConstants.WORKFLOW_OPERATIONS_MAP.get(operationName).get(scope)); + } + } + return wflName.get(); + + } + + /** + * This method validates the execution parameters to be passed for health check workflow. + * + * @param execution Delegate execution obj + * @param scope Controller scope * Throws workflow exception if validation fails + */ + protected void validateParamsWithScope(DelegateExecution execution, final String scope, List params) { + List invalidVariables = new ArrayList<>(); + for (String param : params) { + if (!execution.hasVariable(param) || execution.getVariable(param) == null + || String.valueOf(execution.getVariable(param)).isEmpty()) { + invalidVariables.add(param); + } + } + if (invalidVariables.size() > 0) { + LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables); + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Validation of health check workflow parameters failed for the scope: " + scope); + } + + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java index 34ebb308c3..d94641846d 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java @@ -1,5 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * 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. + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + package org.onap.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + public class ServiceLevelConstants { public static final String BPMN_REQUEST = "bpmnRequest"; public static final String RESOURCE_TYPE = "resourceType"; @@ -8,13 +32,27 @@ public class ServiceLevelConstants { public static final String PNF = "pnf"; public static final String VNF = "vnf"; public static final String EMPTY_STRING = ""; - public static final String WORKFLOW_TO_INVOKE = "healthCheckWorkflow"; + public static final String HEALTH_CHECK_WORKFLOW_TO_INVOKE = "healthCheckWorkflow"; public static final String SOFTWARE_WORKFLOW_TO_INVOKE = "softwareUpgradeWorkflow"; - public static final String GENERIC_PNF_HEALTH_CHECK_WORKFLOW = "GenericPnfHealthCheck"; - public static final String PNF_SOFTWARE_UPGRADE_WORKFLOW = "PNFSoftwareUpgrade"; + public static final String HEALTH_CHECK_OPERATION = "ResourceHealthCheck"; + public static final String SW_UP_OPERATION = "ResourceSoftwareUpgrade"; public static final String CONTROLLER_STATUS = "ControllerStatus"; public static final int ERROR_CODE = 601; - // TODO This value needs to be updated once vnf health check workflow is available - protected static final String GENERIC_VNF_HEALTH_CHECK_WORKFLOW = "GenericVNFHealthCheck"; + + // TODO GenericVNFHealthCheck and GenericVnfSoftwareUpgrade workflow names should be updated once the workflow is + // implemented. + public static final Map DEFAULT_HEALTH_CHECK_WORKFLOWS = + Map.of(PNF, "GenericPnfHealthCheck", VNF, "GenericVNFHealthCheck"); + + public static final Map DEFAULT_SOFTWARE_UP_WORKFLOWS = + Map.of(PNF, "PNFSoftwareUpgrade", VNF, "GenericVnfSoftwareUpgrade"); + + // Maps operation name with workflows + public static final Map> WORKFLOW_OPERATIONS_MAP = Map.of(HEALTH_CHECK_OPERATION, + DEFAULT_HEALTH_CHECK_WORKFLOWS, SW_UP_OPERATION, DEFAULT_SOFTWARE_UP_WORKFLOWS); + + public static final List VALID_CONTROLLER_SCOPE = Arrays.asList(PNF, VNF); + + } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java index 5b772760f0..59884ecbc2 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java @@ -20,59 +20,54 @@ package org.onap.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.so.bpmn.infrastructure.service.level.AbstractServiceLevelPreparable; +import org.onap.so.bpmn.infrastructure.service.level.ServiceLevelPreparable; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.List; + /** * Fetches health check workflow based on the controller_scope. Invoke the corresponding health check workflow after * validation. */ @Component("ServiceLevelPreparation") -public class ServiceLevelPreparation extends AbstractServiceLevelPreparable implements JavaDelegate { +public class ServiceLevelPreparation extends ServiceLevelPreparable implements JavaDelegate { - private static final List PNF_HC_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, + private static final List PNF_HEALTH_CHECK_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME); + // TODO Update the list with vnf health check parameters if any validation needed + private static final List VNF_HEALTH_CHECK_PARAMS = Collections.emptyList(); + + private static final Map> HEALTH_CHECK_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF, + PNF_HEALTH_CHECK_PARAMS, ServiceLevelConstants.VNF, VNF_HEALTH_CHECK_PARAMS); + @Override public void execute(DelegateExecution execution) throws Exception { if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE) && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) { final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE); LOG.debug("Scope retrieved from delegate execution: " + controllerScope); - final String wflName = fetchWorkflowUsingScope(execution, controllerScope); - LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); - - if (ServiceLevelConstants.PNF.equalsIgnoreCase(controllerScope)) { - validateParamsWithScope(execution, controllerScope, PNF_HC_PARAMS); + if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) { + final String wflName = + fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.HEALTH_CHECK_OPERATION); + LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); + validateParamsWithScope(execution, controllerScope, HEALTH_CHECK_PARAMS_MAP.get(controllerScope)); LOG.info("Parameters validated successfully for {}", wflName); + execution.setVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE, wflName); + execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING); + } else { + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Invalid Controller scope to prepare resource level health check"); } - execution.setVariable(ServiceLevelConstants.WORKFLOW_TO_INVOKE, wflName); - execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING); } else { exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Controller scope not found to invoke resource level health check"); - } - } - - @Override - public String fetchWorkflowUsingScope(DelegateExecution execution, final String scope) { - String wflName = null; - switch (scope.toLowerCase()) { - case ServiceLevelConstants.PNF: - wflName = ServiceLevelConstants.GENERIC_PNF_HEALTH_CHECK_WORKFLOW; - break; - case ServiceLevelConstants.VNF: - wflName = ServiceLevelConstants.GENERIC_VNF_HEALTH_CHECK_WORKFLOW; - break; - default: - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "No valid health check work flow retrieved for the scope: " + scope); + "Resource type not found in the execution to invoke resource level health check"); } - return wflName; } } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java index dfc77d6f0c..9d7d8efb65 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java @@ -1,34 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * 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. + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + package org.onap.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.so.bpmn.infrastructure.service.level.AbstractServiceLevelPreparable; +import org.onap.so.bpmn.infrastructure.service.level.ServiceLevelPreparable; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.List; @Component -public class ServiceLevelUpgrade extends AbstractServiceLevelPreparable implements JavaDelegate { +public class ServiceLevelUpgrade extends ServiceLevelPreparable implements JavaDelegate { - private static final List PNF_SWU_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, + private static final List PNF_SOFTWARE_UP_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME); - @Override - protected String fetchWorkflowUsingScope(DelegateExecution execution, String scope) { - String wflName = null; - switch (scope.toLowerCase()) { - case ServiceLevelConstants.PNF: - wflName = ServiceLevelConstants.PNF_SOFTWARE_UPGRADE_WORKFLOW; - break; - case ServiceLevelConstants.VNF: - wflName = ServiceLevelConstants.GENERIC_VNF_HEALTH_CHECK_WORKFLOW; - break; - default: - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "No valid health check work flow retrieved for the scope: " + scope); - } - return wflName; - } + // TODO Update the list with vnf software upgrade parameters if any validation needed + private static final List VNF_SOFTWARE_UP_PARAMS = Collections.emptyList(); + + private static final Map> SOFTWARE_UP_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF, + PNF_SOFTWARE_UP_PARAMS, ServiceLevelConstants.VNF, VNF_SOFTWARE_UP_PARAMS); + @Override public void execute(DelegateExecution execution) throws Exception { @@ -36,19 +48,20 @@ public class ServiceLevelUpgrade extends AbstractServiceLevelPreparable implemen && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) { final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE); LOG.debug("Scope retrieved from delegate execution: " + controllerScope); - final String wflName = fetchWorkflowUsingScope(execution, controllerScope); - LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); - - if ("pnf".equalsIgnoreCase(controllerScope)) { - validateParamsWithScope(execution, controllerScope, PNF_SWU_PARAMS); + if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) { + final String wflName = fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.SW_UP_OPERATION); + LOG.debug("Software Upgrade workflow fetched for the scope: {} is: {}", controllerScope, wflName); + validateParamsWithScope(execution, controllerScope, SOFTWARE_UP_PARAMS_MAP.get(controllerScope)); LOG.info("Parameters validated successfully for {}", wflName); execution.setVariable(ServiceLevelConstants.SOFTWARE_WORKFLOW_TO_INVOKE, wflName); execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ""); + } else { + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Invalid Controller scope for resource level software upgrade"); } - } else { exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Controller scope not found to invoke resource level health check"); + "Resource type not found in the execution to invoke resource level software upgrade"); } } } -- cgit 1.2.3-korg