- } else {
- collectedWarningMessages.get(warningMessage).add(messageArguments);
- }
-
- result = Either.right(false);
- } else {
-
- result = Either.left(artifactGroupType);
- }
- } catch (Exception e) {
- log.debug("detectArtifactGroupType failed with exception", e);
- result = Either.right(false);
- }
- return result;
- }
-
- private static ArtifactDefinition buildArtifactDefinitionFromCsarArtifactPath(Entry<String, byte[]> entry, Map<String, Set<List<String>>> collectedWarningMessages, String[] parsedCsarArtifactPath, ArtifactGroupTypeEnum 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;
- }
-
- public static final class NonMetaArtifactInfo {
- private final String path;
- private final String artifactName;
- private final String displayName;
- private final String artifactLabel;
- private final ArtifactTypeEnum artifactType;
- private final ArtifactGroupTypeEnum artifactGroupType;
- private String payloadData;
- private String artifactChecksum;
- private String artifactUniqueId;
- private final boolean isFromCsar;
-
- public NonMetaArtifactInfo(String artifactName, String path, ArtifactTypeEnum artifactType, ArtifactGroupTypeEnum artifactGroupType, byte[] payloadData, String artifactUniqueId, 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 = Base64.encodeBase64String(payloadData);
- this.artifactChecksum = GeneralUtility.calculateMD5Base64EncodedByByteArray(payloadData);
- }
- this.artifactUniqueId = artifactUniqueId;
- }
-
- public String getPath() {
- return path;
- }
-
- public String getArtifactName() {
- return artifactName;
- }
-
- public ArtifactTypeEnum getArtifactType() {
- return artifactType;
- }
-
- public String getDisplayName() {
- return displayName;
- }
-
- public ArtifactGroupTypeEnum getArtifactGroupType() {
- return artifactGroupType;
- }
-
- public String getArtifactLabel() {
- return artifactLabel;
- }
-
- public boolean isFromCsar(){
- return isFromCsar;
- }
-
- public String getPayloadData() {
- return payloadData;
- }
-
- public String getArtifactChecksum() {
- return artifactChecksum;
- }
-
- public String getArtifactUniqueId() {
- return artifactUniqueId;
- }
-
- public void setArtifactUniqueId(String artifactUniqueId) {
- 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) {
- Either<NonMetaArtifactInfo, Boolean> ret;
- try {
- String[] parsedArtifactPath = artifactPath.split(PATH_DELIMITER);
- // Validate Artifact Group Type
- Either<ArtifactGroupTypeEnum, Boolean> eitherGroupType = detectArtifactGroupType(parsedArtifactPath[1], collectedWarningMessages);
- if (eitherGroupType.isLeft()) {
- final ArtifactGroupTypeEnum groupTypeEnum = eitherGroupType.left().value();
-
- // Validate Artifact Type
- String artifactType = parsedArtifactPath[2];
- artifactType = detectArtifactTypeVF(groupTypeEnum, artifactType, collectedWarningMessages);
-
- String artifactFileNameType = parsedArtifactPath[3];
- ret = Either.left(new NonMetaArtifactInfo(artifactFileNameType, artifactPath, ArtifactTypeEnum.findType(artifactType), groupTypeEnum, payloadData, null, true));
-
- } else {
- ret = Either.right(eitherGroupType.right().value());
- }
- } catch (Exception e) {
- log.debug("detectArtifactGroupType failed with exception", e);
- ret = Either.right(false);
- }
- return ret;
-
- }
-
- 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(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, String warningMessage, Map<String, Set<List<String>>> collectedWarningMessages, String... arguments) {
-
- ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(receivedTypeName);
- Map<String, ArtifactTypeConfig> resourceValidTypeArtifacts = null;
-
- if(artifactGroupType != null){
- switch (artifactGroupType) {
- case INFORMATIONAL:
- resourceValidTypeArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
- .getResourceInformationalArtifacts();
- break;
- case DEPLOYMENT:
- resourceValidTypeArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
- .getResourceDeploymentArtifacts();
- break;
- default:
- break;
- }
- }
-
- Set<String> validArtifactTypes = null;
- if(resourceValidTypeArtifacts != null){
- validArtifactTypes = resourceValidTypeArtifacts.keySet();
- }
-
- if (validArtifactTypes == null || artifactType == null || !validArtifactTypes.contains(artifactType.getType())) {
- List<String> messageArguments = new ArrayList<>();
- messageArguments.add(receivedTypeName);
- messageArguments.addAll(Arrays.asList(arguments));
- if (!collectedWarningMessages.containsKey(warningMessage)) {
- Set<List<String>> messageArgumentLists = new HashSet<>();
- messageArgumentLists.add(messageArguments);
- collectedWarningMessages.put(warningMessage, messageArgumentLists);
- } else {
- collectedWarningMessages.get(warningMessage).add(messageArguments);
- }
- }
-
- return artifactType == null ? ArtifactTypeEnum.OTHER.getType() : artifactType.getType();
- }
-
- 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 = writeArtifactsInfoToSpecifiedtPath(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 = writeArtifactsInfoToSpecifiedtPath(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();
- 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) {
- if (Objects.isNull(operation.getImplementation())) {
- log.debug("Component Name {}, Interface Id {}, Operation Name {} - no Operation Implementation found",
- component.getNormalizedName(), interfaceEntry.getValue().getUniqueId(),
- operation.getName());
- return true;
- }
- if (Objects.isNull(operation.getImplementation().getArtifactName())) {
- log.debug("Component Name {}, Interface Id {}, Operation Name {} - no artifact found",
- 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 = writeArtifactsInfoToSpecifiedtPath(mainComponent, componentArtifacts2, zipstream, pathWithAssetName, isInCertificationRequest);
-
- if(writeArtifactsInfoToSpecifiedPath.isRight()){
- return writeArtifactsInfoToSpecifiedPath;
- }
- }
-
- return Either.left(zipstream);
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeArtifactsInfoToSpecifiedtPath(Component mainComponent, ArtifactsInfo currArtifactsInfo, ZipOutputStream zip, String path, boolean isInCertificationRequest) throws IOException {
- Map<ArtifactGroupTypeEnum, Map<ArtifactTypeEnum, List<ArtifactDefinition>>> artifactsInfo = currArtifactsInfo
- .getArtifactsInfo();
- Set<ArtifactGroupTypeEnum> groupTypeEnumKeySet = artifactsInfo.keySet();
-
- for (ArtifactGroupTypeEnum artifactGroupTypeEnum : groupTypeEnumKeySet) {
- String groupTypeFolder = path + WordUtils.capitalizeFully(artifactGroupTypeEnum.getType()) + PATH_DELIMITER;
-
- Map<ArtifactTypeEnum, List<ArtifactDefinition>> artifactTypesMap = artifactsInfo.get(artifactGroupTypeEnum);
- Set<ArtifactTypeEnum> artifactTypeEnumKeySet = artifactTypesMap.keySet();
-
- for (ArtifactTypeEnum artifactTypeEnum : artifactTypeEnumKeySet) {
- List<ArtifactDefinition> artifactDefinitionList = artifactTypesMap.get(artifactTypeEnum);
- String artifactTypeFolder = groupTypeFolder + artifactTypeEnum.toString() + PATH_DELIMITER;
-
- if(artifactTypeEnum == ArtifactTypeEnum.WORKFLOW && path.contains(ARTIFACTS_PATH + RESOURCES_PATH)){
- // Ignore this packaging as BPMN artifacts needs to be packaged in different manner
- continue;
- }
- if (artifactTypeEnum == ArtifactTypeEnum.WORKFLOW) {
- artifactTypeFolder += OperationArtifactUtil.BPMN_ARTIFACT_PATH + File.separator;
- }
-
- Either<ZipOutputStream, ResponseFormat> writeArtifactDefinition = writeArtifactDefinition(mainComponent, zip, artifactDefinitionList, artifactTypeFolder, isInCertificationRequest);
-
- if(writeArtifactDefinition.isRight()){
- return writeArtifactDefinition;
- }
- }
- }
-
- return Either.left(zip);
- }
-
- private Either<ZipOutputStream, ResponseFormat> writeArtifactDefinition(Component mainComponent, ZipOutputStream zip, List<ArtifactDefinition> artifactDefinitionList,
- String artifactPathAndFolder, boolean isInCertificationRequest) throws IOException {
-
- ComponentTypeEnum componentType = mainComponent.getComponentType();
- String heatEnvType = ArtifactTypeEnum.HEAT_ENV.getType();
-
- for (ArtifactDefinition artifactDefinition : artifactDefinitionList) {
- if (!isInCertificationRequest && componentType == ComponentTypeEnum.SERVICE
- && artifactDefinition.getArtifactType().equals(heatEnvType) ||
- //this is placeholder
- (artifactDefinition.getEsId() == null && artifactDefinition.getMandatory())){
- continue;
- }
-
- byte[] payloadData = artifactDefinition.getPayloadData();
- String artifactFileName = artifactDefinition.getArtifactName();
-
- if (payloadData == null) {
- Either<byte[], ActionStatus> fromCassandra = getFromCassandra(artifactDefinition.getEsId());
-
- if (fromCassandra.isRight()) {
- log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDefinition.getArtifactName(), artifactDefinition.getUniqueId());
- log.debug("Failed to get {} payload from DB reason: {}", artifactFileName, fromCassandra.right().value());
- continue;
- }
- payloadData = fromCassandra.left().value();
- }
- zip.putNextEntry(new ZipEntry(artifactPathAndFolder + artifactFileName));
- zip.write(payloadData);
- }
-
- return Either.left(zip);
- }
-
- /************************************ 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<ArtifactTypeEnum, List<ArtifactDefinition>>> artifactsInfoField;
-
- public ArtifactsInfo() {
- this.artifactsInfoField = new EnumMap<>(ArtifactGroupTypeEnum.class);
- }
-
- public Map<ArtifactGroupTypeEnum, Map<ArtifactTypeEnum, List<ArtifactDefinition>>> getArtifactsInfo() {
- return artifactsInfoField;
- }
-
- public List<ArtifactDefinition> getFlatArtifactsListByType(ArtifactTypeEnum artifactType){
- List<ArtifactDefinition> artifacts = new ArrayList<>();
- for (List<ArtifactDefinition> artifactsByType:artifactsInfoField.get(artifactType).values()){
- artifacts.addAll(artifactsByType);