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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
19 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
21 import org.apache.commons.collections4.CollectionUtils;
22 import org.apache.commons.collections4.MapUtils;
23 import org.openecomp.core.dao.UniqueValueDaoFactory;
24 import org.openecomp.core.model.dao.ServiceModelDao;
25 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
26 import org.openecomp.core.util.UniqueValueUtil;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.core.utilities.file.FileUtils;
29 import org.openecomp.core.utilities.json.JsonUtil;
30 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.common.errors.Messages;
33 import org.openecomp.sdc.common.utils.CommonUtil;
34 import org.openecomp.sdc.common.utils.SdcCommon;
35 import org.openecomp.sdc.datatypes.error.ErrorLevel;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
38 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
39 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
40 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDaoFactory;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
64 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
65 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManagerFactory;
66 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
67 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
68 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
69 import org.openecomp.sdc.versioning.dao.types.Version;
71 import java.io.IOException;
72 import java.io.InputStream;
73 import java.nio.ByteBuffer;
74 import java.util.Collection;
75 import java.util.HashMap;
76 import java.util.List;
78 import java.util.Objects;
79 import java.util.Optional;
81 import java.util.stream.Collectors;
83 public class OrchestrationUtil {
85 public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
86 public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl";
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(),
100 MonitoringUploadDaoFactory.getInstance().createInterface(),
101 ProcessDaoFactory.getInstance().createInterface(),
102 OrchestrationTemplateDaoFactory.getInstance().createInterface(),
103 ComponentDaoFactory.getInstance().createInterface(),
104 ServiceModelDaoFactory.getInstance().createInterface(),
105 ComponentDependencyModelDaoFactory.getInstance().createInterface(),
106 CompositionEntityDataManagerFactory.getInstance().createInterface(),
107 CompositionDataExtractorFactory.getInstance().createInterface());
110 private OrchestrationUtil(
112 ComponentArtifactDao componentArtifactDao,
113 ProcessDao processDao,
114 OrchestrationTemplateDao orchestrationTemplateDataDao,
115 ComponentDao componentDao,
116 ServiceModelDao serviceModelDao,
117 ComponentDependencyModelDao componentDependencyModelDao,
118 CompositionEntityDataManager compositionEntityDataManager,
119 CompositionDataExtractor compositionDataExtractor) {
120 this.nicDao = nicDao;
121 this.componentArtifactDao = componentArtifactDao;
122 this.processDao = processDao;
123 this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
124 this.componentDao = componentDao;
125 this.serviceModelDao = serviceModelDao;
126 this.componentDependencyModelDao = componentDependencyModelDao;
127 this.compositionEntityDataManager = compositionEntityDataManager;
128 this.compositionDataExtractor = compositionDataExtractor;
131 public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type,
132 UploadFileResponse uploadFileResponse,
133 byte[] uploadedFileData) {
134 FileContentHandler contentMap = null;
136 contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
137 } catch (IOException exception) {
138 uploadFileResponse.addStructureError(
139 SdcCommon.UPLOAD_FILE,
140 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
141 } catch (CoreException coreException) {
142 uploadFileResponse.addStructureError(
143 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
145 return Optional.ofNullable(contentMap);
148 public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version version,
149 Map<String, String> componentsQustanniare,
150 Map<String, Map<String, String>>
151 componentNicsQustanniare,
152 Map<String, Collection<ComponentMonitoringUploadEntity>>
154 Map<String, Collection<ProcessEntity>>
156 Map<String, ProcessEntity> processArtifact) {
158 Collection<ComponentEntity> componentsCompositionAndQuestionnaire =
159 componentDao.listCompositionAndQuestionnaire(vspId, version);
160 componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
161 String componentName = componentEntity.getComponentCompositionData().getName();
162 componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData());
163 backupMibData(vspId, version, componentEntity, componentName, componentMibList);
164 backupProcess(vspId, version, componentEntity.getId(), componentName, componentProcesses,
166 backupNicsQuestionnaire(vspId, version, componentEntity, componentName,
167 componentNicsQustanniare);
171 private void backupMibData(String vspId, Version version, ComponentEntity componentEntity,
172 String componentName,
173 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
174 Collection<ComponentMonitoringUploadEntity> componentMib =
175 componentArtifactDao.listArtifacts(new
176 ComponentMonitoringUploadEntity(vspId, version, componentEntity.getId(),
178 if (CollectionUtils.isNotEmpty(componentMib)) {
179 componentMibList.put(componentName, componentMib);
183 private void backupProcess(String vspId, Version version, String componentId,
184 String componentName, Map<String,
185 Collection<ProcessEntity>> processes,
186 Map<String, ProcessEntity> processArtifact) {
187 Collection<ProcessEntity> processList =
188 processDao.list(new ProcessEntity(vspId, version, componentId, null));
189 if (!processList.isEmpty()) {
190 processes.put(componentName, processList);
191 processList.forEach(process -> {
192 ProcessEntity artifact = processDao
193 .getArtifact(new ProcessEntity(vspId, version, componentId, process.getId()));
194 if (artifact.getArtifact() != null) {
195 processArtifact.put(process.getId(), artifact);
201 private void backupNicsQuestionnaire(String vspId, Version version,
202 ComponentEntity componentEntity,
203 String componentName,
204 Map<String, Map<String, String>> componentNicsQustanniare) {
205 Collection<NicEntity>
206 nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
207 if (CollectionUtils.isNotEmpty(nics)) {
208 Map<String, String> nicsQuestionnaire = new HashMap<>();
209 nics.forEach(nicEntity -> {
210 NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version,
211 componentEntity.getId(), nicEntity.getId());
213 nicsQuestionnaire.put(nicEntity.getNicCompositionData().getName(),
214 nicQuestionnaire.getQuestionnaireData());
216 componentNicsQustanniare.put(componentName, nicsQuestionnaire);
220 public void retainComponentQuestionnaireData(String vspId, Version version,
221 Map<String, String> componentsQustanniare,
222 Map<String, Map<String, String>>
223 componentNicsQustanniare,
224 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
225 Map<String, Collection<ProcessEntity>> processes,
226 Map<String, ProcessEntity> processArtifact) {
228 restoreProcess(vspId, version, null, null, processes, processArtifact);
229 Collection<ComponentEntity> components =
230 componentDao.list(new ComponentEntity(vspId, version, null));
231 components.forEach(componentEntity -> {
232 String componentName = componentEntity.getComponentCompositionData().getName();
233 if (componentsQustanniare.containsKey(componentName)) {
234 componentDao.updateQuestionnaireData(vspId, version,
235 componentEntity.getId(),
236 componentsQustanniare.get(componentEntity.getComponentCompositionData()
238 if (componentNicsQustanniare.containsKey(componentName)) {
239 restoreComponentNicQuestionnaire(vspId, version, componentName, componentEntity,
240 componentNicsQustanniare);
242 //MIB //todo add for VES_EVENTS
243 if (componentMibList.containsKey(componentName)) {
244 restoreComponentMibData(componentName, componentEntity, componentMibList);
247 restoreProcess(vspId, version, componentEntity.getId(), componentName, processes,
253 private void restoreComponentNicQuestionnaire(String vspId, Version version,
254 String componentName,
255 ComponentEntity componentEntity,
256 Map<String, Map<String, String>> componentNicsQustanniare) {
257 Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
258 Collection<NicEntity> nics =
259 nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
260 nics.forEach(nicEntity -> {
261 if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
262 nicDao.updateQuestionnaireData(vspId, version,
263 componentEntity.getId(), nicEntity.getId(),
264 nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
269 private void restoreComponentMibData(String componentName, ComponentEntity componentEntity,
270 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
271 Collection<ComponentMonitoringUploadEntity> mibList = componentMibList.get(componentName);
272 mibList.forEach(mib -> {
273 mib.setComponentId(componentEntity.getId());
274 componentArtifactDao.create(mib);
278 private void restoreProcess(String vspId, Version version, String componentId,
279 String componentName,
280 Map<String, Collection<ProcessEntity>> processes,
281 Map<String, ProcessEntity> processArtifact) {
282 if (processes.containsKey(componentName)) {
283 Collection<ProcessEntity> processList = processes.get(componentName);
284 processList.forEach(process -> {
285 process.setComponentId(componentId);
286 UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance()
288 uniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, version.getId(), componentId,
290 processDao.create(process);
291 if (processArtifact.containsKey(process.getId())) {
292 ProcessEntity artifact = processArtifact.get(process.getId());
293 processDao.uploadArtifact(artifact);
299 public void deleteUploadDataAndContent(String vspId, Version version) {
300 VendorSoftwareProductInfoDaoFactory.getInstance().createInterface()
301 .delete(new VspDetails(vspId, version));
304 public void saveUploadData(VspDetails vspDetails,
305 OrchestrationTemplateCandidateData candidateData,
306 InputStream uploadedFileData,
307 FileContentHandler fileContentMap, HeatStructureTree tree) {
308 Map<String, Object> manifestAsMap =
309 fileContentMap.containsFile(SdcCommon.MANIFEST_NAME)
310 ? (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
311 SdcCommon.MANIFEST_NAME), Map.class)
314 OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity();
315 uploadData.setFileSuffix(candidateData.getFileSuffix());
316 uploadData.setFileName(candidateData.getFileName());
317 uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
318 uploadData.setValidationDataStructure(new ValidationStructureList(tree));
319 uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null :
320 (String) manifestAsMap.get("name"));
321 uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null :
322 (String) manifestAsMap.get("version"));
323 uploadData.setFilesDataStructure(candidateData.getFilesDataStructure());
324 orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
326 VspMergeDaoFactory.getInstance().createInterface()
327 .updateVspModelId(vspDetails.getId(), vspDetails.getVersion());
330 public void saveServiceModel(String vspId, Version version,
331 ToscaServiceModel serviceModelToExtract,
332 ToscaServiceModel serviceModelToStore) {
333 if (serviceModelToExtract != null) {
334 serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
335 //Extracting the compostion data from the output service model of the first phase of
337 compositionEntityDataManager.saveCompositionData(vspId, version,
338 compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
342 public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
343 Map<String, List<ErrorMessage>> validationErrors) {
344 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
345 heatTreeManager.createTree();
346 heatTreeManager.addErrors(validationErrors);
347 return heatTreeManager.getTree();
350 public void updateVspComponentDependencies(String vspId, Version version,
352 vspComponentIdNameInfoBeforeProcess,
353 Collection<ComponentDependencyModelEntity>
354 componentDependenciesBeforeDelete) {
355 Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
356 if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
357 Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
358 if (CollectionUtils.isNotEmpty(componentDependenciesBeforeDelete)) {
359 restoreComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess,
360 componentDependenciesBeforeDelete, updatedVspComponentNames,
361 updatedVspComponentNameIdInfo);
366 private void restoreComponentDependencies(String vspId, Version version,
367 Map<String, String> vspComponentIdNameInfoBeforeProcess,
368 Collection<ComponentDependencyModelEntity>
369 componentDependenciesBeforeDelete,
370 Set<String> updatedVspComponentNames,
371 Map<String, String> updatedVspComponentNameIdInfo) {
372 for (ComponentDependencyModelEntity componentDependency : componentDependenciesBeforeDelete) {
373 String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
374 .getSourceComponentId());
375 String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
376 .getTargetComponentId());
377 if (updatedVspComponentNames.contains(sourceComponentName)
378 && (updatedVspComponentNames.contains(targetComponentName))) {
379 ComponentDependencyModelEntity restoredDependency =
380 new ComponentDependencyModelEntity(vspId, version, null);
381 String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
382 restoredDependency.setSourceComponentId(newSourceComponentId);
383 String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
384 restoredDependency.setTargetComponentId(newTargetComponentId);
385 restoredDependency.setRelation(componentDependency.getRelation());
386 componentDependencyModelDao.create(restoredDependency);
391 public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
392 Collection<ComponentEntity> updatedVspComponents =
393 componentDao.list(new ComponentEntity(vspId, version, null));
394 Map<String, String> vspComponentIdNameMap = new HashMap<>();
395 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
396 vspComponentIdNameMap = updatedVspComponents.stream()
397 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
398 .collect(Collectors.toMap(ComponentEntity::getId,
399 componentEntity -> componentEntity.getComponentCompositionData().getName()));
402 return vspComponentIdNameMap;
405 public Collection<ComponentDependencyModelEntity> getComponentDependenciesBeforeDelete(String
406 vspId, Version version) {
407 return componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId,
411 private Map<String, String> getVspComponentNameIdInfo(String vspId,
413 Collection<ComponentEntity> updatedVspComponents =
414 componentDao.list(new ComponentEntity(vspId, version, null));
415 Map<String, String> vspComponentNameIdMap = new HashMap<>();
416 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
417 vspComponentNameIdMap = updatedVspComponents.stream()
418 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
420 .toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(),
421 ComponentEntity::getId));
423 return vspComponentNameIdMap;