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.common.errors.CoreException;
24 import org.openecomp.sdc.common.utils.CommonUtil;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.heat.services.HeatConstants;
29 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
35 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
36 import org.openecomp.sdc.tosca.services.DataModelUtil;
37 import org.openecomp.sdc.tosca.services.ToscaConstants;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
39 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
40 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
41 import org.openecomp.sdc.translator.services.heattotosca.errors.InvalidPropertyValueErrorBuilder;
42 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Optional;
50 public class ResourceTranslationResourceGroupImpl extends ResourceTranslationBase {
53 protected void translate(TranslateTo translateTo) {
56 mdcDataDebugMessage.debugEntryMessage(null, null);
58 final String heatFileName = translateTo.getHeatFileName();
60 translateTo.getResource().getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
61 Resource nestedResource = new Resource();
62 Object typeDefinition = ((Map) resourceDef).get("type");
63 if (!(typeDefinition instanceof String)) {
64 logger.warn("Resource '" + translateTo.getResourceId() + "' of type'"
65 + HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()
66 + "' with resourceDef which is not pointing to nested heat file is not supported and "
67 + "will be ignored in the translation ");
70 String type = (String) typeDefinition;
71 if (!HeatToToscaUtil.isYmlFileType(type)) {
72 logger.warn("Resource '" + translateTo.getResourceId() + "' of type'"
73 + HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()
74 + "' with resourceDef which is not pointing to nested heat file is not supported and "
75 + "will be ignored in the translation ");
77 mdcDataDebugMessage.debugExitMessage(null, null);
81 nestedResource.setType(type);
82 nestedResource.setProperties((Map<String, Object>) ((Map) resourceDef).get("properties"));
83 nestedResource.setMetadata(((Map) resourceDef).get("metadata"));
85 Optional<String> substitutionNodeTemplateId =
86 ResourceTranslationFactory.getInstance(nestedResource)
87 .translateResource(heatFileName, translateTo.getServiceTemplate(),
88 translateTo.getHeatOrchestrationTemplate(), nestedResource,
89 translateTo.getResourceId(), translateTo.getContext());
90 if (substitutionNodeTemplateId.isPresent()) {
91 NodeTemplate substitutionNodeTemplate = DataModelUtil
92 .getNodeTemplate(translateTo.getServiceTemplate(), substitutionNodeTemplateId.get());
93 Map serviceTemplateFilter = (Map<String, Object>) substitutionNodeTemplate.getProperties()
94 .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
96 populateServiceTemplateFilterProperties(translateTo, substitutionNodeTemplate,
97 serviceTemplateFilter);
98 handlingIndexVar(translateTo, substitutionNodeTemplate);
100 .addNodeTemplate(translateTo.getServiceTemplate(), substitutionNodeTemplateId.get(),
101 substitutionNodeTemplate);
104 mdcDataDebugMessage.debugExitMessage(null, null);
107 private void handlingIndexVar(TranslateTo translateTo, NodeTemplate substitutionNodeTemplate) {
110 mdcDataDebugMessage.debugEntryMessage(null, null);
112 String indexVarValue = getIndexVarValue(translateTo);
113 replacePropertiesIndexVarValue(indexVarValue, substitutionNodeTemplate.getProperties());
115 mdcDataDebugMessage.debugExitMessage(null, null);
118 private Map<String, List> getNewIndexVarValue() {
119 final Map<String, List> newIndexVarValue = new HashMap<>();
120 List indexVarValList = new ArrayList<>();
121 indexVarValList.add(ToscaConstants.MODELABLE_ENTITY_NAME_SELF);
122 indexVarValList.add(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
123 indexVarValList.add(ToscaConstants.INDEX_VALUE_PROPERTY_NAME);
124 newIndexVarValue.put(ToscaFunctions.GET_PROPERTY.getDisplayName(), indexVarValList);
125 return newIndexVarValue;
128 private void replacePropertiesIndexVarValue(String indexVarValue,
129 Map<String, Object> properties) {
132 mdcDataDebugMessage.debugEntryMessage(null, null);
134 if (properties == null || properties.isEmpty()) {
138 for (Map.Entry<String, Object> propertyEntry : properties.entrySet()) {
139 Object propertyValue = propertyEntry.getValue();
140 Object newPropertyValue = getUpdatedPropertyValueWithIndex(indexVarValue, propertyValue);
141 if (newPropertyValue != null) {
142 properties.put(propertyEntry.getKey(), newPropertyValue);
146 mdcDataDebugMessage.debugExitMessage(null, null);
149 private Object getUpdatedPropertyValueWithIndex(String indexVarValue, Object propertyValue) {
152 mdcDataDebugMessage.debugEntryMessage(null, null);
154 if (propertyValue instanceof String && propertyValue != null) {
155 if (propertyValue.equals(indexVarValue)) {
156 return getNewIndexVarValue();
158 if (((String) propertyValue).contains(indexVarValue)) {
159 Map<String, List<Object>> concatMap = new HashMap<>();
160 List<Object> concatList = new ArrayList<>();
161 String value = (String) propertyValue;
163 while (value.contains(indexVarValue)) {
164 if (value.indexOf(indexVarValue) == 0) {
165 concatList.add(getNewIndexVarValue());
166 value = value.substring(indexVarValue.length());
168 int end = value.indexOf(indexVarValue);
169 concatList.add(value.substring(0, end));
170 value = value.substring(end);
173 if (!value.isEmpty()) {
174 concatList.add(value);
177 concatMap.put(ToscaFunctions.CONCAT.getDisplayName(), concatList);
181 mdcDataDebugMessage.debugExitMessage(null, null);
182 return propertyValue; //no update is needed
183 } else if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
184 replacePropertiesIndexVarValue(indexVarValue, (Map<String, Object>) propertyValue);
185 return propertyValue;
186 } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
187 List newPropertyValueList = new ArrayList<>();
188 for (Object entry : ((List) propertyValue)) {
189 newPropertyValueList.add(getUpdatedPropertyValueWithIndex(indexVarValue, entry));
192 mdcDataDebugMessage.debugExitMessage(null, null);
193 return newPropertyValueList;
196 mdcDataDebugMessage.debugExitMessage(null, null);
197 return propertyValue;
200 private String getIndexVarValue(TranslateTo translateTo) {
202 mdcDataDebugMessage.debugEntryMessage(null, null);
205 translateTo.getResource().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
206 if (indexVar == null) {
207 return HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE;
210 if (indexVar instanceof String) {
212 mdcDataDebugMessage.debugExitMessage(null, null);
213 return (String) indexVar;
215 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
216 LoggerTragetServiceName.GET_RESOURCE, ErrorLevel.ERROR.name(),
217 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY);
218 throw new CoreException(
219 new InvalidPropertyValueErrorBuilder("index_var", indexVar.toString(), "String").build());
223 private void populateServiceTemplateFilterProperties(TranslateTo translateTo,
224 NodeTemplate substitutionNodeTemplate,
225 Map serviceTemplateFilter) {
228 mdcDataDebugMessage.debugEntryMessage(null, null);
230 boolean mandatory = false;
231 Object countValue = TranslatorHeatToToscaPropertyConverter
232 .getToscaPropertyValue(translateTo.getServiceTemplate(),translateTo.getResourceId(),
233 ToscaConstants.COUNT_PROPERTY_NAME, translateTo.getResource().getProperties().get
234 (ToscaConstants.COUNT_PROPERTY_NAME), null,
235 translateTo.getHeatFileName(), translateTo.getHeatOrchestrationTemplate(),
236 substitutionNodeTemplate, translateTo.getContext());
238 if (countValue != null) {
239 serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, countValue);
241 serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, 1);
243 if (countValue instanceof Integer && (Integer) countValue > 0) {
246 if (countValue == null) {
249 serviceTemplateFilter.put("mandatory", mandatory);
251 mdcDataDebugMessage.debugExitMessage(null, null);