Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / datatypes / heattotosca / unifiedmodel / composition / UnifiedSubstitutionData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import org.apache.commons.collections4.MapUtils;
31 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
32
33 public class UnifiedSubstitutionData {
34
35   //Key - node template id, Value - related abstract node template id
36   private Map<String, String> nodesRelatedAbstractNode = new HashMap<>();
37   //Key - node template id, Value - related node template id in the substitution service template
38   private Map<String, String> nodesRelatedSubstitutionServiceTemplateNode = new HashMap<>();
39   private Map<String, NodeTemplateInformation> cleanedNodeTemplates = new HashMap<>();
40   //Key - nested node template id, Value - related unified nested node template id
41   private Map<String, String> nestedNodeTemplateRelatedUnifiedTranslatedId = new HashMap<>();
42   //Key - nested node type id, Value - related unified nested node template id
43   private Map<String, String> nestedNodeTypeRelatedUnifiedTranslatedId = new HashMap<>();
44   //Key - handled compute type, Value - number of times it was handled
45   private Map<String, Integer> handledComputeTypesInNestedSubstitutionTemplate =
46       new HashMap<>();
47   //Key - nested compute type, Value - list of nested files that the compute type is present
48   private Map<String, Integer> handledNestedComputeTypesNestedFiles = new HashMap<>();
49   //Key - new property id, Value - orig property value
50   private Map<String, Object> newParameterIdsToPropertiesFromOrigNodeTemplate = new HashMap<>();
51   //handled nested files
52   private Set<String> handledNestedFiles = new HashSet<>();
53   //handled nested nodes
54   private Set<String> handledNestedNodes = new HashSet<>();
55
56   public Map<String, String> getNodesRelatedAbstractNode() {
57     return nodesRelatedAbstractNode;
58   }
59
60   public void setNodesRelatedAbstractNode(
61       Map<String, String> nodesRelatedAbstractNode) {
62     this.nodesRelatedAbstractNode = nodesRelatedAbstractNode;
63   }
64
65   public void addHandledNestedNodes(String handledNestedNodeId) {
66     this.handledNestedNodes.add(handledNestedNodeId);
67   }
68
69   public Map<String, String> getNodesRelatedSubstitutionServiceTemplateNode() {
70     return nodesRelatedSubstitutionServiceTemplateNode;
71   }
72
73   public void setNodesRelatedSubstitutionServiceTemplateNode(
74       Map<String, String> nodesRelatedSubstitutionServiceTemplateNode) {
75     this.nodesRelatedSubstitutionServiceTemplateNode = nodesRelatedSubstitutionServiceTemplateNode;
76   }
77
78   public String getNodeRelatedAbstractNode(String origNodeId) {
79     return this.nodesRelatedAbstractNode.get(origNodeId);
80   }
81
82   public Collection<String> getAllRelatedAbstractNodeIds() {
83     return this.nodesRelatedAbstractNode.values();
84   }
85
86   public Collection<String> getAllUnifiedNestedNodeTemplateIds() {
87     return this.nestedNodeTemplateRelatedUnifiedTranslatedId.values();
88   }
89
90   /**
91    * Add cleaned node template.
92    *
93    * @param nodeTemplateId           the node template id
94    * @param unifiedCompositionEntity the unified composition entity
95    * @param nodeTemplate             the node template
96    */
97   public void addCleanedNodeTemplate(String nodeTemplateId,
98                                      UnifiedCompositionEntity unifiedCompositionEntity,
99                                      NodeTemplate nodeTemplate) {
100     NodeTemplateInformation nodeTemplateInformation = new NodeTemplateInformation(
101         unifiedCompositionEntity, nodeTemplate);
102     this.cleanedNodeTemplates.putIfAbsent(nodeTemplateId, nodeTemplateInformation);
103   }
104
105   public NodeTemplate getCleanedNodeTemplate(String nodeTemplateId) {
106     return this.cleanedNodeTemplates.get(nodeTemplateId).getNodeTemplate().clone();
107   }
108
109   public UnifiedCompositionEntity getCleanedNodeTemplateCompositionEntity(String nodeTemplateId) {
110     return this.cleanedNodeTemplates.get(nodeTemplateId).getUnifiedCompositionEntity();
111   }
112
113   public void addUnifiedNestedNodeTemplateId(String nestedNodeTemplateId,
114                                              String unifiedNestedNodeRelatedId) {
115     this.nestedNodeTemplateRelatedUnifiedTranslatedId
116         .put(nestedNodeTemplateId, unifiedNestedNodeRelatedId);
117   }
118
119   public Optional<String> getUnifiedNestedNodeTemplateId(String nestedNodeTemplateId) {
120     return this.nestedNodeTemplateRelatedUnifiedTranslatedId.get(nestedNodeTemplateId) == null
121         ? Optional.empty()
122         : Optional.of(this.nestedNodeTemplateRelatedUnifiedTranslatedId.get(nestedNodeTemplateId));
123   }
124
125   public void addUnifiedNestedNodeTypeId(String nestedNodeTypeId,
126                                          String unifiedNestedNodeRelatedId) {
127     this.nestedNodeTypeRelatedUnifiedTranslatedId.put(nestedNodeTypeId, unifiedNestedNodeRelatedId);
128   }
129
130   public Optional<String> getUnifiedNestedNodeTypeId(String nestedNodeTypeId) {
131     return this.nestedNodeTypeRelatedUnifiedTranslatedId.get(nestedNodeTypeId) == null ? Optional
132         .empty()
133         : Optional.of(this.nestedNodeTypeRelatedUnifiedTranslatedId.get(nestedNodeTypeId));
134   }
135
136   public Set<String> getAllRelatedNestedNodeTypeIds(){
137     if(MapUtils.isEmpty(nestedNodeTypeRelatedUnifiedTranslatedId)){
138       return new HashSet<>();
139     }
140     return new HashSet<>(this.nestedNodeTypeRelatedUnifiedTranslatedId.values());
141   }
142
143   public void addHandledComputeType(String handledComputeType) {
144
145     if (this.handledComputeTypesInNestedSubstitutionTemplate.containsKey(handledComputeType)) {
146       Integer timesHandled =
147           this.handledComputeTypesInNestedSubstitutionTemplate.get(handledComputeType);
148       this.handledComputeTypesInNestedSubstitutionTemplate
149           .put(handledComputeType, timesHandled + 1);
150     } else {
151       //this.handledNestedFiles.add(nestedServiceTemplateFileName);
152       handledComputeTypesInNestedSubstitutionTemplate.put(handledComputeType, 0);
153     }
154   }
155
156   public boolean isComputeTypeHandledInServiceTemplate(String computeType) {
157     return this.handledComputeTypesInNestedSubstitutionTemplate.containsKey(computeType);
158   }
159
160   public int getHandledNestedComputeNodeTemplateIndex(String computeType) {
161     return this.handledComputeTypesInNestedSubstitutionTemplate.containsKey(computeType) ?
162         this.handledComputeTypesInNestedSubstitutionTemplate.get(computeType) : 0;
163   }
164
165   public void addHandlesNestedServiceTemplate(String nestedServiceTemplateFileName){
166     this.handledNestedFiles.add(nestedServiceTemplateFileName);
167   }
168
169   public boolean isNestedServiceTemplateWasHandled(String nestedServiceTemplateFileName) {
170     return this.handledNestedFiles.contains(nestedServiceTemplateFileName);
171   }
172
173   public void updateUsedTimesForNestedComputeNodeType(String computeType) {
174     this.handledNestedComputeTypesNestedFiles.putIfAbsent(computeType, 0);
175
176     Integer usedNumber = this.handledNestedComputeTypesNestedFiles.get(computeType);
177     this.handledNestedComputeTypesNestedFiles.put(computeType, usedNumber + 1);
178
179   }
180
181   public int getGlobalNodeTypeIndex(String computeType) {
182     return Objects.isNull(this.handledNestedComputeTypesNestedFiles.get(computeType))
183         || this.handledNestedComputeTypesNestedFiles.get(computeType)== 0 ? 0
184         : this.handledNestedComputeTypesNestedFiles.get(computeType);
185   }
186
187   public boolean isNestedNodeWasHandled(String nestedNodeId) {
188     return this.handledNestedNodes.contains(nestedNodeId);
189   }
190
191
192   public Map<String, Object> getAllNewPropertyInputParamIds(){
193     return this.newParameterIdsToPropertiesFromOrigNodeTemplate;
194   }
195
196   public void addNewPropertyIdToNodeTemplate(String newPropertyId,
197                                              Object origPropertyValue){
198     if(!newParameterIdsToPropertiesFromOrigNodeTemplate.containsKey(newPropertyId)) {
199       newParameterIdsToPropertiesFromOrigNodeTemplate.put(newPropertyId, origPropertyValue);
200     }
201   }
202
203   public Optional<Object> getNewPropertyInputParam(String newPropertyId){
204     if(!newParameterIdsToPropertiesFromOrigNodeTemplate.containsKey(newPropertyId)){
205       return Optional.empty();
206     }
207
208     return Optional.of(newParameterIdsToPropertiesFromOrigNodeTemplate.get(newPropertyId));
209   }
210
211 }