[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.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;
48
49 import java.io.InputStream;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54 public class HeatToToscaTranslatorImpl implements HeatToToscaTranslator {
55
56   private TranslationContext translationContext = new TranslationContext();
57   private ValidationManager validationManager =
58       ValidationManagerFactory.getInstance().createInterface();
59   private boolean isValid = false;
60
61
62   @Override
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());
72     isValid = false;
73   }
74
75   @Override
76   public void addFile(String name, byte[] content) {
77     translationContext.addFile(name, content);
78     validationManager.addFile(name, content);
79     isValid = false;
80   }
81
82   @Override
83   public void addFile(String name, InputStream content) {
84     addFile(name, FileUtils.toByteArray(content));
85   }
86
87
88   @Override
89   public Map<String, List<ErrorMessage>> validate() {
90
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()));
95       return errors;
96     }
97
98     if (MapUtils.isEmpty(errors)) {
99       errors = validationManager.validate();
100     }
101     if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
102       isValid = true;
103     }
104     return errors;
105   }
106
107   @Override
108   public TranslatorOutput translate() {
109     TranslationService translationService = new TranslationService();
110     TranslatorOutput translatorOutput = new TranslatorOutput();
111     UnifiedCompositionManager unifiedCompositionManager = new UnifiedCompositionManager(new
112         ConsolidationService(new UnifiedCompositionService()));
113     if (!isValid) {
114       Map<String, List<ErrorMessage>> errors = validate();
115
116       if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
117         translatorOutput.setErrorMessages(errors);
118         return translatorOutput;
119       }
120     }
121
122     translatorOutput = translationService.translateHeatFiles(translationContext);
123     ToscaServiceModel unifiedToscaServiceModel = unifiedCompositionManager
124         .createUnifiedComposition(translatorOutput.getToscaServiceModel(), translationContext);
125     translatorOutput.setToscaServiceModel(unifiedToscaServiceModel);
126     return translatorOutput;
127   }
128
129   @Override
130   public void addExternalArtifacts(String name, byte[] content) {
131     translationContext.addExternalArtifacts(name, content);
132   }
133
134   @Override
135   public void addExternalArtifacts(String name, InputStream content) {
136     addExternalArtifacts(name, FileUtils.toByteArray(content));
137   }
138
139   private void addFilesFromManifestToTranslationContextManifestFilesMap(
140       List<FileData> fileDataListFromManifest) {
141     for (FileData fileFromManfiest : fileDataListFromManifest) {
142       translationContext.addManifestFile(fileFromManfiest.getFile(), fileFromManfiest.getType());
143     }
144   }
145
146
147 }