Rename packages from openecomp to onap.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / unifiedcomposition / UnifiedCompositionSingleSubstitution.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.services.heattotosca.impl.unifiedcomposition;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
21 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
22 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedCompositionData;
23 import org.openecomp.sdc.translator.services.heattotosca.UnifiedComposition;
24 import org.openecomp.sdc.translator.services.heattotosca.UnifiedCompositionService;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29
30
31 /**
32  * The type Unified composition single substitution.
33  */
34 public class UnifiedCompositionSingleSubstitution implements UnifiedComposition {
35
36   private UnifiedCompositionService unifiedCompositionService = new UnifiedCompositionService();
37
38   // There is no consolidation in SingleSubstitution implementation.
39   // In case of single substitution, if there is more than one entry in the
40   // unifiedCompositionDataList, they all should contain the same compute type but the
41   // consolidation between them was canceled.
42   // For different compute type, this implementation will be called more than once, each time
43   // per diff compute type, while sending one entry in the unifiedCompositionDataList.
44   @Override
45   public void createUnifiedComposition(ServiceTemplate serviceTemplate,
46                                        ServiceTemplate nestedServiceTemplate,
47                                        List<UnifiedCompositionData> unifiedCompositionDataList,
48                                        TranslationContext context) {
49     if (CollectionUtils.isEmpty(unifiedCompositionDataList)
50         || context.isUnifiedHandledServiceTemplate(serviceTemplate)) {
51       return;
52     }
53
54     unifiedCompositionService.handleComplexVfcType(serviceTemplate, context);
55
56     for (int i = 0; i < unifiedCompositionDataList.size(); i++) {
57       List<UnifiedCompositionData> singleSubstitutionUnifiedList = new ArrayList<>();
58       singleSubstitutionUnifiedList.add(unifiedCompositionDataList.get(i));
59
60       String substitutionNodeTypeId =
61           unifiedCompositionService.getSubstitutionNodeTypeId(serviceTemplate,
62               singleSubstitutionUnifiedList.get(0), null, context);
63
64       Optional<ServiceTemplate> substitutionServiceTemplate =
65           unifiedCompositionService.createUnifiedSubstitutionServiceTemplate(serviceTemplate,
66               singleSubstitutionUnifiedList, context, substitutionNodeTypeId, null);
67
68       if (!substitutionServiceTemplate.isPresent()) {
69         continue;
70       }
71
72       String abstractSubstituteNodeTemplateId = unifiedCompositionService
73           .createAbstractSubstituteNodeTemplate(serviceTemplate, substitutionServiceTemplate.get(),
74               singleSubstitutionUnifiedList, substitutionNodeTypeId, context, null);
75
76       unifiedCompositionService.createVfcInstanceGroup(abstractSubstituteNodeTemplateId,
77           serviceTemplate, singleSubstitutionUnifiedList, context);
78
79       unifiedCompositionService
80           .updateCompositionConnectivity(serviceTemplate, singleSubstitutionUnifiedList, context);
81
82       unifiedCompositionService
83           .cleanUnifiedCompositionEntities(serviceTemplate, singleSubstitutionUnifiedList, context);
84
85       unifiedCompositionService.updateSubstitutionNodeTypePrefix(substitutionServiceTemplate.get());
86     }
87
88     unifiedCompositionService
89         .cleanNodeTypes(serviceTemplate, unifiedCompositionDataList, context);
90
91   }
92 }