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