[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceTranslationResourceGroupImpl.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.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;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49
50 public class ResourceTranslationResourceGroupImpl extends ResourceTranslationBase {
51
52   @Override
53   protected void translate(TranslateTo translateTo) {
54
55
56     mdcDataDebugMessage.debugEntryMessage(null, null);
57
58     final String heatFileName = translateTo.getHeatFileName();
59     Object resourceDef =
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 ");
68       return;
69     }
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 ");
76
77       mdcDataDebugMessage.debugExitMessage(null, null);
78       return;
79     }
80
81     nestedResource.setType(type);
82     nestedResource.setProperties((Map<String, Object>) ((Map) resourceDef).get("properties"));
83     nestedResource.setMetadata(((Map) resourceDef).get("metadata"));
84
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);
95
96       populateServiceTemplateFilterProperties(translateTo, substitutionNodeTemplate,
97           serviceTemplateFilter);
98       handlingIndexVar(translateTo, substitutionNodeTemplate);
99       DataModelUtil
100           .addNodeTemplate(translateTo.getServiceTemplate(), substitutionNodeTemplateId.get(),
101               substitutionNodeTemplate);
102     }
103
104     mdcDataDebugMessage.debugExitMessage(null, null);
105   }
106
107   private void handlingIndexVar(TranslateTo translateTo, NodeTemplate substitutionNodeTemplate) {
108
109
110     mdcDataDebugMessage.debugEntryMessage(null, null);
111
112     String indexVarValue = getIndexVarValue(translateTo);
113     replacePropertiesIndexVarValue(indexVarValue, substitutionNodeTemplate.getProperties());
114
115     mdcDataDebugMessage.debugExitMessage(null, null);
116   }
117
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;
126   }
127
128   private void replacePropertiesIndexVarValue(String indexVarValue,
129                                               Map<String, Object> properties) {
130
131
132     mdcDataDebugMessage.debugEntryMessage(null, null);
133
134     if (properties == null || properties.isEmpty()) {
135       return;
136     }
137
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);
143       }
144     }
145
146     mdcDataDebugMessage.debugExitMessage(null, null);
147   }
148
149   private Object getUpdatedPropertyValueWithIndex(String indexVarValue, Object propertyValue) {
150
151
152     mdcDataDebugMessage.debugEntryMessage(null, null);
153
154     if (propertyValue instanceof String && propertyValue != null) {
155       if (propertyValue.equals(indexVarValue)) {
156         return getNewIndexVarValue();
157       }
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;
162
163         while (value.contains(indexVarValue)) {
164           if (value.indexOf(indexVarValue) == 0) {
165             concatList.add(getNewIndexVarValue());
166             value = value.substring(indexVarValue.length());
167           } else {
168             int end = value.indexOf(indexVarValue);
169             concatList.add(value.substring(0, end));
170             value = value.substring(end);
171           }
172         }
173         if (!value.isEmpty()) {
174           concatList.add(value);
175         }
176
177         concatMap.put(ToscaFunctions.CONCAT.getDisplayName(), concatList);
178         return concatMap;
179       }
180
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));
190       }
191
192       mdcDataDebugMessage.debugExitMessage(null, null);
193       return newPropertyValueList;
194     }
195
196     mdcDataDebugMessage.debugExitMessage(null, null);
197     return propertyValue;
198   }
199
200   private String getIndexVarValue(TranslateTo translateTo) {
201
202     mdcDataDebugMessage.debugEntryMessage(null, null);
203
204     Object indexVar =
205         translateTo.getResource().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
206     if (indexVar == null) {
207       return HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE;
208     }
209
210     if (indexVar instanceof String) {
211
212       mdcDataDebugMessage.debugExitMessage(null, null);
213       return (String) indexVar;
214     } else {
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());
220     }
221   }
222
223   private void populateServiceTemplateFilterProperties(TranslateTo translateTo,
224                                                        NodeTemplate substitutionNodeTemplate,
225                                                        Map serviceTemplateFilter) {
226
227
228     mdcDataDebugMessage.debugEntryMessage(null, null);
229
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());
237
238     if (countValue != null) {
239       serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, countValue);
240     } else {
241       serviceTemplateFilter.put(ToscaConstants.COUNT_PROPERTY_NAME, 1);
242     }
243     if (countValue instanceof Integer && (Integer) countValue > 0) {
244       mandatory = true;
245     }
246     if (countValue == null) {
247       mandatory = true;
248     }
249     serviceTemplateFilter.put("mandatory", mandatory);
250
251     mdcDataDebugMessage.debugExitMessage(null, null);
252   }
253 }