aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/ManifestValidator.java
blob: 43cafb494c9bb22921abfeb001f0a81c1eb160c5 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 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.openecomp.sdc.validation.impl.validators;

import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.validation.Validator;
import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
import org.openecomp.core.validation.types.GlobalValidationContext;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.heat.datatypes.manifest.FileData;
import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
import org.openecomp.sdc.logging.types.LoggerConstants;
import org.openecomp.sdc.logging.types.LoggerErrorCode;
import org.openecomp.sdc.logging.types.LoggerErrorDescription;
import org.openecomp.sdc.logging.types.LoggerTragetServiceName;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


public class ManifestValidator implements Validator {
  public static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
  private static Logger logger = (Logger) LoggerFactory.getLogger(YamlValidator.class);

  @Override
  public void validate(GlobalValidationContext globalContext) {
    mdcDataDebugMessage.debugEntryMessage(null, null);

    Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
    ManifestContent manifestContent;

    try {
      if (content.isPresent()) {
        manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
      } else {
        MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
            LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
            LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
        throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
      }
    } catch (Exception re) {
      globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
          Messages.INVALID_MANIFEST_FILE.getErrorMessage(),
          LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT,
          LoggerErrorDescription.INVALID_MANIFEST);
      return;
    }

    List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
    manifestFiles.stream().filter(name ->
        !globalContext.getFileContextMap().containsKey(name)
    ).forEach(name -> globalContext
        .addMessage(name, ErrorLevel.ERROR, Messages.MISSING_FILE_IN_ZIP.getErrorMessage(),
            LoggerTragetServiceName.VALIDATE_FILE_IN_ZIP, LoggerErrorDescription.MISSING_FILE));

    globalContext.getFileContextMap().keySet().stream().filter(name ->
        !manifestFiles.contains(name) && !SdcCommon.MANIFEST_NAME.equals(name)
    ).forEach(name ->
        globalContext.addMessage(name, ErrorLevel.WARNING,
            Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage(),
            LoggerTragetServiceName.VALIDATE_FILE_IN_MANIFEST, LoggerErrorDescription.MISSING_FILE)
    );

    mdcDataDebugMessage.debugExitMessage(null, null);
  }


  private List<String> getManifestFileList(ManifestContent manifestContent,
                                           GlobalValidationContext context) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    ManifestScanner manifestScanner = new ManifestScanner();
    manifestScanner.init(context);
    manifestScanner.scan(null, manifestContent.getData(), context);

    mdcDataDebugMessage.debugExitMessage(null, null);
    return manifestScanner.getFileList();
  }


  private class ManifestScanner {
    private GlobalValidationContext globalValidationContext;
    private List<String> fileList;

    public void init(GlobalValidationContext globalValidationContext) {
      this.globalValidationContext = globalValidationContext;
      this.fileList = new ArrayList<>();
    }


    public void scan(FileData fileData, List<FileData> data,
                     GlobalValidationContext globalContext) {
      if (fileData == null) {
        for (FileData childFileData : data) {
          if (childFileData.getType() != null
              && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
            globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
                ErrorMessagesFormatBuilder
                    .getErrorWithParameters(Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()),
                LoggerTragetServiceName.SCAN_MANIFEST_STRUCTURE,
                "env file is not associated to HEAT file");
          }
        }
      }
      if (fileData != null) {
        fileList.add(fileData.getFile());
        validateFileTypeVsFileName(fileData);
      }
      if (data == null) {
        return;
      }
      data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
    }


    public List<String> getFileList() {
      return this.fileList;
    }

    private void validateFileTypeVsFileName(FileData fileData) {
      String fileName = fileData.getFile();
      if (fileName == null) {
        this.globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
            Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage(),
            LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Missing file name in manifest");

      }
      FileData.Type type = fileData.getType();
      if (type == null) {
        this.globalValidationContext
            .addMessage(fileName, ErrorLevel.ERROR, Messages.INVALID_FILE_TYPE.getErrorMessage(),
                LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Invalid file type");
      } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
          || type.equals(FileData.Type.HEAT)) {
        if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
          this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
              ErrorMessagesFormatBuilder
                  .getErrorWithParameters(Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
                      fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
              "Wrong HEAT file extention");
        }
      } else if (type.equals(FileData.Type.HEAT_ENV)) {
        if (fileName != null && !fileName.endsWith(".env")) {
          this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
              ErrorMessagesFormatBuilder
                  .getErrorWithParameters(Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
                      fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
              "Wrong env file extention");
        }
      }
    }
  }


}