2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.translator.impl.heattotosca;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.translator.api.HeatToToscaTranslator;
25 import org.openecomp.core.translator.datatypes.TranslatorOutput;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.core.validation.api.ValidationManager;
29 import org.openecomp.core.validation.errors.Messages;
30 import org.openecomp.core.validation.factory.ValidationManagerFactory;
31 import org.openecomp.core.validation.types.MessageContainerUtil;
32 import org.openecomp.sdc.common.utils.AsdcCommon;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.datatypes.error.ErrorMessage;
35 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
36 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
37 import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
38 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
39 import org.openecomp.sdc.translator.services.heattotosca.TranslationService;
41 import java.io.InputStream;
42 import java.util.HashMap;
43 import java.util.List;
46 public class HeatToToscaTranslatorImpl implements HeatToToscaTranslator {
48 private TranslationContext translationContext = new TranslationContext();
49 private ValidationManager validationManager =
50 ValidationManagerFactory.getInstance().createInterface();
51 private boolean isValid = false;
55 public void addManifest(String name, byte[] content) {
56 ManifestContent manifestData = JsonUtil.json2Object(new String(content), ManifestContent.class);
57 ManifestFile manifest = new ManifestFile();
58 manifest.setName(name);
59 manifest.setContent(manifestData);
60 translationContext.setManifest(manifest);
61 translationContext.addFile(name, content);
62 validationManager.addFile(AsdcCommon.MANIFEST_NAME, content);
63 addFilesFromManifestToTranslationContextManifestFilesMap(manifestData.getData());
68 public void addFile(String name, byte[] content) {
69 translationContext.addFile(name, content);
70 validationManager.addFile(name, content);
75 public void addFile(String name, InputStream content) {
76 addFile(name, FileUtils.toByteArray(content));
81 public Map<String, List<ErrorMessage>> validate() {
83 Map<String, List<ErrorMessage>> errors = new HashMap<>();
84 if (translationContext.getManifest() == null) {
85 ErrorMessage.ErrorMessageUtil.addMessage(AsdcCommon.MANIFEST_NAME, errors)
86 .add(new ErrorMessage(ErrorLevel.ERROR, Messages.MANIFEST_NOT_EXIST.getErrorMessage()));
90 if (MapUtils.isEmpty(errors)) {
91 errors = validationManager.validate();
93 if (MapUtils.isEmpty(errors)) {
100 public TranslatorOutput translate() {
101 TranslationService translationService = new TranslationService();
102 TranslatorOutput translatorOutput = new TranslatorOutput();
104 Map<String, List<ErrorMessage>> errors = validate();
106 if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
107 translatorOutput.setErrorMessages(errors);
108 return translatorOutput;
112 translatorOutput = translationService.translateHeatFiles(translationContext);
113 return translatorOutput;
117 public void addExternalArtifacts(String name, byte[] content) {
118 translationContext.addExternalArtifacts(name, content);
122 public void addExternalArtifacts(String name, InputStream content) {
123 addExternalArtifacts(name, FileUtils.toByteArray(content));
126 private void addFilesFromManifestToTranslationContextManifestFilesMap(
127 List<FileData> fileDataListFromManifest) {
128 for (FileData fileFromManfiest : fileDataListFromManifest) {
129 translationContext.addManifestFile(fileFromManfiest.getFile(), fileFromManfiest.getType());