[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / composition / CompositionEntityDataManagerImpl.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.vendorsoftwareproduct.services.impl.composition;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.core.utilities.json.JsonSchemaDataGenerator;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
51 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
52 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
61 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
62 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image;
63 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
64 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
66 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
67 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
68 import org.openecomp.sdc.versioning.dao.types.Version;
69
70 import java.util.ArrayList;
71 import java.util.Collection;
72 import java.util.HashMap;
73 import java.util.HashSet;
74 import java.util.List;
75 import java.util.Map;
76 import java.util.Objects;
77 import java.util.Set;
78
79 public class CompositionEntityDataManagerImpl implements CompositionEntityDataManager {
80
81   private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR =
82       "COMPOSITION_ENTITY_DATA_MANAGER_ERR";
83   private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG =
84       "Invalid input: %s may not be null";
85
86   private static final Logger logger =
87       LoggerFactory.getLogger(CompositionEntityDataManagerImpl.class);
88   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
89
90   private Map<CompositionEntityId, CompositionEntityData> entities = new HashMap<>();
91   private Map<CompositionEntityType, String> nonDynamicSchemas = new HashMap<>();
92   private List<CompositionEntityValidationData> roots = new ArrayList<>();
93
94   private VendorSoftwareProductInfoDao vspInfoDao;
95   private ComponentDao componentDao;
96   private NicDao nicDao;
97   private NetworkDao networkDao;
98   private ImageDao imageDao;
99   private ComputeDao computeDao;
100   private DeploymentFlavorDao deploymentFlavorDao;
101   private VendorSoftwareProductDao vendorSoftwareProductDao;
102
103   public CompositionEntityDataManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
104                                           ComponentDao componentDao,
105                                           NicDao nicDao, NetworkDao networkDao,
106                                           ImageDao imageDao, ComputeDao computeDao,
107                                           DeploymentFlavorDao deploymentFlavorDao,
108                                           VendorSoftwareProductDao vendorSoftwareProductDao ) {
109     this.vspInfoDao = vspInfoDao;
110     this.componentDao = componentDao;
111     this.nicDao = nicDao;
112     this.networkDao = networkDao;
113     this.imageDao = imageDao;
114     this.computeDao = computeDao;
115     this.deploymentFlavorDao = deploymentFlavorDao;
116     this.vendorSoftwareProductDao = vendorSoftwareProductDao;
117   }
118
119   /**
120    * Validate entity composition entity validation data.
121    *
122    * @param entity                the entity
123    * @param schemaTemplateContext the schema template context
124    * @param schemaTemplateInput   the schema template input
125    * @return the composition entity validation data
126    */
127   @Override
128   public CompositionEntityValidationData validateEntity(CompositionEntity entity,
129                                                         SchemaTemplateContext schemaTemplateContext,
130                                                         SchemaTemplateInput schemaTemplateInput) {
131     mdcDataDebugMessage.debugEntryMessage(null);
132
133     if (entity == null) {
134       throw new CoreException(
135           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
136               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
137               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
138               .build());
139     }
140     if (schemaTemplateContext == null) {
141       throw new CoreException(
142           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
143               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
144               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "schema template context"))
145               .build());
146     }
147
148     CompositionEntityValidationData validationData =
149         new CompositionEntityValidationData(entity.getType(), entity.getId());
150     String json =
151         schemaTemplateContext == SchemaTemplateContext.composition ? entity.getCompositionData()
152             : entity.getQuestionnaireData();
153     validationData.setErrors(JsonUtil.validate(
154         json == null ? JsonUtil.object2Json(new Object()) : json,
155         generateSchema(schemaTemplateContext, entity.getType(), schemaTemplateInput)));
156
157     mdcDataDebugMessage.debugExitMessage(null);
158     return validationData;
159   }
160
161   /**
162    * Add entity.
163    *
164    * @param entity              the entity
165    * @param schemaTemplateInput the schema template input
166    */
167   @Override
168   public void addEntity(CompositionEntity entity, SchemaTemplateInput schemaTemplateInput) {
169     if (entity == null) {
170       throw new CoreException(
171           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
172               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
173               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
174               .build());
175     }
176     entities.put(entity.getCompositionEntityId(),
177         new CompositionEntityData(entity, schemaTemplateInput));
178   }
179
180   /**
181    * Validate entities questionnaire map.
182    *
183    * @return the map
184    */
185   @Override
186   public Map<CompositionEntityId, Collection<String>> validateEntitiesQuestionnaire() {
187     mdcDataDebugMessage.debugEntryMessage(null);
188
189     Map<CompositionEntityId, Collection<String>> errorsByEntityId = new HashMap<>();
190     entities.entrySet().forEach(entry -> {
191       Collection<String> errors = validateQuestionnaire(entry.getValue());
192       if (errors != null) {
193         errorsByEntityId.put(entry.getKey(), errors);
194       }
195     });
196
197     mdcDataDebugMessage.debugExitMessage(null);
198     return errorsByEntityId;
199   }
200
201   /**
202    * Build trees.
203    */
204   @Override
205   public void buildTrees() {
206     Map<CompositionEntityId, CompositionEntityValidationData> entitiesValidationData =
207         new HashMap<>();
208     entities.entrySet().forEach(
209         entry -> addValidationDataEntity(entitiesValidationData, entry.getKey(),
210             entry.getValue().entity));
211   }
212
213   public Collection<CompositionEntityValidationData> getTrees() {
214     return roots;
215   }
216
217   @Override
218   public void saveCompositionData(String vspId, Version version, CompositionData compositionData) {
219     mdcDataDebugMessage.debugEntryMessage(null);
220
221     if (Objects.isNull(compositionData)) {
222       return;
223     }
224
225     Map<String, String> networkIdByName = saveNetworks(vspId, version, compositionData);
226     saveComponents(vspId, version, compositionData, networkIdByName);
227     saveDeploymentFlavors(vspId, version, compositionData);
228
229     mdcDataDebugMessage.debugExitMessage(null);
230   }
231
232   @Override
233   public Set<CompositionEntityValidationData> getAllErrorsByVsp(String vspId) {
234     CompositionEntityValidationData matchVsp = null;
235     Set<CompositionEntityValidationData> entitiesWithErrors = new HashSet<>();
236     for (CompositionEntityValidationData root : roots) {
237       if (root.getEntityId().equals(vspId)) {
238         matchVsp = root;
239         break;
240       }
241     }
242
243     getEntityListWithErrors(matchVsp, entitiesWithErrors);
244     if (CollectionUtils.isNotEmpty(entitiesWithErrors)) {
245       updateValidationCompositionEntityName(entitiesWithErrors);
246       return entitiesWithErrors;
247     }
248
249     return null;
250   }
251
252   private boolean isThereErrorsInSubTree(CompositionEntityValidationData entity) {
253     if (Objects.isNull(entity)) {
254       return false;
255     }
256
257     if (CollectionUtils.isNotEmpty(entity.getErrors())) {
258       return true;
259     }
260
261     Collection<CompositionEntityValidationData> subEntitiesValidationData =
262         entity.getSubEntitiesValidationData();
263     return !CollectionUtils.isEmpty(subEntitiesValidationData) &&
264         checkForErrorsInChildren(subEntitiesValidationData);
265
266   }
267
268   private boolean checkForErrorsInChildren(
269       Collection<CompositionEntityValidationData> subEntitiesValidationData) {
270     boolean result = false;
271     for (CompositionEntityValidationData subEntity : subEntitiesValidationData) {
272       if (CollectionUtils.isNotEmpty(subEntity.getErrors())) {
273         return true;
274       }
275
276       result = result || isThereErrorsInSubTree(subEntity);
277       if (result) {
278         return true;
279       }
280     }
281     return false;
282   }
283
284   public void saveComponents(String vspId, Version version, CompositionData compositionData,
285                              Map<String, String> networkIdByName) {
286
287
288     mdcDataDebugMessage.debugEntryMessage(null);
289
290     if (CollectionUtils.isNotEmpty(compositionData.getComponents())) {
291       for (Component component : compositionData.getComponents()) {
292         ComponentEntity componentEntity = new ComponentEntity(vspId, version, null);
293         componentEntity.setComponentCompositionData(component.getData());
294
295         String componentId = createComponent(componentEntity).getId();
296
297         saveImagesByComponent(vspId, version, component, componentId);
298         saveComputesFlavorByComponent(vspId, version, component, componentId);
299
300         saveNicsByComponent(vspId, version, networkIdByName, component, componentId);
301       }
302     }
303
304     mdcDataDebugMessage.debugExitMessage(null);
305   }
306
307   public void saveNicsByComponent(String vspId, Version version,
308                                   Map<String, String> networkIdByName, Component component,
309                                   String componentId) {
310     if (CollectionUtils.isNotEmpty(component.getNics())) {
311       for (Nic nic : component.getNics()) {
312         if (nic.getNetworkName() != null && MapUtils.isNotEmpty(networkIdByName)) {
313           nic.setNetworkId(networkIdByName.get(nic.getNetworkName()));
314         }
315         nic.setNetworkName(null);
316         //For heat flow set network type to be internal by default for NIC
317         nic.setNetworkType(NetworkType.Internal);
318
319         NicEntity nicEntity = new NicEntity(vspId, version, componentId, null);
320         nicEntity.setNicCompositionData(nic);
321         createNic(nicEntity);
322       }
323     }
324   }
325
326   public Map<String, String> saveNetworks(String vspId, Version version,
327                                           CompositionData compositionData) {
328     mdcDataDebugMessage.debugEntryMessage(null);
329
330     Map<String, String> networkIdByName = new HashMap<>();
331     if (CollectionUtils.isNotEmpty(compositionData.getNetworks())) {
332       for (Network network : compositionData.getNetworks()) {
333
334         NetworkEntity networkEntity = new NetworkEntity(vspId, version, null);
335         networkEntity.setNetworkCompositionData(network);
336
337         if (network.getName() != null) {
338           networkIdByName.put(network.getName(), createNetwork(networkEntity).getId());
339         }
340       }
341     }
342
343     mdcDataDebugMessage.debugExitMessage(null);
344     return networkIdByName;
345   }
346
347   @Override
348   public NetworkEntity createNetwork(NetworkEntity network) {
349     mdcDataDebugMessage.debugEntryMessage(null);
350
351     //network.setId(CommonMethods.nextUuId()); will be set by the dao
352     networkDao.create(network);
353     mdcDataDebugMessage.debugExitMessage(null);
354     return network;
355   }
356
357   @Override
358   public ComponentEntity createComponent(ComponentEntity component) {
359     mdcDataDebugMessage.debugEntryMessage(null);
360
361     //component.setId(CommonMethods.nextUuId()); will be set by the dao
362     component.setQuestionnaireData(
363         new JsonSchemaDataGenerator(
364             generateSchema(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
365                 null))
366             .generateData());
367
368     componentDao.create(component);
369
370     mdcDataDebugMessage.debugExitMessage(null);
371     return component;
372   }
373
374   @Override
375   public NicEntity createNic(NicEntity nic) {
376     mdcDataDebugMessage.debugEntryMessage(null);
377
378     //nic.setId(CommonMethods.nextUuId()); will be set by the dao
379     nic.setQuestionnaireData(
380         new JsonSchemaDataGenerator(
381             generateSchema(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, null))
382             .generateData());
383
384     nicDao.create(nic);
385
386     mdcDataDebugMessage.debugExitMessage(null);
387     return nic;
388   }
389
390
391   public void addErrorsToTrees(Map<CompositionEntityId, Collection<String>> errors) {
392     roots.forEach(root -> addErrorsToTree(root, null, errors));
393   }
394
395   /* *
396   * get a flat list of all questionnaire entities that have validation errors
397   * */
398   public Set<CompositionEntityValidationData> getEntityListWithErrors() {
399     mdcDataDebugMessage.debugEntryMessage(null);
400     Set<CompositionEntityValidationData> treeAsList = new HashSet<>();
401
402     for (CompositionEntityValidationData entity : roots) {
403       if (CollectionUtils.isNotEmpty(entity.getErrors())) {
404         addNodeWithErrors(entity, treeAsList);
405       }
406       getEntityListWithErrors(entity, treeAsList);
407     }
408
409     updateValidationCompositionEntityName(treeAsList);
410
411     mdcDataDebugMessage.debugExitMessage(null);
412     return treeAsList;
413   }
414
415   public void getEntityListWithErrors(CompositionEntityValidationData entity,
416                                       Set<CompositionEntityValidationData> compositionSet) {
417     Collection<CompositionEntityValidationData> childNodes =
418         entity.getSubEntitiesValidationData();
419
420     if (CollectionUtils.isEmpty(childNodes)) {
421       return;
422     }
423
424     for (CompositionEntityValidationData child : childNodes) {
425       if (CollectionUtils.isNotEmpty(child.getErrors())) {
426         addNodeWithErrors(child, compositionSet);
427       }
428       getEntityListWithErrors(child, compositionSet);
429     }
430   }
431
432
433   public void addNodeWithErrors(CompositionEntityValidationData node,
434                                 Set<CompositionEntityValidationData> entitiesWithErrors) {
435     CompositionEntityValidationData compositionNodeToAdd = new CompositionEntityValidationData(node
436         .getEntityType(), node.getEntityId());
437     compositionNodeToAdd.setErrors(node.getErrors());
438     compositionNodeToAdd.setSubEntitiesValidationData(null);
439
440     entitiesWithErrors.add(compositionNodeToAdd);
441   }
442
443   public void removeNodesWithoutErrors() {
444     roots.forEach(root -> removeNodesWithoutErrors(root, null));
445   }
446
447
448   private CompositionEntityData getCompositionEntityDataById(CompositionEntityValidationData
449                                                                  entity) {
450     for (Map.Entry<CompositionEntityId, CompositionEntityData> entityEntry : entities
451         .entrySet()) {
452       if (entityEntry.getKey().getId().equals(entity.getEntityId())) {
453         return entityEntry.getValue();
454       }
455     }
456     return null;
457   }
458
459
460   private void updateValidationCompositionEntityName(Set<CompositionEntityValidationData>
461                                                          compositionSet) {
462     for (CompositionEntityValidationData entity : compositionSet) {
463       String compositionData = getCompositionDataAsString(entity);
464       if (entity.getEntityType().equals(CompositionEntityType.vsp) ||
465           Objects.nonNull(compositionData)) {
466         entity.setEntityName(getEntityNameByEntityType(compositionData, entity));
467       }
468     }
469   }
470
471   private String getCompositionDataAsString(CompositionEntityValidationData entity) {
472     CompositionEntityData compositionEntityData = getCompositionEntityDataById(entity);
473     return compositionEntityData == null ? null : compositionEntityData.entity.getCompositionData();
474   }
475
476
477   private String getEntityNameByEntityType(String compositionData,
478                                            CompositionEntityValidationData entity) {
479     switch (entity.getEntityType()) {
480       case component:
481         ComponentData component = JsonUtil.json2Object(compositionData, ComponentData.class);
482         return component.getDisplayName();
483
484       case nic:
485         Nic nic = JsonUtil.json2Object(compositionData, Nic.class);
486         return nic.getName();
487
488       case network:
489         Network network = JsonUtil.json2Object(compositionData, Network.class);
490         return network.getName();
491
492       case vsp:
493         CompositionEntityData vspEntity = getCompositionEntityDataById(entity);
494         VspQuestionnaireEntity vspQuestionnaireEntity = (VspQuestionnaireEntity) vspEntity.entity;
495         VspDetails vspDetails =
496             vspInfoDao.get(new VspDetails(vspQuestionnaireEntity.getId(),
497                 vspQuestionnaireEntity.getVersion()));
498         return vspDetails.getName();
499     }
500
501     return null;
502   }
503
504   private void removeNodesWithoutErrors(CompositionEntityValidationData node,
505                                         CompositionEntityValidationData parent) {
506
507     if (Objects.isNull(node)) {
508       return;
509     }
510
511     if (hasChildren(node)) {
512       Collection<CompositionEntityValidationData> subNodes =
513           new ArrayList<>(node.getSubEntitiesValidationData());
514       subNodes.forEach(subNode -> removeNodesWithoutErrors(subNode, node));
515       node.setSubEntitiesValidationData(subNodes);
516
517       if (canNodeGetRemovedFromValidationDataTree(node)) {
518         removeNodeFromChildren(parent, node);
519       }
520     } else if (canNodeGetRemovedFromValidationDataTree(node)) {
521       removeNodeFromChildren(parent, node);
522     }
523   }
524
525   private void removeNodeFromChildren(CompositionEntityValidationData parent,
526                                       CompositionEntityValidationData childToRemove) {
527     if (!Objects.isNull(parent)) {
528       parent.getSubEntitiesValidationData().remove(childToRemove);
529     }
530   }
531
532   private boolean hasChildren(CompositionEntityValidationData node) {
533     return !CollectionUtils.isEmpty(node.getSubEntitiesValidationData());
534   }
535
536   private boolean canNodeGetRemovedFromValidationDataTree(CompositionEntityValidationData node) {
537     return !hasChildren(node) && CollectionUtils.isEmpty(node.getErrors());
538   }
539
540
541   private void addValidationDataEntity(
542       Map<CompositionEntityId, CompositionEntityValidationData> entitiesValidationData,
543       CompositionEntityId entityId, CompositionEntity entity) {
544     if (entitiesValidationData.containsKey(entityId)) {
545       return;
546     }
547
548     CompositionEntityValidationData validationData =
549         new CompositionEntityValidationData(entity.getType(), entity.getId());
550     entitiesValidationData.put(entityId, validationData);
551
552     CompositionEntityId parentEntityId = entityId.getParentId();
553     if (parentEntityId == null) {
554       roots.add(validationData);
555     } else {
556       CompositionEntityData parentEntity = entities.get(parentEntityId);
557       if (parentEntity == null) {
558         roots.add(validationData);
559       } else {
560         addValidationDataEntity(entitiesValidationData, parentEntityId, parentEntity.entity);
561         entitiesValidationData.get(parentEntityId).addSubEntityValidationData(validationData);
562       }
563     }
564   }
565
566   private void addErrorsToTree(CompositionEntityValidationData node,
567                                CompositionEntityId parentNodeId,
568                                Map<CompositionEntityId, Collection<String>> errors) {
569     if (node == null) {
570       return;
571     }
572     CompositionEntityId nodeId = new CompositionEntityId(node.getEntityId(), parentNodeId);
573     node.setErrors(errors.get(nodeId));
574
575     if (node.getSubEntitiesValidationData() != null) {
576       node.getSubEntitiesValidationData()
577           .forEach(subNode -> addErrorsToTree(subNode, nodeId, errors));
578     }
579   }
580
581   private Collection<String> validateQuestionnaire(CompositionEntityData compositionEntityData) {
582     logger.debug(String.format("validateQuestionnaire start:  " +
583             "[entity.type]=%s, [entity.id]=%s, [entity.questionnaireString]=%s",
584         compositionEntityData.entity.getType().name(),
585         compositionEntityData.entity.getCompositionEntityId().toString(),
586         compositionEntityData.entity.getQuestionnaireData()));
587
588     return JsonUtil.validate(
589         compositionEntityData.entity.getQuestionnaireData() == null
590             ? JsonUtil.object2Json(new Object())
591             : compositionEntityData.entity.getQuestionnaireData(),
592         getSchema(compositionEntityData.entity.getType(), SchemaTemplateContext.questionnaire,
593             compositionEntityData.schemaTemplateInput));
594   }
595
596   private String getSchema(CompositionEntityType compositionEntityType,
597                            SchemaTemplateContext schemaTemplateContext,
598                            SchemaTemplateInput schemaTemplateInput) {
599     return schemaTemplateInput == null
600         ? nonDynamicSchemas.computeIfAbsent(compositionEntityType,
601         k -> generateSchema(schemaTemplateContext, compositionEntityType, null))
602         : generateSchema(schemaTemplateContext, compositionEntityType, schemaTemplateInput);
603   }
604
605   private static class CompositionEntityData {
606     private CompositionEntity entity;
607     private SchemaTemplateInput schemaTemplateInput;
608
609     CompositionEntityData(CompositionEntity entity, SchemaTemplateInput schemaTemplateInput) {
610       this.entity = entity;
611       this.schemaTemplateInput = schemaTemplateInput;
612     }
613
614   }
615
616   // todo - make SchemaGenerator non static and mock it in UT instead of mocking this method (and
617   // make the method private
618
619   protected String generateSchema(SchemaTemplateContext schemaTemplateContext,
620                                   CompositionEntityType compositionEntityType,
621                                   SchemaTemplateInput schemaTemplateInput) {
622     return SchemaGenerator
623         .generate(schemaTemplateContext, compositionEntityType, schemaTemplateInput);
624   }
625
626   @Override
627   public DeploymentFlavorEntity createDeploymentFlavor(DeploymentFlavorEntity deploymentFlavor) {
628     mdcDataDebugMessage.debugEntryMessage(null, null);
629
630     deploymentFlavor.setId(CommonMethods.nextUuId());
631     deploymentFlavorDao.create(deploymentFlavor);
632     return deploymentFlavor;
633   }
634
635   @Override
636   public ImageEntity createImage(ImageEntity image) {
637     mdcDataDebugMessage.debugEntryMessage(null, null);
638
639     image.setId(CommonMethods.nextUuId());
640
641     image.setQuestionnaireData(
642         new JsonSchemaDataGenerator(SchemaGenerator
643             .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.image, null))
644             .generateData());
645
646     imageDao.create(image);
647     mdcDataDebugMessage.debugExitMessage(null, null);
648     return image;
649   }
650
651   @Override
652   public ComputeEntity createCompute(ComputeEntity compute) {
653     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", compute.getVspId(),
654         compute.getComponentId());
655
656     compute.setId(CommonMethods.nextUuId());
657     compute.setQuestionnaireData(
658         new JsonSchemaDataGenerator(SchemaGenerator
659             .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.compute,
660                 null)).generateData());
661
662     computeDao.create(compute);
663
664     mdcDataDebugMessage.debugExitMessage("VSP id, component id", compute.getVspId(),
665         compute.getComponentId());
666     return compute;
667   }
668
669   public void saveComputesFlavorByComponent(String vspId, Version version, Component component, String
670       componentId) {
671     if (CollectionUtils.isNotEmpty(component.getCompute())) {
672       for (ComputeData flavor : component.getCompute()) {
673         ComputeEntity computeEntity = new ComputeEntity(vspId, version, componentId, null);
674         computeEntity.setComputeCompositionData(flavor);
675         createCompute(computeEntity);
676       }
677     }
678   }
679
680   public void saveImagesByComponent(String vspId, Version version, Component component, String
681       componentId) {
682     if (CollectionUtils.isNotEmpty(component.getImages())) {
683       for (Image img : component.getImages()) {
684         ImageEntity imageEntity = new ImageEntity(vspId, version, componentId, null);
685         imageEntity.setImageCompositionData(img);
686         createImage(imageEntity);
687       }
688     }
689   }
690
691   public void saveDeploymentFlavors(String vspId, Version version,
692                                     CompositionData compositionData) {
693
694     mdcDataDebugMessage.debugEntryMessage(null, null);
695
696     if (CollectionUtils.isNotEmpty(compositionData.getComponents())) {
697       DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vspId, version,
698           null);
699       DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
700       VspDetails vendorSoftwareProductInfo =
701           vspInfoDao.get(new VspDetails(vspId, version));
702       if (vendorSoftwareProductInfo.getName() != null) {
703         deploymentFlavor.setModel(vendorSoftwareProductInfo.getName());
704         List<ComponentComputeAssociation> componentComputeAssociationList = new ArrayList<>();
705         Collection<ComputeEntity> computes= vendorSoftwareProductDao.listComputesByVsp(vspId,
706             version);
707         for (ComputeEntity compute : computes) {
708           ComponentComputeAssociation componentComputeAssociation = new
709               ComponentComputeAssociation();
710           if (compute.getComponentId() != null && compute.getId() != null){
711             componentComputeAssociation.setComponentId(compute.getComponentId());
712             componentComputeAssociation.setComputeFlavorId(compute.getId());
713             componentComputeAssociationList.add(componentComputeAssociation);
714           }
715         }
716         deploymentFlavor.setComponentComputeAssociations(componentComputeAssociationList);
717       }
718       deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
719       createDeploymentFlavor(deploymentFlavorEntity);
720     }
721
722     mdcDataDebugMessage.debugExitMessage(null, null);
723   }
724
725 }