54a0285026791110a152ac74736c12982fb4fb76
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.impl.heattotosca;
22
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.factory.ValidationManagerFactory;
30 import org.openecomp.core.validation.util.MessageContainerUtil;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.common.utils.SdcCommon;
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.tosca.datatypes.ToscaServiceModel;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
40 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationService;
41 import org.openecomp.sdc.translator.services.heattotosca.TranslationService;
42 import org.openecomp.sdc.translator.services.heattotosca.UnifiedCompositionManager;
43 import org.openecomp.sdc.translator.services.heattotosca.UnifiedCompositionService;
44
45 import java.io.InputStream;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49
50 public class HeatToToscaTranslatorImpl implements HeatToToscaTranslator {
51
52   private TranslationContext translationContext = new TranslationContext();
53   private ValidationManager validationManager =
54       ValidationManagerFactory.getInstance().createInterface();
55   private boolean isValid = false;
56
57
58   @Override
59   public void addManifest(String name, byte[] content) {
60     ManifestContent manifestData = JsonUtil.json2Object(new String(content), ManifestContent.class);
61     ManifestFile manifest = new ManifestFile();
62     manifest.setName(name);
63     manifest.setContent(manifestData);
64     translationContext.setManifest(manifest);
65     translationContext.addFile(name, content);
66     validationManager.addFile(SdcCommon.MANIFEST_NAME, content);
67     addFilesFromManifestToTranslationContextManifestFilesMap(manifestData.getData());
68     isValid = false;
69   }
70
71   @Override
72   public void addFile(String name, byte[] content) {
73     translationContext.addFile(name, content);
74     validationManager.addFile(name, content);
75     isValid = false;
76   }
77
78   @Override
79   public void addFile(String name, InputStream content) {
80     addFile(name, FileUtils.toByteArray(content));
81   }
82
83
84   @Override
85   public Map<String, List<ErrorMessage>> validate() {
86
87     Map<String, List<ErrorMessage>> errors = new HashMap<>();
88     if (translationContext.getManifest() == null) {
89       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.MANIFEST_NAME, errors)
90           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.MANIFEST_NOT_EXIST.getErrorMessage()));
91       return errors;
92     }
93
94     if (MapUtils.isEmpty(errors)) {
95       errors = validationManager.validate();
96     }
97     if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
98       isValid = true;
99     }
100     return errors;
101   }
102
103   @Override
104   public TranslatorOutput translate() {
105     TranslationService translationService = new TranslationService();
106     TranslatorOutput translatorOutput = new TranslatorOutput();
107     UnifiedCompositionManager unifiedCompositionManager = new UnifiedCompositionManager(new
108         ConsolidationService(new UnifiedCompositionService()));
109     if (!isValid) {
110       Map<String, List<ErrorMessage>> errors = validate();
111
112       if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
113         translatorOutput.setErrorMessages(errors);
114         return translatorOutput;
115       }
116     }
117
118     translatorOutput = translationService.translateHeatFiles(translationContext);
119     ToscaServiceModel unifiedToscaServiceModel = unifiedCompositionManager
120         .createUnifiedComposition(translatorOutput.getToscaServiceModel(), translationContext);
121     translatorOutput.setToscaServiceModel(unifiedToscaServiceModel);
122     return translatorOutput;
123   }
124
125   @Override
126   public void addExternalArtifacts(String name, byte[] content) {
127     translationContext.addExternalArtifacts(name, content);
128   }
129
130   @Override
131   public void addExternalArtifacts(String name, InputStream content) {
132     addExternalArtifacts(name, FileUtils.toByteArray(content));
133   }
134
135   private void addFilesFromManifestToTranslationContextManifestFilesMap(
136       List<FileData> fileDataListFromManifest) {
137     for (FileData fileFromManfiest : fileDataListFromManifest) {
138       translationContext.addManifestFile(fileFromManfiest.getFile(), fileFromManfiest.getType());
139     }
140   }
141
142
143 }