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