diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-healing-lib')
2 files changed, 423 insertions, 373 deletions
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java index e1807bafad..3b716da1ca 100644 --- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java @@ -12,6 +12,9 @@ * 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. + * ================================================================================ + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.healing.healers; @@ -59,318 +62,293 @@ import static org.openecomp.core.zusammen.api.ZusammenUtil.*; public class NetworkPackageHealer implements Healer { - private static final byte[] EMPTY_DATA_BYTES = "{}".getBytes(); - private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidate"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidateContent" - + " under OrchestrationTemplateCandidate"; - - private static final String MISSING_VSP_MODEL_ERROR = - "Vsp with invalid structure: does not contain element VspModel"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplate" - + " under VspModel element"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateValidationData" - + " under OrchestrationTemplate element"; - - private final VendorSoftwareProductInfoDao vspInfoDao; - private final ZusammenAdaptor zusammenAdaptor; - private final CandidateService candidateService; - - public NetworkPackageHealer() { - this(VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(), - ZusammenAdaptorFactory.getInstance().createInterface(), CandidateServiceFactory - .getInstance().createInterface()); - } - - public NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, - ZusammenAdaptor zusammenAdaptor, - CandidateService candidateService) { - this.vspInfoDao = vspInfoDao; - this.zusammenAdaptor = zusammenAdaptor; - this.candidateService = candidateService; - } - - @Override - public boolean isHealingNeeded(String itemId, Version version) { - return OnboardingMethod.NetworkPackage.name() - .equals(vspInfoDao.get(new VspDetails(itemId, version)).getOnboardingMethod()) && - isVspMissingAddedElements(itemId, version); - } - - @Override - public void heal(String itemId, Version version) throws Exception { - SessionContext context = createSessionContext(); - ElementContext elementContext = new ElementContext(itemId, version.getId()); - - Element candidateElement = - getElement(context, elementContext, null, ElementType.OrchestrationTemplateCandidate, - MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR); - - Collection<Element> candidateSubs = - zusammenAdaptor.listElementData(context, elementContext, candidateElement.getElementId()); - - Id candidateValidationElementId = - creatIfAbsentCandidateValidationElementId(candidateSubs, context, - elementContext, candidateElement); - - Element orchestrationTemplateElement = getOrchestrationTemplateElement(context, elementContext); - - Collection<Element> orchestrationTemplateSubs = zusammenAdaptor - .listElementData(context, elementContext, orchestrationTemplateElement.getElementId()); - - Id structureElementId = createIfAbsentStructureElementId(orchestrationTemplateSubs, context, - elementContext, orchestrationTemplateElement); - - Element orchestrationTemplateValidationElement = getOrchestrationTemplateValidationElement - (orchestrationTemplateSubs); - - OrchestrationTemplateEntity orchestrationTemplateEntity = - getOrchestrationTemplateEntity(orchestrationTemplateElement, - orchestrationTemplateValidationElement); - - if (StringUtils.isEmpty(orchestrationTemplateEntity.getFileSuffix())) { - return; + private static final byte[] EMPTY_DATA_BYTES = "{}".getBytes(); + private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidate"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidateContent" + + " under OrchestrationTemplateCandidate"; + + private static final String MISSING_VSP_MODEL_ERROR = + "Vsp with invalid structure: does not contain element VspModel"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplate" + " under VspModel element"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateValidationData" + + " under OrchestrationTemplate element"; + + private final VendorSoftwareProductInfoDao vspInfoDao; + private final ZusammenAdaptor zusammenAdaptor; + private final CandidateEntityBuilder candidateEntityBuilder; + + public NetworkPackageHealer() { + this(VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(), + ZusammenAdaptorFactory.getInstance().createInterface(), + CandidateServiceFactory.getInstance().createInterface()); } - Element candidateContentElement = getCandidateContentElement(candidateSubs); - - VspDetails vspDetails = vspInfoDao.get(new VspDetails(itemId, version)); - if (isEqual(orchestrationTemplateEntity, - getCandidateData(candidateElement, candidateContentElement))) { - - if (isProcessedEntityValid(orchestrationTemplateEntity)) { - emptyStructureElementAndSub(context, elementContext, candidateElement.getElementId(), - ElementType.OrchestrationTemplateCandidate, candidateContentElement.getElementId(), - ElementType.OrchestrationTemplateCandidateContent); - - populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), - structureElementId, vspDetails, context, elementContext); - } else { - emptyStructureElementAndSub(context, elementContext, - orchestrationTemplateElement.getElementId(), ElementType.OrchestrationTemplate, - orchestrationTemplateValidationElement.getElementId(), - ElementType.OrchestrationTemplateValidationData); - - populateCandidateValidationData(context, elementContext, candidateValidationElementId, - orchestrationTemplateEntity.getValidationData()); - } - } else { - populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), - structureElementId, vspDetails, context, elementContext); + private NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, ZusammenAdaptor zusammenAdaptor, + CandidateService candidateService) { + this(vspInfoDao, zusammenAdaptor, new CandidateEntityBuilder(candidateService)); } - } - - private boolean isVspMissingAddedElements(String vspId, Version version) { - SessionContext context = createSessionContext(); - ElementContext elementContext = new ElementContext(vspId, version.getId()); - - return zusammenAdaptor.listElementsByName(context, elementContext, null, - ElementType.OrchestrationTemplateCandidate.name()).stream() - .noneMatch(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() - .equals(candidateSub.getInfo().getName())); - } - - private boolean isEqual(OrchestrationTemplateEntity orchestrationTemplateEntity, - OrchestrationTemplateCandidateData orchestrationTemplateCandidateData) { - return orchestrationTemplateEntity.getFileName() - .equals(orchestrationTemplateCandidateData.getFileName()) && - orchestrationTemplateEntity.getFileSuffix() - .equals(orchestrationTemplateCandidateData.getFileSuffix()); - } - - private boolean isProcessedEntityValid(OrchestrationTemplateEntity orchestrationTemplateEntity) { - return !orchestrationTemplateEntity.getValidationData().contains(ErrorLevel.ERROR.name()); - } - - private void populateCandidateValidationData(SessionContext context, - ElementContext elementContext, - Id candidateValidationElementId, - String validationData) { - ZusammenElement candidateValidationElement = - buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, - Action.UPDATE); - candidateValidationElement.setElementId(candidateValidationElementId); - candidateValidationElement.setData(new ByteArrayInputStream(validationData.getBytes())); - - zusammenAdaptor.saveElement(context, elementContext, candidateValidationElement, - "Healed Orchestration Template Candidate Validation data"); - } - - private void populateOrchestrationTemplateStructure(InputStream processedFile, - Id structureElementId, VspDetails vspDetails, - SessionContext context, - ElementContext elementContext) - throws Exception { - byte[] byteData = FileUtils.toByteArray(processedFile); - FileContentHandler contentMap = - CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteData); - OrchestrationTemplateCandidateData orchestrationTemplateEntityData = - new CandidateEntityBuilder(candidateService) - .buildCandidateEntityFromZip(vspDetails, byteData, contentMap, null); - String fileDataStructure = orchestrationTemplateEntityData.getFilesDataStructure(); - - ZusammenElement orchestrationTemplateStructure = - buildStructuralElement(ElementType.OrchestrationTemplateStructure, Action.UPDATE); - orchestrationTemplateStructure.setElementId(structureElementId); - orchestrationTemplateStructure.setData(new ByteArrayInputStream(fileDataStructure.getBytes())); - - zusammenAdaptor.saveElement(context, elementContext, orchestrationTemplateStructure, - "Healed Orchestration Template Structure"); - } - - private Element getOrchestrationTemplateElement(SessionContext context, - ElementContext elementContext) { - ElementInfo vspModelElement = zusammenAdaptor - .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name()) - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_VSP_MODEL_ERROR).build())); - - return getElement(context, elementContext, vspModelElement.getId(), - ElementType.OrchestrationTemplate, MISSING_ORCHESTRATION_TEMPLATE_ERROR); - } - - private Element getOrchestrationTemplateValidationElement( - Collection<Element> orchestrationTemplateSubs) { - return orchestrationTemplateSubs.stream() - .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateValidationData.name() - .equals(orchestrationTemplateSub.getInfo().getName())) - .findFirst() - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR).build())); - - } - - private Element getCandidateContentElement(Collection<Element> candidateSubs) { - return candidateSubs.stream() - .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateContent.name() - .equals(candidateSub.getInfo().getName())) - .findFirst() - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR).build())); - } - - private Id createIfAbsentStructureElementId(Collection<Element> orchestrationTemplateSubs, - SessionContext context, ElementContext elementContext, - Element orchestrationTemplateElement) { - return orchestrationTemplateSubs.stream() - .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateStructure.name() - .equals(orchestrationTemplateSub.getInfo().getName())) - .findFirst().map(Element::getElementId) - .orElse(addStructureSubElement(context, elementContext, - ElementType.OrchestrationTemplateStructure, - orchestrationTemplateElement.getElementId())); - } - - private Id creatIfAbsentCandidateValidationElementId(Collection<Element> candidateSubs, - SessionContext context, ElementContext - elementContext, Element candidateElement) { - return candidateSubs.stream() - .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() - .equals(candidateSub.getInfo().getName())) - .findFirst().map(Element::getElementId) - .orElse(addStructureSubElement(context, elementContext, - ElementType.OrchestrationTemplateCandidateValidationData, - candidateElement.getElementId())); - } - - private Element getElement(SessionContext context, ElementContext elementContext, - Id parentElementId, ElementType elementType, String errorMessage) { - return zusammenAdaptor - .getElementByName(context, elementContext, parentElementId, elementType.name()) - .orElseThrow(() -> new CoreException( - new ErrorCode.ErrorCodeBuilder().withMessage(errorMessage).build())); - } - - private OrchestrationTemplateEntity getOrchestrationTemplateEntity( - Element orchestrationTemplateElement, Element validationDataElement) { - OrchestrationTemplateEntity orchestrationTemplateEntity = new OrchestrationTemplateEntity(); - - if (isNotEmpty(orchestrationTemplateElement.getData())) { - orchestrationTemplateEntity.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.getData()))); + + NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, ZusammenAdaptor zusammenAdaptor, + CandidateEntityBuilder candidateEntityBuilder) { + this.vspInfoDao = vspInfoDao; + this.zusammenAdaptor = zusammenAdaptor; + this.candidateEntityBuilder = candidateEntityBuilder; } - orchestrationTemplateEntity.setFileSuffix( - validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); - orchestrationTemplateEntity.setFileName( - validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); - if (isNotEmpty(validationDataElement.getData())) { - orchestrationTemplateEntity - .setValidationData(new String(FileUtils.toByteArray(validationDataElement.getData()))); + + @Override + public boolean isHealingNeeded(String itemId, Version version) { + return OnboardingMethod.NetworkPackage.name() + .equals(vspInfoDao.get(new VspDetails(itemId, version)).getOnboardingMethod()) + && isVspMissingAddedElements(itemId, version); } - return orchestrationTemplateEntity; - } - - private OrchestrationTemplateCandidateData getCandidateData(Element candidateElement, - Element candidateContentElement) { - OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); - candidateData - .setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.getData()))); - candidateData - .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateContentElement.getData()))); - candidateData.setFileSuffix( - candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); - candidateData.setFileName( - candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); - return candidateData; - } - - private Id addStructureSubElement(SessionContext context, ElementContext elementContext, - ElementType elementType, Id parentElementId) { - ZusammenElement newElement = buildStructuralElement(elementType, Action.CREATE); - - ZusammenElement parentElement = buildElement(parentElementId, Action.IGNORE); - parentElement.addSubElement(newElement); - - return zusammenAdaptor.saveElement(context, elementContext, parentElement, - String.format("Add element %s under element id %s", elementType.name(), parentElementId)) - .getSubElements().iterator().next().getElementId(); - } - - private void emptyStructureElementAndSub(SessionContext context, ElementContext elementContext, - Id elementId, ElementType elementType, Id subElementId, - ElementType subElementType) { - ZusammenElement subElement = buildStructuralElement(subElementType, Action.UPDATE); - subElement.setElementId(subElementId); - subElement.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); - - ZusammenElement element = buildStructuralElement(elementType, Action.UPDATE); - element.setElementId(elementId); - element.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); - element.addSubElement(subElement); - - zusammenAdaptor.saveElement(context, elementContext, element, - String.format("Empty element %s and its sub element %s", elementType.name(), - subElementType.name())); - } - - public enum InfoPropertyName { - FILE_SUFFIX("fileSuffix"), - FILE_NAME("fileName"); - - private String val; - - InfoPropertyName(String val) { - this.val = val; + + @Override + public void heal(String itemId, Version version) throws Exception { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(itemId, version.getId()); + + Element candidateElement = getElement(context, elementContext, null, ElementType.OrchestrationTemplateCandidate, + MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR); + + Collection<Element> candidateSubs = + zusammenAdaptor.listElementData(context, elementContext, candidateElement.getElementId()); + + Id candidateValidationElementId = + creatIfAbsentCandidateValidationElementId(candidateSubs, context, elementContext, candidateElement); + + Element orchestrationTemplateElement = getOrchestrationTemplateElement(context, elementContext); + + Collection<Element> orchestrationTemplateSubs = + zusammenAdaptor.listElementData(context, elementContext, orchestrationTemplateElement.getElementId()); + + Id structureElementId = createIfAbsentStructureElementId(orchestrationTemplateSubs, context, elementContext, + orchestrationTemplateElement); + + Element orchestrationTemplateValidationElement = + getOrchestrationTemplateValidationElement(orchestrationTemplateSubs); + + OrchestrationTemplateEntity orchestrationTemplateEntity = + getOrchestrationTemplateEntity(orchestrationTemplateElement, orchestrationTemplateValidationElement); + + if (StringUtils.isEmpty(orchestrationTemplateEntity.getFileSuffix())) { + return; + } + + Element candidateContentElement = getCandidateContentElement(candidateSubs); + + VspDetails vspDetails = vspInfoDao.get(new VspDetails(itemId, version)); + if (isEqual(orchestrationTemplateEntity, getCandidateData(candidateElement, candidateContentElement))) { + + if (isProcessedEntityValid(orchestrationTemplateEntity)) { + emptyStructureElementAndSub(context, elementContext, candidateElement.getElementId(), + ElementType.OrchestrationTemplateCandidate, candidateContentElement.getElementId(), + ElementType.OrchestrationTemplateCandidateContent); + + populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), structureElementId, + vspDetails, context, elementContext); + } else { + emptyStructureElementAndSub(context, elementContext, orchestrationTemplateElement.getElementId(), + ElementType.OrchestrationTemplate, orchestrationTemplateValidationElement.getElementId(), + ElementType.OrchestrationTemplateValidationData); + + populateCandidateValidationData(context, elementContext, candidateValidationElementId, + orchestrationTemplateEntity.getValidationData()); + } + } else { + populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), structureElementId, + vspDetails, context, elementContext); + } + } + + private boolean isVspMissingAddedElements(String vspId, Version version) { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(vspId, version.getId()); + + return zusammenAdaptor.listElementsByName(context, elementContext, null, + ElementType.OrchestrationTemplateCandidate.name()).stream().noneMatch( + candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() + .equals(candidateSub.getInfo().getName())); + } + + private boolean isEqual(OrchestrationTemplateEntity orchestrationTemplateEntity, + OrchestrationTemplateCandidateData orchestrationTemplateCandidateData) { + return orchestrationTemplateEntity.getFileName().equals(orchestrationTemplateCandidateData.getFileName()) + && orchestrationTemplateEntity.getFileSuffix() + .equals(orchestrationTemplateCandidateData.getFileSuffix()); } - private String getVal() { - return val; + private boolean isProcessedEntityValid(OrchestrationTemplateEntity orchestrationTemplateEntity) { + return !orchestrationTemplateEntity.getValidationData().contains(ErrorLevel.ERROR.name()); } - } - private boolean isNotEmpty(InputStream elementData) { - byte[] byteElementData; - if (Objects.isNull(elementData)) { - return false; + private void populateCandidateValidationData(SessionContext context, ElementContext elementContext, + Id candidateValidationElementId, String validationData) { + ZusammenElement candidateValidationElement = + buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE); + candidateValidationElement.setElementId(candidateValidationElementId); + candidateValidationElement.setData(new ByteArrayInputStream(validationData.getBytes())); + + zusammenAdaptor.saveElement(context, elementContext, candidateValidationElement, + "Healed Orchestration Template Candidate Validation data"); + } + + private void populateOrchestrationTemplateStructure(InputStream processedFile, Id structureElementId, + VspDetails vspDetails, SessionContext context, ElementContext elementContext) throws Exception { + byte[] byteData = FileUtils.toByteArray(processedFile); + FileContentHandler contentMap = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteData); + OrchestrationTemplateCandidateData orchestrationTemplateEntityData = + candidateEntityBuilder + .buildCandidateEntityFromZip(vspDetails, byteData, contentMap, null); + String fileDataStructure = orchestrationTemplateEntityData.getFilesDataStructure(); + + ZusammenElement orchestrationTemplateStructure = + buildStructuralElement(ElementType.OrchestrationTemplateStructure, Action.UPDATE); + orchestrationTemplateStructure.setElementId(structureElementId); + orchestrationTemplateStructure.setData(new ByteArrayInputStream(fileDataStructure.getBytes())); + + zusammenAdaptor.saveElement(context, elementContext, orchestrationTemplateStructure, + "Healed Orchestration Template Structure"); } - try { - byteElementData = IOUtils.toByteArray(elementData); - } catch (IOException e) { - return true; + + private Element getOrchestrationTemplateElement(SessionContext context, ElementContext elementContext) { + ElementInfo vspModelElement = + zusammenAdaptor.getElementInfoByName(context, elementContext, null, ElementType.VspModel.name()) + .orElseThrow(() -> new CoreException( + new ErrorCode.ErrorCodeBuilder().withMessage(MISSING_VSP_MODEL_ERROR).build())); + + return getElement(context, elementContext, vspModelElement.getId(), ElementType.OrchestrationTemplate, + MISSING_ORCHESTRATION_TEMPLATE_ERROR); + } + + private Element getOrchestrationTemplateValidationElement(Collection<Element> orchestrationTemplateSubs) { + return orchestrationTemplateSubs.stream() + .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateValidationData.name() + .equals(orchestrationTemplateSub.getInfo() + .getName())).findFirst().orElseThrow( + () -> new CoreException(new ErrorCode.ErrorCodeBuilder() + .withMessage(MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR) + .build())); + + } + + private Element getCandidateContentElement(Collection<Element> candidateSubs) { + return candidateSubs.stream().filter(candidateSub -> ElementType.OrchestrationTemplateCandidateContent.name() + .equals(candidateSub.getInfo().getName())) + .findFirst().orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage( + MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR).build())); + } + + private Id createIfAbsentStructureElementId(Collection<Element> orchestrationTemplateSubs, SessionContext context, + ElementContext elementContext, Element orchestrationTemplateElement) { + return orchestrationTemplateSubs.stream() + .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateStructure.name() + .equals(orchestrationTemplateSub.getInfo() + .getName())).findFirst() + .map(Element::getElementId).orElse(addStructureSubElement(context, elementContext, + ElementType.OrchestrationTemplateStructure, orchestrationTemplateElement.getElementId())); + } + + private Id creatIfAbsentCandidateValidationElementId(Collection<Element> candidateSubs, SessionContext context, + ElementContext elementContext, Element candidateElement) { + return candidateSubs.stream() + .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() + .equals(candidateSub.getInfo().getName())).findFirst() + .map(Element::getElementId).orElse(addStructureSubElement(context, elementContext, + ElementType.OrchestrationTemplateCandidateValidationData, candidateElement.getElementId())); + } + + private Element getElement(SessionContext context, ElementContext elementContext, Id parentElementId, + ElementType elementType, String errorMessage) { + return zusammenAdaptor.getElementByName(context, elementContext, parentElementId, elementType.name()) + .orElseThrow(() -> new CoreException( + new ErrorCode.ErrorCodeBuilder().withMessage(errorMessage).build())); + } + + private OrchestrationTemplateEntity getOrchestrationTemplateEntity(Element orchestrationTemplateElement, + Element validationDataElement) { + OrchestrationTemplateEntity orchestrationTemplateEntity = new OrchestrationTemplateEntity(); + + if (isNotEmpty(orchestrationTemplateElement.getData())) { + orchestrationTemplateEntity + .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.getData()))); + } + orchestrationTemplateEntity + .setFileSuffix(validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); + orchestrationTemplateEntity + .setFileName(validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); + if (isNotEmpty(validationDataElement.getData())) { + orchestrationTemplateEntity + .setValidationData(new String(FileUtils.toByteArray(validationDataElement.getData()))); + } + return orchestrationTemplateEntity; + } + + private OrchestrationTemplateCandidateData getCandidateData(Element candidateElement, + Element candidateContentElement) { + OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); + candidateData.setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.getData()))); + candidateData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateContentElement.getData()))); + candidateData + .setFileSuffix(candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); + candidateData.setFileName(candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); + return candidateData; + } + + private Id addStructureSubElement(SessionContext context, ElementContext elementContext, ElementType elementType, + Id parentElementId) { + ZusammenElement newElement = buildStructuralElement(elementType, Action.CREATE); + + ZusammenElement parentElement = buildElement(parentElementId, Action.IGNORE); + parentElement.addSubElement(newElement); + + return zusammenAdaptor.saveElement(context, elementContext, parentElement, + String.format("Add element %s under element id %s", elementType.name(), parentElementId)) + .getSubElements().iterator().next().getElementId(); + } + + private void emptyStructureElementAndSub(SessionContext context, ElementContext elementContext, Id elementId, + ElementType elementType, Id subElementId, ElementType subElementType) { + ZusammenElement subElement = buildStructuralElement(subElementType, Action.UPDATE); + subElement.setElementId(subElementId); + subElement.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); + + ZusammenElement element = buildStructuralElement(elementType, Action.UPDATE); + element.setElementId(elementId); + element.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); + element.addSubElement(subElement); + + zusammenAdaptor.saveElement(context, elementContext, element, + String.format("Empty element %s and its sub element %s", elementType.name(), subElementType.name())); + } + + public enum InfoPropertyName { + FILE_SUFFIX("fileSuffix"), FILE_NAME("fileName"); + + private String val; + + InfoPropertyName(String val) { + this.val = val; + } + + private String getVal() { + return val; + } + } + + private boolean isNotEmpty(InputStream elementData) { + byte[] byteElementData; + if (Objects.isNull(elementData)) { + return false; + } + try { + byteElementData = IOUtils.toByteArray(elementData); + } catch (IOException e) { + return true; + } + return !ArrayUtils.isEmpty(byteElementData); } - return !ArrayUtils.isEmpty(byteElementData); - } } diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/NetworkPackageHealerTest.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/NetworkPackageHealerTest.java index d94d10cf0f..cf813f8363 100644 --- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/NetworkPackageHealerTest.java +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/NetworkPackageHealerTest.java @@ -12,12 +12,22 @@ * 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. + * ================================================================================ + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.healing.healers; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.item.Info; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -27,6 +37,8 @@ import org.mockito.Mock; import static java.lang.Boolean.FALSE; import static java.lang.Boolean.TRUE; import static org.mockito.Mockito.any; +import static org.openecomp.sdc.datatypes.model.ElementType.OrchestrationTemplateCandidateValidationData; +import static org.openecomp.sdc.datatypes.model.ElementType.OrchestrationTemplateValidationData; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -35,82 +47,142 @@ import org.openecomp.sdc.common.session.SessionContextProviderFactory; import org.openecomp.sdc.datatypes.model.ElementType; import org.openecomp.sdc.healing.healers.NetworkPackageHealer; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder; import org.openecomp.sdc.versioning.dao.types.Version; import java.util.ArrayList; import java.util.Collection; public class NetworkPackageHealerTest { - @Mock - private VendorSoftwareProductInfoDao vspInfoDaoMock; - @Mock - private ZusammenAdaptor zusammenAdaptorMock; - @Mock - private CandidateService candidateService; - - private NetworkPackageHealer networkPackageHealer; - private static final String tenant = "dox"; - - @Before - public void init() { - SessionContextProviderFactory.getInstance().createInterface().create("test", tenant); - MockitoAnnotations.initMocks(this); - networkPackageHealer = new NetworkPackageHealer(vspInfoDaoMock, zusammenAdaptorMock, candidateService); - } - - @After - public void tearDown(){ - SessionContextProviderFactory.getInstance().createInterface().close(); - } - - @Test - public void testIsHealingNeeded_Positive() { - VspDetails vspDetails = new VspDetails("ITEM_ID",new Version()); - vspDetails.setOnboardingMethod("NetworkPackage"); - Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); - Collection<ElementInfo> elementInfos = new ArrayList<>(); - ElementInfo elementInfo = new ElementInfo(); - Info info = new Info(); - info.setName(ElementType.OrchestrationTemplateCandidate.name()); - elementInfo.setInfo(info); - elementInfos.add(elementInfo); - Mockito.when(zusammenAdaptorMock.listElementsByName(any(),any(),any(),any())).thenReturn - (elementInfos); - Assert.assertEquals(TRUE,networkPackageHealer.isHealingNeeded("ITEM_ID", new Version())); -} - - @Test - public void testIsHealingNeeded_Negative() { - VspDetails vspDetails = new VspDetails("ITEM_ID",new Version()); - vspDetails.setOnboardingMethod("NetworkPackage"); - Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); - Collection<ElementInfo> elementInfos = new ArrayList<>(); - - ElementInfo elementInfo = new ElementInfo(); - Info info = new Info(); - info.setName(ElementType.OrchestrationTemplateCandidate.name()); - elementInfo.setInfo(info); - elementInfos.add(elementInfo); - - ElementInfo elementInfo1 = new ElementInfo(); - Info info1 = new Info(); - info1.setName(ElementType.OrchestrationTemplateCandidateValidationData.name()); - elementInfo1.setInfo(info1); - elementInfos.add(elementInfo1); - - Mockito.when(zusammenAdaptorMock.listElementsByName(any(),any(),any(),any())).thenReturn - (elementInfos); - Assert.assertEquals(FALSE,networkPackageHealer.isHealingNeeded("ITEM_ID", new Version())); - } - - @Test - public void testIsHealingNeeded_OnboardingMethod() { - VspDetails vspDetails = new VspDetails("ITEM_ID",new Version()); - vspDetails.setOnboardingMethod("Manual"); - Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); - - Assert.assertEquals(FALSE,networkPackageHealer.isHealingNeeded("ITEM_ID", new Version())); - } + private static final String ITEM_ID = "ITEM_ID"; + private static final String ANY = "ANY"; + private static final String UTF_8 = "UTF-8"; + private static final String FILE_SUFFIX = "fileSuffix"; + private static final String FILE_NAME = "fileName"; + private static final String OTHER_THAN_ANY = "OTHER_THAN_ANY"; + private static final String NETWORK_PACKAGE = "NetworkPackage"; + + @Mock + private VendorSoftwareProductInfoDao vspInfoDaoMock; + @Mock + private ZusammenAdaptor zusammenAdaptorMock; + @Mock + private Element element; + @Mock + private Element subElement; + @Mock + private ElementInfo elementInfo; + @Mock + private Element subElement2; + @Mock + private CandidateEntityBuilder candidateEntityBuilder; + @Mock + private OrchestrationTemplateCandidateData orchestrationData; + + private NetworkPackageHealer networkPackageHealer; + private static final String tenant = "dox"; + + @Before + public void init() { + SessionContextProviderFactory.getInstance().createInterface().create("test", tenant); + MockitoAnnotations.initMocks(this); + networkPackageHealer = new NetworkPackageHealer(vspInfoDaoMock, zusammenAdaptorMock, candidateEntityBuilder); + } + + @After + public void tearDown() { + SessionContextProviderFactory.getInstance().createInterface().close(); + } + + @Test + public void testIsHealingNeeded_Positive() { + VspDetails vspDetails = new VspDetails(ITEM_ID, new Version()); + vspDetails.setOnboardingMethod(NETWORK_PACKAGE); + Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); + Collection<ElementInfo> elementInfos = new ArrayList<>(); + ElementInfo elementInfo = new ElementInfo(); + Info info = new Info(); + info.setName(ElementType.OrchestrationTemplateCandidate.name()); + elementInfo.setInfo(info); + elementInfos.add(elementInfo); + Mockito.when(zusammenAdaptorMock.listElementsByName(any(), any(), any(), any())).thenReturn(elementInfos); + Assert.assertEquals(TRUE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version())); + } + + @Test + public void testIsHealingNeeded_Negative() { + VspDetails vspDetails = new VspDetails(ITEM_ID, new Version()); + vspDetails.setOnboardingMethod(NETWORK_PACKAGE); + Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); + Collection<ElementInfo> elementInfos = new ArrayList<>(); + + ElementInfo elementInfo = new ElementInfo(); + Info info = new Info(); + info.setName(ElementType.OrchestrationTemplateCandidate.name()); + elementInfo.setInfo(info); + elementInfos.add(elementInfo); + + ElementInfo elementInfo1 = new ElementInfo(); + Info info1 = new Info(); + info1.setName(OrchestrationTemplateCandidateValidationData.name()); + elementInfo1.setInfo(info1); + elementInfos.add(elementInfo1); + + Mockito.when(zusammenAdaptorMock.listElementsByName(any(), any(), any(), any())).thenReturn(elementInfos); + Assert.assertEquals(FALSE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version())); + } + + @Test + public void testIsHealingNeeded_OnboardingMethod() { + VspDetails vspDetails = new VspDetails(ITEM_ID, new Version()); + vspDetails.setOnboardingMethod("Manual"); + Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails); + + Assert.assertEquals(FALSE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version())); + } + + @Test + public void shouldHeal() throws Exception { + Mockito.when(zusammenAdaptorMock.getElementByName(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(Optional.of(element)); + Mockito.when(zusammenAdaptorMock.saveElement(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(element); + Mockito.when(element.getSubElements()).thenReturn(Collections.singletonList(subElement)); + Mockito.when( + zusammenAdaptorMock.getElementInfoByName(Mockito.any(), Mockito.any(), Mockito.isNull(), Mockito.any())).thenReturn(Optional.of(elementInfo)); + ArrayList<Element> subElements = new ArrayList<>(); + Mockito.when(element.getData()).thenReturn(IOUtils.toInputStream(ANY, UTF_8)); + Mockito.when(subElement2.getData()).thenReturn(IOUtils.toInputStream(ANY, UTF_8)); + subElements.add(subElement); + subElements.add(subElement2); + Mockito.when(zusammenAdaptorMock.listElementData(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(subElements); + Info info = new Info(); + info.setName(OrchestrationTemplateCandidateValidationData.name()); + Mockito.when(element.getInfo()).thenReturn(info); + Id id = new Id(); + Mockito.when(element.getElementId()).thenReturn(id); + Info subinfo = new Info(); + Info subinfo2 = new Info(); + Map<String, Object> props2 = new HashMap<>(); + props2.put(FILE_SUFFIX, ANY); + props2.put(FILE_NAME, ANY); + subinfo2.setProperties(props2); + subinfo2.setName(ElementType.OrchestrationTemplateCandidateContent.name()); + Map<String, Object> props = new HashMap<>(); + props.put(FILE_SUFFIX, ANY); + props.put(FILE_NAME, OTHER_THAN_ANY); + subinfo.setProperties(props); + subinfo.setName(OrchestrationTemplateValidationData.name()); + Mockito.when(subElement.getInfo()).thenReturn(subinfo); + Mockito.when(subElement2.getInfo()).thenReturn(subinfo2); + Mockito.when(candidateEntityBuilder.buildCandidateEntityFromZip(Mockito.isNull(), Mockito.any(), Mockito.any(), Mockito.isNull())).thenReturn(orchestrationData); + Mockito.when(orchestrationData.getFilesDataStructure()).thenReturn(ANY); + + networkPackageHealer.heal(ITEM_ID, Version.valueOf("1.1")); + + Mockito.verify(zusammenAdaptorMock, Mockito.times(3)).saveElement(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); + } } |