push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / impl / heattotosca / HeatToToscaTranslatorImpl.java
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.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;
40
41 import java.io.InputStream;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 public class HeatToToscaTranslatorImpl implements HeatToToscaTranslator {
47
48   private TranslationContext translationContext = new TranslationContext();
49   private ValidationManager validationManager =
50       ValidationManagerFactory.getInstance().createInterface();
51   private boolean isValid = false;
52
53
54   @Override
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());
64     isValid = false;
65   }
66
67   @Override
68   public void addFile(String name, byte[] content) {
69     translationContext.addFile(name, content);
70     validationManager.addFile(name, content);
71     isValid = false;
72   }
73
74   @Override
75   public void addFile(String name, InputStream content) {
76     addFile(name, FileUtils.toByteArray(content));
77   }
78
79
80   @Override
81   public Map<String, List<ErrorMessage>> validate() {
82
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()));
87       return errors;
88     }
89
90     if (MapUtils.isEmpty(errors)) {
91       errors = validationManager.validate();
92     }
93     if (MapUtils.isEmpty(errors)) {
94       isValid = true;
95     }
96     return errors;
97   }
98
99   @Override
100   public TranslatorOutput translate() {
101     TranslationService translationService = new TranslationService();
102     TranslatorOutput translatorOutput = new TranslatorOutput();
103     if (!isValid) {
104       Map<String, List<ErrorMessage>> errors = validate();
105
106       if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
107         translatorOutput.setErrorMessages(errors);
108         return translatorOutput;
109       }
110     }
111
112     translatorOutput = translationService.translateHeatFiles(translationContext);
113     return translatorOutput;
114   }
115
116   @Override
117   public void addExternalArtifacts(String name, byte[] content) {
118     translationContext.addExternalArtifacts(name, content);
119   }
120
121   @Override
122   public void addExternalArtifacts(String name, InputStream content) {
123     addExternalArtifacts(name, FileUtils.toByteArray(content));
124   }
125
126   private void addFilesFromManifestToTranslationContextManifestFilesMap(
127       List<FileData> fileDataListFromManifest) {
128     for (FileData fileFromManfiest : fileDataListFromManifest) {
129       translationContext.addManifestFile(fileFromManfiest.getFile(), fileFromManfiest.getType());
130     }
131   }
132
133
134 }