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.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
26 import org.openecomp.sdc.heat.datatypes.model.Resource;
27 import org.openecomp.sdc.heat.services.HeatConstants;
28 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
34 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
35 import org.openecomp.sdc.tosca.services.DataModelUtil;
36 import org.openecomp.sdc.tosca.services.ToscaConstants;
37 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
40 import org.openecomp.sdc.translator.services.heattotosca.errors.InvalidPropertyValueErrorBuilder;
41 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.Objects;
48 import java.util.Optional;
50 public class ResourceTranslationResourceGroupImpl extends ResourceTranslationBase {
53 protected void translate(TranslateTo translateTo) {
54 final String heatFileName = translateTo.getHeatFileName();
56 translateTo.getResource().getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
57 Resource nestedResource = new Resource();
58 Object typeDefinition = ((Map) resourceDef).get("type");
59 if (!(typeDefinition instanceof String)) {
60 logger.warn("Resource '" + translateTo.getResourceId() + "' of type'"
61 + HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()
62 + "' with resourceDef which is not pointing to nested heat file is not supported and "
63 + "will be ignored in the translation ");
66 String type = (String) typeDefinition;
67 if (!HeatToToscaUtil.isYmlFileType(type)) {
68 logger.warn("Resource '" + translateTo.getResourceId() + "' of type'"
69 + HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()
70 + "' with resourceDef which is not pointing to nested heat file is not supported and "
71 + "will be ignored in the translation ");
75 nestedResource.setType(type);
76 nestedResource.setProperties((Map<String, Object>) ((Map) resourceDef).get("properties"));
77 nestedResource.setMetadata(((Map) resourceDef).get("metadata"));
79 Optional<String> substitutionNodeTemplateId =
80 ResourceTranslationFactory.getInstance(nestedResource)
81 .translateResource(heatFileName, translateTo.getServiceTemplate(),
82 translateTo.getHeatOrchestrationTemplate(), nestedResource,
83 translateTo.getResourceId(), translateTo.getContext());
84 if (substitutionNodeTemplateId.isPresent()) {
85 NodeTemplate substitutionNodeTemplate =
86 DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(), substitutionNodeTemplateId.get());
87 if(!Objects.isNull(substitutionNodeTemplate)) {
88 Map serviceTemplateFilter = (Map<String, Object>) substitutionNodeTemplate.getProperties()
89 .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
91 populateServiceTemplateFilterProperties(translateTo, substitutionNodeTemplate,
92 serviceTemplateFilter);
93 handlingIndexVar(translateTo, substitutionNodeTemplate);
95 .addNodeTemplate(translateTo.getServiceTemplate(), substitutionNodeTemplateId.get(),
96 substitutionNodeTemplate);
101 private void handlingIndexVar(TranslateTo translateTo, NodeTemplate substitutionNodeTemplate) {
102 String indexVarValue = getIndexVarValue(translateTo);
103 replacePropertiesIndexVarValue(indexVarValue, substitutionNodeTemplate.getProperties());
106 private Map<String, List> getNewIndexVarValue() {
107 final Map<String, List> newIndexVarValue = new HashMap<>();
108 List indexVarValList = new ArrayList<>();
109 indexVarValList.add(ToscaConstants.MODELABLE_ENTITY_NAME_SELF);
110 indexVarValList.add(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
111 indexVarValList.add(ToscaConstants.INDEX_VALUE_PROPERTY_NAME);
112 newIndexVarValue.put(ToscaFunctions.GET_PROPERTY.getDisplayName(), indexVarValList);
113 return newIndexVarValue;
116 private void replacePropertiesIndexVarValue(String indexVarValue,
117 Map<String, Object> properties) {
118 if (properties == null || properties.isEmpty()) {
122 for (Map.Entry<String, Object> propertyEntry : properties.entrySet()) {
123 Object propertyValue = propertyEntry.getValue();
124 Object newPropertyValue = getUpdatedPropertyValueWithIndex(indexVarValue, propertyValue);
125 if (newPropertyValue != null) {
126 properties.put(propertyEntry.getKey(), newPropertyValue);
131 private Object getUpdatedPropertyValueWithIndex(String indexVarValue, Object propertyValue) {
132 if (propertyValue != null && propertyValue instanceof String) {
133 if (propertyValue.equals(indexVarValue)) {
134 return getNewIndexVarValue();
136 if (((String) propertyValue).contains(indexVarValue)) {
137 Map<String, List<Object>> concatMap = new HashMap<>();
138 List<Object> concatList = new ArrayList<>();
139 String value = (String) propertyValue;
141 while (value.contains(indexVarValue)) {
142 if (value.indexOf(indexVarValue) == 0) {
143 concatList.add(getNewIndexVarValue());
144 value = value.substring(indexVarValue.length());
146 int end = value.indexOf(indexVarValue);
147 concatList.add(value.substring(0, end));
148 value = value.substring(end);
151 if (!value.isEmpty()) {
152 concatList.add(value);
155 concatMap.put(ToscaFunctions.CONCAT.getDisplayName(), concatList);
158 return propertyValue; //no update is needed
159 } else if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
160 replacePropertiesIndexVarValue(indexVarValue, (Map<String, Object>) propertyValue);
161 return propertyValue;
162 } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
163 List newPropertyValueList = new ArrayList<>();
164 for (Object entry : ((List) propertyValue)) {
165 newPropertyValueList.add(getUpdatedPropertyValueWithIndex(indexVarValue, entry));
167 return newPropertyValueList;
169 return propertyValue;
172 private String getIndexVarValue(TranslateTo translateTo) {
174 translateTo.getResource().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
175 if (indexVar == null) {
176 return HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE;
179 if (indexVar instanceof String) {
180 return (String) indexVar;
182 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
183 LoggerTragetServiceName.GET_RESOURCE, ErrorLevel.ERROR.name(),
184 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY);
185 throw new CoreException(
186 new InvalidPropertyValueErrorBuilder("index_var", indexVar.toString(), "String").build());
190 private void populateServiceTemplateFilterProperties(TranslateTo translateTo,
191 NodeTemplate substitutionNodeTemplate,
192 Map serviceTemplateFilter) {
193 boolean mandatory = false;
194 Object countValue = TranslatorHeatToToscaPropertyConverter
195 .getToscaPropertyValue(translateTo.getServiceTemplate(),translateTo.getResourceId(),
196 ToscaConstants.COUNT_PROPERTY_NAME, translateTo.getResource().getProperties().get
197 (ToscaConstants.COUNT_PROPERTY_NAME), null,
198 translateTo.getHeatFileName(), translateTo.getHeatOrchestrationTemplate(),
199 substitutionNodeTemplate, translateTo.getContext());
201 if (countValue != null) {
202 serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, countValue);
204 serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, 1);
206 if (countValue instanceof Integer && (Integer) countValue > 0) {
209 if (countValue == null) {
212 serviceTemplateFilter.put("mandatory", mandatory);