From cd4e5b408603a13dabc58f832e4aae4194e81995 Mon Sep 17 00:00:00 2001 From: "Boslet, Cory" Date: Wed, 25 Mar 2020 16:04:41 -0400 Subject: convert openstack to external tasks Use external tasks to communicate with openstack adapter Will be more resilient to failures Refactored all flows to use external tasks not REST calls Rollback is now an external task Ensured backwards compatibility Issue-ID: SO-2767 Signed-off-by: Benjamin, Max (mb388a) Change-Id: I8afdb89c688d756aed0f25f7d9a7fb898142554d --- .../org/onap/so/adapters/audit/AbstractAudit.java | 39 --- .../so/adapters/audit/AbstractAuditService.java | 88 ------ .../so/adapters/audit/AuditCreateStackService.java | 111 ------- .../onap/so/adapters/audit/AuditDataService.java | 77 ----- .../so/adapters/audit/AuditDeleteStackService.java | 121 -------- .../so/adapters/audit/AuditQueryStackService.java | 85 ------ .../onap/so/adapters/audit/AuditStackService.java | 91 ------ .../org/onap/so/adapters/audit/AuditVServer.java | 171 ----------- .../org/onap/so/adapters/audit/HeatStackAudit.java | 327 --------------------- .../inventory/create/CreateAAIInventory.java | 73 ----- .../inventory/create/CreateInventoryService.java | 52 ---- .../inventory/create/CreateInventoryTask.java | 121 -------- .../inventory/create/InventoryException.java | 34 --- .../org/onap/so/adapters/tasks/TaskServices.java | 132 +++++++++ .../so/adapters/tasks/audit/AbstractAudit.java | 39 +++ .../adapters/tasks/audit/AbstractAuditService.java | 87 ++++++ .../tasks/audit/AuditCreateStackService.java | 111 +++++++ .../so/adapters/tasks/audit/AuditDataService.java | 77 +++++ .../tasks/audit/AuditDeleteStackService.java | 122 ++++++++ .../tasks/audit/AuditQueryStackService.java | 85 ++++++ .../onap/so/adapters/tasks/audit/AuditVServer.java | 171 +++++++++++ .../so/adapters/tasks/audit/HeatStackAudit.java | 327 +++++++++++++++++++++ .../tasks/inventory/CreateAAIInventory.java | 73 +++++ .../tasks/inventory/CreateInventoryTask.java | 122 ++++++++ .../tasks/inventory/InventoryException.java | 34 +++ .../adapters/tasks/orchestration/PollService.java | 191 ++++++++++++ .../tasks/orchestration/RollbackService.java | 92 ++++++ .../adapters/tasks/orchestration/StackService.java | 207 +++++++++++++ .../onap/so/adapters/vnf/MsoVnfAdapterImpl.java | 53 +++- .../org/onap/so/adapters/vnf/VnfAdapterRest.java | 1 + .../org/onap/so/adapters/vnf/VnfAdapterRestV2.java | 8 +- .../onap/so/adapters/vnf/VolumeAdapterRestV2.java | 47 ++- 32 files changed, 1942 insertions(+), 1427 deletions(-) delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAudit.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAuditService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditCreateStackService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDataService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDeleteStackService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditQueryStackService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditVServer.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/HeatStackAudit.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateAAIInventory.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryTask.java delete mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/InventoryException.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/TaskServices.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAudit.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAuditService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditCreateStackService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDataService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDeleteStackService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditQueryStackService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditVServer.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/HeatStackAudit.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateInventoryTask.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/InventoryException.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/PollService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/RollbackService.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/StackService.java (limited to 'adapters/mso-openstack-adapters/src/main/java/org') diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAudit.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAudit.java deleted file mode 100644 index ad28f0daeb..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAudit.java +++ /dev/null @@ -1,39 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.audit; - -import org.onap.so.client.aai.AAIResourcesClient; - -public class AbstractAudit { - - private AAIResourcesClient aaiClient; - - protected AAIResourcesClient getAaiClient() { - if (aaiClient == null) - return new AAIResourcesClient(); - else - return aaiClient; - } - - protected void setAaiClient(AAIResourcesClient aaiResource) { - aaiClient = aaiResource; - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAuditService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAuditService.java deleted file mode 100644 index ddd9fb1b5e..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AbstractAuditService.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.audit; - -import java.util.Optional; -import org.onap.so.objects.audit.AAIObjectAudit; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.onap.so.utils.ExternalTaskUtils; -import org.onap.so.utils.RetrySequenceLevel; -import org.onap.logging.filter.base.ONAPComponents; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -public abstract class AbstractAuditService extends ExternalTaskUtils { - - private static final Logger logger = LoggerFactory.getLogger(AbstractAuditService.class); - - - - protected static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI = - "Unable to find all VServers and L-Interaces in A&AI"; - - protected static final String UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK = "Unable to find VServers in Openstack"; - - @Autowired - public Environment env; - - public AbstractAuditService() { - super(RetrySequenceLevel.LONG); - } - - /** - * @param auditHeatStackFailed - * @param auditList - * @return - */ - protected boolean didCreateAuditFail(Optional auditList) { - if (auditList.get().getAuditList() != null && !auditList.get().getAuditList().isEmpty()) { - if (logger.isInfoEnabled()) { - logger.info("Audit Results: {}", auditList.get().toString()); - } - return auditList.get().getAuditList().stream().filter(auditObject -> !auditObject.isDoesObjectExist()) - .findFirst().map(v -> true).orElse(false); - } else { - return false; - } - } - - /** - * @param auditHeatStackFailed - * @param auditList - * @return - */ - protected boolean didDeleteAuditFail(AAIObjectAuditList auditList) { - if (auditList.getAuditList() != null && !auditList.getAuditList().isEmpty()) { - if (logger.isInfoEnabled()) { - logger.info("Audit Results: {}", auditList.toString()); - } - return auditList.getAuditList().stream().filter(AAIObjectAudit::isDoesObjectExist).findFirst() - .map(v -> true).orElse(false); - } else { - return false; - } - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditCreateStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditCreateStackService.java deleted file mode 100644 index 8291fa9882..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditCreateStackService.java +++ /dev/null @@ -1,111 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Modifications Copyright (c) 2019 Samsung - * ================================================================================ - * 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.so.adapters.audit; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import org.camunda.bpm.client.task.ExternalTask; -import org.camunda.bpm.client.task.ExternalTaskService; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.audit.beans.AuditInventory; -import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import org.onap.so.externaltasks.logging.AuditMDCSetup; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.onap.so.utils.RetrySequenceLevel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class AuditCreateStackService extends AbstractAuditService { - - private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class); - - @Autowired - public HeatStackAudit heatStackAudit; - - @Autowired - private AuditMDCSetup mdcSetup; - - public AuditCreateStackService() { - super(); - } - - protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { - mdcSetup.setupMDC(externalTask); - AuditInventory auditInventory = externalTask.getVariable("auditInventory"); - Map variables = new HashMap<>(); - boolean success = false; - try { - Integer retryCount = externalTask.getRetries(); - logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount); - Optional auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(), - auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName()); - if (auditListOpt.isPresent()) { - auditListOpt.get().setAuditType("create"); - auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName()); - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get())); - success = !didCreateAuditFail(auditListOpt); - } - } catch (Exception e) { - logger.error("Error during audit of stack", e); - } - variables.put("auditIsSuccessful", success); - mdcSetup.setElapsedTime(); - String externalTaskId = externalTask.getId(); - if (success) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); - logger.debug("The External Task Id: {} Successful", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - Integer retryCount = externalTask.getRetries(); - if (retryCount == null) { - logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", - externalTaskId, getRetrySequence().length); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, - UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000); - } else if (retryCount == 1) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", - externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, - UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1, - calculateRetryDelay(retryCount)); - } - logger.debug("The External Task Id: {} Failed", externalTaskId); - } - } - - - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDataService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDataService.java deleted file mode 100644 index 1c707fe795..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDataService.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.onap.so.adapters.audit; - -import java.io.IOException; -import java.util.List; -import java.util.Optional; -import org.onap.so.audit.beans.AuditInventory; -import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import org.onap.so.db.request.beans.RequestProcessingData; -import org.onap.so.db.request.client.RequestsDbClient; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; - -@Component -public class AuditDataService { - - @Autowired - private RequestsDbClient requestsDbClient; - - /** - * Checks to see if an entry already exist for the given heat stack and writes audit stack data to the request - * database if it doesn't. - * - * @throws JsonProcessingException - */ - public void writeStackDataToRequestDb(AuditInventory auditInventory, AAIObjectAuditList auditList) - throws JsonProcessingException { - List requestProcessingDataList = - requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(), - auditInventory.getHeatStackName(), "AuditStackData"); - if (requestProcessingDataList.isEmpty()) { - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - String auditListString = objectMapper.getMapper().writeValueAsString(auditList);; - - RequestProcessingData requestProcessingData = new RequestProcessingData(); - requestProcessingData.setSoRequestId(auditInventory.getMsoRequestId()); - requestProcessingData.setGroupingId(auditInventory.getVfModuleId()); - requestProcessingData.setName(auditInventory.getHeatStackName()); - requestProcessingData.setTag("AuditStackData"); - requestProcessingData.setValue(auditListString); - - requestsDbClient.saveRequestProcessingData(requestProcessingData); - } - } - - /** - * Retrieves audit stack data from the request database. - * - * @throws IOException - * @throws JsonMappingException - * @throws JsonParseException - */ - public Optional getStackDataFromRequestDb(AuditInventory auditInventory) - throws JsonParseException, JsonMappingException, IOException { - - List requestProcessingDataList = - requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(), - auditInventory.getHeatStackName(), "AuditStackData"); - if (!requestProcessingDataList.isEmpty()) { - RequestProcessingData requestProcessingData = requestProcessingDataList.get(0); - String auditListString = requestProcessingData.getValue(); - - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - AAIObjectAuditList auditList = - objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class); - - return Optional.of(auditList); - } else { - return Optional.empty(); - } - } - - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDeleteStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDeleteStackService.java deleted file mode 100644 index cf077915c9..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDeleteStackService.java +++ /dev/null @@ -1,121 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * 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.so.adapters.audit; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import org.camunda.bpm.client.task.ExternalTask; -import org.camunda.bpm.client.task.ExternalTaskService; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.audit.beans.AuditInventory; -import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import org.onap.so.externaltasks.logging.AuditMDCSetup; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -public class AuditDeleteStackService extends AbstractAuditService { - - private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class); - - @Autowired - protected HeatStackAudit heatStackAudit; - - @Autowired - private AuditMDCSetup mdcSetup; - - @Autowired - protected AuditVServer auditVservers; - - @Autowired - protected AuditDataService auditDataService; - - @Autowired - protected Environment env; - - public AuditDeleteStackService() { - super(); - } - - protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { - mdcSetup.setupMDC(externalTask); - AuditInventory auditInventory = externalTask.getVariable("auditInventory"); - Map variables = new HashMap<>(); - boolean success = false; - try { - Integer retryCount = externalTask.getRetries(); - logger.info("Executing External Task Delete Audit Inventory. Retry Number: {}", retryCount); - Optional auditListOpt = auditDataService.getStackDataFromRequestDb(auditInventory); - if (auditListOpt.isPresent()) { - auditVservers.auditVservers(auditListOpt.get()); - } else { - logger.debug("Auditing Vservers based on vf module relationships"); - auditListOpt = auditVservers.auditVserversThroughRelationships(auditInventory.getGenericVnfId(), - auditInventory.getHeatStackName()); - } - auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName()); - auditListOpt.get().setAuditType("delete"); - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get())); - success = !didDeleteAuditFail(auditListOpt.get()); - - } catch (Exception e) { - logger.error("Error during audit of stack", e); - } - variables.put("auditIsSuccessful", success); - mdcSetup.setElapsedTime(); - String externalTaskId = externalTask.getId(); - if (success) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); - logger.debug("The External Task Id: {} Successful", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - Integer retryCount = externalTask.getRetries(); - if (retryCount == null) { - logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", - externalTaskId, getRetrySequence().length); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, - UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000); - } else if (retryCount == 1) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", - externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, - UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1, - calculateRetryDelay(retryCount)); - } - logger.debug("The External Task Id: {} Failed", externalTaskId); - } - } - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditQueryStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditQueryStackService.java deleted file mode 100644 index 1c69a3e5e7..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditQueryStackService.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.onap.so.adapters.audit; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import org.camunda.bpm.client.task.ExternalTask; -import org.camunda.bpm.client.task.ExternalTaskService; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.audit.beans.AuditInventory; -import org.onap.so.externaltasks.logging.AuditMDCSetup; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class AuditQueryStackService extends AbstractAuditService { - - private static final Logger logger = LoggerFactory.getLogger(AuditQueryStackService.class); - - @Autowired - protected HeatStackAudit heatStackAudit; - - @Autowired - private AuditMDCSetup mdcSetup; - - @Autowired - protected AuditDataService auditDataService; - - public AuditQueryStackService() { - super(); - } - - protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { - mdcSetup.setupMDC(externalTask); - AuditInventory auditInventory = externalTask.getVariable("auditInventory"); - boolean success = false; - Map variables = new HashMap<>(); - try { - Integer retryCount = externalTask.getRetries(); - logger.info("Executing External Task Query Audit Inventory. Audit Inventory: {} \n Retry Number: {}", - auditInventory.toString(), retryCount); - - Optional auditList = heatStackAudit.queryHeatStack(auditInventory.getCloudOwner(), - auditInventory.getCloudRegion(), auditInventory.getTenantId(), auditInventory.getHeatStackName()); - - if (auditList.isPresent()) { - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); - success = true; - auditDataService.writeStackDataToRequestDb(auditInventory, auditList.get()); - } - mdcSetup.setElapsedTime(); - String externalTaskId = externalTask.getId(); - if (success) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); - logger.debug("The External Task {} was Successful", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - if (retryCount == null) { - logger.debug("The External Task {} Failed. Setting Retries to Default Start Value: {}", - externalTaskId, getRetrySequence().length); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, - UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, getRetrySequence().length, 10000); - } else if (retryCount == 1) { - externalTaskService.complete(externalTask, variables); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.debug("The External Task {} Failed. All Retries Exhausted", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else { - logger.debug("The External Task {} Failed. Decrementing Retries to {} , Retry Delay: ", - externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); - externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, - UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, retryCount - 1, calculateRetryDelay(retryCount)); - } - logger.debug("The External Task {} Failed", externalTaskId); - } - } catch (Exception e) { - logger.error("Error during audit query of stack", e); - } - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java deleted file mode 100644 index 7cc7c02f23..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.audit; - -import javax.annotation.PostConstruct; -import org.camunda.bpm.client.ExternalTaskClient; -import org.onap.so.utils.ExternalTaskServiceUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Profile; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Profile("!test") -public class AuditStackService { - - private static final String MSO_AUDIT_LOCK_TIME = "mso.audit.lock-time"; - - private static final Logger logger = LoggerFactory.getLogger(AuditStackService.class); - - private static final String DEFAULT_AUDIT_LOCK_TIME = "60000"; - - private static final String DEFAULT_MAX_CLIENTS_FOR_TOPIC = "10"; - - - @Autowired - public Environment env; - - @Autowired - private AuditCreateStackService auditCreateStack; - - @Autowired - private AuditDeleteStackService auditDeleteStack; - - @Autowired - private AuditQueryStackService auditQueryStack; - - @Autowired - private ExternalTaskServiceUtils externalTaskServiceUtils; - - @PostConstruct - public void auditAddAAIInventory() throws Exception { - for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { - ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); - client.subscribe("InventoryAddAudit") - .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) - .handler(auditCreateStack::executeExternalTask).open(); - } - } - - @PostConstruct - public void auditDeleteAAIInventory() throws Exception { - for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { - ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); - client.subscribe("InventoryDeleteAudit") - .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) - .handler(auditDeleteStack::executeExternalTask).open(); - } - } - - @PostConstruct - public void auditQueryInventory() throws Exception { - for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { - ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); - client.subscribe("InventoryQueryAudit") - .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) - .handler(auditQueryStack::executeExternalTask).open(); - } - } - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditVServer.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditVServer.java deleted file mode 100644 index 585e001f1a..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditVServer.java +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.audit; - -import java.util.List; -import java.util.Optional; -import java.util.Set; -import org.onap.aai.domain.yang.LInterface; -import org.onap.aai.domain.yang.VfModule; -import org.onap.aai.domain.yang.VfModules; -import org.onap.aai.domain.yang.Vserver; -import org.onap.so.client.aai.AAIObjectPlurals; -import org.onap.so.client.aai.AAIObjectType; -import org.onap.so.client.aai.entities.AAIResultWrapper; -import org.onap.so.client.aai.entities.uri.AAIPluralResourceUri; -import org.onap.so.client.aai.entities.uri.AAIResourceUri; -import org.onap.so.client.aai.entities.uri.AAIUriFactory; -import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import org.onap.so.objects.audit.AAIObjectAudit; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.BeanUtils; -import org.springframework.stereotype.Component; -import com.fasterxml.jackson.core.JsonProcessingException; - -@Component -public class AuditVServer extends AbstractAudit { - private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class); - - public void auditVservers(AAIObjectAuditList aaiObjectAuditList) { - - aaiObjectAuditList.getAuditList().forEach(aaiObjectAudit -> { - boolean vserverExist = getAaiClient().exists(AAIUriFactory - .createResourceFromExistingURI(AAIObjectType.VSERVER, aaiObjectAudit.getResourceURI())); - aaiObjectAudit.setDoesObjectExist(vserverExist); - }); - } - - public Optional auditVserversThroughRelationships(String genericVnfId, String vfModuleName) { - AAIObjectAuditList auditList = new AAIObjectAuditList(); - AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, genericVnfId) - .queryParam("vf-module-name", vfModuleName); - Optional wrapper = getAaiClient().getFirstWrapper(VfModules.class, VfModule.class, uri); - if (wrapper.isPresent() && wrapper.get().getRelationships().isPresent()) { - List relatedVservers = - wrapper.get().getRelationships().get().getRelatedUris(AAIObjectType.VSERVER); - if (!relatedVservers.isEmpty()) { - relatedVservers.forEach(vserverUri -> { - Optional vserver = getAaiClient().get(vserverUri).asBean(Vserver.class); - Vserver vServerShallow = new Vserver(); - BeanUtils.copyProperties(vserver, vServerShallow); - AAIObjectAudit vServerAudit = new AAIObjectAudit(); - vServerAudit.setAaiObject(vServerShallow); - vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); - vServerAudit.setDoesObjectExist(true); - auditList.getAuditList().add(vServerAudit); - }); - } - } - return Optional.of(auditList); - } - - public Optional auditVservers(Set vServersToAudit, String tenantId, String cloudOwner, - String cloudRegion) { - if (vServersToAudit == null || vServersToAudit.isEmpty()) { - return Optional.empty(); - } - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - vServersToAudit.stream().forEach(vserver -> { - try { - logger.debug("Vserver to Audit: {}", objectMapper.getMapper().writeValueAsString(vserver)); - } catch (JsonProcessingException e) { - logger.error("Json parse exception: ", e); - } - - }); - AAIObjectAuditList auditList = new AAIObjectAuditList(); - vServersToAudit.stream().forEach(vServer -> auditList.getAuditList() - .addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList())); - return Optional.of(auditList); - } - - private AAIObjectAuditList doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, - String cloudRegion) { - AAIObjectAuditList auditList = new AAIObjectAuditList(); - AAIObjectAudit vServerAudit = new AAIObjectAudit(); - AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion, - tenantId, vServer.getVserverId()); - Vserver vServerShallow = new Vserver(); - BeanUtils.copyProperties(vServer, vServerShallow, "LInterfaces"); - boolean vServerExists = getAaiClient().exists(vserverURI); - logger.info("v-server {} exists: {}", vServer.getVserverId(), vServerExists); - vServerAudit.setAaiObject(vServerShallow); - vServerAudit.setDoesObjectExist(vServerExists); - vServerAudit.setResourceURI(vserverURI.build()); - vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); - auditList.getAuditList().add(vServerAudit); - if (vServer.getLInterfaces() != null) { - vServer.getLInterfaces().getLInterface().stream().forEach(lInterface -> auditList.getAuditList().addAll( - doesLinterfaceExistinAAI(lInterface, vServer.getVserverId(), tenantId, cloudOwner, cloudRegion) - .getAuditList())); - } - return auditList; - } - - private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId, - String cloudOwner, String cloudRegion) { - AAIObjectAuditList auditList = new AAIObjectAuditList(); - AAIObjectAudit lInterfaceAudit = new AAIObjectAudit(); - AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, - cloudRegion, tenantId, vServerId, lInterface.getInterfaceName()); - Optional queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI); - if (queriedLInterface.isPresent()) { - lInterfaceAudit.setDoesObjectExist(true); - lInterface.setInterfaceName(lInterface.getInterfaceName()); - } - lInterfaceAudit.setAaiObject(lInterface); - lInterfaceAudit.setResourceURI(linterfaceURI.build()); - lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName()); - auditList.getAuditList().add(lInterfaceAudit); - logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(), - lInterface.getInterfaceName(), lInterfaceAudit.isDoesObjectExist()); - - if (lInterface.getLInterfaces() != null) { - lInterface.getLInterfaces().getLInterface().stream() - .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface, - lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion))); - } - logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces", - lInterface.getInterfaceId()); - - return auditList; - } - - private AAIObjectAudit doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId, - String tenantId, String cloudOwner, String cloudRegion) { - logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {} exists", - subInterface.getInterfaceName(), linterfaceName, vServerId); - AAIObjectAudit subInterfaceAudit = new AAIObjectAudit(); - - - AAIResourceUri subInterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, cloudOwner, - cloudRegion, tenantId, vServerId, linterfaceName, subInterface.getInterfaceName()); - subInterfaceAudit.setResourceURI(subInterfaceURI.build()); - boolean doesExist = getAaiClient().exists(subInterfaceURI); - logger.info("sub-l-interface-id:{} exists: {}", subInterface.getInterfaceId(), doesExist); - subInterfaceAudit.setAaiObject(subInterface); - subInterfaceAudit.setDoesObjectExist(doesExist); - subInterfaceAudit.setAaiObjectType(AAIObjectType.SUB_L_INTERFACE.typeName()); - return subInterfaceAudit; - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/HeatStackAudit.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/HeatStackAudit.java deleted file mode 100644 index ffa76cf8ee..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/HeatStackAudit.java +++ /dev/null @@ -1,327 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * - * Modifications Copyright (C) 2019 IBM. - * ================================================================================ - * 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.so.adapters.audit; - -import java.net.URI; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.onap.aai.domain.yang.LInterface; -import org.onap.aai.domain.yang.LInterfaces; -import org.onap.aai.domain.yang.Vlan; -import org.onap.aai.domain.yang.Vlans; -import org.onap.aai.domain.yang.Vserver; -import org.onap.so.client.aai.AAIObjectType; -import org.onap.so.client.aai.entities.uri.AAIUriFactory; -import org.onap.so.objects.audit.AAIObjectAudit; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.onap.so.openstack.utils.MsoHeatUtils; -import org.onap.so.openstack.utils.MsoNeutronUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import com.woorea.openstack.heat.model.Link; -import com.woorea.openstack.heat.model.Resource; -import com.woorea.openstack.heat.model.Resources; -import com.woorea.openstack.heat.model.Stack; -import com.woorea.openstack.quantum.model.Port; - -@Component -public class HeatStackAudit { - - private static final String RESOURCES = "/resources"; - - protected static final Logger logger = LoggerFactory.getLogger(HeatStackAudit.class); - - private static final String EXCEPTION_MSG = "Error finding Path from Self Link"; - - @Autowired - protected MsoHeatUtils heat; - - @Autowired - protected MsoNeutronUtils neutron; - - @Autowired - protected AuditVServer auditVservers; - - public Optional queryHeatStack(String cloudOwner, String cloudRegion, String tenantId, - String heatStackName) { - try { - logger.debug("Fetching Top Level Stack Information"); - Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName, 3); - List novaResources = resources.getList().stream() - .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); - if (novaResources.isEmpty()) - return Optional.of(new AAIObjectAuditList()); - else { - Set vserversToAudit = createVserverSet(novaResources); - AAIObjectAuditList aaiObjectAuditList = new AAIObjectAuditList(); - vserversToAudit.stream().forEach(vServer -> aaiObjectAuditList.getAuditList() - .add(createAAIObjectAudit(cloudOwner, cloudRegion, tenantId, vServer))); - return Optional.of(aaiObjectAuditList); - } - } catch (Exception e) { - logger.error("Error during query stack resources", e); - return Optional.of(new AAIObjectAuditList()); - } - } - - public Optional auditHeatStack(String cloudRegion, String cloudOwner, String tenantId, - String heatStackName) { - try { - logger.debug("Fetching Top Level Stack Information"); - Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName, 3); - List novaResources = resources.getList().stream() - .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); - List resourceGroups = resources.getList().stream() - .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType()) && p.getName().contains("subinterfaces")) - .collect(Collectors.toList()); - List> neutronPortDetails = retrieveNeutronPortDetails(resources, cloudRegion, tenantId); - if (novaResources.isEmpty()) - return Optional.of(new AAIObjectAuditList()); - else { - Set vserversToAudit = createVserverSet(resources, novaResources, neutronPortDetails); - Set vserversWithSubInterfaces = - processSubInterfaces(cloudRegion, tenantId, resourceGroups, vserversToAudit); - return auditVservers.auditVservers(vserversWithSubInterfaces, tenantId, cloudOwner, cloudRegion); - } - } catch (Exception e) { - logger.error("Error during auditing stack resources", e); - return Optional.empty(); - } - } - - protected Set processSubInterfaces(String cloudRegion, String tenantId, List resourceGroups, - Set vServersToAudit) throws Exception { - for (Resource resourceGroup : resourceGroups) { - processResourceGroups(cloudRegion, tenantId, vServersToAudit, resourceGroup); - } - return vServersToAudit; - } - - protected void processResourceGroups(String cloudRegion, String tenantId, Set vServersWithLInterface, - Resource resourceGroup) throws Exception { - Optional stackLink = - resourceGroup.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny(); - if (stackLink.isPresent()) { - try { - Optional path = extractResourcePathFromHref(stackLink.get().getHref()); - if (path.isPresent()) { - logger.debug("Fetching nested Resource Stack Information"); - Resources nestedResourceGroupResources = - heat.executeHeatClientRequest(path.get(), cloudRegion, tenantId, Resources.class); - processNestedResourceGroup(cloudRegion, tenantId, vServersWithLInterface, - nestedResourceGroupResources); - } else - throw new Exception(EXCEPTION_MSG); - } catch (Exception e) { - logger.error("Error Parsing Link to obtain Path", e); - throw new Exception(EXCEPTION_MSG); - } - } - } - - protected void processNestedResourceGroup(String cloudRegion, String tenantId, Set vServersWithLInterface, - Resources nestedResourceGroupResources) throws Exception { - for (Resource resourceGroupNested : nestedResourceGroupResources) { - Optional subInterfaceStackLink = - resourceGroupNested.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny(); - if (subInterfaceStackLink.isPresent()) { - addSubInterface(cloudRegion, tenantId, vServersWithLInterface, subInterfaceStackLink.get()); - } - } - } - - protected void addSubInterface(String cloudRegion, String tenantId, Set vServersWithLInterface, - Link subInterfaceStackLink) throws Exception { - Optional resourcePath = extractResourcePathFromHref(subInterfaceStackLink.getHref()); - Optional stackPath = extractStackPathFromHref(subInterfaceStackLink.getHref()); - if (resourcePath.isPresent() && stackPath.isPresent()) { - logger.debug("Fetching nested Sub-Interface Stack Information"); - Stack subinterfaceStack = - heat.executeHeatClientRequest(stackPath.get(), cloudRegion, tenantId, Stack.class); - Resources subinterfaceResources = - heat.executeHeatClientRequest(resourcePath.get(), cloudRegion, tenantId, Resources.class); - if (subinterfaceStack != null) { - addSubInterfaceToVserver(vServersWithLInterface, subinterfaceStack, subinterfaceResources); - } - } else - throw new Exception(EXCEPTION_MSG); - - } - - protected void addSubInterfaceToVserver(Set vServersWithLInterface, Stack subinterfaceStack, - Resources subinterfaceResources) throws Exception { - String parentNeutronPortId = (String) subinterfaceStack.getParameters().get("port_interface"); - logger.debug("Parent neutron Port: {} on SubInterface: {}", parentNeutronPortId, subinterfaceStack.getId()); - for (Vserver auditVserver : vServersWithLInterface) - for (LInterface lInterface : auditVserver.getLInterfaces().getLInterface()) - - if (parentNeutronPortId.equals(lInterface.getInterfaceId())) { - logger.debug("Found Parent Port on VServer: {} on Port: {}", auditVserver.getVserverId(), - lInterface.getInterfaceId()); - Resource contrailVm = subinterfaceResources.getList().stream() - .filter(resource -> "OS::ContrailV2::VirtualMachineInterface".equals(resource.getType())) - .findAny().orElse(null); - if (contrailVm == null) { - throw new Exception("Cannnot find Contrail Virtual Machine Interface on Stack: " - + subinterfaceStack.getId()); - } - LInterface subInterface = new LInterface(); - subInterface.setInterfaceId(contrailVm.getPhysicalResourceId()); - subInterface.setIsPortMirrored(false); - subInterface.setInMaint(false); - subInterface.setIsIpUnnumbered(false); - String macAddr = (String) subinterfaceStack.getParameters().get("mac_address"); - subInterface.setMacaddr(macAddr); - - String namePrefix = (String) subinterfaceStack.getParameters().get("subinterface_name_prefix"); - Integer vlanIndex = Integer.parseInt((String) subinterfaceStack.getParameters().get("counter")); - String vlanTagList = (String) subinterfaceStack.getParameters().get("vlan_tag"); - List subInterfaceVlanTagList = Arrays.asList(vlanTagList.split(",")); - subInterface.setInterfaceName(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex)); - subInterface.setVlans(new Vlans()); - Vlan vlan = new Vlan(); - vlan.setInMaint(false); - vlan.setIsIpUnnumbered(false); - vlan.setVlanIdInner(Long.parseLong(subInterfaceVlanTagList.get(vlanIndex))); - vlan.setVlanInterface(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex)); - subInterface.getVlans().getVlan().add(vlan); - if (lInterface.getLInterfaces() == null) - lInterface.setLInterfaces(new LInterfaces()); - - lInterface.getLInterfaces().getLInterface().add(subInterface); - } else - logger.debug("Did Not Find Parent Port on VServer: {} Parent Port: SubInterface: {}", - auditVserver.getVserverId(), lInterface.getInterfaceId(), subinterfaceStack.getId()); - } - - protected Set createVserverSet(Resources resources, List novaResources, - List> neutronPortDetails) { - Set vserversToAudit = new HashSet<>(); - for (Resource novaResource : novaResources) { - Vserver auditVserver = new Vserver(); - auditVserver.setLInterfaces(new LInterfaces()); - auditVserver.setVserverId(novaResource.getPhysicalResourceId()); - Stream filteredNeutronPorts = filterNeutronPorts(novaResource, neutronPortDetails); - filteredNeutronPorts.forEach(port -> { - LInterface lInterface = new LInterface(); - lInterface.setInterfaceId(port.getId()); - lInterface.setInterfaceName(port.getName()); - auditVserver.getLInterfaces().getLInterface().add(lInterface); - }); - vserversToAudit.add(auditVserver); - } - return vserversToAudit; - } - - protected Set createVserverSet(List novaResources) { - Set vserversToAudit = new HashSet<>(); - for (Resource novaResource : novaResources) { - Vserver auditVserver = new Vserver(); - auditVserver.setLInterfaces(new LInterfaces()); - auditVserver.setVserverId(novaResource.getPhysicalResourceId()); - vserversToAudit.add(auditVserver); - } - return vserversToAudit; - } - - protected AAIObjectAudit createAAIObjectAudit(String cloudOwner, String cloudRegion, String tenantId, - Vserver vServer) { - AAIObjectAudit aaiObjectAudit = new AAIObjectAudit(); - Vserver vServerShallow = new Vserver(); - BeanUtils.copyProperties(vServer, vServerShallow); - aaiObjectAudit.setAaiObject(vServerShallow); - aaiObjectAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); - aaiObjectAudit.setResourceURI(AAIUriFactory - .createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion, tenantId, vServer.getVserverId()) - .build()); - - return aaiObjectAudit; - } - - /** - * @param novaResource Single openstack resource that is of type Nova - * @param neutronPorts List of Neutron ports created within the stack - * @return Filtered list of neutron ports taht relate to the nova server in openstack - */ - protected Stream filterNeutronPorts(Resource novaResource, List> neutronPorts) { - List filteredNeutronPorts = - neutronPorts.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()); - return filteredNeutronPorts.stream() - .filter(port -> port.getDeviceId().equalsIgnoreCase(novaResource.getPhysicalResourceId())); - } - - /** - * @param resources Resource stream created by the stack in openstack - * @param cloudSiteId Unique site id to identify which openstack we talk to - * @param tenantId The tenant within the cloud we are talking to where resouces exist - * @return List of optional neutron ports found within the cloud site and tenant - */ - protected List> retrieveNeutronPortDetails(Resources resources, String cloudSiteId, - String tenantId) { - return resources.getList().parallelStream().filter(resource -> "OS::Neutron::Port".equals(resource.getType())) - .map(resource -> neutron.getNeutronPort(resource.getPhysicalResourceId(), tenantId, cloudSiteId)) - .collect(Collectors.toList()); - - } - - protected Optional extractResourcePathFromHref(String href) { - try { - Optional stackPath = extractStackPathFromHref(href); - if (stackPath.isPresent()) { - return Optional.of(stackPath.get() + RESOURCES); - } else - return Optional.empty(); - } catch (Exception e) { - logger.error("Error parsing URI", e); - } - return Optional.empty(); - } - - protected Optional extractStackPathFromHref(String href) { - try { - URI uri = new URI(href); - Pattern p = Pattern.compile("/stacks.*"); - Matcher m = p.matcher(uri.getPath()); - if (m.find()) { - return Optional.of(m.group()); - } else - return Optional.empty(); - } catch (Exception e) { - logger.error("Error parsing URI", e); - } - return Optional.empty(); - } - - -} - diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateAAIInventory.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateAAIInventory.java deleted file mode 100644 index 69e16986da..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateAAIInventory.java +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.inventory.create; - -import java.util.Optional; -import java.util.stream.Stream; -import org.onap.so.client.aai.AAIObjectType; -import org.onap.so.client.aai.AAIResourcesClient; -import org.onap.so.client.aai.entities.uri.AAIUriFactory; -import org.onap.so.objects.audit.AAIObjectAudit; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.springframework.stereotype.Component; - -@Component -public class CreateAAIInventory { - - private AAIResourcesClient aaiClient; - - public void createInventory(AAIObjectAuditList auditList) throws InventoryException { - if (didAuditFailVserverLInterfaces(auditList)) { - throw new InventoryException("Audit failed for VServer or LInterface cannot write Sub-Interfaces"); - } - auditList.getAuditList().parallelStream() - .filter(auditObject -> !auditObject.isDoesObjectExist() - && AAIObjectType.SUB_L_INTERFACE.typeName().equals(auditObject.getAaiObjectType())) - .forEach(auditObject -> getAaiClient().createIfNotExists(AAIUriFactory.createResourceFromExistingURI( - AAIObjectType.fromTypeName(auditObject.getAaiObjectType()), auditObject.getResourceURI()), - Optional.of(auditObject.getAaiObject()))); - } - - - /** - * @param auditHeatStackFailed - * @param auditList - * @return - */ - protected boolean didAuditFailVserverLInterfaces(AAIObjectAuditList auditList) { - Stream issue = auditList.getAuditList().stream() - .filter(auditObject -> auditObject.getAaiObjectType().equals(AAIObjectType.VSERVER.typeName()) - || auditObject.getAaiObjectType().equals(AAIObjectType.L_INTERFACE.typeName())); - - return issue.filter(auditObject -> !auditObject.isDoesObjectExist()).findFirst().map(v -> true).orElse(false); - } - - protected AAIResourcesClient getAaiClient() { - if (aaiClient == null) - return new AAIResourcesClient(); - else - return aaiClient; - } - - protected void setAaiClient(AAIResourcesClient aaiResource) { - aaiClient = aaiResource; - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java deleted file mode 100644 index 4ab4d14dac..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 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.so.adapters.inventory.create; - -import javax.annotation.PostConstruct; -import org.camunda.bpm.client.ExternalTaskClient; -import org.onap.so.utils.ExternalTaskServiceUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Profile; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Profile("!test") -public class CreateInventoryService { - - @Autowired - public Environment env; - - @Autowired - private CreateInventoryTask createInventory; - - @Autowired - private ExternalTaskServiceUtils externalTaskServiceUtils; - - @PostConstruct - public void auditAAIInventory() throws Exception { - ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); - client.subscribe("InventoryCreate") - .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", "60000"))) - .handler(createInventory::executeExternalTask).open(); - } - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryTask.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryTask.java deleted file mode 100644 index e02258c619..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryTask.java +++ /dev/null @@ -1,121 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * - * Copyright (C) 2019 IBM - * ================================================================================ - * 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.so.adapters.inventory.create; - -import org.camunda.bpm.client.task.ExternalTask; -import org.camunda.bpm.client.task.ExternalTaskService; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import org.onap.so.externaltasks.logging.AuditMDCSetup; -import org.onap.so.objects.audit.AAIObjectAuditList; -import org.onap.so.utils.ExternalTaskUtils; -import org.onap.so.utils.RetrySequenceLevel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class CreateInventoryTask extends ExternalTaskUtils { - - private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI"; - - private static final Logger logger = LoggerFactory.getLogger(CreateInventoryTask.class); - - private static final String AAI_INVENTORY_FAILURE = "AAIInventoryFailure"; - - @Autowired - CreateAAIInventory createInventory; - - @Autowired - private AuditMDCSetup mdcSetup; - - public CreateInventoryTask() { - super(RetrySequenceLevel.SHORT); - } - - protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { - mdcSetup.setupMDC(externalTask); - boolean success = true; - boolean inventoryException = false; - String auditInventoryString = externalTask.getVariable("auditInventoryResult"); - AAIObjectAuditList auditInventory = null; - String externalTaskId = externalTask.getId(); - try { - GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); - auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class); - } catch (Exception e) { - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.error("Error Parsing Audit Results", e); - } - mdcSetup.setElapsedTime(); - if (auditInventory != null) { - Integer retryCount = externalTask.getRetries(); - try { - logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory, - retryCount); - createInventory.createInventory(auditInventory); - } catch (InventoryException e) { - logger.error("Error during inventory of stack", e); - success = false; - inventoryException = true; - } catch (Exception e) { - logger.error("Error during inventory of stack", e); - success = false; - } - mdcSetup.setElapsedTime(); - if (success) { - externalTaskService.complete(externalTask); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); - logger.debug("The External Task Id: {} Successful", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - mdcSetup.clearClientMDCs(); - } else if (inventoryException) { - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.debug("The External Task Id: {} Failed, Retry not needed", externalTaskId); - externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); - } else { - if (retryCount == null) { - logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", - externalTaskId, getRetrySequence().length); - externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, - UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000); - } else if (retryCount != null && retryCount - 1 == 0) { - externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); - mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); - logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - } else { - logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", - externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); - externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, - UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, retryCount - 1, calculateRetryDelay(retryCount)); - } - logger.debug("The External Task Id: {} Failed", externalTaskId); - } - } else { - logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTaskId); - externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); - } - } -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/InventoryException.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/InventoryException.java deleted file mode 100644 index ed31c1c67e..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/InventoryException.java +++ /dev/null @@ -1,34 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * 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.so.adapters.inventory.create; - -public class InventoryException extends Exception { - - /** - * - */ - private static final long serialVersionUID = 912652713891488731L; - - public InventoryException(String errorMessage) { - super(errorMessage); - } - -} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/TaskServices.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/TaskServices.java new file mode 100644 index 0000000000..d282046b3f --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/TaskServices.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 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.so.adapters.tasks; + +import javax.annotation.PostConstruct; +import org.camunda.bpm.client.ExternalTaskClient; +import org.onap.so.utils.ExternalTaskServiceUtils; +import org.onap.so.adapters.tasks.audit.AuditCreateStackService; +import org.onap.so.adapters.tasks.audit.AuditDeleteStackService; +import org.onap.so.adapters.tasks.audit.AuditQueryStackService; +import org.onap.so.adapters.tasks.inventory.CreateInventoryTask; +import org.onap.so.adapters.tasks.orchestration.PollService; +import org.onap.so.adapters.tasks.orchestration.RollbackService; +import org.onap.so.adapters.tasks.orchestration.StackService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Component +@Profile("!test") +public class TaskServices { + + private static final Logger logger = LoggerFactory.getLogger(TaskServices.class); + + @Autowired + private ExternalTaskServiceUtils externalTaskServiceUtils; + + @Autowired + private AuditCreateStackService auditCreateStack; + + @Autowired + private AuditDeleteStackService auditDeleteStack; + + @Autowired + private AuditQueryStackService auditQueryStack; + + @Autowired + private CreateInventoryTask createInventory; + + @Autowired + private StackService stackService; + + @Autowired + private PollService pollService; + + @Autowired + private RollbackService rollbackService; + + @PostConstruct + public void auditAddAAIInventory() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("InventoryAddAudit").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(auditCreateStack::executeExternalTask).open(); + } + } + + @PostConstruct + public void auditDeleteAAIInventory() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("InventoryDeleteAudit").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(auditDeleteStack::executeExternalTask).open(); + } + } + + @PostConstruct + public void auditQueryInventory() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("InventoryQueryAudit").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(auditQueryStack::executeExternalTask).open(); + } + } + + @PostConstruct + public void auditAAIInventory() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("InventoryCreate").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(createInventory::executeExternalTask).open(); + } + } + + @PostConstruct + public void openstackInvoker() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("OpenstackAdapterInvoke").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(stackService::executeExternalTask).open(); + } + } + + @PostConstruct + public void openstackPoller() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("OpenstackAdapterPolling").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(pollService::executeExternalTask).open(); + } + } + + @PostConstruct + public void openstackRollback() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("OpenstackAdapterRollback").lockDuration(externalTaskServiceUtils.getLockDuration()) + .handler(rollbackService::executeExternalTask).open(); + } + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAudit.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAudit.java new file mode 100644 index 0000000000..7622c1ba28 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAudit.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 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.so.adapters.tasks.audit; + +import org.onap.so.client.aai.AAIResourcesClient; + +public class AbstractAudit { + + private AAIResourcesClient aaiClient; + + protected AAIResourcesClient getAaiClient() { + if (aaiClient == null) + return new AAIResourcesClient(); + else + return aaiClient; + } + + protected void setAaiClient(AAIResourcesClient aaiResource) { + aaiClient = aaiResource; + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAuditService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAuditService.java new file mode 100644 index 0000000000..2f7155bffc --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AbstractAuditService.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 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.so.adapters.tasks.audit; + +import java.util.Optional; +import org.onap.so.objects.audit.AAIObjectAudit; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.onap.so.utils.ExternalTaskUtils; +import org.onap.so.utils.RetrySequenceLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public abstract class AbstractAuditService extends ExternalTaskUtils { + + private static final Logger logger = LoggerFactory.getLogger(AbstractAuditService.class); + + + + protected static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI = + "Unable to find all VServers and L-Interaces in A&AI"; + + protected static final String UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK = "Unable to find VServers in Openstack"; + + @Autowired + public Environment env; + + public AbstractAuditService() { + super(RetrySequenceLevel.LONG); + } + + /** + * @param auditHeatStackFailed + * @param auditList + * @return + */ + protected boolean didCreateAuditFail(Optional auditList) { + if (auditList.get().getAuditList() != null && !auditList.get().getAuditList().isEmpty()) { + if (logger.isInfoEnabled()) { + logger.info("Audit Results: {}", auditList.get().toString()); + } + return auditList.get().getAuditList().stream().filter(auditObject -> !auditObject.isDoesObjectExist()) + .findFirst().map(v -> true).orElse(false); + } else { + return false; + } + } + + /** + * @param auditHeatStackFailed + * @param auditList + * @return + */ + protected boolean didDeleteAuditFail(AAIObjectAuditList auditList) { + if (auditList.getAuditList() != null && !auditList.getAuditList().isEmpty()) { + if (logger.isInfoEnabled()) { + logger.info("Audit Results: {}", auditList.toString()); + } + return auditList.getAuditList().stream().filter(AAIObjectAudit::isDoesObjectExist).findFirst() + .map(v -> true).orElse(false); + } else { + return false; + } + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditCreateStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditCreateStackService.java new file mode 100644 index 0000000000..874823a7cd --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditCreateStackService.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.so.adapters.tasks.audit; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.audit.beans.AuditInventory; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.onap.so.utils.RetrySequenceLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AuditCreateStackService extends AbstractAuditService { + + private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class); + + @Autowired + public HeatStackAudit heatStackAudit; + + @Autowired + public AuditMDCSetup mdcSetup; + + public AuditCreateStackService() { + super(); + } + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + AuditInventory auditInventory = externalTask.getVariable("auditInventory"); + Map variables = new HashMap<>(); + boolean success = false; + try { + Integer retryCount = externalTask.getRetries(); + logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount); + Optional auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(), + auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName()); + if (auditListOpt.isPresent()) { + auditListOpt.get().setAuditType("create"); + auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName()); + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get())); + success = !didCreateAuditFail(auditListOpt); + } + } catch (Exception e) { + logger.error("Error during audit of stack", e); + } + variables.put("auditIsSuccessful", success); + mdcSetup.setElapsedTime(); + String externalTaskId = externalTask.getId(); + if (success) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + logger.debug("The External Task Id: {} Successful", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + Integer retryCount = externalTask.getRetries(); + if (retryCount == null) { + logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", + externalTaskId, getRetrySequence().length); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, + UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000); + } else if (retryCount == 1) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", + externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, + UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1, + calculateRetryDelay(retryCount)); + } + logger.debug("The External Task Id: {} Failed", externalTaskId); + } + } + + + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDataService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDataService.java new file mode 100644 index 0000000000..2c76acdb1b --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDataService.java @@ -0,0 +1,77 @@ +package org.onap.so.adapters.tasks.audit; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import org.onap.so.audit.beans.AuditInventory; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.db.request.beans.RequestProcessingData; +import org.onap.so.db.request.client.RequestsDbClient; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; + +@Component +public class AuditDataService { + + @Autowired + private RequestsDbClient requestsDbClient; + + /** + * Checks to see if an entry already exist for the given heat stack and writes audit stack data to the request + * database if it doesn't. + * + * @throws JsonProcessingException + */ + public void writeStackDataToRequestDb(AuditInventory auditInventory, AAIObjectAuditList auditList) + throws JsonProcessingException { + List requestProcessingDataList = + requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(), + auditInventory.getHeatStackName(), "AuditStackData"); + if (requestProcessingDataList.isEmpty()) { + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + String auditListString = objectMapper.getMapper().writeValueAsString(auditList);; + + RequestProcessingData requestProcessingData = new RequestProcessingData(); + requestProcessingData.setSoRequestId(auditInventory.getMsoRequestId()); + requestProcessingData.setGroupingId(auditInventory.getVfModuleId()); + requestProcessingData.setName(auditInventory.getHeatStackName()); + requestProcessingData.setTag("AuditStackData"); + requestProcessingData.setValue(auditListString); + + requestsDbClient.saveRequestProcessingData(requestProcessingData); + } + } + + /** + * Retrieves audit stack data from the request database. + * + * @throws IOException + * @throws JsonMappingException + * @throws JsonParseException + */ + public Optional getStackDataFromRequestDb(AuditInventory auditInventory) + throws JsonParseException, JsonMappingException, IOException { + + List requestProcessingDataList = + requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(), + auditInventory.getHeatStackName(), "AuditStackData"); + if (!requestProcessingDataList.isEmpty()) { + RequestProcessingData requestProcessingData = requestProcessingDataList.get(0); + String auditListString = requestProcessingData.getValue(); + + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + AAIObjectAuditList auditList = + objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class); + + return Optional.of(auditList); + } else { + return Optional.empty(); + } + } + + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDeleteStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDeleteStackService.java new file mode 100644 index 0000000000..55cde4d174 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditDeleteStackService.java @@ -0,0 +1,122 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * 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.so.adapters.tasks.audit; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.audit.beans.AuditInventory; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class AuditDeleteStackService extends AbstractAuditService { + + private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class); + + @Autowired + protected HeatStackAudit heatStackAudit; + + @Autowired + protected AuditVServer auditVservers; + + @Autowired + protected AuditDataService auditDataService; + + @Autowired + protected Environment env; + + @Autowired + public AuditMDCSetup mdcSetup; + + public AuditDeleteStackService() { + super(); + } + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + + mdcSetup.setupMDC(externalTask); + AuditInventory auditInventory = externalTask.getVariable("auditInventory"); + Map variables = new HashMap<>(); + boolean success = false; + try { + Integer retryCount = externalTask.getRetries(); + logger.info("Executing External Task Delete Audit Inventory. Retry Number: {}", retryCount); + Optional auditListOpt = auditDataService.getStackDataFromRequestDb(auditInventory); + if (auditListOpt.isPresent()) { + auditVservers.auditVservers(auditListOpt.get()); + } else { + logger.debug("Auditing Vservers based on vf module relationships"); + auditListOpt = auditVservers.auditVserversThroughRelationships(auditInventory.getGenericVnfId(), + auditInventory.getHeatStackName()); + } + auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName()); + auditListOpt.get().setAuditType("delete"); + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get())); + success = !didDeleteAuditFail(auditListOpt.get()); + + } catch (Exception e) { + logger.error("Error during audit of stack", e); + } + variables.put("auditIsSuccessful", success); + mdcSetup.setElapsedTime(); + String externalTaskId = externalTask.getId(); + if (success) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + logger.debug("The External Task Id: {} Successful", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + Integer retryCount = externalTask.getRetries(); + if (retryCount == null) { + logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", + externalTaskId, getRetrySequence().length); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, + UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000); + } else if (retryCount == 1) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", + externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, + UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1, + calculateRetryDelay(retryCount)); + } + logger.debug("The External Task Id: {} Failed", externalTaskId); + } + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditQueryStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditQueryStackService.java new file mode 100644 index 0000000000..ebef4425eb --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditQueryStackService.java @@ -0,0 +1,85 @@ +package org.onap.so.adapters.tasks.audit; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.audit.beans.AuditInventory; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AuditQueryStackService extends AbstractAuditService { + + private static final Logger logger = LoggerFactory.getLogger(AuditQueryStackService.class); + + @Autowired + protected HeatStackAudit heatStackAudit; + + @Autowired + protected AuditDataService auditDataService; + + @Autowired + public AuditMDCSetup mdcSetup; + + public AuditQueryStackService() { + super(); + } + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + AuditInventory auditInventory = externalTask.getVariable("auditInventory"); + boolean success = false; + Map variables = new HashMap<>(); + try { + Integer retryCount = externalTask.getRetries(); + logger.info("Executing External Task Query Audit Inventory. Audit Inventory: {} \n Retry Number: {}", + auditInventory.toString(), retryCount); + + Optional auditList = heatStackAudit.queryHeatStack(auditInventory.getCloudOwner(), + auditInventory.getCloudRegion(), auditInventory.getTenantId(), auditInventory.getHeatStackName()); + + if (auditList.isPresent()) { + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + success = true; + auditDataService.writeStackDataToRequestDb(auditInventory, auditList.get()); + } + mdcSetup.setElapsedTime(); + String externalTaskId = externalTask.getId(); + if (success) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + logger.debug("The External Task {} was Successful", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + if (retryCount == null) { + logger.debug("The External Task {} Failed. Setting Retries to Default Start Value: {}", + externalTaskId, getRetrySequence().length); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, + UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, getRetrySequence().length, 10000); + } else if (retryCount == 1) { + externalTaskService.complete(externalTask, variables); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.debug("The External Task {} Failed. All Retries Exhausted", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else { + logger.debug("The External Task {} Failed. Decrementing Retries to {} , Retry Delay: ", + externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); + externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, + UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK, retryCount - 1, calculateRetryDelay(retryCount)); + } + logger.debug("The External Task {} Failed", externalTaskId); + } + } catch (Exception e) { + logger.error("Error during audit query of stack", e); + } + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditVServer.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditVServer.java new file mode 100644 index 0000000000..63853c34fa --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/AuditVServer.java @@ -0,0 +1,171 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 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.so.adapters.tasks.audit; + +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.onap.aai.domain.yang.LInterface; +import org.onap.aai.domain.yang.VfModule; +import org.onap.aai.domain.yang.VfModules; +import org.onap.aai.domain.yang.Vserver; +import org.onap.so.client.aai.AAIObjectPlurals; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.entities.AAIResultWrapper; +import org.onap.so.client.aai.entities.uri.AAIPluralResourceUri; +import org.onap.so.client.aai.entities.uri.AAIResourceUri; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.objects.audit.AAIObjectAudit; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Component +public class AuditVServer extends AbstractAudit { + private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class); + + public void auditVservers(AAIObjectAuditList aaiObjectAuditList) { + + aaiObjectAuditList.getAuditList().forEach(aaiObjectAudit -> { + boolean vserverExist = getAaiClient().exists(AAIUriFactory + .createResourceFromExistingURI(AAIObjectType.VSERVER, aaiObjectAudit.getResourceURI())); + aaiObjectAudit.setDoesObjectExist(vserverExist); + }); + } + + public Optional auditVserversThroughRelationships(String genericVnfId, String vfModuleName) { + AAIObjectAuditList auditList = new AAIObjectAuditList(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, genericVnfId) + .queryParam("vf-module-name", vfModuleName); + Optional wrapper = getAaiClient().getFirstWrapper(VfModules.class, VfModule.class, uri); + if (wrapper.isPresent() && wrapper.get().getRelationships().isPresent()) { + List relatedVservers = + wrapper.get().getRelationships().get().getRelatedUris(AAIObjectType.VSERVER); + if (!relatedVservers.isEmpty()) { + relatedVservers.forEach(vserverUri -> { + Optional vserver = getAaiClient().get(vserverUri).asBean(Vserver.class); + Vserver vServerShallow = new Vserver(); + BeanUtils.copyProperties(vserver, vServerShallow); + AAIObjectAudit vServerAudit = new AAIObjectAudit(); + vServerAudit.setAaiObject(vServerShallow); + vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); + vServerAudit.setDoesObjectExist(true); + auditList.getAuditList().add(vServerAudit); + }); + } + } + return Optional.of(auditList); + } + + public Optional auditVservers(Set vServersToAudit, String tenantId, String cloudOwner, + String cloudRegion) { + if (vServersToAudit == null || vServersToAudit.isEmpty()) { + return Optional.empty(); + } + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + vServersToAudit.stream().forEach(vserver -> { + try { + logger.debug("Vserver to Audit: {}", objectMapper.getMapper().writeValueAsString(vserver)); + } catch (JsonProcessingException e) { + logger.error("Json parse exception: ", e); + } + + }); + AAIObjectAuditList auditList = new AAIObjectAuditList(); + vServersToAudit.stream().forEach(vServer -> auditList.getAuditList() + .addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList())); + return Optional.of(auditList); + } + + private AAIObjectAuditList doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, + String cloudRegion) { + AAIObjectAuditList auditList = new AAIObjectAuditList(); + AAIObjectAudit vServerAudit = new AAIObjectAudit(); + AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion, + tenantId, vServer.getVserverId()); + Vserver vServerShallow = new Vserver(); + BeanUtils.copyProperties(vServer, vServerShallow, "LInterfaces"); + boolean vServerExists = getAaiClient().exists(vserverURI); + logger.info("v-server {} exists: {}", vServer.getVserverId(), vServerExists); + vServerAudit.setAaiObject(vServerShallow); + vServerAudit.setDoesObjectExist(vServerExists); + vServerAudit.setResourceURI(vserverURI.build()); + vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); + auditList.getAuditList().add(vServerAudit); + if (vServer.getLInterfaces() != null) { + vServer.getLInterfaces().getLInterface().stream().forEach(lInterface -> auditList.getAuditList().addAll( + doesLinterfaceExistinAAI(lInterface, vServer.getVserverId(), tenantId, cloudOwner, cloudRegion) + .getAuditList())); + } + return auditList; + } + + private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId, + String cloudOwner, String cloudRegion) { + AAIObjectAuditList auditList = new AAIObjectAuditList(); + AAIObjectAudit lInterfaceAudit = new AAIObjectAudit(); + AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, + cloudRegion, tenantId, vServerId, lInterface.getInterfaceName()); + Optional queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI); + if (queriedLInterface.isPresent()) { + lInterfaceAudit.setDoesObjectExist(true); + lInterface.setInterfaceName(lInterface.getInterfaceName()); + } + lInterfaceAudit.setAaiObject(lInterface); + lInterfaceAudit.setResourceURI(linterfaceURI.build()); + lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName()); + auditList.getAuditList().add(lInterfaceAudit); + logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(), + lInterface.getInterfaceName(), lInterfaceAudit.isDoesObjectExist()); + + if (lInterface.getLInterfaces() != null) { + lInterface.getLInterfaces().getLInterface().stream() + .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface, + lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion))); + } + logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces", + lInterface.getInterfaceId()); + + return auditList; + } + + private AAIObjectAudit doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId, + String tenantId, String cloudOwner, String cloudRegion) { + logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {} exists", + subInterface.getInterfaceName(), linterfaceName, vServerId); + AAIObjectAudit subInterfaceAudit = new AAIObjectAudit(); + + + AAIResourceUri subInterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, cloudOwner, + cloudRegion, tenantId, vServerId, linterfaceName, subInterface.getInterfaceName()); + subInterfaceAudit.setResourceURI(subInterfaceURI.build()); + boolean doesExist = getAaiClient().exists(subInterfaceURI); + logger.info("sub-l-interface-id:{} exists: {}", subInterface.getInterfaceId(), doesExist); + subInterfaceAudit.setAaiObject(subInterface); + subInterfaceAudit.setDoesObjectExist(doesExist); + subInterfaceAudit.setAaiObjectType(AAIObjectType.SUB_L_INTERFACE.typeName()); + return subInterfaceAudit; + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/HeatStackAudit.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/HeatStackAudit.java new file mode 100644 index 0000000000..1554881193 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/audit/HeatStackAudit.java @@ -0,0 +1,327 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2019 IBM. + * ================================================================================ + * 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.so.adapters.tasks.audit; + +import java.net.URI; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.onap.aai.domain.yang.LInterface; +import org.onap.aai.domain.yang.LInterfaces; +import org.onap.aai.domain.yang.Vlan; +import org.onap.aai.domain.yang.Vlans; +import org.onap.aai.domain.yang.Vserver; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.objects.audit.AAIObjectAudit; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.onap.so.openstack.utils.MsoHeatUtils; +import org.onap.so.openstack.utils.MsoNeutronUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import com.woorea.openstack.heat.model.Link; +import com.woorea.openstack.heat.model.Resource; +import com.woorea.openstack.heat.model.Resources; +import com.woorea.openstack.heat.model.Stack; +import com.woorea.openstack.quantum.model.Port; + +@Component +public class HeatStackAudit { + + private static final String RESOURCES = "/resources"; + + protected static final Logger logger = LoggerFactory.getLogger(HeatStackAudit.class); + + private static final String EXCEPTION_MSG = "Error finding Path from Self Link"; + + @Autowired + protected MsoHeatUtils heat; + + @Autowired + protected MsoNeutronUtils neutron; + + @Autowired + protected AuditVServer auditVservers; + + public Optional queryHeatStack(String cloudOwner, String cloudRegion, String tenantId, + String heatStackName) { + try { + logger.debug("Fetching Top Level Stack Information"); + Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName, 3); + List novaResources = resources.getList().stream() + .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); + if (novaResources.isEmpty()) + return Optional.of(new AAIObjectAuditList()); + else { + Set vserversToAudit = createVserverSet(novaResources); + AAIObjectAuditList aaiObjectAuditList = new AAIObjectAuditList(); + vserversToAudit.stream().forEach(vServer -> aaiObjectAuditList.getAuditList() + .add(createAAIObjectAudit(cloudOwner, cloudRegion, tenantId, vServer))); + return Optional.of(aaiObjectAuditList); + } + } catch (Exception e) { + logger.error("Error during query stack resources", e); + return Optional.of(new AAIObjectAuditList()); + } + } + + public Optional auditHeatStack(String cloudRegion, String cloudOwner, String tenantId, + String heatStackName) { + try { + logger.debug("Fetching Top Level Stack Information"); + Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName, 3); + List novaResources = resources.getList().stream() + .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); + List resourceGroups = resources.getList().stream() + .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType()) && p.getName().contains("subinterfaces")) + .collect(Collectors.toList()); + List> neutronPortDetails = retrieveNeutronPortDetails(resources, cloudRegion, tenantId); + if (novaResources.isEmpty()) + return Optional.of(new AAIObjectAuditList()); + else { + Set vserversToAudit = createVserverSet(resources, novaResources, neutronPortDetails); + Set vserversWithSubInterfaces = + processSubInterfaces(cloudRegion, tenantId, resourceGroups, vserversToAudit); + return auditVservers.auditVservers(vserversWithSubInterfaces, tenantId, cloudOwner, cloudRegion); + } + } catch (Exception e) { + logger.error("Error during auditing stack resources", e); + return Optional.empty(); + } + } + + protected Set processSubInterfaces(String cloudRegion, String tenantId, List resourceGroups, + Set vServersToAudit) throws Exception { + for (Resource resourceGroup : resourceGroups) { + processResourceGroups(cloudRegion, tenantId, vServersToAudit, resourceGroup); + } + return vServersToAudit; + } + + protected void processResourceGroups(String cloudRegion, String tenantId, Set vServersWithLInterface, + Resource resourceGroup) throws Exception { + Optional stackLink = + resourceGroup.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny(); + if (stackLink.isPresent()) { + try { + Optional path = extractResourcePathFromHref(stackLink.get().getHref()); + if (path.isPresent()) { + logger.debug("Fetching nested Resource Stack Information"); + Resources nestedResourceGroupResources = + heat.executeHeatClientRequest(path.get(), cloudRegion, tenantId, Resources.class); + processNestedResourceGroup(cloudRegion, tenantId, vServersWithLInterface, + nestedResourceGroupResources); + } else + throw new Exception(EXCEPTION_MSG); + } catch (Exception e) { + logger.error("Error Parsing Link to obtain Path", e); + throw new Exception(EXCEPTION_MSG); + } + } + } + + protected void processNestedResourceGroup(String cloudRegion, String tenantId, Set vServersWithLInterface, + Resources nestedResourceGroupResources) throws Exception { + for (Resource resourceGroupNested : nestedResourceGroupResources) { + Optional subInterfaceStackLink = + resourceGroupNested.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny(); + if (subInterfaceStackLink.isPresent()) { + addSubInterface(cloudRegion, tenantId, vServersWithLInterface, subInterfaceStackLink.get()); + } + } + } + + protected void addSubInterface(String cloudRegion, String tenantId, Set vServersWithLInterface, + Link subInterfaceStackLink) throws Exception { + Optional resourcePath = extractResourcePathFromHref(subInterfaceStackLink.getHref()); + Optional stackPath = extractStackPathFromHref(subInterfaceStackLink.getHref()); + if (resourcePath.isPresent() && stackPath.isPresent()) { + logger.debug("Fetching nested Sub-Interface Stack Information"); + Stack subinterfaceStack = + heat.executeHeatClientRequest(stackPath.get(), cloudRegion, tenantId, Stack.class); + Resources subinterfaceResources = + heat.executeHeatClientRequest(resourcePath.get(), cloudRegion, tenantId, Resources.class); + if (subinterfaceStack != null) { + addSubInterfaceToVserver(vServersWithLInterface, subinterfaceStack, subinterfaceResources); + } + } else + throw new Exception(EXCEPTION_MSG); + + } + + protected void addSubInterfaceToVserver(Set vServersWithLInterface, Stack subinterfaceStack, + Resources subinterfaceResources) throws Exception { + String parentNeutronPortId = (String) subinterfaceStack.getParameters().get("port_interface"); + logger.debug("Parent neutron Port: {} on SubInterface: {}", parentNeutronPortId, subinterfaceStack.getId()); + for (Vserver auditVserver : vServersWithLInterface) + for (LInterface lInterface : auditVserver.getLInterfaces().getLInterface()) + + if (parentNeutronPortId.equals(lInterface.getInterfaceId())) { + logger.debug("Found Parent Port on VServer: {} on Port: {}", auditVserver.getVserverId(), + lInterface.getInterfaceId()); + Resource contrailVm = subinterfaceResources.getList().stream() + .filter(resource -> "OS::ContrailV2::VirtualMachineInterface".equals(resource.getType())) + .findAny().orElse(null); + if (contrailVm == null) { + throw new Exception("Cannnot find Contrail Virtual Machine Interface on Stack: " + + subinterfaceStack.getId()); + } + LInterface subInterface = new LInterface(); + subInterface.setInterfaceId(contrailVm.getPhysicalResourceId()); + subInterface.setIsPortMirrored(false); + subInterface.setInMaint(false); + subInterface.setIsIpUnnumbered(false); + String macAddr = (String) subinterfaceStack.getParameters().get("mac_address"); + subInterface.setMacaddr(macAddr); + + String namePrefix = (String) subinterfaceStack.getParameters().get("subinterface_name_prefix"); + Integer vlanIndex = Integer.parseInt((String) subinterfaceStack.getParameters().get("counter")); + String vlanTagList = (String) subinterfaceStack.getParameters().get("vlan_tag"); + List subInterfaceVlanTagList = Arrays.asList(vlanTagList.split(",")); + subInterface.setInterfaceName(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex)); + subInterface.setVlans(new Vlans()); + Vlan vlan = new Vlan(); + vlan.setInMaint(false); + vlan.setIsIpUnnumbered(false); + vlan.setVlanIdInner(Long.parseLong(subInterfaceVlanTagList.get(vlanIndex))); + vlan.setVlanInterface(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex)); + subInterface.getVlans().getVlan().add(vlan); + if (lInterface.getLInterfaces() == null) + lInterface.setLInterfaces(new LInterfaces()); + + lInterface.getLInterfaces().getLInterface().add(subInterface); + } else + logger.debug("Did Not Find Parent Port on VServer: {} Parent Port: SubInterface: {}", + auditVserver.getVserverId(), lInterface.getInterfaceId(), subinterfaceStack.getId()); + } + + protected Set createVserverSet(Resources resources, List novaResources, + List> neutronPortDetails) { + Set vserversToAudit = new HashSet<>(); + for (Resource novaResource : novaResources) { + Vserver auditVserver = new Vserver(); + auditVserver.setLInterfaces(new LInterfaces()); + auditVserver.setVserverId(novaResource.getPhysicalResourceId()); + Stream filteredNeutronPorts = filterNeutronPorts(novaResource, neutronPortDetails); + filteredNeutronPorts.forEach(port -> { + LInterface lInterface = new LInterface(); + lInterface.setInterfaceId(port.getId()); + lInterface.setInterfaceName(port.getName()); + auditVserver.getLInterfaces().getLInterface().add(lInterface); + }); + vserversToAudit.add(auditVserver); + } + return vserversToAudit; + } + + protected Set createVserverSet(List novaResources) { + Set vserversToAudit = new HashSet<>(); + for (Resource novaResource : novaResources) { + Vserver auditVserver = new Vserver(); + auditVserver.setLInterfaces(new LInterfaces()); + auditVserver.setVserverId(novaResource.getPhysicalResourceId()); + vserversToAudit.add(auditVserver); + } + return vserversToAudit; + } + + protected AAIObjectAudit createAAIObjectAudit(String cloudOwner, String cloudRegion, String tenantId, + Vserver vServer) { + AAIObjectAudit aaiObjectAudit = new AAIObjectAudit(); + Vserver vServerShallow = new Vserver(); + BeanUtils.copyProperties(vServer, vServerShallow); + aaiObjectAudit.setAaiObject(vServerShallow); + aaiObjectAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName()); + aaiObjectAudit.setResourceURI(AAIUriFactory + .createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion, tenantId, vServer.getVserverId()) + .build()); + + return aaiObjectAudit; + } + + /** + * @param novaResource Single openstack resource that is of type Nova + * @param neutronPorts List of Neutron ports created within the stack + * @return Filtered list of neutron ports taht relate to the nova server in openstack + */ + protected Stream filterNeutronPorts(Resource novaResource, List> neutronPorts) { + List filteredNeutronPorts = + neutronPorts.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()); + return filteredNeutronPorts.stream() + .filter(port -> port.getDeviceId().equalsIgnoreCase(novaResource.getPhysicalResourceId())); + } + + /** + * @param resources Resource stream created by the stack in openstack + * @param cloudSiteId Unique site id to identify which openstack we talk to + * @param tenantId The tenant within the cloud we are talking to where resouces exist + * @return List of optional neutron ports found within the cloud site and tenant + */ + protected List> retrieveNeutronPortDetails(Resources resources, String cloudSiteId, + String tenantId) { + return resources.getList().parallelStream().filter(resource -> "OS::Neutron::Port".equals(resource.getType())) + .map(resource -> neutron.getNeutronPort(resource.getPhysicalResourceId(), tenantId, cloudSiteId)) + .collect(Collectors.toList()); + + } + + protected Optional extractResourcePathFromHref(String href) { + try { + Optional stackPath = extractStackPathFromHref(href); + if (stackPath.isPresent()) { + return Optional.of(stackPath.get() + RESOURCES); + } else + return Optional.empty(); + } catch (Exception e) { + logger.error("Error parsing URI", e); + } + return Optional.empty(); + } + + protected Optional extractStackPathFromHref(String href) { + try { + URI uri = new URI(href); + Pattern p = Pattern.compile("/stacks.*"); + Matcher m = p.matcher(uri.getPath()); + if (m.find()) { + return Optional.of(m.group()); + } else + return Optional.empty(); + } catch (Exception e) { + logger.error("Error parsing URI", e); + } + return Optional.empty(); + } + + +} + diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java new file mode 100644 index 0000000000..a3cb99e3af --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 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.so.adapters.tasks.inventory; + +import java.util.Optional; +import java.util.stream.Stream; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.objects.audit.AAIObjectAudit; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.springframework.stereotype.Component; + +@Component +public class CreateAAIInventory { + + private AAIResourcesClient aaiClient; + + public void createInventory(AAIObjectAuditList auditList) throws InventoryException { + if (didAuditFailVserverLInterfaces(auditList)) { + throw new InventoryException("Audit failed for VServer or LInterface cannot write Sub-Interfaces"); + } + auditList.getAuditList().parallelStream() + .filter(auditObject -> !auditObject.isDoesObjectExist() + && AAIObjectType.SUB_L_INTERFACE.typeName().equals(auditObject.getAaiObjectType())) + .forEach(auditObject -> getAaiClient().createIfNotExists(AAIUriFactory.createResourceFromExistingURI( + AAIObjectType.fromTypeName(auditObject.getAaiObjectType()), auditObject.getResourceURI()), + Optional.of(auditObject.getAaiObject()))); + } + + + /** + * @param auditHeatStackFailed + * @param auditList + * @return + */ + protected boolean didAuditFailVserverLInterfaces(AAIObjectAuditList auditList) { + Stream issue = auditList.getAuditList().stream() + .filter(auditObject -> auditObject.getAaiObjectType().equals(AAIObjectType.VSERVER.typeName()) + || auditObject.getAaiObjectType().equals(AAIObjectType.L_INTERFACE.typeName())); + + return issue.filter(auditObject -> !auditObject.isDoesObjectExist()).findFirst().map(v -> true).orElse(false); + } + + protected AAIResourcesClient getAaiClient() { + if (aaiClient == null) + return new AAIResourcesClient(); + else + return aaiClient; + } + + protected void setAaiClient(AAIResourcesClient aaiResource) { + aaiClient = aaiResource; + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateInventoryTask.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateInventoryTask.java new file mode 100644 index 0000000000..04dcd9ff61 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateInventoryTask.java @@ -0,0 +1,122 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * + * Copyright (C) 2019 IBM + * ================================================================================ + * 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.so.adapters.tasks.inventory; + +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.objects.audit.AAIObjectAuditList; +import org.onap.so.utils.ExternalTaskUtils; +import org.onap.so.utils.RetrySequenceLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class CreateInventoryTask extends ExternalTaskUtils { + + private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI"; + + private static final Logger logger = LoggerFactory.getLogger(CreateInventoryTask.class); + + private static final String AAI_INVENTORY_FAILURE = "AAIInventoryFailure"; + + @Autowired + CreateAAIInventory createInventory; + + @Autowired + public AuditMDCSetup mdcSetup; + + public CreateInventoryTask() { + super(RetrySequenceLevel.SHORT); + } + + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + boolean success = true; + boolean inventoryException = false; + String auditInventoryString = externalTask.getVariable("auditInventoryResult"); + AAIObjectAuditList auditInventory = null; + String externalTaskId = externalTask.getId(); + try { + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class); + } catch (Exception e) { + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.error("Error Parsing Audit Results", e); + } + mdcSetup.setElapsedTime(); + if (auditInventory != null) { + Integer retryCount = externalTask.getRetries(); + try { + logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory, + retryCount); + createInventory.createInventory(auditInventory); + } catch (InventoryException e) { + logger.error("Error during inventory of stack", e); + success = false; + inventoryException = true; + } catch (Exception e) { + logger.error("Error during inventory of stack", e); + success = false; + } + mdcSetup.setElapsedTime(); + if (success) { + externalTaskService.complete(externalTask); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + logger.debug("The External Task Id: {} Successful", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + mdcSetup.clearClientMDCs(); + } else if (inventoryException) { + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.debug("The External Task Id: {} Failed, Retry not needed", externalTaskId); + externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); + } else { + if (retryCount == null) { + logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", + externalTaskId, getRetrySequence().length); + externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, + UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000); + } else if (retryCount != null && retryCount - 1 == 0) { + externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); + mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString()); + logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); + } else { + logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", + externalTaskId, retryCount - 1, calculateRetryDelay(retryCount)); + externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, + UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, retryCount - 1, calculateRetryDelay(retryCount)); + } + logger.debug("The External Task Id: {} Failed", externalTaskId); + } + } else { + logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTaskId); + externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE); + } + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/InventoryException.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/InventoryException.java new file mode 100644 index 0000000000..7df3822961 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/InventoryException.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * 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.so.adapters.tasks.inventory; + +public class InventoryException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 912652713891488731L; + + public InventoryException(String errorMessage) { + super(errorMessage); + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/PollService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/PollService.java new file mode 100644 index 0000000000..6e181c4696 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/PollService.java @@ -0,0 +1,191 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.so.adapters.tasks.orchestration; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.xml.bind.JAXB; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.apache.commons.lang3.mutable.MutableBoolean; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.so.adapters.vnfrest.CreateVfModuleRequest; +import org.onap.so.adapters.vnfrest.CreateVolumeGroupRequest; +import org.onap.so.adapters.vnfrest.DeleteVfModuleRequest; +import org.onap.so.adapters.vnfrest.DeleteVolumeGroupRequest; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.openstack.exceptions.MsoException; +import org.onap.so.openstack.utils.MsoHeatUtils; +import org.onap.so.utils.ExternalTaskUtils; +import org.onap.so.utils.RetrySequenceLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import com.woorea.openstack.heat.model.Stack; + +@Component +public class PollService extends ExternalTaskUtils { + + private static final Logger logger = LoggerFactory.getLogger(PollService.class); + + @Autowired + private MsoHeatUtils msoHeatUtils; + + @Autowired + private AuditMDCSetup mdcSetup; + + public PollService() { + super(RetrySequenceLevel.SHORT); + } + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + logger.trace("Executing External Task Poll Service"); + Map variables = new HashMap<>(); + MutableBoolean success = new MutableBoolean(); + String errorMessage = null; + try { + String xmlRequest = externalTask.getVariable("vnfAdapterTaskRequest"); + if (xmlRequest != null) { + Optional requestType = findRequestType(xmlRequest); + if ("createVolumeGroupRequest".equals(requestType.get())) { + determineCreateVolumeGroupStatus(xmlRequest, externalTask, success); + } else if ("createVfModuleRequest".equals(requestType.get())) { + determineCreateVfModuleStatus(xmlRequest, externalTask, success); + } else if ("deleteVfModuleRequest".equals(requestType.get())) { + logger.debug("Executing External Task Poll Service for Delete Vf Module"); + DeleteVfModuleRequest req = + JAXB.unmarshal(new StringReader(xmlRequest), DeleteVfModuleRequest.class); + pollDeleteResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } else if ("deleteVolumeGroupRequest".equals(requestType.get())) { + logger.debug("Executing External Task Poll Service for Delete Volume Group"); + DeleteVolumeGroupRequest req = + JAXB.unmarshal(new StringReader(xmlRequest), DeleteVolumeGroupRequest.class); + pollDeleteResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } + } + } catch (Exception e) { + logger.error("Error during External Task Poll Service", e); + errorMessage = e.getMessage(); + } + + variables.put("OpenstackPollSuccess", success.booleanValue()); + variables.put("openstackAdapterErrorMessage", errorMessage); + if (success.isTrue()) { + externalTaskService.complete(externalTask, variables); + logger.debug("The External Task Id: {} Successful", externalTask.getId()); + } else { + if (externalTask.getRetries() == null) { + logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", + externalTask.getId(), getRetrySequence().length); + externalTaskService.handleFailure(externalTask, "errorMessage", "errorDetails", + getRetrySequence().length, 10000); + } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) { + logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId()); + externalTaskService.complete(externalTask, variables); + } else { + logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: {}", + externalTask.getId(), externalTask.getRetries() - 1, + calculateRetryDelay(externalTask.getRetries())); + externalTaskService.handleFailure(externalTask, "errorMessage", "errorDetails", + externalTask.getRetries() - 1, calculateRetryDelay(externalTask.getRetries())); + } + } + } + + private void determineCreateVolumeGroupStatus(String xmlRequest, ExternalTask externalTask, MutableBoolean success) + throws MsoException { + CreateVolumeGroupRequest req = JAXB.unmarshal(new StringReader(xmlRequest), CreateVolumeGroupRequest.class); + boolean pollRollbackStatus = externalTask.getVariable("PollRollbackStatus"); + if (pollRollbackStatus) { + logger.debug("Executing External Task Poll Service for Rollback Create Volume Group"); + pollDeleteResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } else { + pollCreateResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } + } + + private void determineCreateVfModuleStatus(String xmlRequest, ExternalTask externalTask, MutableBoolean success) + throws MsoException { + CreateVfModuleRequest req = JAXB.unmarshal(new StringReader(xmlRequest), CreateVfModuleRequest.class); + boolean pollRollbackStatus = externalTask.getVariable("PollRollbackStatus"); + if (pollRollbackStatus) { + logger.debug("Executing External Task Poll Service for Rollback Create Vf Module"); + pollDeleteResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } else { + logger.debug("Executing External Task Poll Service for Create Vf Module"); + pollCreateResource(req.getCloudSiteId(), req.getTenantId(), externalTask, success); + } + } + + private void pollCreateResource(String cloudSiteId, String tenantId, ExternalTask externalTask, + MutableBoolean success) throws MsoException { + Stack currentStack = createCurrentStack(externalTask.getVariable("stackId")); + Stack stack = + msoHeatUtils.pollStackForStatus(1, currentStack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId, false); + msoHeatUtils.postProcessStackCreate(stack, false, 0, false, cloudSiteId, tenantId, null); + success.setTrue(); + } + + private void pollDeleteResource(String cloudSiteId, String tenantId, ExternalTask externalTask, + MutableBoolean success) throws MsoException { + Stack currentStack = createCurrentStack(externalTask.getVariable("stackId")); + Stack stack = + msoHeatUtils.pollStackForStatus(1, currentStack, "DELETE_IN_PROGRESS", cloudSiteId, tenantId, true); + if (stack != null) { // if stack is null it was not found and no need to do post process + msoHeatUtils.postProcessStackDelete(stack); + } + success.setTrue(); + } + + protected Optional findRequestType(String xmlString) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + org.w3c.dom.Document doc; + doc = builder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); + return Optional.of(doc.getDocumentElement().getNodeName()); + } catch (Exception e) { + logger.error("Error Finding Request Type", e); + return Optional.empty(); + } + } + + private Stack createCurrentStack(String canonicalStackId) { + Stack currentStack = new Stack(); + String stackName = canonicalStackId; + if (canonicalStackId.contains("/")) { + String[] stacks = canonicalStackId.split("/"); + stackName = stacks[0]; + currentStack.setId(stacks[1]); + } + currentStack.setStackName(stackName); + return currentStack; + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/RollbackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/RollbackService.java new file mode 100644 index 0000000000..c302293169 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/RollbackService.java @@ -0,0 +1,92 @@ +package org.onap.so.adapters.tasks.orchestration; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.xml.bind.JAXB; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.ws.Holder; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.so.adapters.vnf.MsoVnfAdapterImpl; +import org.onap.so.adapters.vnfrest.CreateVfModuleRequest; +import org.onap.so.adapters.vnfrest.CreateVolumeGroupRequest; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.utils.ExternalTaskUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class RollbackService extends ExternalTaskUtils { + + private static final Logger logger = LoggerFactory.getLogger(RollbackService.class); + + @Autowired + private MsoVnfAdapterImpl vnfAdapterImpl; + + @Autowired + private AuditMDCSetup mdcSetup; + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + logger.trace("Executing External Task Rollback Service"); + Map variables = new HashMap<>(); + boolean success = false; + boolean pollRollbackStatus = false; + try { + String xmlRequest = externalTask.getVariable("vnfAdapterTaskRequest"); + if (xmlRequest != null) { + Optional requestType = findRequestType(xmlRequest); + if ("createVolumeGroupRequest".equals(requestType.get())) { + logger.debug("Executing External Task Rollback Service for Create Volume Group"); + CreateVolumeGroupRequest req = + JAXB.unmarshal(new StringReader(xmlRequest), CreateVolumeGroupRequest.class); + vnfAdapterImpl.deleteVnf(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + req.getVolumeGroupName(), req.getMsoRequest(), false); + pollRollbackStatus = true; + success = true; + } else if ("createVfModuleRequest".equals(requestType.get())) { + logger.debug("Executing External Task Rollback Service for Create Vf Module"); + CreateVfModuleRequest req = + JAXB.unmarshal(new StringReader(xmlRequest), CreateVfModuleRequest.class); + vnfAdapterImpl.deleteVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + req.getVfModuleName(), req.getVnfId(), req.getVfModuleId(), req.getModelCustomizationUuid(), + req.getMsoRequest(), new Holder<>()); + pollRollbackStatus = true; + success = true; + } + } + } catch (Exception e) { + logger.error("Error during External Task Rollback Service", e); + } + variables.put("OpenstackRollbackSuccess", success); + variables.put("rollbackPerformed", true); + variables.put("PollRollbackStatus", pollRollbackStatus); + if (success) { + externalTaskService.complete(externalTask, variables); + logger.debug("The External Task Id: {} Successful", externalTask.getId()); + } else { + logger.debug("The External Task Id: {} Failed. Not Retrying", externalTask.getId()); + externalTaskService.complete(externalTask, variables); + } + } + + protected Optional findRequestType(String xmlString) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + org.w3c.dom.Document doc; + doc = builder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); + return Optional.of(doc.getDocumentElement().getNodeName()); + } catch (Exception e) { + logger.error("Error Finding Request Type", e); + return Optional.empty(); + } + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/StackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/StackService.java new file mode 100644 index 0000000000..34952a056b --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/orchestration/StackService.java @@ -0,0 +1,207 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.so.adapters.tasks.orchestration; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.xml.bind.JAXB; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.ws.Holder; +import org.apache.commons.lang3.mutable.MutableBoolean; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.so.adapters.vnf.MsoVnfAdapterImpl; +import org.onap.so.adapters.vnf.exceptions.VnfException; +import org.onap.so.adapters.vnfrest.CreateVfModuleRequest; +import org.onap.so.adapters.vnfrest.CreateVfModuleResponse; +import org.onap.so.adapters.vnfrest.CreateVolumeGroupRequest; +import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse; +import org.onap.so.adapters.vnfrest.DeleteVfModuleRequest; +import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse; +import org.onap.so.adapters.vnfrest.DeleteVolumeGroupRequest; +import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse; +import org.onap.so.adapters.vnfrest.VfModuleRollback; +import org.onap.so.adapters.vnfrest.VolumeGroupRollback; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.openstack.beans.VnfRollback; +import org.onap.so.utils.ExternalTaskUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class StackService extends ExternalTaskUtils { + + private static final Logger logger = LoggerFactory.getLogger(StackService.class); + + @Autowired + private MsoVnfAdapterImpl vnfAdapterImpl; + + @Autowired + private AuditMDCSetup mdcSetup; + + public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + Map variables = new HashMap<>(); + mdcSetup.setupMDC(externalTask); + String xmlRequest = externalTask.getVariable("vnfAdapterTaskRequest"); + logger.debug("Executing External Task Stack Service. {}", xmlRequest); + MutableBoolean success = new MutableBoolean(); + MutableBoolean backout = new MutableBoolean(); + String response = ""; + Holder canonicalStackId = new Holder<>(); + String errorMessage = ""; + try { + if (xmlRequest != null) { + Holder> outputs = new Holder<>(); + Holder vnfRollback = new Holder<>(); + Optional requestType = findRequestType(xmlRequest); + if ("createVolumeGroupRequest".equals(requestType.get())) { + logger.debug("Executing External Task Stack Service For Create Volume Group"); + response = createVolumeGroup(xmlRequest, outputs, vnfRollback, canonicalStackId, backout, success); + } else if ("createVfModuleRequest".equals(requestType.get())) { + logger.debug("Executing External Task Stack Service For Create Vf Module"); + response = createVfModule(xmlRequest, outputs, vnfRollback, canonicalStackId, backout, success); + } else if ("deleteVfModuleRequest".equals(requestType.get())) { + logger.debug("Executing External Task Stack Service For Delete Vf Module"); + response = deleteVfModule(xmlRequest, outputs, vnfRollback, canonicalStackId, backout, success); + } else if ("deleteVolumeGroupRequest".equals(requestType.get())) { + logger.debug("Executing External Task Stack Service For Delete Volume Group"); + response = deleteVolumeGroup(xmlRequest, outputs, vnfRollback, canonicalStackId, backout, success); + } + } + } catch (Exception e) { + logger.error("Error during External Task Stack Service", e); + errorMessage = e.getMessage(); + } + variables.put("backout", backout.booleanValue()); + variables.put("WorkflowResponse", response); + variables.put("OpenstackInvokeSuccess", success.booleanValue()); + variables.put("stackId", canonicalStackId.value); + variables.put("openstackAdapterErrorMessage", errorMessage); + variables.put("PollRollbackStatus", false); + variables.put("rollbackPerformed", false); + variables.put("OpenstackRollbackSuccess", false); + variables.put("OpenstackPollSuccess", false); + + if (success.isTrue()) { + externalTaskService.complete(externalTask, variables); + logger.debug("The External Task Id: {} Successful", externalTask.getId()); + } else { + logger.debug("The External Task Id: {} Failed. Not Retrying", externalTask.getId()); + externalTaskService.complete(externalTask, variables); + } + } + + private String createVolumeGroup(String xmlRequest, Holder> outputs, + Holder vnfRollback, Holder canonicalStackId, MutableBoolean backout, + MutableBoolean success) throws VnfException { + Holder stackId = new Holder<>(); + CreateVolumeGroupRequest req = JAXB.unmarshal(new StringReader(xmlRequest), CreateVolumeGroupRequest.class); + String completeVnfVfModuleType = req.getVnfType() + "::" + req.getVfModuleType(); + vnfAdapterImpl.createVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + completeVnfVfModuleType, req.getVnfVersion(), "", req.getVolumeGroupName(), "", "VOLUME", null, null, + req.getModelCustomizationUuid(), req.getVolumeGroupParams(), false, true, req.getEnableBridge(), + req.getMsoRequest(), stackId, outputs, vnfRollback); + success.setTrue(); + backout.setValue(!req.getSuppressBackout()); + VolumeGroupRollback rb = new VolumeGroupRollback(req.getVolumeGroupId(), stackId.value, + vnfRollback.value.getVnfCreated(), req.getTenantId(), req.getCloudOwner(), req.getCloudSiteId(), + req.getMsoRequest(), req.getMessageId()); + canonicalStackId.value = stackId.value; + CreateVolumeGroupResponse createResponse = new CreateVolumeGroupResponse(req.getVolumeGroupId(), stackId.value, + vnfRollback.value.getVnfCreated(), outputs.value, rb, req.getMessageId()); + return createResponse.toXmlString(); + } + + private String createVfModule(String xmlRequest, Holder> outputs, + Holder vnfRollback, Holder canonicalStackId, MutableBoolean backout, + MutableBoolean success) throws VnfException { + Holder stackId = new Holder<>(); + CreateVfModuleRequest req = JAXB.unmarshal(new StringReader(xmlRequest), CreateVfModuleRequest.class); + String completeVnfVfModuleType = req.getVnfType() + "::" + req.getVfModuleType(); + vnfAdapterImpl.createVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + completeVnfVfModuleType, req.getVnfVersion(), req.getVnfId(), req.getVfModuleName(), + req.getVfModuleId(), req.getRequestType(), req.getVolumeGroupStackId(), req.getBaseVfModuleStackId(), + req.getModelCustomizationUuid(), req.getVfModuleParams(), false, false, req.getEnableBridge(), + req.getMsoRequest(), stackId, outputs, vnfRollback); + success.setTrue(); + backout.setValue(req.getBackout()); + canonicalStackId.value = stackId.value; + VfModuleRollback modRollback = + new VfModuleRollback(vnfRollback.value, req.getVfModuleId(), stackId.value, req.getMessageId()); + CreateVfModuleResponse createResponse = new CreateVfModuleResponse(req.getVnfId(), req.getVfModuleId(), + stackId.value, Boolean.TRUE, outputs.value, modRollback, req.getMessageId()); + return createResponse.toXmlString(); + } + + private String deleteVfModule(String xmlRequest, Holder> outputs, + Holder vnfRollback, Holder canonicalStackId, MutableBoolean backout, + MutableBoolean success) throws VnfException { + backout.setFalse(); + DeleteVfModuleRequest req = JAXB.unmarshal(new StringReader(xmlRequest), DeleteVfModuleRequest.class); + vnfAdapterImpl.deleteVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + req.getVfModuleStackId(), req.getVnfId(), req.getVfModuleId(), req.getModelCustomizationUuid(), + req.getMsoRequest(), outputs); + success.setTrue(); + if (outputs != null && outputs.value != null) { + canonicalStackId.value = outputs.value.get("canonicalStackId"); + } else { + canonicalStackId.value = req.getVfModuleStackId(); + } + DeleteVfModuleResponse deleteResponse = new DeleteVfModuleResponse(req.getVnfId(), req.getVfModuleId(), + Boolean.TRUE, req.getMessageId(), outputs.value); + return deleteResponse.toXmlString(); + } + + private String deleteVolumeGroup(String xmlRequest, Holder> outputs, + Holder vnfRollback, Holder canonicalStackId, MutableBoolean backout, + MutableBoolean success) throws VnfException { + backout.setFalse(); + DeleteVolumeGroupRequest req = JAXB.unmarshal(new StringReader(xmlRequest), DeleteVolumeGroupRequest.class); + + vnfAdapterImpl.deleteVnf(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), + req.getVolumeGroupStackId(), req.getMsoRequest(), false); + success.setTrue(); + canonicalStackId.value = req.getVolumeGroupStackId(); + DeleteVolumeGroupResponse deleteResponse = new DeleteVolumeGroupResponse(true, req.getMessageId()); + return deleteResponse.toXmlString(); + } + + protected Optional findRequestType(String xmlString) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + org.w3c.dom.Document doc; + doc = builder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); + return Optional.of(doc.getDocumentElement().getNodeName()); + } catch (Exception e) { + logger.error("Error Finding Request Type", e); + return Optional.empty(); + } + } +} diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/MsoVnfAdapterImpl.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/MsoVnfAdapterImpl.java index 60c5a0ca2a..3a2d3bc5eb 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/MsoVnfAdapterImpl.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/MsoVnfAdapterImpl.java @@ -322,6 +322,42 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter { return; } + /** + * This is the "Delete VNF" web service implementation. It will delete a VNF by name or ID in the specified cloud + * and tenant. + * + * The method has no outputs. + * + * @param cloudSiteId CLLI code of the cloud site in which to delete + * @param cloudOwner cloud owner of the cloud region in which to delete + * @param tenantId Openstack tenant identifier + * @param vnfName VNF Name or Openstack ID + * @param msoRequest Request tracking information for logs + */ + public void deleteVnf(String cloudSiteId, String cloudOwner, String tenantId, String vnfName, MsoRequest msoRequest, + boolean pollStackStatus) throws VnfException { + + logger.debug("Deleting VNF {} in {}", vnfName, cloudSiteId + "/" + tenantId); + + try { + msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfName, pollStackStatus, 118); + } catch (MsoException me) { + me.addContext(DELETE_VNF); + // Failed to query the Stack due to an openstack exception. + // Convert to a generic VnfException + String error = + "Delete VNF: " + vnfName + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me; + logger.error(LoggingAnchor.NINE, MessageEnum.RA_DELETE_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId, + tenantId, OPENSTACK, DELETE_VNF, ErrorCode.DataError.getValue(), "Exception - " + DELETE_VNF, me); + logger.debug(error); + throw new VnfException(me); + } + + // On success, nothing is returned. + return; + } + + /** * This web service endpoint will rollback a previous Create VNF operation. A rollback object is returned to the * client in a successful creation response. The client can pass that object as-is back to the rollbackVnf operation @@ -536,6 +572,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter { Map inputs, Boolean failIfExists, Boolean backout, Boolean enableBridge, MsoRequest msoRequest, Holder vnfId, Holder> outputs, Holder rollback) throws VnfException { + boolean pollForCompletion = false; String vfModuleName = vnfName; String vfModuleType = vnfType; String vfVersion = vnfVersion; @@ -1050,8 +1087,9 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter { } if (msoHeatUtils != null) { heatStack = msoHeatUtils.createStack(cloudSiteId, cloudOwner, tenantId, vfModuleName, null, - template, goldenInputs, true, heatTemplate.getTimeoutMinutes(), newEnvironmentString, - nestedTemplatesChecked, heatFilesObjects, backout.booleanValue(), failIfExists); + template, goldenInputs, pollForCompletion, heatTemplate.getTimeoutMinutes(), + newEnvironmentString, nestedTemplatesChecked, heatFilesObjects, backout.booleanValue(), + failIfExists); if (msoRequest.getRequestId() != null) { msoHeatUtils.updateResourceStatus(msoRequest.getRequestId(), heatStack.isOperationPerformed() ? VF_EXIST_STATUS_MESSAGE : VF_CREATED_STATUS_MESSAGE); @@ -1135,10 +1173,15 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter { } try { - StackInfo stackInfo = - msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfName, true, timeoutMinutes); + StackInfo currentStack = + msoHeatUtils.deleteStack(tenantId, cloudOwner, cloudSiteId, vnfName, false, timeoutMinutes); + if (currentStack != null && outputs != null && outputs.value != null) { + logger.debug("Adding canonical stack id to outputs " + currentStack.getCanonicalName()); + outputs.value.put("canonicalStackId", currentStack.getCanonicalName()); + } msoHeatUtils.updateResourceStatus(msoRequest.getRequestId(), - stackInfo.isOperationPerformed() ? VF_DELETED_STATUS_MESSAGE : VF_NOT_EXIST_STATUS_MESSAGE); + currentStack.isOperationPerformed() ? VF_DELETED_STATUS_MESSAGE : VF_NOT_EXIST_STATUS_MESSAGE); + } catch (MsoException me) { me.addContext(DELETE_VNF); // Failed to query the Stack due to an openstack exception. diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRest.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRest.java index 0836f69ba7..5b78d2f066 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRest.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRest.java @@ -78,6 +78,7 @@ import io.swagger.annotations.ApiResponses; * can be produced/consumed. Set Accept: and Content-Type: headers appropriately. XML is the default. For testing, call * with cloudSiteId = ___TESTING___ To test exceptions, also set tenantId = ___TESTING___ */ +@Deprecated @Path("/v1/vnfs") @Api(value = "/v1/vnfs", description = "root of vnf adapters restful web service") @Transactional diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRestV2.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRestV2.java index 18ed9872a0..63b4c5d0f3 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRestV2.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VnfAdapterRestV2.java @@ -39,7 +39,6 @@ import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.xml.ws.Holder; -import org.onap.so.logger.LoggingAnchor; import org.apache.http.HttpStatus; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.onap.so.adapters.vnf.exceptions.VnfException; @@ -56,6 +55,7 @@ import org.onap.so.adapters.vnfrest.VfModuleExceptionResponse; import org.onap.so.adapters.vnfrest.VfModuleRollback; import org.onap.so.entity.MsoRequest; import org.onap.logging.filter.base.ErrorCode; +import org.onap.so.logger.LoggingAnchor; import org.onap.so.logger.MessageEnum; import org.onap.so.openstack.beans.VnfRollback; import org.onap.so.openstack.beans.VnfStatus; @@ -78,6 +78,7 @@ import io.swagger.annotations.ApiResponses; * * V2 incorporates run-time selection of sub-orchestrator implementation (Heat or Cloudify) based on the target cloud. */ +@Deprecated @Path("/v2/vnfs") @Api(value = "/v2/vnfs", description = "root of vnf adapters restful web service v2") @Component @@ -189,10 +190,7 @@ public class VnfAdapterRestV2 { } String cloudsite = req.getCloudSiteId(); Holder> outputs = new Holder<>(); - if (cloudsite != null && !cloudsite.equals(TESTING_KEYWORD)) { - // vnfAdapter.deleteVnf (req.getCloudSiteId(), req.getTenantId(), req.getVfModuleStackId(), - // req.getMsoRequest()); - // Support different Adapter Implementations + if (cloudsite != null) { MsoVnfAdapter adapter = vnfAdapterRestUtils.getVnfAdapterImpl(mode, cloudsite); adapter.deleteVfModule(req.getCloudSiteId(), req.getCloudOwner(), req.getTenantId(), req.getVfModuleStackId(), req.getVnfId(), req.getVfModuleId(), diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VolumeAdapterRestV2.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VolumeAdapterRestV2.java index cfddbba34e..a424fa92fd 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VolumeAdapterRestV2.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/vnf/VolumeAdapterRestV2.java @@ -174,33 +174,26 @@ public class VolumeAdapterRestV2 { logger.debug("in createVfModuleVolumes - completeVnfVfModuleType={}", completeVnfVfModuleType); String cloudsiteId = req.getCloudSiteId(); - if (cloudsiteId != null && cloudsiteId.equals(TESTING_KEYWORD)) { - String tenant = req.getTenantId(); - if (tenant != null && tenant.equals(TESTING_KEYWORD)) { - throw new VnfException("testing."); - } - stackId.value = "479D3D8B-6360-47BC-AB75-21CC91981484"; - outputs.value = testMap(); - } else { - // Support different Adapter Implementations - MsoVnfAdapter vnfAdapter = vnfAdapterRestUtils.getVnfAdapterImpl(mode, cloudsiteId); - vnfAdapter.createVfModule(req.getCloudSiteId(), // cloudSiteId, - req.getCloudOwner(), // cloudOwner, - req.getTenantId(), // tenantId, - completeVnfVfModuleType, // vnfType, - req.getVnfVersion(), // vnfVersion, - "", // genericVnfId - req.getVolumeGroupName(), // vnfName, - "", // vfModuleId - "VOLUME", // requestType, - null, // volumeGroupHeatStackId, - null, // baseVfHeatStackId, - req.getModelCustomizationUuid(), req.getVolumeGroupParams(), // inputs, - req.getFailIfExists(), // failIfExists, - req.getSuppressBackout(), // backout, - req.getEnableBridge(), req.getMsoRequest(), // msoRequest, - stackId, outputs, vnfRollback); - } + + // Support different Adapter Implementations + MsoVnfAdapter vnfAdapter = vnfAdapterRestUtils.getVnfAdapterImpl(mode, cloudsiteId); + vnfAdapter.createVfModule(req.getCloudSiteId(), // cloudSiteId, + req.getCloudOwner(), // cloudOwner, + req.getTenantId(), // tenantId, + completeVnfVfModuleType, // vnfType, + req.getVnfVersion(), // vnfVersion, + "", // genericVnfId + req.getVolumeGroupName(), // vnfName, + "", // vfModuleId + "VOLUME", // requestType, + null, // volumeGroupHeatStackId, + null, // baseVfHeatStackId, + req.getModelCustomizationUuid(), req.getVolumeGroupParams(), // inputs, + req.getFailIfExists(), // failIfExists, + req.getSuppressBackout(), // backout, + req.getEnableBridge(), req.getMsoRequest(), // msoRequest, + stackId, outputs, vnfRollback); + VolumeGroupRollback rb = new VolumeGroupRollback(req.getVolumeGroupId(), stackId.value, vnfRollback.value.getVnfCreated(), req.getTenantId(), req.getCloudOwner(), req.getCloudSiteId(), -- cgit 1.2.3-korg