aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java/org/openecomp/sdc/enrichment/impl/tosca/ComponentQuestionnaireData.java
blob: 7457c367012bab0c2e28434706a3c56d0bb020bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.openecomp.sdc.enrichment.impl.tosca;

import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE;
import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY;
import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES;
import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES;
import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE;

import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ComponentQuestionnaireData {

  ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
  ComponentDependencyModelDao componentDependencyModelDao = ComponentDependencyModelDaoFactory.getInstance()
      .createInterface();

  private  Map<String,String> sourceToTargetComponent;

  public Map<String,String> getSourceToTargetComponent() {
    return sourceToTargetComponent;
  }

  public void setSourceToTargetComponent(Map<String,String> sourceToTargetComponent) {
    this.sourceToTargetComponent = sourceToTargetComponent;
  }

  public Map<String, Map<String, Object>> getPropertiesfromCompQuestionnaire(String key,
                                                                             Version version) {
    Map<String, Map<String,Object>> componentProperties =
        new HashMap<String, Map<String,Object>>();

    ComponentEntity entity = new ComponentEntity(key, version, null);
    final Collection<ComponentEntity> componentEntities =
        componentDao.listCompositionAndQuestionnaire(key, version);

    Map<String,String> sourceToTarget = new HashMap<String, String>();

    for (ComponentEntity component : componentEntities) {
      Map<String, Object> questionnaireParams = new HashMap<String, Object>();

      final ComponentQuestionnaire componentQuestionnaire =
          JsonUtil.json2Object(component.getQuestionnaireData(), ComponentQuestionnaire.class);

      final ComponentData componentData =
          JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);

      sourceToTarget.put(component.getId(), componentData.getDisplayName());

      String vfc_code = componentData != null ? componentData.getVfcCode() : null;
      questionnaireParams.put(VFC_NAMING_CODE, vfc_code);

      if (componentQuestionnaire.getHighAvailabilityAndLoadBalancing() != null ) {
        String mandatory = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
            .getIsComponentMandatory();
        questionnaireParams.put(MANDATORY, mandatory);

        String mode = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
            .getHighAvailabilityMode();

        questionnaireParams.put(HIGH_AVAIL_MODE, mode);
      }

      final Integer maxVms =
          componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
              .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
              .getMaximum(): null) : null;

      final Integer minVms =
          componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
              .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
              .getMinimum(): null) : null;

      questionnaireParams.put(MIN_INSTANCES,minVms != null &&  minVms == 0 ? null : minVms);
      questionnaireParams.put(MAX_INSTANCES,maxVms != null &&  maxVms == 0 ? null : maxVms);

      if (! questionnaireParams.isEmpty())
        componentProperties.put(JsonUtil.json2Object(component.getCompositionData(),
            ComponentData.class).getDisplayName(), questionnaireParams);
    }

    setSourceToTargetComponent(sourceToTarget);

    return componentProperties;
  }

  public Map<String,List<String>> populateDependencies(String vspId, Version version, Map<String,
                                                       String> componentNameData) {
    Collection<ComponentDependencyModelEntity> componentDependencies =
        componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));

    Map<String,List<String>> sourceToTargetComponent = new HashMap<String, List<String>>();
    List<String> targetComponents = null;
    for (ComponentDependencyModelEntity dependency : componentDependencies) {
      String sourceComponentName = componentNameData.get(dependency.getSourceComponentId());
      String targetComponentName = componentNameData.get(dependency.getTargetComponentId());
      if (!sourceToTargetComponent.containsKey(sourceComponentName)) {
        targetComponents = new ArrayList<String>();
      } else {
        targetComponents = sourceToTargetComponent.get(sourceComponentName);
      }
      targetComponents.add(targetComponentName);
      sourceToTargetComponent.put(sourceComponentName, targetComponents);
    }

    return sourceToTargetComponent;
  }

}