2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
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;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Optional;
36 import java.util.function.Predicate;
38 public abstract class ResourceConnectionUsingRequirementHelper
39 extends BaseResourceConnection<RequirementDefinition> {
40 public ResourceConnectionUsingRequirementHelper(ResourceTranslationBase resourceTranslationBase,
41 TranslateTo translateTo, FileData nestedFileData,
42 NodeTemplate substitutionNodeTemplate,
44 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
48 String getMappedNodeTranslatedResourceId(ServiceTemplate nestedServiceTemplate,
50 RequirementDefinition> connectionPointEntry) {
53 mdcDataDebugMessage.debugEntryMessage(null, null);
55 List<String> substitutionMapping =
56 nestedServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
57 .get(connectionPointEntry.getKey());
59 mdcDataDebugMessage.debugExitMessage(null, null);
60 return substitutionMapping.get(0);
64 Map.Entry<String, RequirementDefinition> getMappedConnectionPointEntry(
65 ServiceTemplate nestedServiceTemplate,
66 Map.Entry<String, RequirementDefinition> connectionPointEntry) {
69 mdcDataDebugMessage.debugEntryMessage(null, null);
71 List<String> substitutionMapping =
72 nestedServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
73 .get(connectionPointEntry.getKey());
74 String mappedNodeTranslatedId = substitutionMapping.get(0);
75 String mappedReqId = substitutionMapping.get(1);
76 NodeTemplate mappedNodeTemplate =
77 nestedServiceTemplate.getTopology_template().getNode_templates()
78 .get(mappedNodeTranslatedId);
79 NodeType substituteNodeType =
80 translateTo.getContext().getGlobalSubstitutionServiceTemplate().getNode_types()
81 .get(mappedNodeTemplate.getType());
82 Optional<RequirementDefinition> requirementDefinition =
83 DataModelUtil.getRequirementDefinition(substituteNodeType, mappedReqId);
85 mdcDataDebugMessage.debugExitMessage(null, null);
86 return new Map.Entry<String, RequirementDefinition>() {
88 public String getKey() {
93 public RequirementDefinition getValue() {
94 return requirementDefinition.get();
98 public RequirementDefinition setValue(RequirementDefinition value) {
105 List<Map<String, RequirementDefinition>> getAllConnectionPoints() {
108 mdcDataDebugMessage.debugEntryMessage(null, null);
110 List<Map<String, RequirementDefinition>> exposedRequirementsList = new ArrayList<>();
111 List<Predicate<RequirementDefinition>> predicates = getPredicatesListForConnectionPoints();
112 List<Map<String, RequirementDefinition>> requirements = this.nodeType.getRequirements();
113 if (requirements == null) {
114 return exposedRequirementsList;
116 requirements.stream()
118 .forEach(x -> x.stream()
119 .filter(entry -> predicates
121 .anyMatch(p -> p.test(entry.getValue())))
123 Map<String, RequirementDefinition> exposedRequirementsMap = new HashMap<>();
124 exposedRequirementsMap.put(entry.getKey(), entry.getValue());
125 exposedRequirementsList.add(exposedRequirementsMap);
128 mdcDataDebugMessage.debugExitMessage(null, null);
129 return exposedRequirementsList;
132 void addRequirementToConnectResource(
133 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry, String paramName,
134 Object paramValue, List<String> supportedNetworkTypes) {
137 mdcDataDebugMessage.debugEntryMessage(null, null);
139 if (paramValue == null) {
140 logger.warn("'" + paramName + "' property is not define in nested resource '"
141 + translateTo.getResourceId() + "' for the nested heat file, therefore, '"
142 + requirementDefinitionEntry.getKey() + "' TOSCA requirement will not be connected.");
145 Optional<String> targetTranslatedNodeId =
146 getConnectionTranslatedNodeUsingGetResourceFunc(requirementDefinitionEntry, paramName,
147 paramValue, supportedNetworkTypes);
148 if (targetTranslatedNodeId.isPresent()) {
149 createRequirementAssignment(requirementDefinitionEntry, targetTranslatedNodeId.get(),
150 substitutionNodeTemplate);
152 targetTranslatedNodeId =
153 getConnectionTranslatedNodeUsingGetParamFunc(requirementDefinitionEntry, paramName,
154 supportedNetworkTypes);
155 targetTranslatedNodeId
156 .ifPresent(targetTranslatedId -> createRequirementAssignment(requirementDefinitionEntry,
157 targetTranslatedId, substitutionNodeTemplate));
160 mdcDataDebugMessage.debugExitMessage(null, null);