2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
18 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.ByteBuffer;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
30 import java.util.stream.Collectors;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.apache.commons.collections4.MapUtils;
33 import org.openecomp.core.dao.UniqueValueDaoFactory;
34 import org.openecomp.core.model.dao.ServiceModelDao;
35 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
36 import org.openecomp.core.util.UniqueValueUtil;
37 import org.openecomp.core.utilities.file.FileContentHandler;
38 import org.openecomp.core.utilities.file.FileUtils;
39 import org.openecomp.core.utilities.json.JsonUtil;
40 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
41 import org.openecomp.sdc.common.errors.CoreException;
42 import org.openecomp.sdc.common.errors.Messages;
43 import org.openecomp.sdc.common.utils.CommonUtil;
44 import org.openecomp.sdc.common.utils.SdcCommon;
45 import org.openecomp.sdc.datatypes.error.ErrorLevel;
46 import org.openecomp.sdc.datatypes.error.ErrorMessage;
47 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
48 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
49 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
50 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
51 import org.openecomp.sdc.logging.api.Logger;
52 import org.openecomp.sdc.logging.api.LoggerFactory;
53 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
54 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
55 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManagerFactory;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
64 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
65 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
66 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
67 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
68 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
69 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDaoFactory;
70 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
73 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
74 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
75 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
77 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
78 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
79 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
81 import org.openecomp.sdc.versioning.dao.types.Version;
83 public class OrchestrationUtil {
85 public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
86 public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl";
87 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationUtil.class);
88 private final NicDao nicDao;
89 private final ComponentArtifactDao componentArtifactDao;
90 private final ProcessDao processDao;
91 private final OrchestrationTemplateDao orchestrationTemplateDataDao;
92 private final ComponentDao componentDao;
93 private final ServiceModelDao serviceModelDao;
94 private final ComponentDependencyModelDao componentDependencyModelDao;
95 private final CompositionEntityDataManager compositionEntityDataManager;
96 private final CompositionDataExtractor compositionDataExtractor;
98 public OrchestrationUtil() {
99 this(NicDaoFactory.getInstance().createInterface(), MonitoringUploadDaoFactory.getInstance().createInterface(),
100 ProcessDaoFactory.getInstance().createInterface(), OrchestrationTemplateDaoFactory.getInstance().createInterface(),
101 ComponentDaoFactory.getInstance().createInterface(), ServiceModelDaoFactory.getInstance().createInterface(),
102 ComponentDependencyModelDaoFactory.getInstance().createInterface(), CompositionEntityDataManagerFactory.getInstance().createInterface(),
103 CompositionDataExtractorFactory.getInstance().createInterface());
106 private OrchestrationUtil(NicDao nicDao, ComponentArtifactDao componentArtifactDao, ProcessDao processDao,
107 OrchestrationTemplateDao orchestrationTemplateDataDao, ComponentDao componentDao, ServiceModelDao serviceModelDao,
108 ComponentDependencyModelDao componentDependencyModelDao, CompositionEntityDataManager compositionEntityDataManager,
109 CompositionDataExtractor compositionDataExtractor) {
110 this.nicDao = nicDao;
111 this.componentArtifactDao = componentArtifactDao;
112 this.processDao = processDao;
113 this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
114 this.componentDao = componentDao;
115 this.serviceModelDao = serviceModelDao;
116 this.componentDependencyModelDao = componentDependencyModelDao;
117 this.compositionEntityDataManager = compositionEntityDataManager;
118 this.compositionDataExtractor = compositionDataExtractor;
121 public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type, UploadFileResponse uploadFileResponse,
122 byte[] uploadedFileData) {
123 FileContentHandler contentMap = null;
125 contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
126 } catch (IOException exception) {
128 .addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
129 LOGGER.error("{}\n{}", Messages.INVALID_ZIP_FILE.getErrorMessage(), exception.getMessage(), exception);
130 } catch (CoreException coreException) {
131 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
132 LOGGER.error(coreException.getMessage(), coreException);
134 return Optional.ofNullable(contentMap);
137 public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap, Map<String, List<ErrorMessage>> validationErrors) {
138 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
139 heatTreeManager.createTree();
140 heatTreeManager.addErrors(validationErrors);
141 return heatTreeManager.getTree();
144 public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version version, Map<String, String> componentsQustanniare,
145 Map<String, Map<String, String>> componentNicsQustanniare,
146 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
147 Map<String, Collection<ProcessEntity>> componentProcesses,
148 Map<String, ProcessEntity> processArtifact) {
149 Collection<ComponentEntity> componentsCompositionAndQuestionnaire = componentDao.listCompositionAndQuestionnaire(vspId, version);
150 componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
151 String componentName = componentEntity.getComponentCompositionData().getName();
152 componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData());
153 backupMibData(vspId, version, componentEntity, componentName, componentMibList);
154 backupProcess(vspId, version, componentEntity.getId(), componentName, componentProcesses, processArtifact);
155 backupNicsQuestionnaire(vspId, version, componentEntity, componentName, componentNicsQustanniare);
159 private void backupMibData(String vspId, Version version, ComponentEntity componentEntity, String componentName,
160 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
161 Collection<ComponentMonitoringUploadEntity> componentMib = componentArtifactDao
162 .listArtifacts(new ComponentMonitoringUploadEntity(vspId, version, componentEntity.getId(), null));
163 if (CollectionUtils.isNotEmpty(componentMib)) {
164 componentMibList.put(componentName, componentMib);
168 private void backupProcess(String vspId, Version version, String componentId, String componentName,
169 Map<String, Collection<ProcessEntity>> processes, Map<String, ProcessEntity> processArtifact) {
170 Collection<ProcessEntity> processList = processDao.list(new ProcessEntity(vspId, version, componentId, null));
171 if (!processList.isEmpty()) {
172 processes.put(componentName, processList);
173 processList.forEach(process -> {
174 ProcessEntity artifact = processDao.getArtifact(new ProcessEntity(vspId, version, componentId, process.getId()));
175 if (artifact.getArtifact() != null) {
176 processArtifact.put(process.getId(), artifact);
182 private void backupNicsQuestionnaire(String vspId, Version version, ComponentEntity componentEntity, String componentName,
183 Map<String, Map<String, String>> componentNicsQustanniare) {
184 Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
185 if (CollectionUtils.isNotEmpty(nics)) {
186 Map<String, String> nicsQuestionnaire = new HashMap<>();
187 nics.forEach(nicEntity -> {
188 NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version, componentEntity.getId(), nicEntity.getId());
189 nicsQuestionnaire.put(nicEntity.getNicCompositionData().getName(), nicQuestionnaire.getQuestionnaireData());
191 componentNicsQustanniare.put(componentName, nicsQuestionnaire);
195 public void retainComponentQuestionnaireData(String vspId, Version version, Map<String, String> componentsQustanniare,
196 Map<String, Map<String, String>> componentNicsQustanniare,
197 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
198 Map<String, Collection<ProcessEntity>> processes, Map<String, ProcessEntity> processArtifact) {
200 restoreProcess(vspId, version, null, null, processes, processArtifact);
201 Collection<ComponentEntity> components = componentDao.list(new ComponentEntity(vspId, version, null));
202 components.forEach(componentEntity -> {
203 String componentName = componentEntity.getComponentCompositionData().getName();
204 if (componentsQustanniare.containsKey(componentName)) {
205 componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
206 componentsQustanniare.get(componentEntity.getComponentCompositionData().getName()));
207 if (componentNicsQustanniare.containsKey(componentName)) {
208 restoreComponentNicQuestionnaire(vspId, version, componentName, componentEntity, componentNicsQustanniare);
210 //MIB //todo add for VES_EVENTS
211 if (componentMibList.containsKey(componentName)) {
212 restoreComponentMibData(componentName, componentEntity, componentMibList);
215 restoreProcess(vspId, version, componentEntity.getId(), componentName, processes, processArtifact);
220 private void restoreComponentNicQuestionnaire(String vspId, Version version, String componentName, ComponentEntity componentEntity,
221 Map<String, Map<String, String>> componentNicsQustanniare) {
222 Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
223 Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
224 nics.forEach(nicEntity -> {
225 if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
226 nicDao.updateQuestionnaireData(vspId, version, componentEntity.getId(), nicEntity.getId(),
227 nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
232 private void restoreComponentMibData(String componentName, ComponentEntity componentEntity,
233 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
234 Collection<ComponentMonitoringUploadEntity> mibList = componentMibList.get(componentName);
235 mibList.forEach(mib -> {
236 mib.setComponentId(componentEntity.getId());
237 componentArtifactDao.create(mib);
241 private void restoreProcess(String vspId, Version version, String componentId, String componentName,
242 Map<String, Collection<ProcessEntity>> processes, Map<String, ProcessEntity> processArtifact) {
243 if (processes.containsKey(componentName)) {
244 Collection<ProcessEntity> processList = processes.get(componentName);
245 processList.forEach(process -> {
246 process.setComponentId(componentId);
247 UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface());
248 uniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, version.getId(), componentId, process.getName());
249 processDao.create(process);
250 if (processArtifact.containsKey(process.getId())) {
251 ProcessEntity artifact = processArtifact.get(process.getId());
252 processDao.uploadArtifact(artifact);
258 public void deleteUploadDataAndContent(String vspId, Version version) {
259 VendorSoftwareProductInfoDaoFactory.getInstance().createInterface().delete(new VspDetails(vspId, version));
262 public void saveUploadData(VspDetails vspDetails, OrchestrationTemplateCandidateData candidateData, InputStream uploadedFileData,
263 FileContentHandler fileContentMap, HeatStructureTree tree) {
264 Map<String, Object> manifestAsMap = fileContentMap.containsFile(SdcCommon.MANIFEST_NAME) ? (Map<String, Object>) JsonUtil
265 .json2Object(fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME), Map.class) : new HashMap<>();
266 OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity();
267 uploadData.setFileSuffix(candidateData.getFileSuffix());
268 uploadData.setFileName(candidateData.getFileName());
269 uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
270 uploadData.setValidationDataStructure(new ValidationStructureList(tree));
271 uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null : (String) manifestAsMap.get("name"));
272 uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null : (String) manifestAsMap.get("version"));
273 uploadData.setFilesDataStructure(candidateData.getFilesDataStructure());
274 orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
275 VspMergeDaoFactory.getInstance().createInterface().updateHint(vspDetails.getId(), vspDetails.getVersion());
278 public void saveServiceModel(String vspId, Version version, ToscaServiceModel serviceModelToExtract, ToscaServiceModel serviceModelToStore) {
279 if (serviceModelToExtract != null) {
280 serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
281 //Extracting the compostion data from the output service model of the first phase of
284 compositionEntityDataManager
285 .saveCompositionData(vspId, version, compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
289 public void updateVspComponentDependencies(String vspId, Version version, Map<String, String> vspComponentIdNameInfoBeforeProcess,
290 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete) {
291 Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
292 if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
293 Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
294 if (CollectionUtils.isNotEmpty(componentDependenciesBeforeDelete)) {
295 restoreComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete,
296 updatedVspComponentNames, updatedVspComponentNameIdInfo);
301 private void restoreComponentDependencies(String vspId, Version version, Map<String, String> vspComponentIdNameInfoBeforeProcess,
302 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete,
303 Set<String> updatedVspComponentNames, Map<String, String> updatedVspComponentNameIdInfo) {
304 for (ComponentDependencyModelEntity componentDependency : componentDependenciesBeforeDelete) {
305 String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency.getSourceComponentId());
306 String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency.getTargetComponentId());
307 if (updatedVspComponentNames.contains(sourceComponentName) && (updatedVspComponentNames.contains(targetComponentName))) {
308 ComponentDependencyModelEntity restoredDependency = new ComponentDependencyModelEntity(vspId, version, null);
309 String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
310 restoredDependency.setSourceComponentId(newSourceComponentId);
311 String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
312 restoredDependency.setTargetComponentId(newTargetComponentId);
313 restoredDependency.setRelation(componentDependency.getRelation());
314 componentDependencyModelDao.create(restoredDependency);
319 public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
320 Collection<ComponentEntity> updatedVspComponents = componentDao.list(new ComponentEntity(vspId, version, null));
321 Map<String, String> vspComponentIdNameMap = new HashMap<>();
322 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
323 vspComponentIdNameMap = updatedVspComponents.stream().filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
324 .collect(Collectors.toMap(ComponentEntity::getId, componentEntity -> componentEntity.getComponentCompositionData().getName()));
326 return vspComponentIdNameMap;
329 public Collection<ComponentDependencyModelEntity> getComponentDependenciesBeforeDelete(String vspId, Version version) {
330 return componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));
333 private Map<String, String> getVspComponentNameIdInfo(String vspId, Version version) {
334 Collection<ComponentEntity> updatedVspComponents = componentDao.list(new ComponentEntity(vspId, version, null));
335 Map<String, String> vspComponentNameIdMap = new HashMap<>();
336 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
337 vspComponentNameIdMap = updatedVspComponents.stream().filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
338 .collect(Collectors.toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(), ComponentEntity::getId));
340 return vspComponentNameIdMap;