diff options
Diffstat (limited to 'adapters/mso-openstack-adapters/src/main/java')
11 files changed, 508 insertions, 238 deletions
diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAudit.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAudit.java new file mode 100644 index 0000000000..a2c117b3d9 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAudit.java @@ -0,0 +1,56 @@ +package org.onap.so.adapters.audit; + +import java.io.Serializable; +import java.net.URI; + +import org.onap.so.client.aai.AAIObjectType; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class AAIObjectAudit implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = -4560928512855386021L; + private boolean doesObjectExist = false; + private Object aaiObject; + private URI resourceURI; + private String aaiObjectType; + + @Override + public String toString() { + return new ToStringBuilder(this).append("doesObjectExist", doesObjectExist).append("aaiObject", aaiObject) + .append("resourceURI", resourceURI).append("aaiObjectType", aaiObjectType).toString(); + } + + public String getAaiObjectType() { + return aaiObjectType; + } + + public void setAaiObjectType(String aaiObjectType) { + this.aaiObjectType = aaiObjectType; + } + + public boolean isDoesObjectExist() { + return doesObjectExist; + } + + public void setDoesObjectExist(boolean doesObjectExist) { + this.doesObjectExist = doesObjectExist; + } + + public Object getAaiObject() { + return aaiObject; + } + + public void setAaiObject(Object aaiObject) { + this.aaiObject = aaiObject; + } + + public URI getResourceURI() { + return resourceURI; + } + public void setResourceURI(URI resourceURI) { + this.resourceURI = resourceURI; + } +}
\ No newline at end of file diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAuditList.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAuditList.java new file mode 100644 index 0000000000..a1a8d5b5ed --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AAIObjectAuditList.java @@ -0,0 +1,25 @@ +package org.onap.so.adapters.audit; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class AAIObjectAuditList implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 6712662349909726930L; + private List<AAIObjectAudit> auditList = new ArrayList<>(); + + @Override + public String toString() { + return new ToStringBuilder(this).append("auditList", auditList).toString(); + } + + public List<AAIObjectAudit> getAuditList() { + return auditList; + } + +}
\ No newline at end of file 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 66d8fbd47c..0000000000 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditDeleteStackService.java +++ /dev/null @@ -1,90 +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 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.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -public class AuditDeleteStackService { - - private static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI = "All VServers have not been deleted in A&AI"; - - private static final int[] RETRY_SEQUENCE = new int[] { 1, 1, 2, 3, 5, 8, 13, 20}; - - - private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class); - - @Autowired - public HeatStackAudit heatStackAudit; - - @Autowired - public Environment env; - - protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService){ - AuditInventory auditInventory = externalTask.getVariable("auditInventory"); - setupMDC(externalTask); - boolean success = false; - try { - logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,externalTask.getRetries()); - success=heatStackAudit.auditHeatStackDeleted(auditInventory.getCloudRegion(), auditInventory.getCloudOwner(), - auditInventory.getTenantId(), auditInventory.getHeatStackName()); - } catch (Exception e) { - logger.error("Error during audit of stack", e); - } - - if (success) { - externalTaskService.complete(externalTask); - 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(),RETRY_SEQUENCE.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, RETRY_SEQUENCE.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); - }else{ - logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", externalTask.getId(),externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries())); - 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, externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries())); - } - logger.debug("The External Task Id: {} Failed", externalTask.getId()); - } - } - private void setupMDC(ExternalTask externalTask) { - String msoRequestId = (String)externalTask.getVariable("mso-request-id"); - if(msoRequestId != null && !msoRequestId.isEmpty()) - MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId); - } - protected long calculateRetryDelay(int currentRetries){ - int retrySequence = RETRY_SEQUENCE.length - currentRetries; - long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier","6000")); - return RETRY_SEQUENCE[retrySequence] * retryMultiplier; - } -} 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 index 6ea14dcac7..499c1137c7 100644 --- 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 @@ -46,30 +46,10 @@ public class AuditStackService { public Environment env; @Autowired - private AuditCreateStackService auditCreateStack; - - @Autowired - private AuditDeleteStackService auditDeleteStack; + private AuditStackServiceData auditStack; @PostConstruct - public void auditAddAAIInventory() { - String auth = ""; - try { - auth = CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey")); - } catch (IllegalStateException | GeneralSecurityException e) { - logger.error("Error Decrypting Password", e); - } - ClientRequestInterceptor interceptor = new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), - auth); - ExternalTaskClient client = ExternalTaskClient.create() - .baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1).addInterceptor(interceptor) - .asyncResponseTimeout(120000).build(); - client.subscribe("InventoryAddAudit").lockDuration(60000) - .handler(auditCreateStack::executeExternalTask).open(); - } - - @PostConstruct - public void auditDeleteAAIInventory() { + public void auditAAIInventory() { String auth = ""; try { auth = CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey")); @@ -80,9 +60,9 @@ public class AuditStackService { auth); ExternalTaskClient client = ExternalTaskClient.create() .baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1).addInterceptor(interceptor) - .asyncResponseTimeout(120000).build(); - client.subscribe("InventoryDeleteAudit").lockDuration(60000) - .handler(auditDeleteStack::executeExternalTask).open(); + .asyncResponseTimeout(120000).backoffStrategy(new ExponentialBackoffStrategy(10000, 2, 120000)).build(); + client.subscribe("InventoryAudit").lockDuration(60000) + .handler(auditStack::executeExternalTask).open(); } } 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/AuditStackServiceData.java index 24980ae4e0..da6bea7dec 100644 --- 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/AuditStackServiceData.java @@ -22,26 +22,29 @@ 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component -public class AuditCreateStackService { +public class AuditStackServiceData { private 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"; - private static final int[] RETRY_SEQUENCE = new int[] { 1, 1, 2, 3, 5, 8, 13, 20}; - - - private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class); + private static final Logger logger = LoggerFactory.getLogger(AuditStackServiceData.class); @Autowired public HeatStackAudit heatStackAudit; @@ -51,35 +54,39 @@ public class AuditCreateStackService { protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService){ AuditInventory auditInventory = externalTask.getVariable("auditInventory"); + Map<String, Object> variables = new HashMap<>(); setupMDC(externalTask); boolean success = false; try { logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,externalTask.getRetries()); - success=heatStackAudit.auditHeatStackCreate(auditInventory.getCloudRegion(), auditInventory.getCloudOwner(), + Optional<AAIObjectAuditList> auditListOpt= heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(), auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName()); + if (auditListOpt.isPresent()) { + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get())); + success = !didAuditFail(auditListOpt); + } } catch (Exception e) { logger.error("Error during audit of stack", e); } - + variables.put("auditIsSuccessful", success); if (success) { - externalTaskService.complete(externalTask); + 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(),RETRY_SEQUENCE.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, RETRY_SEQUENCE.length, 10000); + logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", externalTask.getId(),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(externalTask.getRetries() != null && externalTask.getRetries()-1 == 0){ - logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId()); - externalTaskService.handleBpmnError(externalTask, "AuditAAIInventoryFailure", "Number of Retries Exceeded auditing inventory"); + 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, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries())); } logger.debug("The External Task Id: {} Failed", externalTask.getId()); } - - } private void setupMDC(ExternalTask externalTask) { String msoRequestId = externalTask.getVariable("mso-request-id"); @@ -87,8 +94,29 @@ public class AuditCreateStackService { MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId); } protected long calculateRetryDelay(int currentRetries){ - int retrySequence = RETRY_SEQUENCE.length - currentRetries; + int retrySequence = getRetrySequence().length - currentRetries; long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier","6000")); - return RETRY_SEQUENCE[retrySequence] * retryMultiplier; + return Integer.parseInt(getRetrySequence()[retrySequence]) * retryMultiplier; + } + + /** + * @param auditHeatStackFailed + * @param auditList + * @return + */ + protected boolean didAuditFail(Optional<AAIObjectAuditList> 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; + } } + public String[] getRetrySequence() { + return env.getProperty("mso.workflow.topics.retrySequence",String[].class); + } + } 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 index c81dac7c6f..519e1c92b1 100644 --- 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 @@ -24,97 +24,106 @@ import java.util.Optional; import java.util.Set; import org.onap.aai.domain.yang.LInterface; -import org.onap.aai.domain.yang.LInterfaces; 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.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; 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 boolean auditAllVserversDoExist(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) { + public Optional<AAIObjectAuditList> auditVservers(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) { if (vServersToAudit == null || vServersToAudit.isEmpty()){ - return false; + return Optional.empty(); } - return vServersToAudit.stream() - .filter(vServer -> !doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion,true)).findFirst() - .map(v -> false).orElse(true); - } - - public boolean auditAllVserversDoNotExist(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) { - if (vServersToAudit == null || vServersToAudit.isEmpty()){ - return true; - } - return vServersToAudit.stream() - .filter(vServer -> doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion,false)).findFirst() - .map(v -> false).orElse(true); + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + vServersToAudit.stream().forEach(vserver -> { + try { + logger.debug("Vserver to Audit: {}",objectMapper.getMapper().writeValueAsString(vserver)); + } catch (JsonProcessingException e) { + + } + }); + AAIObjectAuditList auditList = new AAIObjectAuditList(); + vServersToAudit.stream() + .forEach(vServer -> auditList.getAuditList().addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList())); + return Optional.of(auditList); } - private boolean doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, String cloudRegion, boolean checkLinterfaces) { + 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); - boolean doesExist = getAaiClient().exists(vserverURI); - logger.info("v-server {} exists: {}", vServer.getVserverId(), doesExist); - boolean allNeutronNetworksExist = true; - if (vServerExists && vServer.getLInterfaces() != null && checkLinterfaces) { - allNeutronNetworksExist = vServer.getLInterfaces() - .getLInterface().stream().filter(lInterface -> !doesLinterfaceExistinAAI(lInterface, - vServer.getVserverId(), tenantId, cloudOwner, cloudRegion)) - .findFirst().map(v -> false).orElse(true); + 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 vServerExists && allNeutronNetworksExist; + return auditList; } - private boolean doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId, + private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId, String cloudOwner, String cloudRegion) { - boolean doesLInterfaceExist = false; - boolean doSubInterfacesExist = true; + AAIObjectAuditList auditList = new AAIObjectAuditList(); + AAIObjectAudit lInterfaceAudit = new AAIObjectAudit(); AAIResourceUri linterfaceURI = AAIUriFactory - .createResourceUri(AAIObjectPlurals.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId) - .queryParam("interface-id", lInterface.getInterfaceId()); - Optional<LInterfaces> queriedLInterface = getAaiClient().get(LInterfaces.class, linterfaceURI); + .createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId, lInterface.getInterfaceName()); + Optional<LInterface> queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI); if (queriedLInterface.isPresent()) { - if (queriedLInterface.get().getLInterface().size() > 1) { - logger.error("Non-Unique LInterface Found stopping audit, L-Interface Id: " +lInterface.getInterfaceId()); - doesLInterfaceExist = false; - } else { - doesLInterfaceExist = true; - lInterface.setInterfaceName(queriedLInterface.get().getLInterface().get(0).getInterfaceName()); - } + lInterfaceAudit.setDoesObjectExist(true); + lInterface.setInterfaceName(lInterface.getInterfaceName()); } - logger.info("l-interface id:{} name: {} exists: {}", lInterface.getInterfaceId(), lInterface.getInterfaceName(), - doesLInterfaceExist); + LInterface lInterfaceShallow = new LInterface(); + BeanUtils.copyProperties(lInterface,lInterfaceShallow,"LInterfaces"); + 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 (doesLInterfaceExist && lInterface.getLInterfaces() != null) { - doSubInterfacesExist = lInterface.getLInterfaces().getLInterface() - .stream().filter(subInterface -> !doesSubInterfaceExistinAAI(subInterface, - lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion)) - .findFirst().map(v -> false).orElse(true); - } else - logger.debug("l-interface {} does not contain any sub-iterfaces", lInterface.getInterfaceId()); + 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 doesLInterfaceExist && doSubInterfacesExist; + return auditList; } - private boolean doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId, + 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.getInterfaceId(), linterfaceName, vServerId); - - AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectPlurals.SUB_L_INTERFACE, cloudOwner, - cloudRegion, tenantId, vServerId, linterfaceName) - .queryParam("interface-id", subInterface.getInterfaceId()); - - boolean doesExist = getAaiClient().exists(linterfaceURI); - logger.info("sub-l-interface {} exists: {}", subInterface.getInterfaceId(), doesExist); - return doesExist; + 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 index 72dee07379..31e913dc27 100644 --- 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 @@ -21,6 +21,7 @@ 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; @@ -32,6 +33,8 @@ 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.openstack.utils.MsoHeatUtils; import org.onap.so.openstack.utils.MsoNeutronUtils; @@ -55,61 +58,38 @@ public class HeatStackAudit { @Autowired protected MsoHeatUtils heat; - + @Autowired protected MsoNeutronUtils neutron; @Autowired protected AuditVServer auditVservers; - public boolean auditHeatStackCreate(String cloudRegion, String cloudOwner, String tenantId, String heatStackName) { + public Optional<AAIObjectAuditList> auditHeatStack(String cloudRegion, String cloudOwner, String tenantId, String heatStackName) { try { - return auditStack(cloudRegion,cloudOwner,tenantId,heatStackName,true); - } catch (Exception e) { - logger.error("Error during auditing stack resources", e); - return false; - } - } - - public boolean auditHeatStackDeleted(String cloudRegion, String cloudOwner, String tenantId, String heatStackName) { - try { - return auditStack(cloudRegion,cloudOwner,tenantId,heatStackName,false); + logger.debug("Fetching Top Level Stack Information"); + Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName); + List<Resource> novaResources = resources.getList().stream() + .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); + List<Resource> resourceGroups = resources.getList().stream() + .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType()) && p.getName().contains("subinterfaces")) + .collect(Collectors.toList()); + List<Optional<Port>> neutronPortDetails = retrieveNeutronPortDetails(resources, cloudRegion, tenantId); + if (novaResources.isEmpty()) + return Optional.of(new AAIObjectAuditList()); + else { + Set<Vserver> vserversToAudit = createVserverSet(resources, novaResources, neutronPortDetails); + Set<Vserver> 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 false; - } - } - - private boolean auditStack(String cloudRegion, String cloudOwner, String tenantId, String heatStackName,boolean isCreateAudit) throws Exception{ - logger.debug("Fetching Top Level Stack Information"); - Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName); - List<Resource> novaResources = extractNovaResources(resources); - if(novaResources.isEmpty()) - return true; - else{ - List<Optional<Port>> neutronPortDetails = retrieveNeutronPortDetails(resources,cloudRegion,tenantId); - List<Resource> resourceGroups = extractResourceGroups(resources); - Set<Vserver> vserversToAudit = createVserverSet(resources, novaResources,neutronPortDetails); - Set<Vserver> vserversWithSubInterfaces = processSubInterfaces(cloudRegion, tenantId, resourceGroups, - vserversToAudit); - if(isCreateAudit){ - return auditVservers.auditAllVserversDoExist(vserversWithSubInterfaces, tenantId, cloudOwner, cloudRegion); - }else{ - return auditVservers.auditAllVserversDoNotExist(vserversWithSubInterfaces, tenantId, cloudOwner, cloudRegion); - } + return Optional.empty(); } } - private List<Resource> extractResourceGroups(Resources resources) { - return resources.getList().stream() - .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType()) && p.getName().contains("subinterfaces")).collect(Collectors.toList()); - } - - private List<Resource> extractNovaResources(Resources resources) { - return resources.getList().stream() - .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList()); - } - protected Set<Vserver> processSubInterfaces(String cloudRegion, String tenantId, List<Resource> resourceGroups, Set<Vserver> vServersToAudit) throws Exception { for (Resource resourceGroup : resourceGroups) { @@ -137,7 +117,6 @@ public class HeatStackAudit { logger.error("Error Parsing Link to obtain Path", e); throw new Exception("Error finding Path from Self Link"); } - } } @@ -182,7 +161,24 @@ public class HeatStackAudit { } 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<String> 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()); @@ -197,11 +193,12 @@ public class HeatStackAudit { for (Resource novaResource : novaResources) { Vserver auditVserver = new Vserver(); auditVserver.setLInterfaces(new LInterfaces()); - auditVserver.setVserverId(novaResource.getPhysicalResourceId()); + auditVserver.setVserverId(novaResource.getPhysicalResourceId()); Stream<Port> 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); @@ -228,9 +225,9 @@ public class HeatStackAudit { * @return List of optional neutron ports found within the cloud site and tenant */ protected List<Optional<Port>> retrieveNeutronPortDetails(Resources resources,String cloudSiteId,String tenantId){ - return resources.getList().stream() + return resources.getList().parallelStream() .filter(resource -> "OS::Neutron::Port".equals(resource.getType())) - .map(resource -> neutron.getNeutronPort(resource.getPhysicalResourceId(),cloudSiteId,tenantId)).collect(Collectors.toList()); + .map(resource -> neutron.getNeutronPort(resource.getPhysicalResourceId(),tenantId,cloudSiteId)).collect(Collectors.toList()); } @@ -262,5 +259,6 @@ public class HeatStackAudit { 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 new file mode 100644 index 0000000000..efced9d9f9 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateAAIInventory.java @@ -0,0 +1,68 @@ +/*- + * ============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.aai.domain.yang.LInterface; +import org.onap.so.adapters.audit.AAIObjectAudit; +import org.onap.so.adapters.audit.AAIObjectAuditList; +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.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<AAIObjectAudit> 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 new file mode 100644 index 0000000000..b2eadaf3c8 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java @@ -0,0 +1,68 @@ +/*- + * ============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.security.GeneralSecurityException; + +import javax.annotation.PostConstruct; + +import org.camunda.bpm.client.ExternalTaskClient; +import org.camunda.bpm.client.backoff.ExponentialBackoffStrategy; +import org.camunda.bpm.client.interceptor.ClientRequestInterceptor; +import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider; +import org.onap.so.utils.CryptoUtils; +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 CreateInventoryService { + + private static final Logger logger = LoggerFactory.getLogger(CreateInventoryService.class); + + @Autowired + public Environment env; + + @Autowired + private CreateInventoryTask createInventory; + + @PostConstruct + public void auditAAIInventory() { + String auth = ""; + try { + auth = CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey")); + } catch (IllegalStateException | GeneralSecurityException e) { + logger.error("Error Decrypting Password", e); + } + ClientRequestInterceptor interceptor = new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), + auth); + ExternalTaskClient client = ExternalTaskClient.create() + .baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1).addInterceptor(interceptor) + .asyncResponseTimeout(120000).backoffStrategy(new ExponentialBackoffStrategy(10000, 2, 120000)).build(); + client.subscribe("InventoryCreate").lockDuration(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 new file mode 100644 index 0000000000..4a14b1460e --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryTask.java @@ -0,0 +1,114 @@ +/*- + * ============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; + +import java.io.IOException; + +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.adapters.audit.AAIObjectAuditList; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class CreateInventoryTask { + + 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); + + @Autowired + CreateAAIInventory createInventory; + + @Autowired + public Environment env; + + protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + boolean success = true; + String auditInventoryString = externalTask.getVariable("auditInventoryResult"); + GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider(); + AAIObjectAuditList auditInventory = null; + try { + auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class); + } catch (IOException e1) { + success = false; + } + setupMDC(externalTask); + + if (auditInventory != null) { + try { + logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory, + externalTask.getRetries()); + createInventory.createInventory(auditInventory); + } catch (Exception e) { + logger.error("Error during inventory of stack", e); + success = false; + } + if (success) { + externalTaskService.complete(externalTask); + 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, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, + UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, 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.handleBpmnError(externalTask, "AAIInventoryFailure"); + } else { + logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", + externalTask.getId(), externalTask.getRetries() - 1, + calculateRetryDelay(externalTask.getRetries())); + externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, + UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, externalTask.getRetries() - 1, + calculateRetryDelay(externalTask.getRetries())); + } + logger.debug("The External Task Id: {} Failed", externalTask.getId()); + } + } else { + logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTask.getId()); + externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure"); + } + } + + private void setupMDC(ExternalTask externalTask) { + String msoRequestId = (String)externalTask.getVariable("mso-request-id"); + if(msoRequestId != null && !msoRequestId.isEmpty()) + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId); + } + protected long calculateRetryDelay(int currentRetries){ + int retrySequence = getRetrySequence().length - currentRetries; + long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier","6000")); + return Integer.parseInt(getRetrySequence()[retrySequence]) * retryMultiplier; + } + + public String[] getRetrySequence() { + return env.getProperty("mso.workflow.topics.retrySequence",String[].class); + } +} 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 new file mode 100644 index 0000000000..22c6902ed2 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/InventoryException.java @@ -0,0 +1,14 @@ +package org.onap.so.adapters.inventory.create; + +public class InventoryException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 912652713891488731L; + + public InventoryException(String errorMessage) { + super(errorMessage); + } + +} |