df6bad594da4f254ddbccabca4ad64a62d2faa9e
[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.translator.datatypes.heattotosca;
18
19 import com.google.common.collect.ArrayListMultimap;
20 import com.google.common.collect.ListMultimap;
21 import org.apache.commons.collections.MapUtils;
22 import org.onap.config.api.Configuration;
23 import org.onap.config.api.ConfigurationManager;
24 import org.openecomp.core.utilities.CommonMethods;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
29 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
30 import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
31 import org.openecomp.sdc.heat.datatypes.model.Resource;
32 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
33 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.services.ToscaUtil;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedCompositionEntity;
37 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedSubstitutionData;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.*;
39 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
40 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationEntityType;
41 import org.openecomp.sdc.translator.services.heattotosca.Constants;
42 import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
43 import org.openecomp.sdc.translator.services.heattotosca.errors.DuplicateResourceIdsInDifferentFilesErrorBuilder;
44 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
45
46 import java.io.InputStream;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Objects;
52 import java.util.Optional;
53 import java.util.Set;
54
55
56 public class TranslationContext {
57
58   private ManifestFile manifest;
59
60
61   private static final Map<String, Map<String, Map<String, String>>> translationMapping;
62   private static final Map<String, ServiceTemplate> globalServiceTemplates;
63   private static final Map<String, ImplementationConfiguration> nameExtractorImplMap;
64   private static final List<String> vfcGroupSubInterfaceExposedProperties;
65   private static final List<String> enrichPortResourceProperties;
66   private static final ImplementationConfiguration vfcInstanceGroupConfiguration;
67
68   private static Map<String, ImplementationConfiguration> supportedConsolidationComputeResources;
69   private static Map<String, ImplementationConfiguration> supportedConsolidationPortResources;
70
71   private final FileContentHandler files = new FileContentHandler();
72   private final Map<String, FileData.Type> manifestFiles = new HashMap<>();
73   //Key - file name, value - file type
74   private final Set<String> nestedHeatsFiles = new HashSet<>();
75   private final FileContentHandler externalArtifacts = new FileContentHandler();
76   // Key - heat file name,value - set of heat resource ids which were translated
77   private final Map<String, Set<String>> translatedResources = new HashMap<>();
78   // Key - heat file name, value - translated Node template id
79   private final Map<String, Set<String>> heatStackGroupMembers = new HashMap<>();
80   // Key - heat file name, value - Map with Key - heat resource Id, Value - tosca entity template id
81   private final Map<String, Map<String, String>> translatedIds = new HashMap<>();
82   // key - service template type, value - translated service templates
83   private final Map<String, ServiceTemplate> translatedServiceTemplates = new HashMap<>();
84   //key - heat param name, value - shared resource data
85   private final Map<String, TranslatedHeatResource> heatSharedResourcesByParam = new HashMap<>();
86   //key - translated substitute service template file name, value - source nested heat file name
87   private final Map<String, String> nestedHeatFileName = new HashMap<>();
88   //Key - heat file name,value - Map eith key - heat pseudo param name,
89   // value - translated tosca parameter name
90   private final Map<String, Map<String, String>> usedHeatPseudoParams = new HashMap<>();
91   //Consolidation data gathered for Unified TOSCA model
92   private ConsolidationData consolidationData = new ConsolidationData();
93   private Map<String, UnifiedSubstitutionData> unifiedSubstitutionData = new HashMap<>();
94   private final Set<String> unifiedHandledServiceTemplates = new HashSet<>();
95
96   private final Map<String, Map<String, Map<String, Integer>>> requirementIdAppearanceInNodeTemplate = new HashMap<>();
97
98   private final Set<String> serviceTemplatesWithoutNodeTemplateSection = new HashSet<>();
99
100   private final Set<String> nodeTemplateIdsPointingToStWithoutNodeTemplates = new HashSet<>();
101
102   //Key - service template name, value - Map of key: node template id, value: properties with %index%
103   private final Map<String, ListMultimap<String, String>> indexVarProperties = new HashMap<>();
104
105   static {
106     Configuration config = ConfigurationManager.lookup();
107     translationMapping =
108             config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY);
109     try {
110       globalServiceTemplates =
111               GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP);
112     } catch (Exception exc) {
113       throw new RuntimeException("Failed to load GlobalTypes", exc);
114     }
115     nameExtractorImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
116             ConfigConstants.NAMING_CONVENTION_EXTRACTOR_IMPL_KEY, ImplementationConfiguration.class);
117     supportedConsolidationComputeResources = config.populateMap(ConfigConstants.MANDATORY_UNIFIED_MODEL_NAMESPACE,
118             ConfigConstants.SUPPORTED_CONSOLIDATION_COMPUTE_RESOURCES_KEY, ImplementationConfiguration.class);
119     supportedConsolidationPortResources = config.populateMap(ConfigConstants.MANDATORY_UNIFIED_MODEL_NAMESPACE,
120             ConfigConstants.SUPPORTED_CONSOLIDATION_PORT_RESOURCES_KEY, ImplementationConfiguration.class);
121     enrichPortResourceProperties = config.getAsStringValues(ConfigConstants.MANDATORY_UNIFIED_MODEL_NAMESPACE,
122             ConfigConstants.ENRICH_PORT_RESOURCE_PROP);
123     vfcInstanceGroupConfiguration = getVfcInstanceGroupConfiguration(config);
124     vfcGroupSubInterfaceExposedProperties = config.getAsStringValues(ConfigConstants.UNIFIED_MODEL_NAMESPACE,
125             ConfigConstants.FULL_EXPOSED_PROPERTIES_KEY);
126   }
127
128
129   private static ImplementationConfiguration getVfcInstanceGroupConfiguration(Configuration config) {
130     Map<String, ImplementationConfiguration> supportedUnifiedModelProperties =
131             config.populateMap(ConfigConstants.UNIFIED_MODEL_NAMESPACE, ConfigConstants.UNIFIED_MODEL_IMPL_KEY,
132                     ImplementationConfiguration.class);
133     return MapUtils.isEmpty(supportedUnifiedModelProperties) ? null : supportedUnifiedModelProperties.get(ConfigConstants.VFC_INSTANCE_GROUP_KEY);
134   }
135
136   public static boolean isVfcInstanceGroupingEnabled() {
137     return Objects.nonNull(vfcInstanceGroupConfiguration) && vfcInstanceGroupConfiguration.isEnable();
138   }
139
140   public static List<String> getExposedVfcInstanceGroupingProperties() {
141     return vfcGroupSubInterfaceExposedProperties;
142   }
143
144   public static List<String> getEnrichPortResourceProperties() {
145     return enrichPortResourceProperties;
146   }
147
148   public static Map<String, ImplementationConfiguration> getSupportedConsolidationComputeResources() {
149     return supportedConsolidationComputeResources;
150   }
151
152   public static void setSupportedConsolidationComputeResources(Map<String, ImplementationConfiguration> supportedConsolidationComputeResources) {
153     TranslationContext.supportedConsolidationComputeResources = supportedConsolidationComputeResources;
154   }
155
156   public static Map<String, ImplementationConfiguration> getSupportedConsolidationPortResources() {
157     return supportedConsolidationPortResources;
158   }
159
160   public static void setSupportedConsolidationPortResources(Map<String, ImplementationConfiguration> supportedConsolidationPortResources) {
161     TranslationContext.supportedConsolidationPortResources = supportedConsolidationPortResources;
162   }
163
164   /**
165    * Get nameExtractor implemetation class instance.
166    *
167    * @param extractorImplKey configuration key for the implementation class
168    * @return implemetation class instance
169    */
170   public static NameExtractor getNameExtractorImpl(String extractorImplKey) {
171     String nameExtractorImplClassName = nameExtractorImplMap.get(extractorImplKey).getImplementationClass();
172
173     return CommonMethods.newInstance(nameExtractorImplClassName, NameExtractor.class);
174   }
175
176   public Map<String, UnifiedSubstitutionData> getUnifiedSubstitutionData() {
177     return unifiedSubstitutionData;
178   }
179
180   public void setUnifiedSubstitutionData(Map<String, UnifiedSubstitutionData> unifiedSubstitutionData) {
181     this.unifiedSubstitutionData = unifiedSubstitutionData;
182   }
183
184   public void addCleanedNodeTemplate(String serviceTemplateName, String nodeTemplateId, UnifiedCompositionEntity unifiedCompositionEntity,
185                                             NodeTemplate nodeTemplate) {
186     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
187     this.unifiedSubstitutionData.get(serviceTemplateName)
188                                 .addCleanedNodeTemplate(nodeTemplateId, unifiedCompositionEntity, nodeTemplate);
189   }
190
191   public Optional<List<String>> getIndexVarProperties(String serviceTemplateName, String nodeTemplateId) {
192     ListMultimap<String, String> serviceTemplateIndexVarProperties = this.indexVarProperties.get(serviceTemplateName);
193     if (Objects.nonNull(serviceTemplateIndexVarProperties)) {
194       return Optional.of(this.indexVarProperties.get(serviceTemplateName).get(nodeTemplateId));
195     }
196     return Optional.empty();
197   }
198
199   public void addIndexVarProperties(String serviceTemplateName, String nodeTemplateId, List<String> indexVarProperties) {
200     this.indexVarProperties.putIfAbsent(serviceTemplateName, ArrayListMultimap.create());
201     this.indexVarProperties.get(serviceTemplateName).putAll(nodeTemplateId, indexVarProperties);
202   }
203
204   public NodeTemplate getCleanedNodeTemplate(String serviceTemplateName, String nodeTemplateId) {
205     return this.unifiedSubstitutionData.get(serviceTemplateName).getCleanedNodeTemplate(nodeTemplateId);
206   }
207
208   public void addUnifiedNestedNodeTemplateId(String serviceTemplateName, String nestedNodeTemplateId, String unifiedNestedNodeTemplateId) {
209     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
210     this.unifiedSubstitutionData.get(serviceTemplateName)
211                                 .addUnifiedNestedNodeTemplateId(nestedNodeTemplateId, unifiedNestedNodeTemplateId);
212   }
213
214   public Optional<String> getUnifiedNestedNodeTemplateId(String serviceTemplateName, String nestedNodeTemplateId) {
215     return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty() :
216                    this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTemplateId(nestedNodeTemplateId);
217   }
218
219   public void addUnifiedNestedNodeTypeId(String serviceTemplateName, String nestedNodeTypeId, String unifiedNestedNodeTypeId) {
220     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
221     this.unifiedSubstitutionData.get(serviceTemplateName)
222                                 .addUnifiedNestedNodeTypeId(nestedNodeTypeId, unifiedNestedNodeTypeId);
223   }
224
225   public Optional<String> getUnifiedNestedNodeTypeId(String serviceTemplateName, String nestedNodeTemplateId) {
226     return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty() :
227                    this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTypeId(nestedNodeTemplateId);
228   }
229
230   public ConsolidationData getConsolidationData() {
231     return consolidationData;
232   }
233
234   public void setConsolidationData(ConsolidationData consolidationData) {
235     this.consolidationData = consolidationData;
236   }
237
238   public Optional<ConsolidationDataHandler> getConsolidationDataHandler(ConsolidationEntityType type) {
239     return consolidationData.getConsolidationDataHandler(type);
240   }
241
242   public ComputeConsolidationDataHandler getComputeConsolidationDataHandler() {
243     return consolidationData.getComputeConsolidationDataHandler();
244   }
245
246   public PortConsolidationDataHandler getPortConsolidationDataHandler() {
247     return consolidationData.getPortConsolidationDataHandler();
248   }
249
250   public NestedConsolidationDataHandler getNestedConsolidationDataHandler() {
251     return consolidationData.getNestedConsolidationDataHandler();
252   }
253
254   public SubInterfaceConsolidationDataHandler getSubInterfaceComputeConsolidationDataHandler() {
255     return consolidationData.getSubInterfaceConsolidationDataHandler();
256   }
257
258   public void addManifestFile(String fileName, FileData.Type fileType) {
259     this.manifestFiles.put(fileName, fileType);
260   }
261
262   public Set<String> getNestedHeatsFiles() {
263     return nestedHeatsFiles;
264   }
265
266   public Map<String, Set<String>> getHeatStackGroupMembers() {
267     return heatStackGroupMembers;
268   }
269
270   public FileContentHandler getFiles() {
271     return files;
272   }
273
274   public void setFiles(Map<String, byte[]> files) {
275     this.files.putAll(files);
276   }
277
278   public InputStream getFileContent(String fileName) {
279     return files.getFileContent(fileName);
280   }
281
282   public void addFile(String name, byte[] content) {
283     files.addFile(name, content);
284   }
285
286   public ManifestFile getManifest() {
287     return manifest;
288   }
289
290   public void setManifest(ManifestFile manifest) {
291     this.manifest = manifest;
292   }
293
294   public Map<String, Set<String>> getTranslatedResources() {
295     return translatedResources;
296   }
297
298   public Map<String, Map<String, String>> getTranslatedIds() {
299     return translatedIds;
300   }
301
302   public Set<String> getAllTranslatedResourceIdsFromDiffNestedFiles(String
303                                                                         nestedHeatFileNameToSkip) {
304     Set<String> allTranslatedResourceIds = new HashSet<>();
305
306     this.translatedIds.entrySet().stream().filter(
307         heatFileNameToTranslatedIdsEntry -> !heatFileNameToTranslatedIdsEntry.getKey()
308             .equals(nestedHeatFileNameToSkip)).forEach(heatFileNameToTranslatedIdsEntry ->
309       allTranslatedResourceIds.addAll(heatFileNameToTranslatedIdsEntry.getValue().keySet())
310     );
311
312     return allTranslatedResourceIds;
313   }
314
315   // get tosca name from mapping configuration file
316   //element type - parameter/attribute
317   // element name - heat parameter/attribute name
318   //return value - tosca parameter/attribute name
319   public String getElementMapping(String resourceType, String elementType, String elementName) {
320     if (Objects.isNull(translationMapping.get(resourceType))) {
321       return null;
322     }
323     if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
324       return null;
325     }
326     return translationMapping.get(resourceType).get(elementType).get(elementName);
327   }
328
329   public Map<String, String> getElementMapping(String resourceType, String elementType) {
330     if (Objects.isNull(translationMapping.get(resourceType))) {
331       return null;
332     }
333     return translationMapping.get(resourceType).get(elementType);
334   }
335
336   public Set<String> getElementSet(String resourceType, String elementType) {
337     if (Objects.isNull(translationMapping.get(resourceType))) {
338       return new HashSet<>();
339     }
340     if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
341       return new HashSet<>();
342     }
343     return translationMapping.get(resourceType).get(elementType).keySet();
344   }
345
346   public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
347     return translatedServiceTemplates;
348   }
349
350   public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
351     return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
352   }
353
354   public FileContentHandler getExternalArtifacts() {
355     return externalArtifacts;
356   }
357
358   public void addExternalArtifacts(String name, byte[] content) {
359     this.externalArtifacts.addFile(name, content);
360   }
361
362   public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
363     return heatSharedResourcesByParam;
364   }
365
366   public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
367                                             Resource resource) {
368     this.addHeatSharedResourcesByParam(parameterName,
369         new TranslatedHeatResource(resourceId, resource));
370   }
371
372   private void addHeatSharedResourcesByParam(String parameterName,
373                                              TranslatedHeatResource translatedHeatResource) {
374     this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
375   }
376
377   public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
378     return globalServiceTemplates;
379   }
380
381   public Map<String, String> getNestedHeatFileName() {
382     return nestedHeatFileName;
383   }
384
385   public void addNestedHeatFileName(String substituteServiceTempalteName,
386                                     String nestedHeatFileName) {
387     this.nestedHeatFileName.put(substituteServiceTempalteName, nestedHeatFileName);
388   }
389
390   public Map<String, Map<String, String>> getUsedHeatPseudoParams() {
391     return usedHeatPseudoParams;
392   }
393
394   public void addUsedHeatPseudoParams(String heatFileName, String heatPseudoParam, String
395       translatedToscaParam) {
396     if (Objects.isNull(this.usedHeatPseudoParams.get(heatFileName))) {
397       this.usedHeatPseudoParams.put(heatFileName, new HashMap<>());
398     }
399     this.usedHeatPseudoParams.get(heatFileName).put(heatPseudoParam, translatedToscaParam);
400   }
401
402   public Set<String> getTranslatedResourceIdsFromOtherFiles(String fileNameToIgnore){
403     if(MapUtils.isEmpty(this.translatedResources)){
404       return new HashSet<>();
405     }
406
407     Set<String> translatedResourceIds = new HashSet<>();
408
409     this.translatedResources.entrySet().stream().filter(entry -> !entry.getKey().equals(fileNameToIgnore))
410         .forEach(entry -> translatedResourceIds.addAll(entry.getValue()));
411
412     return translatedResourceIds;
413   }
414
415   /**
416    * Add the unified substitution data info in context. Contains a mapping of original node
417    * template id and the new node template id in the abstract substitute
418    *
419    * @param serviceTemplateFileName the service template file name
420    * @param originalNodeTemplateId  the original node template id
421    * @param abstractNodeTemplateId  the node template id in the abstract substitute
422    */
423   public void addUnifiedSubstitutionData(String serviceTemplateFileName,
424                                          String originalNodeTemplateId,
425                                          String abstractNodeTemplateId) {
426
427     Map<String, String> nodeAbstractNodeTemplateIdMap = this.getUnifiedSubstitutionData()
428         .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
429         .getNodesRelatedAbstractNode();
430
431     if (nodeAbstractNodeTemplateIdMap == null) {
432       nodeAbstractNodeTemplateIdMap = new HashMap<>();
433     }
434
435     if(nodeAbstractNodeTemplateIdMap.containsKey(originalNodeTemplateId)){
436       throw new CoreException(new DuplicateResourceIdsInDifferentFilesErrorBuilder(originalNodeTemplateId).build());
437     }
438     nodeAbstractNodeTemplateIdMap.put(originalNodeTemplateId, abstractNodeTemplateId);
439     this.getUnifiedSubstitutionData().get(serviceTemplateFileName).setNodesRelatedAbstractNode(
440         nodeAbstractNodeTemplateIdMap);
441   }
442
443   /**
444    * Add the unified substitution data info in context. Contains a mapping of original node
445    * template id and the new node template id in the abstract substitute
446    *
447    * @param serviceTemplateFileName                   the service template file name
448    * @param originalNodeTemplateId                    the original node template id
449    * @param substitutionServiceTemplateNodeTemplateId the node template id in the substitution
450    *                                                  service template
451    */
452   public void addSubstitutionServiceTemplateUnifiedSubstitutionData(
453       String serviceTemplateFileName,
454       String originalNodeTemplateId,
455       String substitutionServiceTemplateNodeTemplateId) {
456
457     Map<String, String> nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = this
458         .getUnifiedSubstitutionData()
459         .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
460         .getNodesRelatedSubstitutionServiceTemplateNode();
461
462     if (nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap == null) {
463       nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = new HashMap<>();
464     }
465     nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap.put(originalNodeTemplateId,
466         substitutionServiceTemplateNodeTemplateId);
467     this.getUnifiedSubstitutionData().get(serviceTemplateFileName)
468         .setNodesRelatedSubstitutionServiceTemplateNode(
469             nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap);
470   }
471
472   /**
473    * Get unified abstract node template which is mapped to the input node template id.
474    *
475    * @param serviceTemplate the service template
476    * @param nodeTemplateId  the node template id
477    */
478   public String getUnifiedAbstractNodeTemplateId(ServiceTemplate serviceTemplate,
479                                                  String nodeTemplateId) {
480     UnifiedSubstitutionData unifiedSubsData =
481         this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
482     return unifiedSubsData.getNodesRelatedAbstractNode().get(nodeTemplateId);
483   }
484
485   /**
486    * Get unified node template in the substitution service template which is mapped to the
487    * original input node template id.
488    *
489    * @param serviceTemplate the service template
490    * @param nodeTemplateId  the node template id
491    */
492   public String getUnifiedSubstitutionNodeTemplateId(ServiceTemplate serviceTemplate,
493                                                      String nodeTemplateId) {
494     UnifiedSubstitutionData unifiedSubsData =
495         this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
496     return unifiedSubsData.getNodesRelatedSubstitutionServiceTemplateNode()
497         .get(nodeTemplateId);
498   }
499
500   public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
501                                                       String computeType) {
502     return this.unifiedSubstitutionData.get(serviceTemplateName)
503         .getHandledNestedComputeNodeTemplateIndex(computeType);
504   }
505
506   public void updateHandledComputeType(String serviceTemplateName,
507                                        String handledComputeType,
508                                        String nestedServiceTemplateFileName) {
509     String globalSTName =
510         ToscaUtil.getServiceTemplateFileName(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
511     this.unifiedSubstitutionData.putIfAbsent(
512         globalSTName, new UnifiedSubstitutionData());
513     this.unifiedSubstitutionData.get(globalSTName)
514         .addHandledComputeType(handledComputeType);
515     this.unifiedSubstitutionData.get(globalSTName).addHandlesNestedServiceTemplate(nestedServiceTemplateFileName);
516
517     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
518     this.unifiedSubstitutionData.get(serviceTemplateName).addHandlesNestedServiceTemplate(nestedServiceTemplateFileName);
519   }
520
521   public void addHandledComputeTypeInServiceTemplate(String serviceTemplateName,
522                                                      String handledComputeType){
523     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
524     this.unifiedSubstitutionData.get(serviceTemplateName).addHandledComputeType(handledComputeType);
525   }
526
527   public boolean isComputeTypeHandledInServiceTemplate(String serviceTemplateName,
528                                                        String computeType) {
529     return !Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))
530         && this.unifiedSubstitutionData.get(serviceTemplateName)
531         .isComputeTypeHandledInServiceTemplate(computeType);
532   }
533
534   public boolean isNestedServiceTemplateWasHandled(String serviceTemplateName,
535                                                    String nestedServiceTemplateFileName) {
536     if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
537       return false;
538     }
539     return this.unifiedSubstitutionData.get(serviceTemplateName)
540         .isNestedServiceTemplateWasHandled(nestedServiceTemplateFileName);
541   }
542
543   public Set<String> getAllRelatedNestedNodeTypeIds() {
544     String globalName = "GlobalSubstitutionTypes";
545     if (Objects.isNull(this.unifiedSubstitutionData)
546         || Objects.isNull(this.unifiedSubstitutionData.get(globalName))) {
547       return new HashSet<>();
548     }
549
550     return this.unifiedSubstitutionData.get(globalName).getAllRelatedNestedNodeTypeIds();
551   }
552
553   public boolean isUnifiedHandledServiceTemplate(ServiceTemplate serviceTemplate) {
554     String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
555     return unifiedHandledServiceTemplates.contains(serviceTemplateFileName)? true:false;
556   }
557
558
559
560   public void addUnifiedHandledServiceTeamplte(ServiceTemplate serviceTemplate) {
561     String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
562     this.unifiedHandledServiceTemplates.add(serviceTemplateFileName);
563   }
564
565   public boolean isNestedNodeWasHandled(String serviceTemplateName,
566                                         String nestedNodeTemplateId) {
567     if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
568       return false;
569     }
570     return this.unifiedSubstitutionData.get(serviceTemplateName)
571         .isNestedNodeWasHandled(nestedNodeTemplateId);
572   }
573
574   public void addNestedNodeAsHandled(String serviceTemplateName,
575                                      String nestedNodeTemplateId) {
576     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
577     this.unifiedSubstitutionData.get(serviceTemplateName)
578         .addHandledNestedNodes(nestedNodeTemplateId);
579   }
580
581   public void updateUsedTimesForNestedComputeNodeType(String serviceTemplateName,
582                                                       String computeType) {
583     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
584
585     this.unifiedSubstitutionData.get(serviceTemplateName)
586         .updateUsedTimesForNestedComputeNodeType(computeType);
587   }
588
589   public int getGlobalNodeTypeIndex(String serviceTemplateName,
590                                     String computeType) {
591     if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
592       return 0;
593     }
594     return this.unifiedSubstitutionData.get(serviceTemplateName).getGlobalNodeTypeIndex
595         (computeType);
596   }
597
598   public void addNewPropertyIdToNodeTemplate(String serviceTemplateName,
599                                              String newPropertyId,
600                                              Object origPropertyValue){
601     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
602     this.unifiedSubstitutionData.get(serviceTemplateName).addNewPropertyIdToNodeTemplate(
603         newPropertyId, origPropertyValue);
604   }
605
606   public Optional<Object> getNewPropertyInputParamId(String serviceTemplateName,
607                                                      String newPropertyId){
608     if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
609       return Optional.empty();
610     }
611
612     return this.unifiedSubstitutionData.get(serviceTemplateName).getNewPropertyInputParam
613         (newPropertyId);
614   }
615
616   public Map<String, Object> getAllNewPropertyInputParamIdsPerNodeTenplateId(String serviceTemplateName){
617     if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
618       return new HashMap<>();
619     }
620
621     return this.unifiedSubstitutionData.get(serviceTemplateName).getAllNewPropertyInputParamIds();
622
623   }
624
625   public boolean isServiceTemplateWithoutNodeTemplatesSection(String serviceTemplateName){
626     return Objects.nonNull(serviceTemplateName)
627           && serviceTemplatesWithoutNodeTemplateSection.contains(serviceTemplateName);
628   }
629
630   public void addServiceTemplateWithoutNodeTemplates(String serviceTemplateName){
631     this.serviceTemplatesWithoutNodeTemplateSection.add(serviceTemplateName);
632   }
633
634   public void addNestedNodeTemplateIdPointsToStWithoutNodeTemplates(String nodeTemplateId){
635     this.nodeTemplateIdsPointingToStWithoutNodeTemplates.add(nodeTemplateId);
636   }
637
638   public boolean isNodeTemplateIdPointsToStWithoutNodeTemplates(String nodeTemplateId){
639     return Objects.nonNull(nodeTemplateId)
640         && nodeTemplateIdsPointingToStWithoutNodeTemplates.contains(nodeTemplateId);
641   }
642
643   public void updateRequirementAssignmentIdIndex(String serviceTemplateName,
644                                                  String nodeTemplateId,
645                                                  String requirementId){
646     requirementIdAppearanceInNodeTemplate.putIfAbsent(serviceTemplateName, new HashMap<>());
647     requirementIdAppearanceInNodeTemplate
648         .get(serviceTemplateName).putIfAbsent(nodeTemplateId, new HashMap<>());
649
650     Map<String, Integer> requirementIdToAppearance =
651         requirementIdAppearanceInNodeTemplate.get(serviceTemplateName).get(nodeTemplateId);
652
653     if(requirementIdToAppearance.containsKey(requirementId)){
654       requirementIdToAppearance
655           .put(requirementId, requirementIdToAppearance.get(requirementId) + 1);
656     } else {
657       requirementIdToAppearance.put(requirementId, 0);
658     }
659   }
660
661 }