re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / InputsBusinessLogic.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.be.components.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
25 import org.openecomp.sdc.be.components.validation.ComponentValidations;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
28 import org.openecomp.sdc.be.dao.utils.MapUtil;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
33 import org.openecomp.sdc.be.model.*;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
36 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
37 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.openecomp.sdc.exception.ResponseFormat;
40 import org.springframework.stereotype.Component;
41
42 import javax.inject.Inject;
43 import java.util.*;
44 import java.util.stream.Collectors;
45
46 @Component("inputsBusinessLogic")
47 public class InputsBusinessLogic extends BaseBusinessLogic {
48
49     private static final String CREATE_INPUT = "CreateInput";
50     private static final String UPDATE_INPUT = "UpdateInput";
51
52     private static final Logger log = Logger.getLogger(InputsBusinessLogic.class);
53     private static final String FAILED_TO_FOUND_COMPONENT_ERROR = "Failed to found component {}, error: {}";
54     private static final String GET_PROPERTIES_BY_INPUT = "get Properties by input";
55     private static final String FAILED_TO_FOUND_INPUT_UNDER_COMPONENT_ERROR = "Failed to found input {} under component {}, error: {}";
56     private static final String GOING_TO_EXECUTE_ROLLBACK_ON_CREATE_GROUP = "Going to execute rollback on create group.";
57     private static final String GOING_TO_EXECUTE_COMMIT_ON_CREATE_GROUP = "Going to execute commit on create group.";
58
59     @Inject
60     private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
61     @Inject
62     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
63
64     /**
65      * associate inputs to a given component with paging
66      *
67      * @param userId
68      * @param componentId
69      * @return
70      */
71     public Either<List<InputDefinition>, ResponseFormat> getInputs(String userId, String componentId) {
72
73         validateUserExists(userId, "get Inputs", false);
74
75         ComponentParametersView filters = new ComponentParametersView();
76         filters.disableAll();
77         filters.setIgnoreInputs(false);
78
79         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(componentId, filters);
80         if(getComponentEither.isRight()){
81             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
82             log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, componentId, actionStatus);
83             return Either.right(componentsUtils.getResponseFormat(actionStatus));
84
85         }
86         org.openecomp.sdc.be.model.Component component = getComponentEither.left().value();
87         List<InputDefinition> inputs = component.getInputs();
88
89         return Either.left(inputs);
90
91     }
92
93     public Either<List<ComponentInstanceInput>, ResponseFormat> getComponentInstanceInputs(String userId, String componentId, String componentInstanceId) {
94
95         validateUserExists(userId, "get Inputs", false);
96         ComponentParametersView filters = new ComponentParametersView();
97         filters.disableAll();
98         filters.setIgnoreInputs(false);
99         filters.setIgnoreComponentInstances(false);
100         filters.setIgnoreComponentInstancesInputs(false);
101
102         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(componentId, filters);
103         if(getComponentEither.isRight()){
104             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
105             log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, componentId, actionStatus);
106             return Either.right(componentsUtils.getResponseFormat(actionStatus));
107
108         }
109         org.openecomp.sdc.be.model.Component component = getComponentEither.left().value();
110
111         if(!ComponentValidations.validateComponentInstanceExist(component, componentInstanceId)){
112             ActionStatus actionStatus = ActionStatus.COMPONENT_INSTANCE_NOT_FOUND;
113             log.debug("Failed to found component instance inputs {}, error: {}", componentInstanceId, actionStatus);
114             return Either.right(componentsUtils.getResponseFormat(actionStatus));
115         }
116         Map<String, List<ComponentInstanceInput>> ciInputs = Optional.ofNullable(component.getComponentInstancesInputs()).orElse(Collections.emptyMap());
117         return Either.left(ciInputs.getOrDefault(componentInstanceId, Collections.emptyList()));
118     }
119
120     /**
121      * associate properties to a given component instance input
122      *
123      * @param instanceId
124      * @param userId
125      * @param inputId
126      * @return
127      */
128
129     public Either<List<ComponentInstanceProperty>, ResponseFormat> getComponentInstancePropertiesByInputId(String userId, String componentId, String instanceId, String inputId) {
130         validateUserExists(userId, GET_PROPERTIES_BY_INPUT, false);
131         String parentId = componentId;
132         org.openecomp.sdc.be.model.Component component = null;
133         ComponentParametersView filters = new ComponentParametersView();
134         filters.disableAll();
135         filters.setIgnoreComponentInstances(false);
136
137         if(!instanceId.equals(inputId)){
138
139
140             Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(parentId, filters);
141
142             if(getComponentEither.isRight()){
143                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
144                 log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, parentId, actionStatus);
145                 return Either.right(componentsUtils.getResponseFormat(actionStatus));
146
147             }
148             component = getComponentEither.left().value();
149             Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci ->ci.getUniqueId().equals(instanceId)).findAny();
150             if(ciOp.isPresent()){
151                 parentId = ciOp.get().getComponentUid();
152             }
153
154         }
155
156         filters.setIgnoreInputs(false);
157
158         filters.setIgnoreComponentInstancesProperties(false);
159         filters.setIgnoreComponentInstancesInputs(false);
160         filters.setIgnoreProperties(false);
161
162         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(parentId, filters);
163
164         if(getComponentEither.isRight()){
165             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
166             log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, parentId, actionStatus);
167             return Either.right(componentsUtils.getResponseFormat(actionStatus));
168
169         }
170         component = getComponentEither.left().value();
171
172         Optional<InputDefinition> op = component.getInputs().stream().filter(in -> in.getUniqueId().equals(inputId)).findFirst();
173         if(!op.isPresent()){
174             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
175             log.debug(FAILED_TO_FOUND_INPUT_UNDER_COMPONENT_ERROR, inputId, parentId, actionStatus);
176             return Either.right(componentsUtils.getResponseFormat(actionStatus));
177         }
178
179         return Either.left(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId));
180
181     }
182
183     private Either<String,ResponseFormat> updateInputObjectValue(InputDefinition currentInput, InputDefinition newInput, Map<String, DataTypeDefinition> dataTypes) {
184         String innerType = null;
185         String propertyType = currentInput.getType();
186         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
187         log.debug("The type of the property {} is {}", currentInput.getUniqueId(), propertyType);
188
189         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
190             SchemaDefinition def = currentInput.getSchema();
191             if (def == null) {
192                 log.debug("Schema doesn't exists for property of type {}", type);
193                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
194             }
195             PropertyDataDefinition propDef = def.getProperty();
196             if (propDef == null) {
197                 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
198                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
199             }
200             innerType = propDef.getType();
201         }
202         // Specific Update Logic
203
204         Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, newInput.getDefaultValue(), true, innerType, dataTypes);
205
206         String newValue = currentInput.getDefaultValue();
207         if (isValid.isRight()) {
208             Boolean res = isValid.right().value();
209             if (Boolean.FALSE.equals(res)) {
210                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
211             }
212         } else {
213             Object object = isValid.left().value();
214             if (object != null) {
215                 newValue = object.toString();
216             }
217         }
218         return Either.left(newValue);
219     }
220
221     private InputDefinition getInputFromInputsListById(List<InputDefinition> componentsOldInputs, InputDefinition input) {
222         return componentsOldInputs.stream().filter(in -> in.getUniqueId().equals(input.getUniqueId())).findFirst().orElse(null);
223     }
224
225     public Either<List<InputDefinition>, ResponseFormat> updateInputsValue(ComponentTypeEnum componentType, String componentId, List<InputDefinition> inputs, String userId, boolean shouldLockComp, boolean inTransaction) {
226
227         List<InputDefinition> returnInputs = new ArrayList<>();
228         Either<List<InputDefinition>, ResponseFormat> result = null;
229         org.openecomp.sdc.be.model.Component component = null;
230
231         try {
232             validateUserExists(userId, "get input", false);
233
234             ComponentParametersView componentParametersView = new ComponentParametersView();
235             componentParametersView.disableAll();
236             componentParametersView.setIgnoreInputs(false);
237             componentParametersView.setIgnoreUsers(false);
238
239             Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(componentId, componentType, componentParametersView);
240
241             if (validateComponent.isRight()) {
242                 result = Either.right(validateComponent.right().value());
243                 return result;
244             }
245             component = validateComponent.left().value();
246
247             if (shouldLockComp) {
248                 Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, UPDATE_INPUT);
249                 if (lockComponent.isRight()) {
250                     result = Either.right(lockComponent.right().value());
251                     return result;
252                 }
253             }
254
255             Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, userId);
256             if (canWork.isRight()) {
257                 result = Either.right(canWork.right().value());
258                 return result;
259             }
260
261             Either<Map<String, DataTypeDefinition>, ResponseFormat> allDataTypes = getAllDataTypes(applicationDataTypeCache);
262             if (allDataTypes.isRight()) {
263                 result = Either.right(allDataTypes.right().value());
264                 return result;
265             }
266
267             Map<String, DataTypeDefinition> dataTypes = allDataTypes.left().value();
268             List<InputDefinition> componentsOldInputs = Optional.ofNullable(component.getInputs()).orElse(Collections.emptyList());
269             for (InputDefinition newInput: inputs) {
270                 InputDefinition currInput = getInputFromInputsListById(componentsOldInputs, newInput);
271                 if (currInput == null) {
272                     ActionStatus actionStatus = ActionStatus.COMPONENT_NOT_FOUND;
273                     log.debug("Failed to found newInput {} under component {}, error: {}", newInput.getUniqueId(), componentId, actionStatus);
274                     result = Either.right(componentsUtils.getResponseFormat(actionStatus));
275                     return result;
276                 }
277                 Either<String, ResponseFormat> updateInputObjectValue = updateInputObjectValue(currInput, newInput, dataTypes);
278                 if ( updateInputObjectValue.isRight()) {
279                     return Either.right(updateInputObjectValue.right().value());
280                 }
281                 String newValue = updateInputObjectValue.left().value();
282                 currInput.setDefaultValue(newValue);
283                 currInput.setOwnerId(userId);
284                 Either<InputDefinition, StorageOperationStatus> status = toscaOperationFacade.updateInputOfComponent(component, currInput);
285                 if(status.isRight()){
286                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status.right().value());
287                     result = Either.right(componentsUtils.getResponseFormat(actionStatus, ""));
288                     return result;
289                 } else {
290                     returnInputs.add(status.left().value());
291                 }
292             }
293             result = Either.left(returnInputs);
294             return result;
295         } finally {
296                 if (!inTransaction) {
297                     if (result == null || result.isRight()) {
298                         log.debug(GOING_TO_EXECUTE_ROLLBACK_ON_CREATE_GROUP);
299                         titanDao.rollback();
300                     } else {
301                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_CREATE_GROUP);
302                         titanDao.commit();
303                     }
304                 }
305                 // unlock resource
306                 if (shouldLockComp && component != null) {
307                     graphLockOperation.unlockComponent(componentId, componentType.getNodeType());
308                 }
309             }
310     }
311
312     public Either<List<ComponentInstanceInput>, ResponseFormat> getInputsForComponentInput(String userId, String componentId, String inputId) {
313         validateUserExists(userId, GET_PROPERTIES_BY_INPUT, false);
314         org.openecomp.sdc.be.model.Component component = null;
315         ComponentParametersView filters = new ComponentParametersView();
316         filters.disableAll();
317         filters.setIgnoreComponentInstances(false);
318         filters.setIgnoreInputs(false);
319         filters.setIgnoreComponentInstancesInputs(false);
320         filters.setIgnoreProperties(false);
321
322         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(componentId, filters);
323
324         if(getComponentEither.isRight()){
325             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
326             log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, componentId, actionStatus);
327             return Either.right(componentsUtils.getResponseFormat(actionStatus));
328
329         }
330         component = getComponentEither.left().value();
331
332         Optional<InputDefinition> op = component.getInputs().stream().filter(in -> in.getUniqueId().equals(inputId)).findFirst();
333         if(!op.isPresent()){
334             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
335             log.debug(FAILED_TO_FOUND_INPUT_UNDER_COMPONENT_ERROR, inputId, componentId, actionStatus);
336             return Either.right(componentsUtils.getResponseFormat(actionStatus));
337         }
338
339         return Either.left(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId));
340
341     }
342
343     public Either<List<InputDefinition>, ResponseFormat> createMultipleInputs(String userId, String componentId, ComponentTypeEnum componentType, ComponentInstInputsMap componentInstInputsMapUi, boolean shouldLockComp, boolean inTransaction) {
344
345         Either<List<InputDefinition>, ResponseFormat> result = null;
346         org.openecomp.sdc.be.model.Component component = null;
347
348         try {
349             validateUserExists(userId, GET_PROPERTIES_BY_INPUT, false);
350
351             ComponentParametersView componentParametersView = new ComponentParametersView();
352             componentParametersView.disableAll();
353             componentParametersView.setIgnoreInputs(false);
354             componentParametersView.setIgnoreComponentInstancesInputs(false);
355             componentParametersView.setIgnoreComponentInstances(false);
356             componentParametersView.setIgnoreComponentInstancesProperties(false);
357             componentParametersView.setIgnorePolicies(false);
358             componentParametersView.setIgnoreGroups(false);
359             componentParametersView.setIgnoreUsers(false);
360
361             Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(componentId, componentType, componentParametersView);
362
363             if (validateComponent.isRight()) {
364                 result = Either.right(validateComponent.right().value());
365                 return result;
366             }
367             component = validateComponent.left().value();
368
369             if (shouldLockComp) {
370                 Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, CREATE_INPUT);
371                 if (lockComponent.isRight()) {
372                     result = Either.right(lockComponent.right().value());
373                     return result;
374                 }
375             }
376
377             Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, userId);
378             if (canWork.isRight()) {
379                 result = Either.right(canWork.right().value());
380                 return result;
381             }
382
383             result =  propertyDeclarationOrchestrator.declarePropertiesToInputs(component, componentInstInputsMapUi)
384                     .left()
385                     .bind(inputsToCreate -> prepareInputsForCreation(userId, componentId, inputsToCreate))
386                     .right()
387                     .map(err -> componentsUtils.getResponseFormat(err));
388
389             return result;
390
391         } finally {
392
393             if (!inTransaction) {
394                 if (result == null || result.isRight()) {
395                     log.debug(GOING_TO_EXECUTE_ROLLBACK_ON_CREATE_GROUP);
396                     titanDao.rollback();
397                 } else {
398                     log.debug(GOING_TO_EXECUTE_COMMIT_ON_CREATE_GROUP);
399                     titanDao.commit();
400                 }
401             }
402             // unlock resource
403             if (shouldLockComp && component != null) {
404                 graphLockOperation.unlockComponent(componentId, componentType.getNodeType());
405             }
406
407         }
408     }
409
410     private  Either<List<InputDefinition>, StorageOperationStatus> prepareInputsForCreation(String userId, String cmptId, List<InputDefinition> inputsToCreate) {
411         Map<String, InputDefinition> inputsToPersist = MapUtil.toMap(inputsToCreate, InputDefinition::getName);
412         assignOwnerIdToInputs(userId, inputsToPersist);
413         return toscaOperationFacade.addInputsToComponent(inputsToPersist, cmptId)
414                 .left()
415                 .map(persistedInputs -> inputsToCreate);
416     }
417
418     private void assignOwnerIdToInputs(String userId, Map<String, InputDefinition> inputsToCreate) {
419         inputsToCreate.values().forEach(inputDefinition -> inputDefinition.setOwnerId(userId));
420     }
421
422     public Either<List<InputDefinition>, ResponseFormat> createInputsInGraph(Map<String, InputDefinition> inputs, org.openecomp.sdc.be.model.Component component) {
423
424         List<InputDefinition> resourceProperties = component.getInputs();
425         Either<Map<String, DataTypeDefinition>, ResponseFormat> allDataTypes = getAllDataTypes(applicationDataTypeCache);
426         if (allDataTypes.isRight()) {
427             return Either.right(allDataTypes.right().value());
428         }
429
430         Map<String, DataTypeDefinition> dataTypes = allDataTypes.left().value();
431
432         for (Map.Entry<String, InputDefinition> inputDefinition : inputs.entrySet()) {
433             String inputName = inputDefinition.getKey();
434             inputDefinition.getValue().setName(inputName);
435
436             Either<InputDefinition, ResponseFormat> preparedInputEither = prepareAndValidateInputBeforeCreate(inputDefinition.getValue(), dataTypes);
437             if(preparedInputEither.isRight()){
438                 return Either.right(preparedInputEither.right().value());
439             }
440
441         }
442         if (resourceProperties != null) {
443             Map<String, InputDefinition> generatedInputs = resourceProperties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, i -> i));
444             Either<Map<String, InputDefinition>, String> mergeEither = ToscaDataDefinition.mergeDataMaps(generatedInputs, inputs);
445             if(mergeEither.isRight()){
446                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_ALREADY_EXIST, mergeEither.right().value()));
447             }
448             inputs = mergeEither.left().value();
449         }
450
451         Either<List<InputDefinition>, StorageOperationStatus> associateInputsEither = toscaOperationFacade.createAndAssociateInputs(inputs, component.getUniqueId());
452         if(associateInputsEither.isRight()){
453             log.debug("Failed to create inputs under component {}. Status is {}", component.getUniqueId(), associateInputsEither.right().value());
454             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(associateInputsEither.right().value())));
455         }
456         return Either.left(associateInputsEither.left().value());
457     }
458
459     /**
460      * Delete input from service
461      *
462      * @param componentId
463      * @param userId
464      * @param inputId
465      * @return
466      */
467     public Either<InputDefinition, ResponseFormat> deleteInput(String componentId, String userId, String inputId) {
468
469         Either<InputDefinition, ResponseFormat> deleteEither = null;
470         if (log.isDebugEnabled()) {
471             log.debug("Going to delete input id: {}", inputId);
472         }
473
474         validateUserExists(userId, "Delete input", true);
475
476         ComponentParametersView componentParametersView = new ComponentParametersView();
477         componentParametersView.disableAll();
478         componentParametersView.setIgnoreInputs(false);
479         componentParametersView.setIgnoreComponentInstances(false);
480         componentParametersView.setIgnoreComponentInstancesInputs(false);
481         componentParametersView.setIgnoreComponentInstancesProperties(false);
482         componentParametersView.setIgnorePolicies(false);
483         componentParametersView.setIgnoreGroups(false);
484         componentParametersView.setIgnoreUsers(false);
485
486         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> componentEither = toscaOperationFacade.getToscaElement(componentId, componentParametersView);
487         if (componentEither.isRight()) {
488             deleteEither = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentEither.right().value())));
489             return deleteEither;
490         }
491         org.openecomp.sdc.be.model.Component component = componentEither.left().value();
492
493         // Validate inputId is child of the component
494         Optional<InputDefinition> optionalInput = component.getInputs().stream().
495                 // filter by ID
496                         filter(input -> input.getUniqueId().equals(inputId)).
497                 // Get the input
498                         findAny();
499         if (!optionalInput.isPresent()) {
500             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INPUT_IS_NOT_CHILD_OF_COMPONENT, inputId, componentId));
501         }
502
503         InputDefinition inputForDelete = optionalInput.get();
504
505         // Lock component
506         Either<Boolean, ResponseFormat> lockResultEither = lockComponent(componentId, component, "deleteInput");
507         if (lockResultEither.isRight()) {
508             ResponseFormat responseFormat = lockResultEither.right().value();
509             deleteEither = Either.right(responseFormat);
510             return deleteEither;
511         }
512
513         // Delete input operations
514         try {
515             StorageOperationStatus status = toscaOperationFacade.deleteInputOfResource(component, inputForDelete.getName());
516             if (status != StorageOperationStatus.OK) {
517                 log.debug("Component id: {} delete input id: {} failed", componentId, inputId);
518                 deleteEither = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status), component.getName()));
519                 return deleteEither;
520             }
521             StorageOperationStatus storageOperationStatus = propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(component, inputForDelete);
522             if (storageOperationStatus != StorageOperationStatus.OK) {
523                 log.debug("Component id: {} update properties declared as input for input id: {} failed", componentId, inputId);
524                 deleteEither = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status), component.getName()));
525                 return deleteEither;
526             }
527             deleteEither = Either.left(inputForDelete);
528             return deleteEither;
529         } finally {
530             if (deleteEither == null || deleteEither.isRight()) {
531                 log.debug("Component id: {} delete input id: {} failed", componentId, inputId);
532                 titanDao.rollback();
533             } else {
534                 log.debug("Component id: {} delete input id: {} success", componentId, inputId);
535                 titanDao.commit();
536             }
537             unlockComponent(deleteEither, component);
538         }
539     }
540
541     private Either<InputDefinition, ResponseFormat> prepareAndValidateInputBeforeCreate(InputDefinition newInputDefinition, Map<String, DataTypeDefinition> dataTypes) {
542
543
544         // validate input default values
545         Either<Boolean, ResponseFormat> defaultValuesValidation = validatePropertyDefaultValue(newInputDefinition, dataTypes);
546         if (defaultValuesValidation.isRight()) {
547             return Either.right(defaultValuesValidation.right().value());
548         }
549         // convert property
550         ToscaPropertyType type = getType(newInputDefinition.getType());
551         if (type != null) {
552             PropertyValueConverter converter = type.getConverter();
553             // get inner type
554             String innerType = null;
555             SchemaDefinition schema = newInputDefinition.getSchema();
556             if (schema != null) {
557                 PropertyDataDefinition prop = schema.getProperty();
558                 if (prop != null) {
559                     innerType = prop.getType();
560                 }
561             }
562             String convertedValue;
563             if (newInputDefinition.getDefaultValue() != null) {
564                 convertedValue = converter.convert(newInputDefinition.getDefaultValue(), innerType, dataTypes);
565                 newInputDefinition.setDefaultValue(convertedValue);
566             }
567         }
568         return Either.left(newInputDefinition);
569     }
570
571     public Either<InputDefinition, ResponseFormat> getInputsAndPropertiesForComponentInput(String userId, String componentId, String inputId, boolean inTransaction) {
572         Either<InputDefinition, ResponseFormat> result = null;
573         try {
574
575             validateUserExists(userId, GET_PROPERTIES_BY_INPUT, false);
576             ComponentParametersView filters = new ComponentParametersView();
577             filters.disableAll();
578             filters.setIgnoreComponentInstances(false);
579             filters.setIgnoreInputs(false);
580             filters.setIgnoreComponentInstancesInputs(false);
581             filters.setIgnoreComponentInstancesProperties(false);
582             filters.setIgnoreProperties(false);
583             Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(componentId, filters);
584             if(getComponentEither.isRight()){
585                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
586                 log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, componentId, actionStatus);
587                 return Either.right(componentsUtils.getResponseFormat(actionStatus));
588
589             }
590             org.openecomp.sdc.be.model.Component component = getComponentEither.left().value();
591             Optional<InputDefinition> op = component.getInputs().stream().filter(in -> in.getUniqueId().equals(inputId)).findFirst();
592             if(!op.isPresent()){
593                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
594                 log.debug(FAILED_TO_FOUND_INPUT_UNDER_COMPONENT_ERROR, inputId, componentId, actionStatus);
595                 return Either.right(componentsUtils.getResponseFormat(actionStatus));
596             }
597
598             InputDefinition resObj = op.get();
599
600             List<ComponentInstanceInput> inputCIInput = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId) ;
601
602             resObj.setInputs(inputCIInput);
603
604
605             List<ComponentInstanceProperty> inputProps = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId) ;
606
607             resObj.setProperties(inputProps);
608
609
610             result = Either.left(resObj);
611
612             return result;
613
614         } finally {
615
616             if (!inTransaction) {
617
618                 if (result == null || result.isRight()) {
619                     log.debug(GOING_TO_EXECUTE_ROLLBACK_ON_CREATE_GROUP);
620                     titanDao.rollback();
621                 } else {
622                     log.debug(GOING_TO_EXECUTE_COMMIT_ON_CREATE_GROUP);
623                     titanDao.commit();
624                 }
625
626             }
627
628         }
629
630     }
631
632
633 }