2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.be.components.property;
23 import static org.openecomp.sdc.common.api.Constants.GET_INPUT;
24 import static org.openecomp.sdc.common.api.Constants.GET_POLICY;
26 import com.google.gson.Gson;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Arrays;
35 import java.util.Objects;
36 import java.util.Optional;
38 import java.util.stream.Collectors;
39 import org.apache.commons.collections.CollectionUtils;
40 import org.apache.commons.collections.MapUtils;
41 import org.apache.commons.lang.StringUtils;
42 import org.json.simple.JSONObject;
43 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
44 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
45 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.PropertiesOwner;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.model.CapabilityDefinition;
51 import org.openecomp.sdc.be.model.Component;
52 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
53 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
54 import org.openecomp.sdc.be.model.InputDefinition;
55 import org.openecomp.sdc.be.model.PolicyDefinition;
56 import org.openecomp.sdc.be.model.PropertyDefinition;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
59 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
60 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
61 import org.openecomp.sdc.common.log.wrappers.Logger;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.yaml.snakeyaml.Yaml;
65 public abstract class DefaultPropertyDeclarator<PROPERTYOWNER extends PropertiesOwner, PROPERTYTYPE extends PropertyDataDefinition> implements PropertyDeclarator {
67 private static final Logger log = Logger.getLogger(DefaultPropertyDeclarator.class);
68 private static final short LOOP_PROTECTION_LEVEL = 10;
69 private static final String UNDERSCORE = "_";
70 private final Gson gson = new Gson();
71 private ComponentsUtils componentsUtils;
72 private PropertyOperation propertyOperation;
73 private static final String GET_INPUT_INDEX = "INDEX";
75 public DefaultPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation) {
76 this.componentsUtils = componentsUtils;
77 this.propertyOperation = propertyOperation;
81 public Either<List<InputDefinition>, StorageOperationStatus> declarePropertiesAsInputs(Component component, String propertiesOwnerId, List<ComponentInstancePropInput> propsToDeclare) {
82 log.debug("#declarePropertiesAsInputs - declaring properties as inputs for component {} from properties owner {}", component.getUniqueId(), propertiesOwnerId);
83 return resolvePropertiesOwner(component, propertiesOwnerId)
84 .map(propertyOwner -> declarePropertiesAsInputs(component, propertyOwner, propsToDeclare))
85 .orElse(Either.right(onPropertiesOwnerNotFound(component.getUniqueId(), propertiesOwnerId)));
88 protected abstract PROPERTYTYPE createDeclaredProperty(PropertyDataDefinition prop);
90 protected abstract Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String propertiesOwnerId, List<PROPERTYTYPE> properties);
92 protected abstract Optional<PROPERTYOWNER> resolvePropertiesOwner(Component component, String propertiesOwnerId);
94 protected abstract void addPropertiesListToInput(PROPERTYTYPE declaredProp, InputDefinition input);
97 public Either<List<PolicyDefinition>, StorageOperationStatus> declarePropertiesAsPolicies(Component component,
98 String propertiesOwnerId,
99 List<ComponentInstancePropInput> propsToDeclare) {
100 log.debug("#declarePropertiesAsPolicies - declaring properties as policies for component {} from properties owner {}", component.getUniqueId(), propertiesOwnerId);
101 return resolvePropertiesOwner(component, propertiesOwnerId)
102 .map(propertyOwner -> declarePropertiesAsPolicies(component, propertyOwner, propsToDeclare))
103 .orElse(Either.right(onPropertiesOwnerNotFound(component.getUniqueId(), propertiesOwnerId)));
107 public Either<InputDefinition, StorageOperationStatus> declarePropertiesAsListInput(Component component, String propertiesOwnerId, List<ComponentInstancePropInput> propsToDeclare, InputDefinition input) {
108 log.debug("#declarePropertiesAsListInput - declaring properties as inputs for component {} from properties owner {}", component.getUniqueId(), propertiesOwnerId);
109 Optional<PROPERTYOWNER> propertyOwner = resolvePropertiesOwner(component, propertiesOwnerId);
110 if (propertyOwner.isPresent()) {
111 return declarePropertiesAsListInput(component, propertyOwner.get(), propsToDeclare, input);
113 return Either.right(onPropertiesOwnerNotFound(component.getUniqueId(), propertiesOwnerId));
117 public StorageOperationStatus unDeclarePropertiesAsPolicies(Component component, PolicyDefinition policy) {
118 return StorageOperationStatus.OK;
121 private Either<List<PolicyDefinition>, StorageOperationStatus> declarePropertiesAsPolicies(Component component, PROPERTYOWNER propertiesOwner, List<ComponentInstancePropInput> propsToDeclare) {
122 PropertiesDeclarationData policyProperties = createPoliciesAndOverridePropertiesValues(propertiesOwner.getUniqueId(), propertiesOwner, propsToDeclare);
123 return updatePropertiesValues(component, propertiesOwner.getUniqueId(), policyProperties.getPropertiesToUpdate())
125 .map(updatePropsRes -> policyProperties.getPoliciesToCreate());
128 private StorageOperationStatus onPropertiesOwnerNotFound(String componentId, String propertiesOwnerId) {
129 log.debug("#declarePropertiesAsInputs - properties owner {} was not found on component {}", propertiesOwnerId, componentId);
130 return StorageOperationStatus.NOT_FOUND;
133 private Either<List<InputDefinition>, StorageOperationStatus> declarePropertiesAsInputs(Component component, PROPERTYOWNER propertiesOwner, List<ComponentInstancePropInput> propsToDeclare) {
134 PropertiesDeclarationData inputsProperties = createInputsAndOverridePropertiesValues(component, propertiesOwner, propsToDeclare);
135 return updatePropertiesValues(component, propertiesOwner.getUniqueId(), inputsProperties.getPropertiesToUpdate())
137 .map(updatePropsRes -> inputsProperties.getInputsToCreate());
139 private PropertiesDeclarationData createPoliciesAndOverridePropertiesValues(String componentId, PROPERTYOWNER propertiesOwner, List<ComponentInstancePropInput> propsToDeclare) {
140 List<PROPERTYTYPE> declaredProperties = new ArrayList<>();
141 List<PolicyDefinition> policies = new ArrayList<>();
142 propsToDeclare.forEach(property -> policies.add(declarePropertyPolicy(componentId, declaredProperties, property)));
143 return new PropertiesDeclarationData(null, policies, declaredProperties);
146 private PolicyDefinition declarePropertyPolicy(String componentId, List<PROPERTYTYPE> declaredProperties,
147 ComponentInstancePropInput propInput) {
148 PropertyDataDefinition prop = resolveProperty(declaredProperties, propInput);
149 propInput.setOwnerId(null);
150 propInput.setParentUniqueId(null);
152 PolicyDefinition policyDefinition = new PolicyDefinition(prop);
153 policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentId, prop.getName()));
154 policyDefinition.setInputPath(prop.getName());
155 policyDefinition.setInstanceUniqueId(componentId);
156 policyDefinition.setPropertyId(prop.getUniqueId());
158 changePropertyValueToGetPolicy(prop, policyDefinition);
159 PROPERTYTYPE declaredProperty = createDeclaredProperty(prop);
162 if(!declaredProperties.contains(declaredProperty)){
163 declaredProperties.add(declaredProperty);
166 return policyDefinition;
169 private void changePropertyValueToGetPolicy(PropertyDataDefinition prop, PolicyDefinition policyDefinition) {
170 JSONObject jsonObject = new JSONObject();
172 String origValue = Objects.isNull(prop.getValue()) ? prop.getDefaultValue() : prop.getValue();
173 jsonObject.put(GET_POLICY, null);
174 prop.setValue(jsonObject.toJSONString());
175 policyDefinition.setValue(jsonObject.toJSONString());
177 if(CollectionUtils.isEmpty(prop.getGetPolicyValues())){
178 prop.setGetPolicyValues(new ArrayList<>());
180 List<GetPolicyValueDataDefinition> getPolicyValues = prop.getGetPolicyValues();
182 GetPolicyValueDataDefinition getPolicyValueDataDefinition = new GetPolicyValueDataDefinition();
183 getPolicyValueDataDefinition.setPolicyId(policyDefinition.getUniqueId());
184 getPolicyValueDataDefinition.setPropertyName(prop.getName());
186 getPolicyValueDataDefinition.setOrigPropertyValue(origValue);
188 getPolicyValues.add(getPolicyValueDataDefinition);
190 policyDefinition.setGetPolicyValues(getPolicyValues);
195 private Either<InputDefinition, StorageOperationStatus> declarePropertiesAsListInput(Component component, PROPERTYOWNER propertiesOwner, List<ComponentInstancePropInput> propsToDeclare, InputDefinition input) {
196 List<PROPERTYTYPE> declaredProperties = new ArrayList<>();
197 for (ComponentInstancePropInput propInput : propsToDeclare) {
198 if (StringUtils.isNotEmpty(propInput.getPropertiesName()) && propInput.getInput() != null) {
199 // sub-property in complex type is checked on UI. currently not supported.
200 log.debug("skip propInput (propertiesName={}) currently not supported.", propInput.getPropertiesName());
203 PROPERTYTYPE declaredProperty = createDeclaredProperty(propInput);
205 JSONObject jsonObject = new JSONObject();
206 jsonObject.put(GET_INPUT, Arrays.asList(input.getName(), GET_INPUT_INDEX, propInput.getName()));
207 declaredProperty.setValue(jsonObject.toJSONString());
209 GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
210 getInputValueDataDefinition.setInputId(input.getUniqueId());
211 getInputValueDataDefinition.setInputName(input.getName());
212 List<GetInputValueDataDefinition> getInputValues = declaredProperty.getGetInputValues();
213 if (getInputValues == null) {
214 getInputValues = new ArrayList<>();
215 declaredProperty.setGetInputValues(getInputValues);
217 getInputValues.add(getInputValueDataDefinition);
219 if (!declaredProperties.contains(declaredProperty)) {
220 // Add property to the list if not contain in declareProperties.
221 declaredProperties.add(declaredProperty);
224 return updatePropertiesValues(component, propertiesOwner.getUniqueId(), declaredProperties)
225 .left().map(x -> input);
228 private PropertiesDeclarationData createInputsAndOverridePropertiesValues(Component component, PROPERTYOWNER propertiesOwner, List<ComponentInstancePropInput> propsToDeclare) {
229 List<PROPERTYTYPE> declaredProperties = new ArrayList<>();
230 List<InputDefinition> createdInputs = propsToDeclare.stream()
231 .map(propInput -> declarePropertyInput(component, propertiesOwner, declaredProperties, propInput))
232 .collect(Collectors.toList());
233 return new PropertiesDeclarationData(createdInputs, null, declaredProperties);
236 private InputDefinition declarePropertyInput(Component component, PROPERTYOWNER propertiesOwner, List<PROPERTYTYPE> declaredProperties, ComponentInstancePropInput propInput) {
237 PropertyDataDefinition prop = resolveProperty(declaredProperties, propInput);
238 InputDefinition inputDefinition = createInput(component, propertiesOwner, propInput, prop);
239 PROPERTYTYPE declaredProperty = createDeclaredProperty(prop);
240 if(!declaredProperties.contains(declaredProperty)){
241 declaredProperties.add(declaredProperty);
243 addPropertiesListToInput(declaredProperty, inputDefinition);
244 return inputDefinition;
247 private InputDefinition createInput(Component component, PROPERTYOWNER propertiesOwner,
248 ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
249 String generatedInputPrefix = propertiesOwner.getNormalizedName();
250 if (propertiesOwner.getUniqueId().equals(propInput.getParentUniqueId())) {
251 //Creating input from property create on self using add property..Do not add the prefix
252 generatedInputPrefix = null;
255 Optional<CapabilityDefinition> propertyCapability = PropertiesUtils.getPropertyCapabilityOfChildInstance(propInput
256 .getParentUniqueId(), component.getCapabilities());
257 if (propertyCapability.isPresent()) {
258 String capName = propertyCapability.get().getName();
259 if(capName.contains(".")) {
260 capName = capName.replaceAll("\\.", UNDERSCORE);
262 generatedInputPrefix = generatedInputPrefix == null || generatedInputPrefix.isEmpty()?
263 capName : generatedInputPrefix + UNDERSCORE + capName;
266 String generatedInputName = generateInputName(generatedInputPrefix, propInput);
267 log.debug("createInput: propOwner.uniqueId={}, propInput.parentUniqueId={}", propertiesOwner.getUniqueId(), propInput.getParentUniqueId());
268 return createInputFromProperty(component.getUniqueId(), propertiesOwner, generatedInputName, propInput, prop);
271 private String generateInputName(String inputName, ComponentInstancePropInput propInput) {
272 String declaredInputName;
273 String[] parsedPropNames = propInput.getParsedPropNames();
275 if(parsedPropNames != null){
276 declaredInputName = handleInputName(inputName, parsedPropNames);
278 String[] propName = {propInput.getName()};
279 declaredInputName = handleInputName(inputName, propName);
282 return declaredInputName;
285 private String handleInputName(String inputName, String[] parsedPropNames) {
286 StringBuilder prefix = new StringBuilder();
289 if(Objects.isNull(inputName)) {
290 prefix.append(parsedPropNames[0]);
293 prefix.append(inputName);
297 while(startingIndex < parsedPropNames.length){
298 prefix.append(UNDERSCORE);
299 prefix.append(parsedPropNames[startingIndex]);
303 return prefix.toString();
306 private PropertyDataDefinition resolveProperty(List<PROPERTYTYPE> propertiesToCreate, ComponentInstancePropInput propInput) {
307 Optional<PROPERTYTYPE> resolvedProperty = propertiesToCreate.stream()
308 .filter(p -> p.getName().equals(propInput.getName()))
310 return resolvedProperty.isPresent() ? resolvedProperty.get() : propInput;
313 InputDefinition createInputFromProperty(String componentId, PROPERTYOWNER propertiesOwner, String inputName, ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
314 String propertiesName = propInput.getPropertiesName() ;
315 PropertyDefinition selectedProp = propInput.getInput();
316 String[] parsedPropNames = propInput.getParsedPropNames();
317 InputDefinition input;
318 boolean complexProperty = false;
319 if(propertiesName != null && !propertiesName.isEmpty() && selectedProp != null){
320 complexProperty = true;
321 input = new InputDefinition(selectedProp);
322 input.setDefaultValue(selectedProp.getValue());
324 input = new InputDefinition(prop);
325 input.setDefaultValue(prop.getValue());
327 input.setName(inputName);
328 input.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(componentId, input.getName()));
329 input.setInputPath(propertiesName);
330 input.setInstanceUniqueId(propertiesOwner.getUniqueId());
331 input.setPropertyId(propInput.getUniqueId());
332 if (Objects.isNull(input.getSubPropertyInputPath())
333 || (Objects.nonNull(propertiesName)
334 && input.getSubPropertyInputPath().substring(input.getSubPropertyInputPath().lastIndexOf('#'))
335 .equals(propertiesName.substring(propertiesName.lastIndexOf('#'))))) {
336 input.setParentPropertyType(propInput.getType());
337 input.setSubPropertyInputPath(propertiesName);
340 changePropertyValueToGetInputValue(inputName, parsedPropNames, input, prop, complexProperty);
342 if(prop instanceof IComponentInstanceConnectedElement) {
343 ((IComponentInstanceConnectedElement) prop)
344 .setComponentInstanceId(propertiesOwner.getUniqueId());
345 ((IComponentInstanceConnectedElement) prop)
346 .setComponentInstanceName(propertiesOwner.getName());
351 private void changePropertyValueToGetInputValue(String inputName, String[] parsedPropNames, InputDefinition input, PropertyDataDefinition prop, boolean complexProperty) {
352 JSONObject jsonObject = new JSONObject();
353 String value = prop.getValue();
354 if(value == null || value.isEmpty()){
357 jsonObject = createJSONValueForProperty(parsedPropNames.length -1, parsedPropNames, jsonObject, inputName);
358 prop.setValue(jsonObject.toJSONString());
362 jsonObject.put(GET_INPUT, input.getName());
363 prop.setValue(jsonObject.toJSONString());
369 Object objValue = new Yaml().load(value);
370 if( objValue instanceof Map || objValue instanceof List){
371 if(!complexProperty){
372 jsonObject.put(GET_INPUT, input.getName());
373 prop.setValue(jsonObject.toJSONString());
377 Map<String, Object> mappedToscaTemplate = (Map<String, Object>) objValue;
378 createInputValue(mappedToscaTemplate, 1, parsedPropNames, inputName);
380 String json = gson.toJson(mappedToscaTemplate);
386 jsonObject.put(GET_INPUT, input.getName());
387 prop.setValue(jsonObject.toJSONString());
393 if(CollectionUtils.isEmpty(prop.getGetInputValues())){
394 prop.setGetInputValues(new ArrayList<>());
396 List<GetInputValueDataDefinition> getInputValues = prop.getGetInputValues();
398 GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
399 getInputValueDataDefinition.setInputId(input.getUniqueId());
400 getInputValueDataDefinition.setInputName(input.getName());
401 getInputValues.add(getInputValueDataDefinition);
404 private JSONObject createJSONValueForProperty (int i, String [] parsedPropNames, JSONObject ooj, String inputName){
407 if( i == parsedPropNames.length -1){
408 JSONObject jobProp = new JSONObject();
409 jobProp.put(GET_INPUT, inputName);
410 ooj.put(parsedPropNames[i], jobProp);
412 return createJSONValueForProperty (i, parsedPropNames, ooj, inputName);
414 JSONObject res = new JSONObject();
415 res.put(parsedPropNames[i], ooj);
417 res = createJSONValueForProperty (i, parsedPropNames, res, inputName);
425 private Map<String, Object> createInputValue(Map<String, Object> lhm1, int index, String[] inputNames, String inputName){
426 while(index < inputNames.length){
427 if(lhm1.containsKey(inputNames[index])){
428 Object value = lhm1.get(inputNames[index]);
429 if (value instanceof Map){
430 if(index == inputNames.length -1){
431 ((Map) value).put(GET_INPUT, inputName);
436 return createInputValue((Map)value, index, inputNames, inputName);
439 Map<String, Object> jobProp = new HashMap<>();
440 if(index == inputNames.length -1){
441 jobProp.put(GET_INPUT, inputName);
442 lhm1.put(inputNames[index], jobProp);
445 lhm1.put(inputNames[index], jobProp);
447 return createInputValue(jobProp, index, inputNames, inputName);
451 Map<String, Object> jobProp = new HashMap<>();
452 lhm1.put(inputNames[index], jobProp);
453 if(index == inputNames.length -1){
454 jobProp.put(GET_INPUT, inputName);
458 return createInputValue(jobProp, index, inputNames, inputName);
465 private class PropertiesDeclarationData {
466 private List<InputDefinition> inputsToCreate;
467 private List<PolicyDefinition> policiesToCreate;
468 private List<PROPERTYTYPE> propertiesToUpdate;
470 PropertiesDeclarationData(List<InputDefinition> inputsToCreate, List<PolicyDefinition> policiesToCreate, List<PROPERTYTYPE> propertiesToUpdate) {
471 this.inputsToCreate = inputsToCreate;
472 this.policiesToCreate = policiesToCreate;
473 this.propertiesToUpdate = propertiesToUpdate;
476 List<InputDefinition> getInputsToCreate() {
477 return inputsToCreate;
480 public List<PolicyDefinition> getPoliciesToCreate() { return policiesToCreate; }
482 List<PROPERTYTYPE> getPropertiesToUpdate() {
483 return propertiesToUpdate;
487 Either<InputDefinition, ResponseFormat> prepareValueBeforeDelete(InputDefinition inputForDelete, PropertyDataDefinition inputValue, List<String> pathOfComponentInstances) {
488 Either<InputDefinition, ResponseFormat> deleteEither = prepareValueBeforeDelete(inputForDelete, inputValue);
490 Either<String, JanusGraphOperationStatus> findDefaultValue = propertyOperation
491 .findDefaultValueFromSecondPosition(pathOfComponentInstances, inputValue.getUniqueId(),
492 (String) inputValue.getDefaultValue());
493 if (findDefaultValue.isRight()) {
494 deleteEither = Either.right(componentsUtils.getResponseFormat(componentsUtils
495 .convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(findDefaultValue.right().value()))));
499 String defaultValue = findDefaultValue.left().value();
500 inputValue.setDefaultValue(defaultValue);
501 log.debug("The returned default value in ResourceInstanceProperty is {}", defaultValue);
505 Either<InputDefinition, ResponseFormat> prepareValueBeforeDeleteOfCapProp(InputDefinition inputForDelete,
506 PropertyDataDefinition inputValue) {
507 Either<InputDefinition, ResponseFormat> deleteEither = prepareValueBeforeDelete(inputForDelete, inputValue);
508 inputValue.setDefaultValue(inputForDelete.getDefaultValue());
509 log.debug("The returned default value in ResourceInstanceProperty is {}", inputForDelete.getDefaultValue());
513 private Either<InputDefinition, ResponseFormat> prepareValueBeforeDelete(InputDefinition inputForDelete,
514 PropertyDataDefinition inputValue) {
515 Either<InputDefinition, ResponseFormat> deleteEither = Either.left(inputForDelete);
516 String value = inputValue.getValue();
517 Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(value);
519 resetInputName(mappedToscaTemplate, inputForDelete.getName());
522 if (!mappedToscaTemplate.isEmpty()) {
523 Either result = cleanNestedMap(mappedToscaTemplate, true);
524 Map modifiedMappedToscaTemplate = mappedToscaTemplate;
525 if (result.isLeft()) {
526 modifiedMappedToscaTemplate = (Map) result.left().value();
528 log.warn("Map cleanup failed -> " + result.right().value()
529 .toString()); //continue, don't break operation
531 value = gson.toJson(modifiedMappedToscaTemplate);
533 inputValue.setValue(value);
536 List<GetInputValueDataDefinition> getInputsValues = inputValue.getGetInputValues();
537 if (getInputsValues != null && !getInputsValues.isEmpty()) {
538 Optional<GetInputValueDataDefinition> op =
539 getInputsValues.stream().filter(gi -> gi.getInputId().equals(inputForDelete.getUniqueId()))
541 op.ifPresent(getInputsValues::remove);
543 inputValue.setGetInputValues(getInputsValues);
547 private void resetInputName(Map<String, Object> lhm1, String inputName){
548 for (Map.Entry<String, Object> entry : lhm1.entrySet()) {
549 String key = entry.getKey();
550 Object value = entry.getValue();
551 if (value instanceof String && ((String) value).equalsIgnoreCase(inputName) && key.equals(GET_INPUT)) {
554 } else if (value instanceof Map) {
555 Map<String, Object> subMap = (Map<String, Object>)value;
556 resetInputName(subMap, inputName);
557 } else if (value instanceof List && ((List) value).contains(inputName) && key.equals(GET_INPUT)) {
567 private Either cleanNestedMap( Map mappedToscaTemplate , boolean deepClone ){
568 if (MapUtils.isNotEmpty( mappedToscaTemplate ) ){
570 if (!(mappedToscaTemplate instanceof HashMap))
571 return Either.right("expecting mappedToscaTemplate as HashMap ,recieved "+ mappedToscaTemplate.getClass().getSimpleName() );
573 mappedToscaTemplate = (HashMap)((HashMap) mappedToscaTemplate).clone();
575 return Either.left( (Map) cleanEmptyNestedValuesInMap( mappedToscaTemplate , LOOP_PROTECTION_LEVEL ) );
578 log.debug("mappedToscaTemplate is empty ");
579 return Either.right("mappedToscaTemplate is empty ");
583 /* Mutates the object
584 * Tail recurse -> traverse the tosca elements and remove nested empty map properties
585 * this only handles nested maps, other objects are left untouched (even a Set containing a map) since behaviour is unexpected
587 * @param toscaElement - expected map of tosca values
588 * @return mutated @param toscaElement , where empty maps are deleted , return null for empty map.
590 private Object cleanEmptyNestedValuesInMap(Object toscaElement , short loopProtectionLevel ){
591 if (loopProtectionLevel<=0 || toscaElement==null || !(toscaElement instanceof Map))
593 if ( MapUtils.isNotEmpty( (Map)toscaElement ) ) {
595 Set<Object> keysToRemove = new HashSet<>(); // use different set to avoid ConcurrentModificationException
596 for( Object key : ((Map)toscaElement).keySet() ) {
597 Object value = ((Map) toscaElement).get(key);
598 ret = cleanEmptyNestedValuesInMap(value , --loopProtectionLevel );
600 keysToRemove.add(key);
602 Collection set = ((Map) toscaElement).keySet();
603 if (CollectionUtils.isNotEmpty(set))
604 set.removeAll(keysToRemove);
606 if ( isEmptyNestedMap(toscaElement) )
614 //@returns true iff map nested maps are all empty
615 //ignores other collection objects
616 private boolean isEmptyNestedMap(Object element){
617 boolean isEmpty = true;
618 if (element != null){
619 if ( element instanceof Map ){
620 if (MapUtils.isEmpty((Map)element))
624 for( Object key : ((Map)(element)).keySet() ){
625 Object value = ((Map)(element)).get(key);
626 isEmpty &= isEmptyNestedMap( value );