9d16502689a380325ea8f3c8cde51be5f6e57dc8
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceConnectionUsingRequirementHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
22
23 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
24 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
25 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
26 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.services.DataModelUtil;
29 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
30
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.function.Predicate;
37
38 public abstract class ResourceConnectionUsingRequirementHelper
39     extends BaseResourceConnection<RequirementDefinition> {
40   public ResourceConnectionUsingRequirementHelper(ResourceTranslationBase resourceTranslationBase,
41                                                   TranslateTo translateTo, FileData nestedFileData,
42                                                   NodeTemplate substitutionNodeTemplate,
43                                                   NodeType nodeType) {
44     super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
45   }
46
47   @Override
48   String getMappedNodeTranslatedResourceId(ServiceTemplate nestedServiceTemplate,
49                                            Map.Entry<String,
50                                                RequirementDefinition> connectionPointEntry) {
51     List<String> substitutionMapping =
52         nestedServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
53             .get(connectionPointEntry.getKey());
54     return substitutionMapping.get(0);
55   }
56
57   @Override
58   Map.Entry<String, RequirementDefinition> getMappedConnectionPointEntry(
59       ServiceTemplate nestedServiceTemplate,
60       Map.Entry<String, RequirementDefinition> connectionPointEntry) {
61     List<String> substitutionMapping =
62         nestedServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
63             .get(connectionPointEntry.getKey());
64     String mappedNodeTranslatedId = substitutionMapping.get(0);
65     String mappedReqId = substitutionMapping.get(1);
66     NodeTemplate mappedNodeTemplate =
67         nestedServiceTemplate.getTopology_template().getNode_templates()
68             .get(mappedNodeTranslatedId);
69     NodeType substituteNodeType =
70         translateTo.getContext().getGlobalSubstitutionServiceTemplate().getNode_types()
71             .get(mappedNodeTemplate.getType());
72     Optional<RequirementDefinition> requirementDefinition =
73         DataModelUtil.getRequirementDefinition(substituteNodeType, mappedReqId);
74     return new Map.Entry<String, RequirementDefinition>() {
75       @Override
76       public String getKey() {
77         return mappedReqId;
78       }
79
80       @Override
81       public RequirementDefinition getValue() {
82         return requirementDefinition.get();
83       }
84
85       @Override
86       public RequirementDefinition setValue(RequirementDefinition value) {
87         return null;
88       }
89     };
90   }
91
92   @Override
93   List<Map<String, RequirementDefinition>> getAllConnectionPoints() {
94     List<Map<String, RequirementDefinition>> exposedRequirementsList = new ArrayList<>();
95     List<Predicate<RequirementDefinition>> predicates = getPredicatesListForConnectionPoints();
96     List<Map<String, RequirementDefinition>> requirements = this.nodeType.getRequirements();
97     if (requirements == null) {
98       return exposedRequirementsList;
99     }
100     requirements.stream()
101         .map(Map::entrySet)
102         .forEach(x -> x.stream()
103             .filter(entry -> predicates
104                 .stream()
105                 .anyMatch(p -> p.test(entry.getValue())))
106             .forEach(entry -> {
107               Map<String, RequirementDefinition> exposedRequirementsMap = new HashMap<>();
108               exposedRequirementsMap.put(entry.getKey(), entry.getValue());
109               exposedRequirementsList.add(exposedRequirementsMap);
110             }));
111     return exposedRequirementsList;
112   }
113
114   void addRequirementToConnectResource(
115       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry, String paramName,
116       Object paramValue, List<String> supportedNetworkTypes) {
117     if (paramValue == null) {
118       logger.warn("'" + paramName + "' property is not define in nested resource '"
119           + translateTo.getResourceId() + "' for the nested heat file, therefore, '"
120           + requirementDefinitionEntry.getKey() + "' TOSCA requirement will not be connected.");
121       return;
122     }
123     Optional<String> targetTranslatedNodeId =
124         getConnectionTranslatedNodeUsingGetResourceFunc(requirementDefinitionEntry, paramName,
125             paramValue, supportedNetworkTypes);
126     if (targetTranslatedNodeId.isPresent()) {
127       createRequirementAssignment(requirementDefinitionEntry, targetTranslatedNodeId.get(),
128           substitutionNodeTemplate);
129     } else {
130       targetTranslatedNodeId =
131           getConnectionTranslatedNodeUsingGetParamFunc(requirementDefinitionEntry, paramName,
132               supportedNetworkTypes);
133       targetTranslatedNodeId
134           .ifPresent(targetTranslatedId -> createRequirementAssignment(requirementDefinitionEntry,
135               targetTranslatedId, substitutionNodeTemplate));
136     }
137   }
138 }