2 * Copyright © 2016-2017 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 org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.model.dao.ServiceModelDao;
22 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
23 import org.openecomp.core.util.UniqueValueUtil;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.utilities.json.JsonUtil;
27 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.Messages;
30 import org.openecomp.sdc.common.utils.CommonUtil;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
35 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
36 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
37 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
38 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDaoFactory;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
61 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
62 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
63 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
64 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
66 import org.openecomp.sdc.versioning.dao.types.Version;
68 import java.io.IOException;
69 import java.io.InputStream;
70 import java.nio.ByteBuffer;
71 import java.util.Collection;
72 import java.util.HashMap;
73 import java.util.List;
75 import java.util.Objects;
76 import java.util.Optional;
78 import java.util.stream.Collectors;
80 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
82 public class OrchestrationUtil {
84 public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
85 public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl";
87 private final NicDao nicDao;
88 private final ComponentArtifactDao componentArtifactDao;
89 private final ProcessDao processDao;
90 private final OrchestrationTemplateDao orchestrationTemplateDataDao;
91 private final ComponentDao componentDao;
92 private final ServiceModelDao serviceModelDao;
93 private final ComponentDependencyModelDao componentDependencyModelDao;
94 private final CompositionEntityDataManager compositionEntityDataManager;
95 private final CompositionDataExtractor compositionDataExtractor;
97 public OrchestrationUtil() {
98 this(NicDaoFactory.getInstance().createInterface(),
99 MonitoringUploadDaoFactory.getInstance().createInterface(),
100 ProcessDaoFactory.getInstance().createInterface(),
101 OrchestrationTemplateDaoFactory.getInstance().createInterface(),
102 ComponentDaoFactory.getInstance().createInterface(),
103 ServiceModelDaoFactory.getInstance().createInterface(),
104 ComponentDependencyModelDaoFactory.getInstance().createInterface(),
105 CompositionEntityDataManagerFactory.getInstance().createInterface(),
106 CompositionDataExtractorFactory.getInstance().createInterface());
109 private OrchestrationUtil(
111 ComponentArtifactDao componentArtifactDao,
112 ProcessDao processDao,
113 OrchestrationTemplateDao orchestrationTemplateDataDao,
114 ComponentDao componentDao,
115 ServiceModelDao serviceModelDao,
116 ComponentDependencyModelDao componentDependencyModelDao,
117 CompositionEntityDataManager compositionEntityDataManager,
118 CompositionDataExtractor compositionDataExtractor) {
119 this.nicDao = nicDao;
120 this.componentArtifactDao = componentArtifactDao;
121 this.processDao = processDao;
122 this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
123 this.componentDao = componentDao;
124 this.serviceModelDao = serviceModelDao;
125 this.componentDependencyModelDao = componentDependencyModelDao;
126 this.compositionEntityDataManager = compositionEntityDataManager;
127 this.compositionDataExtractor = compositionDataExtractor;
130 public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type,
131 UploadFileResponse uploadFileResponse,
132 byte[] uploadedFileData) {
133 FileContentHandler contentMap = null;
135 contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
136 } catch (IOException exception) {
137 uploadFileResponse.addStructureError(
138 SdcCommon.UPLOAD_FILE,
139 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
140 } catch (CoreException coreException) {
141 uploadFileResponse.addStructureError(
142 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
144 return Optional.ofNullable(contentMap);
147 public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version version,
148 Map<String, String> componentsQustanniare,
149 Map<String, Map<String, String>>
150 componentNicsQustanniare,
151 Map<String, Collection<ComponentMonitoringUploadEntity>>
153 Map<String, Collection<ProcessEntity>>
155 Map<String, ProcessEntity> processArtifact) {
157 Collection<ComponentEntity> componentsCompositionAndQuestionnaire =
158 componentDao.listCompositionAndQuestionnaire(vspId, version);
159 componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
160 String componentName = componentEntity.getComponentCompositionData().getName();
161 componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData());
162 backupMibData(vspId, version, componentEntity, componentName, componentMibList);
163 backupProcess(vspId, version, componentEntity.getId(), componentName, componentProcesses,
165 backupNicsQuestionnaire(vspId, version, componentEntity, componentName,
166 componentNicsQustanniare);
170 private void backupMibData(String vspId, Version version, ComponentEntity componentEntity,
171 String componentName,
172 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
173 Collection<ComponentMonitoringUploadEntity> componentMib =
174 componentArtifactDao.listArtifacts(new
175 ComponentMonitoringUploadEntity(vspId, version, componentEntity.getId(),
177 if (CollectionUtils.isNotEmpty(componentMib)) {
178 componentMibList.put(componentName, componentMib);
182 private void backupProcess(String vspId, Version version, String componentId,
183 String componentName, Map<String,
184 Collection<ProcessEntity>> processes,
185 Map<String, ProcessEntity> processArtifact) {
186 Collection<ProcessEntity> processList =
187 processDao.list(new ProcessEntity(vspId, version, componentId, null));
188 if (!processList.isEmpty()) {
189 processes.put(componentName, processList);
190 processList.forEach(process -> {
191 ProcessEntity artifact = processDao
192 .getArtifact(new ProcessEntity(vspId, version, componentId, process.getId()));
193 if (artifact.getArtifact() != null) {
194 processArtifact.put(process.getId(), artifact);
200 private void backupNicsQuestionnaire(String vspId, Version version,
201 ComponentEntity componentEntity,
202 String componentName,
203 Map<String, Map<String, String>> componentNicsQustanniare) {
204 Collection<NicEntity>
205 nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
206 if (CollectionUtils.isNotEmpty(nics)) {
207 Map<String, String> nicsQuestionnaire = new HashMap<>();
208 nics.forEach(nicEntity -> {
209 NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version,
210 componentEntity.getId(), nicEntity.getId());
212 nicsQuestionnaire.put(nicEntity.getNicCompositionData().getName(),
213 nicQuestionnaire.getQuestionnaireData());
215 componentNicsQustanniare.put(componentName, nicsQuestionnaire);
219 public void retainComponentQuestionnaireData(String vspId, Version version,
220 Map<String, String> componentsQustanniare,
221 Map<String, Map<String, String>>
222 componentNicsQustanniare,
223 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
224 Map<String, Collection<ProcessEntity>> processes,
225 Map<String, ProcessEntity> processArtifact) {
227 restoreProcess(vspId, version, null, null, processes, processArtifact);
228 Collection<ComponentEntity> components =
229 componentDao.list(new ComponentEntity(vspId, version, null));
230 components.forEach(componentEntity -> {
231 String componentName = componentEntity.getComponentCompositionData().getName();
232 if (componentsQustanniare.containsKey(componentName)) {
233 componentDao.updateQuestionnaireData(vspId, version,
234 componentEntity.getId(),
235 componentsQustanniare.get(componentEntity.getComponentCompositionData()
237 if (componentNicsQustanniare.containsKey(componentName)) {
238 restoreComponentNicQuestionnaire(vspId, version, componentName, componentEntity,
239 componentNicsQustanniare);
241 //MIB //todo add for VES_EVENTS
242 if (componentMibList.containsKey(componentName)) {
243 restoreComponentMibData(componentName, componentEntity, componentMibList);
246 restoreProcess(vspId, version, componentEntity.getId(), componentName, processes,
252 private void restoreComponentNicQuestionnaire(String vspId, Version version,
253 String componentName,
254 ComponentEntity componentEntity,
255 Map<String, Map<String, String>> componentNicsQustanniare) {
256 Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
257 Collection<NicEntity> nics =
258 nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
259 nics.forEach(nicEntity -> {
260 if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
261 nicDao.updateQuestionnaireData(vspId, version,
262 componentEntity.getId(), nicEntity.getId(),
263 nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
268 private void restoreComponentMibData(String componentName, ComponentEntity componentEntity,
269 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
270 Collection<ComponentMonitoringUploadEntity> mibList = componentMibList.get(componentName);
271 mibList.forEach(mib -> {
272 mib.setComponentId(componentEntity.getId());
273 componentArtifactDao.create(mib);
277 private void restoreProcess(String vspId, Version version, String componentId,
278 String componentName,
279 Map<String, Collection<ProcessEntity>> processes,
280 Map<String, ProcessEntity> processArtifact) {
281 if (processes.containsKey(componentName)) {
282 Collection<ProcessEntity> processList = processes.get(componentName);
283 processList.forEach(process -> {
284 process.setComponentId(componentId);
285 UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, version.getId(), componentId,
287 processDao.create(process);
288 if (processArtifact.containsKey(process.getId())) {
289 ProcessEntity artifact = processArtifact.get(process.getId());
290 processDao.uploadArtifact(artifact);
296 public void deleteUploadDataAndContent(String vspId, Version version) {
297 VendorSoftwareProductInfoDaoFactory.getInstance().createInterface()
298 .delete(new VspDetails(vspId, version));
301 public void saveUploadData(VspDetails vspDetails,
302 OrchestrationTemplateCandidateData candidateData,
303 InputStream uploadedFileData,
304 FileContentHandler fileContentMap, HeatStructureTree tree) {
305 Map<String, Object> manifestAsMap =
306 fileContentMap.containsFile(SdcCommon.MANIFEST_NAME)
307 ? (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
308 SdcCommon.MANIFEST_NAME), Map.class)
311 OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity();
312 uploadData.setFileSuffix(candidateData.getFileSuffix());
313 uploadData.setFileName(candidateData.getFileName());
314 uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
315 uploadData.setValidationDataStructure(new ValidationStructureList(tree));
316 uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null :
317 (String) manifestAsMap.get("name"));
318 uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null :
319 (String) manifestAsMap.get("version"));
320 orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
322 VspMergeDaoFactory.getInstance().createInterface()
323 .updateVspModelId(vspDetails.getId(), vspDetails.getVersion());
326 public void saveServiceModel(String vspId, Version version,
327 ToscaServiceModel serviceModelToExtract,
328 ToscaServiceModel serviceModelToStore) {
329 if (serviceModelToExtract != null) {
330 serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
331 //Extracting the compostion data from the output service model of the first phase of
333 compositionEntityDataManager.saveCompositionData(vspId, version,
334 compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
338 public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
339 Map<String, List<ErrorMessage>> validationErrors) {
340 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
341 heatTreeManager.createTree();
342 heatTreeManager.addErrors(validationErrors);
343 return heatTreeManager.getTree();
346 public void updateVspComponentDependencies(String vspId, Version version,
347 Map<String, String> vspComponentIdNameInfoBeforeProcess) {
348 Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
349 if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
350 Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
351 Collection<ComponentDependencyModelEntity> componentDependencies =
352 componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId,
354 if (CollectionUtils.isNotEmpty(componentDependencies)) {
355 updateComponentDependency(vspComponentIdNameInfoBeforeProcess, componentDependencies,
356 updatedVspComponentNames, updatedVspComponentNameIdInfo);
361 private void updateComponentDependency(Map<String, String> vspComponentIdNameInfoBeforeProcess,
362 Collection<ComponentDependencyModelEntity>
363 componentDependencies,
364 Set<String> updatedVspComponentNames,
365 Map<String, String> updatedVspComponentNameIdInfo) {
366 for (ComponentDependencyModelEntity componentDependency : componentDependencies) {
367 String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
368 .getSourceComponentId());
369 String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
370 .getTargetComponentId());
371 if (updatedVspComponentNames.contains(sourceComponentName)
372 && (updatedVspComponentNames.contains(targetComponentName))) {
373 String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
374 componentDependency.setSourceComponentId(newSourceComponentId);
375 String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
376 componentDependency.setTargetComponentId(newTargetComponentId);
377 componentDependencyModelDao.update(componentDependency);
379 componentDependencyModelDao.delete(componentDependency);
384 public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
385 Collection<ComponentEntity> updatedVspComponents =
386 componentDao.list(new ComponentEntity(vspId, version, null));
387 Map<String, String> vspComponentIdNameMap = new HashMap<>();
388 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
389 vspComponentIdNameMap = updatedVspComponents.stream()
390 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
391 .collect(Collectors.toMap(ComponentEntity::getId,
392 componentEntity -> componentEntity.getComponentCompositionData().getName()));
395 return vspComponentIdNameMap;
398 private Map<String, String> getVspComponentNameIdInfo(String vspId,
400 Collection<ComponentEntity> updatedVspComponents =
401 componentDao.list(new ComponentEntity(vspId, version, null));
402 Map<String, String> vspComponentNameIdMap = new HashMap<>();
403 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
404 vspComponentNameIdMap = updatedVspComponents.stream()
405 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
407 .toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(),
408 ComponentEntity::getId));
410 return vspComponentNameIdMap;