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