4b9ed4f45e40584d5a3d7330e632bf148e93e254
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.enrichment.impl.tosca;
18
19 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE;
20 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY;
21 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES;
22 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES;
23 import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.NFC_NAMING_CODE;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41
42
43 public class ComponentQuestionnaireData {
44
45   ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
46   ComponentDependencyModelDao componentDependencyModelDao =
47       ComponentDependencyModelDaoFactory.getInstance().createInterface();
48
49   private Map<String, String> sourceToTargetComponent;
50
51   public Map<String, String> getSourceToTargetComponent() {
52     return sourceToTargetComponent;
53   }
54
55   public void setSourceToTargetComponent(Map<String, String> sourceToTargetComponent) {
56     this.sourceToTargetComponent = sourceToTargetComponent;
57   }
58
59   public Map<String, Map<String, Object>> getPropertiesfromCompQuestionnaire(String key,
60                                                                              Version version) {
61     Map<String, Map<String, Object>> componentProperties = new HashMap<>();
62
63     final Collection<ComponentEntity> componentEntities =
64         componentDao.listCompositionAndQuestionnaire(key, version);
65
66     Map<String, String> sourceToTarget = new HashMap<>();
67
68     for (ComponentEntity component : componentEntities) {
69       Map<String, Object> questionnaireParams = new HashMap<>();
70
71       final ComponentQuestionnaire componentQuestionnaire =
72           JsonUtil.json2Object(component.getQuestionnaireData(), ComponentQuestionnaire.class);
73
74       final ComponentData componentData =
75           JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
76
77       sourceToTarget.put(component.getId(), componentData.getDisplayName());
78
79       String nfcNamingCode = componentQuestionnaire.getGeneral().getNfcNamingCode();
80       questionnaireParams.put(NFC_NAMING_CODE, nfcNamingCode);
81
82       String vfcDescription = componentQuestionnaire.getGeneral().getNfcFunction();
83       questionnaireParams.put(EnrichmentConstants.NFC_FUNCTION, vfcDescription);
84
85
86       if (componentQuestionnaire.getHighAvailabilityAndLoadBalancing() != null) {
87         String mandatory = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
88             .getIsComponentMandatory();
89         questionnaireParams.put(MANDATORY, mandatory);
90
91         String mode = componentQuestionnaire.getHighAvailabilityAndLoadBalancing()
92             .getHighAvailabilityMode();
93
94         questionnaireParams.put(HIGH_AVAIL_MODE, mode);
95       }
96
97       final Integer maxVms =
98           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
99               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
100               .getMaximum() : null) : null;
101
102       final Integer minVms =
103           componentQuestionnaire.getCompute() != null ? (componentQuestionnaire.getCompute()
104               .getNumOfVMs() != null ? componentQuestionnaire.getCompute().getNumOfVMs()
105               .getMinimum() : null) : null;
106
107       questionnaireParams.put(MIN_INSTANCES, minVms != null && minVms == 0 ? null : minVms);
108       questionnaireParams.put(MAX_INSTANCES, maxVms != null && maxVms == 0 ? null : maxVms);
109
110       if (!questionnaireParams.isEmpty()) {
111         componentProperties.put(JsonUtil.json2Object(component.getCompositionData(),
112             ComponentData.class).getDisplayName(), questionnaireParams);
113       }
114     }
115
116     setSourceToTargetComponent(sourceToTarget);
117
118     return componentProperties;
119   }
120
121   public Map<String, List<String>> populateDependencies(String vspId, Version version, Map<String,
122       String> componentNameData) {
123     Collection<ComponentDependencyModelEntity> componentDependencies =
124         componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));
125
126     Map<String, List<String>> dependencies = new HashMap<>();
127     List<String> targetComponents;
128     for (ComponentDependencyModelEntity dependency : componentDependencies) {
129       String sourceComponentName = componentNameData.get(dependency.getSourceComponentId());
130       String targetComponentName = componentNameData.get(dependency.getTargetComponentId());
131       if (!dependencies.containsKey(sourceComponentName)) {
132         targetComponents = new ArrayList<>();
133       } else {
134         targetComponents = dependencies.get(sourceComponentName);
135       }
136       targetComponents.add(targetComponentName);
137       dependencies.put(sourceComponentName, targetComponents);
138     }
139
140     return dependencies;
141   }
142
143 }