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.config.api.Configuration;
25 import org.openecomp.config.api.ConfigurationManager;
26 import org.openecomp.core.translator.api.HeatToToscaTranslator;
27 import org.openecomp.core.translator.datatypes.TranslatorOutput;
28 import org.openecomp.core.utilities.file.FileUtils;
29 import org.openecomp.core.utilities.json.JsonUtil;
30 import org.openecomp.core.validation.api.ValidationManager;
31 import org.openecomp.core.validation.factory.ValidationManagerFactory;
32 import org.openecomp.core.validation.util.MessageContainerUtil;
33 import org.openecomp.sdc.common.errors.Messages;
34 import org.openecomp.sdc.common.utils.SdcCommon;
35 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
36 import org.openecomp.sdc.datatypes.error.ErrorLevel;
37 import org.openecomp.sdc.datatypes.error.ErrorMessage;
38 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
39 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
40 import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
43 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
44 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationService;
45 import org.openecomp.sdc.translator.services.heattotosca.TranslationService;
46 import org.openecomp.sdc.translator.services.heattotosca.UnifiedCompositionManager;
47 import org.openecomp.sdc.translator.services.heattotosca.UnifiedCompositionService;
49 import java.io.InputStream;
50 import java.util.HashMap;
51 import java.util.List;
54 public class HeatToToscaTranslatorImpl implements HeatToToscaTranslator {
56 private TranslationContext translationContext = new TranslationContext();
57 private ValidationManager validationManager =
58 ValidationManagerFactory.getInstance().createInterface();
59 private boolean isValid = false;
63 public void addManifest(String name, byte[] content) {
64 ManifestContent manifestData = JsonUtil.json2Object(new String(content), ManifestContent.class);
65 ManifestFile manifest = new ManifestFile();
66 manifest.setName(name);
67 manifest.setContent(manifestData);
68 translationContext.setManifest(manifest);
69 translationContext.addFile(name, content);
70 validationManager.addFile(SdcCommon.MANIFEST_NAME, content);
71 addFilesFromManifestToTranslationContextManifestFilesMap(manifestData.getData());
76 public void addFile(String name, byte[] content) {
77 translationContext.addFile(name, content);
78 validationManager.addFile(name, content);
83 public void addFile(String name, InputStream content) {
84 addFile(name, FileUtils.toByteArray(content));
89 public Map<String, List<ErrorMessage>> validate() {
91 Map<String, List<ErrorMessage>> errors = new HashMap<>();
92 if (translationContext.getManifest() == null) {
93 ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.MANIFEST_NAME, errors)
94 .add(new ErrorMessage(ErrorLevel.ERROR, Messages.MANIFEST_NOT_EXIST.getErrorMessage()));
98 if (MapUtils.isEmpty(errors)) {
99 errors = validationManager.validate();
101 if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
108 public TranslatorOutput translate() {
109 TranslationService translationService = new TranslationService();
110 TranslatorOutput translatorOutput = new TranslatorOutput();
111 UnifiedCompositionManager unifiedCompositionManager = new UnifiedCompositionManager(new
112 ConsolidationService(new UnifiedCompositionService()));
114 Map<String, List<ErrorMessage>> errors = validate();
116 if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
117 translatorOutput.setErrorMessages(errors);
118 return translatorOutput;
122 translatorOutput = translationService.translateHeatFiles(translationContext);
123 ToscaServiceModel unifiedToscaServiceModel = unifiedCompositionManager
124 .createUnifiedComposition(translatorOutput.getToscaServiceModel(), translationContext);
125 translatorOutput.setToscaServiceModel(unifiedToscaServiceModel);
126 return translatorOutput;
130 public void addExternalArtifacts(String name, byte[] content) {
131 translationContext.addExternalArtifacts(name, content);
135 public void addExternalArtifacts(String name, InputStream content) {
136 addExternalArtifacts(name, FileUtils.toByteArray(content));
139 private void addFilesFromManifestToTranslationContextManifestFilesMap(
140 List<FileData> fileDataListFromManifest) {
141 for (FileData fileFromManfiest : fileDataListFromManifest) {
142 translationContext.addManifestFile(fileFromManfiest.getFile(), fileFromManfiest.getType());