aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java
blob: bc999de4fed37c74a9fc63be94a9984e657cbba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;

import org.apache.commons.lang3.tuple.Pair;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.common.utils.CommonUtil;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingManifest;
import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGBLE_FOLDERS;
import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGIBLE_FILES;
import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;

public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
    implements OrchestrationTemplateFileHandler {


  @Override
  public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
                                                        byte[] uploadedFileData) {
    FileContentHandler contentMap = null;
    List<String> folderList = new ArrayList<>();
    try {
      Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip =
          CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
      contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
      folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
    } catch (IOException exception) {
      uploadFileResponse.addStructureError(
          SdcCommon.UPLOAD_FILE,
          new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
    } catch (CoreException coreException) {
      uploadFileResponse.addStructureError(
          SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
    }
    validateContent(uploadFileResponse, contentMap, folderList);
    return Optional.ofNullable(contentMap);
  }

  private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap,
                               List<String> folderList) {
    validateManifest(uploadFileResponse, contentMap);
    validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
    validateNoExtraFiles(uploadFileResponse, contentMap);
    validateFolders(uploadFileResponse, folderList);
  }

  private void validateManifest(UploadFileResponse uploadFileResponse,
                                FileContentHandler contentMap) {

    if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
      return;
    }

    try (InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {

      OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent);
      if (!onboardingManifest.isValid()) {
        onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError(
            SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error)));
      }

    } catch (IOException e) {
      // convert to runtime to keep the throws unchanged
      throw new RuntimeException("Failed to validate manifest", e);
    }
  }

  private void validateNoExtraFiles(UploadFileResponse uploadFileResponse,
                                    FileContentHandler contentMap) {
    List<String> unwantedFiles = contentMap.getFileList().stream()
        .filter(this::filterFiles).collect(Collectors.toList());
    if (!unwantedFiles.isEmpty()) {
      unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
          uploadFileResponse.addStructureError(
              SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
                  getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(),
                      unwantedFile))));
    }
  }

  private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) {
    List<String> filterResult =
        folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
    if (!filterResult.isEmpty()) {
      folderList.stream().filter(this::filterFolders).forEach(unwantedFolder ->
          uploadFileResponse.addStructureError(
              SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
                  getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
                      unwantedFolder))));

    }
  }

  private boolean filterFiles(String inFileName) {
    boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
    return !valid && filterFolders(inFileName);
  }

  private boolean filterFolders(String fileName) {
    return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
  }

  private boolean validateFileExist(UploadFileResponse uploadFileResponse,
                                    FileContentHandler contentMap, String fileName) {

    boolean containsFile = contentMap.containsFile(fileName);
    if (!containsFile) {
      uploadFileResponse.addStructureError(
          SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
              getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
    }
    return containsFile;
  }

  @Override
  protected boolean updateCandidateData(VspDetails vspDetails, byte[] uploadedFileData,
                                        FileContentHandler contentMap,
                                        String fileSuffix, String networkPackageName,
                                        CandidateService candidateService,
                                        UploadFileResponse uploadFileResponse) {
    try {
      candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
          new OrchestrationTemplateCandidateData(ByteBuffer.wrap(uploadedFileData), "", fileSuffix,
              networkPackageName));
    } catch (Exception exception) {
      logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
          getHandlerType().toString()), exception);
      uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
          new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));

      mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
      return true;
    }
    return false;
  }


  @Override
  protected OnboardingTypesEnum getHandlerType() {
    return OnboardingTypesEnum.CSAR;
  }

  @Override
  protected boolean isInvalidRawZipData(String fileSuffix,
                                        UploadFileResponse uploadFileResponse,
                                        byte[] uploadedFileData,
                                        CandidateService candidateService) {
    return super.isInvalidRawZipData(fileSuffix, uploadFileResponse, uploadedFileData, candidateService);
  }
}