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