6a7466d20d2af292cb4ed2c18b98fafe868d221d
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.util;
18
19 import org.openecomp.core.utilities.file.FileContentHandler;
20 import org.openecomp.core.utilities.file.FileUtils;
21 import org.openecomp.core.validation.api.ValidationManager;
22 import org.openecomp.core.validation.factory.ValidationManagerFactory;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.common.utils.SdcCommon;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.List;
31 import java.util.Map;
32
33
34 public class ValidationManagerUtil {
35
36   private ValidationManagerUtil(){}
37
38   /**
39    * Handle missing manifest.
40    *
41    * @param fileContentMap the file content map
42    * @param errors         the errors
43    */
44   public static void handleMissingManifest(FileContentHandler fileContentMap,
45                                            Map<String, List<ErrorMessage>> errors) throws IOException {
46     try (InputStream manifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
47       if (manifest == null) {
48         ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.MANIFEST_NAME, errors)
49                 .add(new ErrorMessage(ErrorLevel.ERROR, Messages.MANIFEST_NOT_EXIST.getErrorMessage()));
50       }
51     }
52   }
53
54   /**
55    * Init validation manager validation manager.
56    *
57    * @param fileContentMap the file content map
58    * @return the validation manager
59    */
60   public static ValidationManager initValidationManager(FileContentHandler fileContentMap) {
61     ValidationManager validationManager = ValidationManagerFactory.getInstance().createInterface();
62     fileContentMap.getFileList().forEach(fileName -> validationManager
63         .addFile(fileName, FileUtils.toByteArray(fileContentMap.getFileContentAsStream(fileName))));
64     return validationManager;
65   }
66 }