- }
- } catch (Exception e) {
- log.debug("detectArtifactGroupType failed with exception", e);
- result = Either.right(false);
- }
- return result;
- }
-
- private static F<ArtifactGroupTypeEnum, ArtifactDefinition> buildArtifactDefinitionFromCsarArtifactPath(
- Entry<String, byte[]> entry,
- Map<String, Set<List<String>>> collectedWarningMessages,
- String[] parsedCsarArtifactPath
- ) {
- return artifactGroupType -> {
- ArtifactDefinition artifact;
- artifact = new ArtifactDefinition();
- artifact.setArtifactGroupType(artifactGroupType);
- artifact.setArtifactType(
- detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1],
- collectedWarningMessages));
- artifact.setArtifactName(
- ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1]));
- artifact.setPayloadData(Base64.encodeBase64String(entry.getValue()));
- artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName()
- .substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName());
- artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName()));
- artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR);
- artifact.setIsFromCsar(true);
- artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue()));
- return artifact;
- };
- }
-
- @Getter
- public static final class NonMetaArtifactInfo {
- @Setter
- private String artifactUniqueId;
- private final String path;
- private final String artifactName;
- private final String displayName;
- private final String artifactLabel;
- private final String artifactType;
- private final ArtifactGroupTypeEnum artifactGroupType;
- private final String payloadData;
- private final String artifactChecksum;
- private final boolean isFromCsar;
-
- public NonMetaArtifactInfo(final String artifactName, final String path, final String artifactType,
- final ArtifactGroupTypeEnum artifactGroupType, final byte[] payloadData,
- final String artifactUniqueId, final boolean isFromCsar) {
- super();
- this.path = path;
- this.isFromCsar = isFromCsar;
- this.artifactName = ValidationUtils.normalizeFileName(artifactName);
- this.artifactType = artifactType;
- this.artifactGroupType = artifactGroupType;
- final int pointIndex = artifactName.lastIndexOf('.');
- if (pointIndex > 0) {
- displayName = artifactName.substring(0, pointIndex);
- } else {
- displayName = artifactName;
- }
- this.artifactLabel = ValidationUtils.normalizeArtifactLabel(artifactName);
- if (payloadData == null) {
- this.payloadData = null;
- this.artifactChecksum = null;
- } else {
- this.payloadData = Base64.encodeBase64String(payloadData);
- this.artifactChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(payloadData);
- }
- this.artifactUniqueId = artifactUniqueId;
- }
-
- }
-
- /**
- * This method checks the artifact GroupType & Artifact Type. <br>
- * if there is any problem warning messages are added to collectedWarningMessages
- *
- * @param artifactPath
- * @param collectedWarningMessages
- * @return
- */
- public static Either<NonMetaArtifactInfo, Boolean> validateNonMetaArtifact(
- String artifactPath,
- byte[] payloadData,
- Map<String, Set<List<String>>> collectedWarningMessages
- ) {
- try {
- String[] parsedArtifactPath = artifactPath.split(PATH_DELIMITER);
- String groupType = parsedArtifactPath[1];
- String receivedTypeName = parsedArtifactPath[2];
- String artifactFileNameType = parsedArtifactPath[3];
-
- return detectArtifactGroupType(groupType, collectedWarningMessages)
- .left().bind(artifactGroupType -> {
- String artifactType =
- detectArtifactTypeVF(artifactGroupType, receivedTypeName, collectedWarningMessages);
-
- return Either.left(new NonMetaArtifactInfo(
- artifactFileNameType, artifactPath, artifactType,
- artifactGroupType, payloadData, null, true
- ));
- });
- } catch (Exception e) {
- log.debug("detectArtifactGroupType failed with exception", e);
- return Either.right(false);
- }
- }
-
- private static String detectArtifactTypeVFC(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, String parentVfName, Map<String, Set<List<String>>> collectedWarningMessages) {
- String warningMessage = "Warning - artifact type {} that was provided for VFC {} is not recognized.";
- return detectArtifactType(artifactGroupType, receivedTypeName, warningMessage, collectedWarningMessages, parentVfName);
- }
-
- private static String detectArtifactTypeVF(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, Map<String, Set<List<String>>> collectedWarningMessages) {
- String warningMessage = "Warning - artifact type {} that was provided for VF is not recognized.";
- return detectArtifactType(artifactGroupType, receivedTypeName, warningMessage, collectedWarningMessages);
- }
-
- private static String detectArtifactType(final ArtifactGroupTypeEnum artifactGroupType,
- final String receivedTypeName, final String warningMessage,
- final Map<String, Set<List<String>>> collectedWarningMessages,
- final String... arguments) {
- final ArtifactConfiguration artifactConfiguration =
- ArtifactConfigManager.getInstance()
- .find(receivedTypeName, artifactGroupType, ComponentType.RESOURCE)
- .orElse(null);
-
- if (artifactConfiguration == null) {
- final List<String> messageArguments = new ArrayList<>();
- messageArguments.add(receivedTypeName);
- messageArguments.addAll(Arrays.asList(arguments));
- if (!collectedWarningMessages.containsKey(warningMessage)) {
- final Set<List<String>> messageArgumentLists = new HashSet<>();
- messageArgumentLists.add(messageArguments);
- collectedWarningMessages.put(warningMessage, messageArgumentLists);
- } else {
- collectedWarningMessages.get(warningMessage).add(messageArguments);
- }
- }
-
- return artifactConfiguration == null ? ArtifactTypeEnum.OTHER.getType() : receivedTypeName;
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeAllFilesToCsar(Component mainComponent, CsarDefinition csarDefinition, ZipOutputStream zipstream, boolean isInCertificationRequest) throws IOException{
- ComponentArtifacts componentArtifacts = csarDefinition.getComponentArtifacts();
-
- Either<ZipOutputStream, ResponseFormat> writeComponentArtifactsToSpecifiedPath = writeComponentArtifactsToSpecifiedPath(mainComponent, componentArtifacts, zipstream, ARTIFACTS_PATH, isInCertificationRequest);
-
- if(writeComponentArtifactsToSpecifiedPath.isRight()){
- return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
- }
-
- ComponentTypeArtifacts mainTypeAndCIArtifacts = componentArtifacts.getMainTypeAndCIArtifacts();
- writeComponentArtifactsToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, mainTypeAndCIArtifacts.getComponentArtifacts(), zipstream, ARTIFACTS_PATH, isInCertificationRequest);
-
- if(writeComponentArtifactsToSpecifiedPath.isRight()){
- return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
- }
-
- Map<String, ArtifactsInfo> componentInstancesArtifacts = mainTypeAndCIArtifacts.getComponentInstancesArtifacts();
- Set<String> keySet = componentInstancesArtifacts.keySet();
-
- String currentPath = ARTIFACTS_PATH + RESOURCES_PATH;
- for (String keyAssetName : keySet) {
- ArtifactsInfo artifactsInfo = componentInstancesArtifacts.get(keyAssetName);
- String pathWithAssetName = currentPath + keyAssetName + PATH_DELIMITER;
- writeComponentArtifactsToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, artifactsInfo, zipstream, pathWithAssetName, isInCertificationRequest);
-
- if(writeComponentArtifactsToSpecifiedPath.isRight()){
- return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
- }
- }
- writeComponentArtifactsToSpecifiedPath = writeOperationsArtifactsToCsar(mainComponent, zipstream);
-
- if (writeComponentArtifactsToSpecifiedPath.isRight()) {
- return Either.right(writeComponentArtifactsToSpecifiedPath.right().value());
- }
- return Either.left(zipstream);
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeOperationsArtifactsToCsar(Component component,
- ZipOutputStream zipstream) {
- if (checkComponentBeforeOperation(component)) {
- return Either.left(zipstream);
- }
- final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
- for (Map.Entry<String, InterfaceDefinition> interfaceEntry : interfaces.entrySet()) {
- for (OperationDataDefinition operation : interfaceEntry.getValue().getOperations().values()) {
- try {
- if (checkComponentBeforeWrite(component, interfaceEntry, operation)) {
- continue;
- }
- final String artifactUUID = operation.getImplementation().getArtifactUUID();
- if (artifactUUID == null) {
- continue;
- }
- final Either<byte[], ActionStatus> artifactFromCassandra = getFromCassandra(artifactUUID);
- final String artifactName = operation.getImplementation().getArtifactName();
- if (artifactFromCassandra.isRight()) {
- log.error(ARTIFACT_NAME_UNIQUE_ID, artifactName, artifactUUID);
- log.error("Failed to get {} payload from DB reason: {}", artifactName,
- artifactFromCassandra.right().value());
- return Either.right(componentsUtils.getResponseFormat(
- ActionStatus.ARTIFACT_PAYLOAD_NOT_FOUND_DURING_CSAR_CREATION, "Resource",
- component.getUniqueId(), artifactName, artifactUUID));
- }
- final byte[] payloadData = artifactFromCassandra.left().value();
- zipstream.putNextEntry(new ZipEntry(OperationArtifactUtil.createOperationArtifactPath(
- component, null, operation, true)));
- zipstream.write(payloadData);
- } catch (IOException e) {
- log.error("Component Name {}, Interface Name {}, Operation Name {}", component.getNormalizedName(),
- interfaceEntry.getKey(), operation.getName());
- log.error("Error while writing the operation's artifacts to the CSAR " + "{}", e);
- return Either.right(componentsUtils
- .getResponseFormat(ActionStatus.ERROR_DURING_CSAR_CREATION, "Resource",
- component.getUniqueId()));
- }
- }
- }
- return Either.left(zipstream);
- }
-
- private boolean checkComponentBeforeWrite(Component component, Entry<String, InterfaceDefinition> interfaceEntry, OperationDataDefinition operation) {
- final ArtifactDataDefinition implementation = operation.getImplementation();
- if (Objects.isNull(implementation)) {
- log.debug("Component Name {}, Interface Id {}, Operation Name {} - no Operation Implementation found",
- component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
- operation.getName());
- return true;
- }
- final String artifactName = implementation.getArtifactName();
- if (Objects.isNull(artifactName)) {
- log.debug("Component Name {}, Interface Id {}, Operation Name {} - no artifact found",
- component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
- operation.getName());
- return true;
- }
- if (OperationArtifactUtil.artifactNameIsALiteralValue(artifactName)) {
- log.debug("Component Name {}, Interface Id {}, Operation Name {} - artifact name is a literal value rather than an SDC artifact",
- component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
- operation.getName());
- return true;
- }
- return false;
- }
-
- private boolean checkComponentBeforeOperation(Component component) {
- if (component instanceof Service) {
- return true;
- }
- if (Objects.isNull(((Resource) component).getInterfaces())) {
- log.debug("Component Name {}- no interfaces found", component.getNormalizedName());
- return true;
- }
- return false;
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeComponentArtifactsToSpecifiedPath(Component mainComponent, ComponentArtifacts componentArtifacts, ZipOutputStream zipstream,
- String currentPath, boolean isInCertificationRequest) throws IOException {
- Map<String, ComponentTypeArtifacts> componentTypeArtifacts = componentArtifacts.getComponentTypeArtifacts();
- //Keys are defined:
- //<Inner Asset TOSCA name (e.g. VFC name)> folder name: <Inner Asset TOSCA name (e.g. VFC name)>_v<version>.
- //E.g. "org.openecomp.resource.vf.vipr_atm_v1.0"
- Set<String> componentTypeArtifactsKeys = componentTypeArtifacts.keySet();
- for (String keyAssetName : componentTypeArtifactsKeys) {
- ComponentTypeArtifacts componentInstanceArtifacts = componentTypeArtifacts.get(keyAssetName);
- ArtifactsInfo componentArtifacts2 = componentInstanceArtifacts.getComponentArtifacts();
- String pathWithAssetName = currentPath + keyAssetName + PATH_DELIMITER;
- Either<ZipOutputStream, ResponseFormat> writeArtifactsInfoToSpecifiedPath = writeArtifactsInfoToSpecifiedPath(mainComponent, componentArtifacts2, zipstream, pathWithAssetName, isInCertificationRequest);
-
- if(writeArtifactsInfoToSpecifiedPath.isRight()){
- return writeArtifactsInfoToSpecifiedPath;
- }
- }
-
- return Either.left(zipstream);
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeArtifactsInfoToSpecifiedPath(final Component mainComponent,
- final ArtifactsInfo currArtifactsInfo,
- final ZipOutputStream zip,
- final String path,
- final boolean isInCertificationRequest) throws IOException {
-
- final Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> artifactsInfo =
- currArtifactsInfo.getArtifactsInfo();
- for (final ArtifactGroupTypeEnum artifactGroupTypeEnum : artifactsInfo.keySet()) {
- final String groupTypeFolder =
- path + WordUtils.capitalizeFully(artifactGroupTypeEnum.getType()) + PATH_DELIMITER;
-
- final Map<String, List<ArtifactDefinition>> artifactTypesMap = artifactsInfo.get(artifactGroupTypeEnum);
-
- for (final String artifactType : artifactTypesMap.keySet()) {
- final List<ArtifactDefinition> artifactDefinitionList = artifactTypesMap.get(artifactType);
- String artifactTypeFolder = groupTypeFolder + artifactType + PATH_DELIMITER;
-
- if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType) && path
- .contains(ARTIFACTS_PATH + RESOURCES_PATH)) {
- // Ignore this packaging as BPMN artifacts needs to be packaged in different manner
- continue;
- }
- if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType)) {
- artifactTypeFolder += OperationArtifactUtil.BPMN_ARTIFACT_PATH + File.separator;
- }
-
- // TODO: We should not do this but in order to keep this refactoring small enough,
- // we'll leave this as is for now
- List<ArtifactDefinition> collect =
- filterArtifactDefinitionToZip(mainComponent, artifactDefinitionList, isInCertificationRequest)
- .collect(Collectors.toList());
- for (ArtifactDefinition ad : collect) {
- zip.putNextEntry(new ZipEntry(artifactTypeFolder + ad.getArtifactName()));
- zip.write(ad.getPayloadData());
- }
- }
- }
- return Either.left(zip);
- }
-
- private Stream<ArtifactDefinition> filterArtifactDefinitionToZip(Component mainComponent,
- List<ArtifactDefinition> artifactDefinitionList, boolean isInCertificationRequest) {
- return artifactDefinitionList
- .stream()
- .filter(shouldBeInZip(isInCertificationRequest, mainComponent))
- .map(this::fetchPayLoadData)
- .filter(Either::isLeft)
- .map(e -> e.left().value());
- }
-
- private Predicate<ArtifactDefinition> shouldBeInZip(boolean isInCertificationRequest, Component component) {
- return artifactDefinition ->
- !(!isInCertificationRequest
- && component.isService()
- && artifactDefinition.isHeatEnvType()
- || artifactDefinition.hasNoMandatoryEsId());
- }
-
- private Either<ArtifactDefinition, ActionStatus> fetchPayLoadData(ArtifactDefinition ad) {
- byte[] payloadData = ad.getPayloadData();
- if(payloadData == null) {
- return getFromCassandra(ad.getEsId())
- .left().map(pd -> {
- ad.setPayload(pd);
- return ad;
- }).right().map(as -> {
- log.debug(ARTIFACT_NAME_UNIQUE_ID, ad.getArtifactName(), ad.getUniqueId());
- log.debug("Failed to get {} payload from DB reason: {}", ad.getArtifactName(), as);
- return as;
- });
- } else {
- return Either.left(ad);
- }
- }
-
- /************************************ Artifacts Structure ******************************************************************/
- /**
- * The artifacts Definition saved by their structure
- */
- private class ArtifactsInfo {
- //Key is the type of artifacts(Informational/Deployment)
- //Value is a map between an artifact type and a list of all artifacts of this type
- private Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> artifactsInfoField;
-
- public ArtifactsInfo() {
- this.artifactsInfoField = new EnumMap<>(ArtifactGroupTypeEnum.class);
- }
-
- public Map<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> getArtifactsInfo() {
- return artifactsInfoField;
- }
-
- public void addArtifactsToGroup(ArtifactGroupTypeEnum artifactGroup,
- Map<String, List<ArtifactDefinition>> artifactsDefinition) {
- if (artifactsInfoField.get(artifactGroup) == null) {
- artifactsInfoField.put(artifactGroup, artifactsDefinition);
- } else {
- Map<String, List<ArtifactDefinition>> artifactTypeEnumListMap =
- artifactsInfoField.get(artifactGroup);
- artifactTypeEnumListMap.putAll(artifactsDefinition);
- artifactsInfoField.put(artifactGroup, artifactTypeEnumListMap);
- }
-
- }
-
- public boolean isEmpty() {
- return artifactsInfoField.isEmpty();
- }
-
- public boolean isNotEmpty() {
- return !isEmpty();
- }
-
- }
-
- /**
- * The artifacts of the component and of all its composed instances
- *
- */
- private class ComponentTypeArtifacts {
- private ArtifactsInfo componentArtifacts; //component artifacts (describes the Informational Deployment folders)
- private Map<String, ArtifactsInfo> componentInstancesArtifacts; //artifacts of the composed instances mapped by the resourceInstance normalized name (describes the Resources folder)
-
- public ComponentTypeArtifacts() {
- componentArtifacts = new ArtifactsInfo();
- componentInstancesArtifacts = new HashMap<>();
- }
-
- public ArtifactsInfo getComponentArtifacts() {
- return componentArtifacts;
- }
- public void setComponentArtifacts(ArtifactsInfo artifactsInfo) {
- this.componentArtifacts = artifactsInfo;
- }
- public Map<String, ArtifactsInfo> getComponentInstancesArtifacts() {
- return componentInstancesArtifacts;
- }
- public void setComponentInstancesArtifacts(Map<String, ArtifactsInfo> componentInstancesArtifacts) {
- this.componentInstancesArtifacts = componentInstancesArtifacts;
- }
-
- public void addComponentInstancesArtifacts(String normalizedName, ArtifactsInfo artifactsInfo) {
- componentInstancesArtifacts.put(normalizedName, artifactsInfo);