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 org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.dao.UniqueValueDaoFactory;
22 import org.openecomp.core.model.dao.ServiceModelDao;
23 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
24 import org.openecomp.core.util.UniqueValueUtil;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.common.utils.CommonUtil;
32 import org.openecomp.sdc.common.utils.SdcCommon;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.datatypes.error.ErrorMessage;
35 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
36 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
37 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
38 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
39 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
40 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
41 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManagerFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.*;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
44 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.versioning.dao.types.Version;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.nio.ByteBuffer;
53 import java.util.stream.Collectors;
55 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
57 public class OrchestrationUtil {
59 public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
60 public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl";
62 private final NicDao nicDao;
63 private final ComponentArtifactDao componentArtifactDao;
64 private final ProcessDao processDao;
65 private final OrchestrationTemplateDao orchestrationTemplateDataDao;
66 private final ComponentDao componentDao;
67 private final ServiceModelDao serviceModelDao;
68 private final ComponentDependencyModelDao componentDependencyModelDao;
69 private final CompositionEntityDataManager compositionEntityDataManager;
70 private final CompositionDataExtractor compositionDataExtractor;
72 public OrchestrationUtil() {
73 this(NicDaoFactory.getInstance().createInterface(),
74 MonitoringUploadDaoFactory.getInstance().createInterface(),
75 ProcessDaoFactory.getInstance().createInterface(),
76 OrchestrationTemplateDaoFactory.getInstance().createInterface(),
77 ComponentDaoFactory.getInstance().createInterface(),
78 ServiceModelDaoFactory.getInstance().createInterface(),
79 ComponentDependencyModelDaoFactory.getInstance().createInterface(),
80 CompositionEntityDataManagerFactory.getInstance().createInterface(),
81 CompositionDataExtractorFactory.getInstance().createInterface());
84 private OrchestrationUtil(
86 ComponentArtifactDao componentArtifactDao,
87 ProcessDao processDao,
88 OrchestrationTemplateDao orchestrationTemplateDataDao,
89 ComponentDao componentDao,
90 ServiceModelDao serviceModelDao,
91 ComponentDependencyModelDao componentDependencyModelDao,
92 CompositionEntityDataManager compositionEntityDataManager,
93 CompositionDataExtractor compositionDataExtractor) {
95 this.componentArtifactDao = componentArtifactDao;
96 this.processDao = processDao;
97 this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
98 this.componentDao = componentDao;
99 this.serviceModelDao = serviceModelDao;
100 this.componentDependencyModelDao = componentDependencyModelDao;
101 this.compositionEntityDataManager = compositionEntityDataManager;
102 this.compositionDataExtractor = compositionDataExtractor;
105 public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type,
106 UploadFileResponse uploadFileResponse,
107 byte[] uploadedFileData) {
108 FileContentHandler contentMap = null;
110 contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
111 } catch (IOException exception) {
112 uploadFileResponse.addStructureError(
113 SdcCommon.UPLOAD_FILE,
114 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
115 } catch (CoreException coreException) {
116 uploadFileResponse.addStructureError(
117 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
119 return Optional.ofNullable(contentMap);
122 public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version version,
123 Map<String, String> componentsQustanniare,
124 Map<String, Map<String, String>>
125 componentNicsQustanniare,
126 Map<String, Collection<ComponentMonitoringUploadEntity>>
128 Map<String, Collection<ProcessEntity>>
130 Map<String, ProcessEntity> processArtifact) {
132 Collection<ComponentEntity> componentsCompositionAndQuestionnaire =
133 componentDao.listCompositionAndQuestionnaire(vspId, version);
134 componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
135 String componentName = componentEntity.getComponentCompositionData().getName();
136 componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData());
137 backupMibData(vspId, version, componentEntity, componentName, componentMibList);
138 backupProcess(vspId, version, componentEntity.getId(), componentName, componentProcesses,
140 backupNicsQuestionnaire(vspId, version, componentEntity, componentName,
141 componentNicsQustanniare);
145 private void backupMibData(String vspId, Version version, ComponentEntity componentEntity,
146 String componentName,
147 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
148 Collection<ComponentMonitoringUploadEntity> componentMib =
149 componentArtifactDao.listArtifacts(new
150 ComponentMonitoringUploadEntity(vspId, version, componentEntity.getId(),
152 if (CollectionUtils.isNotEmpty(componentMib)) {
153 componentMibList.put(componentName, componentMib);
157 private void backupProcess(String vspId, Version version, String componentId,
158 String componentName, Map<String,
159 Collection<ProcessEntity>> processes,
160 Map<String, ProcessEntity> processArtifact) {
161 Collection<ProcessEntity> processList =
162 processDao.list(new ProcessEntity(vspId, version, componentId, null));
163 if (!processList.isEmpty()) {
164 processes.put(componentName, processList);
165 processList.forEach(process -> {
166 ProcessEntity artifact = processDao
167 .getArtifact(new ProcessEntity(vspId, version, componentId, process.getId()));
168 if (artifact.getArtifact() != null) {
169 processArtifact.put(process.getId(), artifact);
175 private void backupNicsQuestionnaire(String vspId, Version version,
176 ComponentEntity componentEntity,
177 String componentName,
178 Map<String, Map<String, String>> componentNicsQustanniare) {
179 Collection<NicEntity>
180 nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
181 if (CollectionUtils.isNotEmpty(nics)) {
182 Map<String, String> nicsQuestionnaire = new HashMap<>();
183 nics.forEach(nicEntity -> {
184 NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version,
185 componentEntity.getId(), nicEntity.getId());
187 nicsQuestionnaire.put(nicEntity.getNicCompositionData().getName(),
188 nicQuestionnaire.getQuestionnaireData());
190 componentNicsQustanniare.put(componentName, nicsQuestionnaire);
194 public void retainComponentQuestionnaireData(String vspId, Version version,
195 Map<String, String> componentsQustanniare,
196 Map<String, Map<String, String>>
197 componentNicsQustanniare,
198 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
199 Map<String, Collection<ProcessEntity>> processes,
200 Map<String, ProcessEntity> processArtifact) {
202 restoreProcess(vspId, version, null, null, processes, processArtifact);
203 Collection<ComponentEntity> components =
204 componentDao.list(new ComponentEntity(vspId, version, null));
205 components.forEach(componentEntity -> {
206 String componentName = componentEntity.getComponentCompositionData().getName();
207 if (componentsQustanniare.containsKey(componentName)) {
208 componentDao.updateQuestionnaireData(vspId, version,
209 componentEntity.getId(),
210 componentsQustanniare.get(componentEntity.getComponentCompositionData()
212 if (componentNicsQustanniare.containsKey(componentName)) {
213 restoreComponentNicQuestionnaire(vspId, version, componentName, componentEntity,
214 componentNicsQustanniare);
216 //MIB //todo add for VES_EVENTS
217 if (componentMibList.containsKey(componentName)) {
218 restoreComponentMibData(componentName, componentEntity, componentMibList);
221 restoreProcess(vspId, version, componentEntity.getId(), componentName, processes,
227 private void restoreComponentNicQuestionnaire(String vspId, Version version,
228 String componentName,
229 ComponentEntity componentEntity,
230 Map<String, Map<String, String>> componentNicsQustanniare) {
231 Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
232 Collection<NicEntity> nics =
233 nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
234 nics.forEach(nicEntity -> {
235 if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
236 nicDao.updateQuestionnaireData(vspId, version,
237 componentEntity.getId(), nicEntity.getId(),
238 nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
243 private void restoreComponentMibData(String componentName, ComponentEntity componentEntity,
244 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
245 Collection<ComponentMonitoringUploadEntity> mibList = componentMibList.get(componentName);
246 mibList.forEach(mib -> {
247 mib.setComponentId(componentEntity.getId());
248 componentArtifactDao.create(mib);
252 private void restoreProcess(String vspId, Version version, String componentId,
253 String componentName,
254 Map<String, Collection<ProcessEntity>> processes,
255 Map<String, ProcessEntity> processArtifact) {
256 if (processes.containsKey(componentName)) {
257 Collection<ProcessEntity> processList = processes.get(componentName);
258 processList.forEach(process -> {
259 process.setComponentId(componentId);
260 UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance()
262 uniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, version.getId(), componentId,
264 processDao.create(process);
265 if (processArtifact.containsKey(process.getId())) {
266 ProcessEntity artifact = processArtifact.get(process.getId());
267 processDao.uploadArtifact(artifact);
273 public void deleteUploadDataAndContent(String vspId, Version version) {
274 VendorSoftwareProductInfoDaoFactory.getInstance().createInterface()
275 .delete(new VspDetails(vspId, version));
278 public void saveUploadData(VspDetails vspDetails,
279 OrchestrationTemplateCandidateData candidateData,
280 InputStream uploadedFileData,
281 FileContentHandler fileContentMap, HeatStructureTree tree) {
282 Map<String, Object> manifestAsMap =
283 fileContentMap.containsFile(SdcCommon.MANIFEST_NAME)
284 ? (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
285 SdcCommon.MANIFEST_NAME), Map.class)
288 OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity();
289 uploadData.setFileSuffix(candidateData.getFileSuffix());
290 uploadData.setFileName(candidateData.getFileName());
291 uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
292 uploadData.setValidationDataStructure(new ValidationStructureList(tree));
293 uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null :
294 (String) manifestAsMap.get("name"));
295 uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null :
296 (String) manifestAsMap.get("version"));
297 uploadData.setFilesDataStructure(candidateData.getFilesDataStructure());
298 orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
300 VspMergeDaoFactory.getInstance().createInterface()
301 .updateHint(vspDetails.getId(), vspDetails.getVersion());
304 public void saveServiceModel(String vspId, Version version,
305 ToscaServiceModel serviceModelToExtract,
306 ToscaServiceModel serviceModelToStore) {
307 if (serviceModelToExtract != null) {
308 serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
309 //Extracting the compostion data from the output service model of the first phase of
311 compositionEntityDataManager.saveCompositionData(vspId, version,
312 compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
316 public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
317 Map<String, List<ErrorMessage>> validationErrors) {
318 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
319 heatTreeManager.createTree();
320 heatTreeManager.addErrors(validationErrors);
321 return heatTreeManager.getTree();
324 public void updateVspComponentDependencies(String vspId, Version version,
326 vspComponentIdNameInfoBeforeProcess,
327 Collection<ComponentDependencyModelEntity>
328 componentDependenciesBeforeDelete) {
329 Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
330 if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
331 Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
332 if (CollectionUtils.isNotEmpty(componentDependenciesBeforeDelete)) {
333 restoreComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess,
334 componentDependenciesBeforeDelete, updatedVspComponentNames,
335 updatedVspComponentNameIdInfo);
340 private void restoreComponentDependencies(String vspId, Version version,
341 Map<String, String> vspComponentIdNameInfoBeforeProcess,
342 Collection<ComponentDependencyModelEntity>
343 componentDependenciesBeforeDelete,
344 Set<String> updatedVspComponentNames,
345 Map<String, String> updatedVspComponentNameIdInfo) {
346 for (ComponentDependencyModelEntity componentDependency : componentDependenciesBeforeDelete) {
347 String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
348 .getSourceComponentId());
349 String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
350 .getTargetComponentId());
351 if (updatedVspComponentNames.contains(sourceComponentName)
352 && (updatedVspComponentNames.contains(targetComponentName))) {
353 ComponentDependencyModelEntity restoredDependency =
354 new ComponentDependencyModelEntity(vspId, version, null);
355 String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
356 restoredDependency.setSourceComponentId(newSourceComponentId);
357 String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
358 restoredDependency.setTargetComponentId(newTargetComponentId);
359 restoredDependency.setRelation(componentDependency.getRelation());
360 componentDependencyModelDao.create(restoredDependency);
365 public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
366 Collection<ComponentEntity> updatedVspComponents =
367 componentDao.list(new ComponentEntity(vspId, version, null));
368 Map<String, String> vspComponentIdNameMap = new HashMap<>();
369 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
370 vspComponentIdNameMap = updatedVspComponents.stream()
371 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
372 .collect(Collectors.toMap(ComponentEntity::getId,
373 componentEntity -> componentEntity.getComponentCompositionData().getName()));
376 return vspComponentIdNameMap;
379 public Collection<ComponentDependencyModelEntity> getComponentDependenciesBeforeDelete(String
380 vspId, Version version) {
381 return componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId,
385 private Map<String, String> getVspComponentNameIdInfo(String vspId,
387 Collection<ComponentEntity> updatedVspComponents =
388 componentDao.list(new ComponentEntity(vspId, version, null));
389 Map<String, String> vspComponentNameIdMap = new HashMap<>();
390 if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
391 vspComponentNameIdMap = updatedVspComponents.stream()
392 .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
394 .toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(),
395 ComponentEntity::getId));
397 return vspComponentNameIdMap;