7457c367012bab0c2e28434706a3c56d0bb020bc
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / tosca / ComponentQuestionnaireData.java
1 package org.openecomp.sdc.enrichment.impl.tosca;
2
3 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE;
4 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY;
5 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES;
6 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES;
7 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE;
8
9 import org.openecomp.core.utilities.json.JsonUtil;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 public class ComponentQuestionnaireData {
27
28   ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
29   ComponentDependencyModelDao componentDependencyModelDao = ComponentDependencyModelDaoFactory.getInstance()
30       .createInterface();
31
32   private  Map<String,String> sourceToTargetComponent;
33
34   public Map<String,String> getSourceToTargetComponent() {
35     return sourceToTargetComponent;
36   }
37
38   public void setSourceToTargetComponent(Map<String,String> sourceToTargetComponent) {
39     this.sourceToTargetComponent = sourceToTargetComponent;
40   }
41
42   public Map<String, Map<String, Object>> getPropertiesfromCompQuestionnaire(String key,
43                                                                              Version version) {
44     Map<String, Map<String,Object>> componentProperties =
45         new HashMap<String, Map<String,Object>>();
46
47     ComponentEntity entity = new ComponentEntity(key, version, null);
48     final Collection<ComponentEntity> componentEntities =
49         componentDao.listCompositionAndQuestionnaire(key, version);
50
51     Map<String,String> sourceToTarget = new HashMap<String, String>();
52
53     for (ComponentEntity component : componentEntities) {
54       Map<String, Object> questionnaireParams = new HashMap<String, Object>();
55
56       final ComponentQuestionnaire componentQuestionnaire =
57           JsonUtil.json2Object(component.getQuestionnaireData(), ComponentQuestionnaire.class);
58
59       final ComponentData componentData =
60           JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
61
62       sourceToTarget.put(component.getId(), componentData.getDisplayName());
63
64       String vfc_code = componentData != null ? componentData.getVfcCode() : null;
65       questionnaireParams.put(VFC_NAMING_CODE, vfc_code);
66
67       if (componentQuestionnaire.getHighAvailabilityAndLoadBalancing() != null ) {
68         String mandatory = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
69             .getIsComponentMandatory();
70         questionnaireParams.put(MANDATORY, mandatory);
71
72         String mode = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
73             .getHighAvailabilityMode();
74
75         questionnaireParams.put(HIGH_AVAIL_MODE, mode);
76       }
77
78       final Integer maxVms =
79           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
80               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
81               .getMaximum(): null) : null;
82
83       final Integer minVms =
84           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
85               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
86               .getMinimum(): null) : null;
87
88       questionnaireParams.put(MIN_INSTANCES,minVms != null &&  minVms == 0 ? null : minVms);
89       questionnaireParams.put(MAX_INSTANCES,maxVms != null &&  maxVms == 0 ? null : maxVms);
90
91       if (! questionnaireParams.isEmpty())
92         componentProperties.put(JsonUtil.json2Object(component.getCompositionData(),
93             ComponentData.class).getDisplayName(), questionnaireParams);
94     }
95
96     setSourceToTargetComponent(sourceToTarget);
97
98     return componentProperties;
99   }
100
101   public Map<String,List<String>> populateDependencies(String vspId, Version version, Map<String,
102                                                        String> componentNameData) {
103     Collection<ComponentDependencyModelEntity> componentDependencies =
104         componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));
105
106     Map<String,List<String>> sourceToTargetComponent = new HashMap<String, List<String>>();
107     List<String> targetComponents = null;
108     for (ComponentDependencyModelEntity dependency : componentDependencies) {
109       String sourceComponentName = componentNameData.get(dependency.getSourceComponentId());
110       String targetComponentName = componentNameData.get(dependency.getTargetComponentId());
111       if (!sourceToTargetComponent.containsKey(sourceComponentName)) {
112         targetComponents = new ArrayList<String>();
113       } else {
114         targetComponents = sourceToTargetComponent.get(sourceComponentName);
115       }
116       targetComponents.add(targetComponentName);
117       sourceToTargetComponent.put(sourceComponentName, targetComponents);
118     }
119
120     return sourceToTargetComponent;
121   }
122
123 }