Add collaboration feature
[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 public class ComponentQuestionnaireData {
30
31   ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
32   ComponentDependencyModelDao componentDependencyModelDao =
33       ComponentDependencyModelDaoFactory.getInstance().createInterface();
34
35   private Map<String, String> sourceToTargetComponent;
36
37   public Map<String, String> getSourceToTargetComponent() {
38     return sourceToTargetComponent;
39   }
40
41   public void setSourceToTargetComponent(Map<String, String> sourceToTargetComponent) {
42     this.sourceToTargetComponent = sourceToTargetComponent;
43   }
44
45   public Map<String, Map<String, Object>> getPropertiesfromCompQuestionnaire(String key,
46                                                                              Version version) {
47     Map<String, Map<String, Object>> componentProperties =
48         new HashMap<String, Map<String, Object>>();
49
50     ComponentEntity entity = new ComponentEntity(key, version, null);
51     final Collection<ComponentEntity> componentEntities =
52         componentDao.listCompositionAndQuestionnaire(key, version);
53
54     Map<String, String> sourceToTarget = new HashMap<>();
55
56     for (ComponentEntity component : componentEntities) {
57       Map<String, Object> questionnaireParams = new HashMap<>();
58
59       final ComponentQuestionnaire componentQuestionnaire =
60           JsonUtil.json2Object(component.getQuestionnaireData(), ComponentQuestionnaire.class);
61
62       final ComponentData componentData =
63           JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
64
65       sourceToTarget.put(component.getId(), componentData.getDisplayName());
66
67       String vfc_code = componentData != null ? componentData.getVfcCode() : null;
68       questionnaireParams.put(VFC_NAMING_CODE, vfc_code);
69
70       String nfcCode = componentData.getNfcCode() != null ? componentData.getNfcCode() : null;
71       questionnaireParams.put(EnrichmentConstants.VFC_CODE, nfcCode);
72
73       String vfcDescription =
74           componentData.getNfcFunction() != null ? componentData.getNfcFunction() :
75               null;
76       questionnaireParams.put(EnrichmentConstants.VFC_FUNCTION, vfcDescription);
77
78
79       if (componentQuestionnaire.getHighAvailabilityAndLoadBalancing() != null) {
80         String mandatory = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
81             .getIsComponentMandatory();
82         questionnaireParams.put(MANDATORY, mandatory);
83
84         String mode = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
85             .getHighAvailabilityMode();
86
87         questionnaireParams.put(HIGH_AVAIL_MODE, mode);
88       }
89
90       final Integer maxVms =
91           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
92               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
93               .getMaximum() : null) : null;
94
95       final Integer minVms =
96           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
97               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
98               .getMinimum() : null) : null;
99
100       questionnaireParams.put(MIN_INSTANCES, minVms != null && minVms == 0 ? null : minVms);
101       questionnaireParams.put(MAX_INSTANCES, maxVms != null && maxVms == 0 ? null : maxVms);
102
103       if (!questionnaireParams.isEmpty()) {
104         componentProperties.put(JsonUtil.json2Object(component.getCompositionData(),
105             ComponentData.class).getDisplayName(), questionnaireParams);
106       }
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 }