Workflow artifact in distribution notification
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / InterfaceOperationBusinessLogic.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 package org.openecomp.sdc.be.components.impl;
19
20 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.createMappedInputPropertyDefaultValue;
21 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.createMappedOutputDefaultValue;
22 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceId;
23 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceType;
24 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOperationFromInterfaceDefinition;
25 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentInput;
26 import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF;
27
28 import com.google.gson.Gson;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Objects;
37 import java.util.Optional;
38 import java.util.UUID;
39 import java.util.stream.Collectors;
40
41 import fj.data.Either;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.apache.commons.collections4.MapUtils;
44 import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils;
45 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
48 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
49 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
52 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
53 import org.openecomp.sdc.be.model.ArtifactDefinition;
54 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
55 import org.openecomp.sdc.be.model.InputDefinition;
56 import org.openecomp.sdc.be.model.InterfaceDefinition;
57 import org.openecomp.sdc.be.model.Operation;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
60 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
61 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.stereotype.Component;
67
68 @Component("interfaceOperationBusinessLogic")
69 public class InterfaceOperationBusinessLogic extends BaseBusinessLogic {
70
71     private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationBusinessLogic.class);
72     private static final String EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION =
73             "Exception occurred during {}. Response is {}";
74     private static final String DELETE_INTERFACE_OPERATION = "deleteInterfaceOperation";
75     private static final String GET_INTERFACE_OPERATION = "getInterfaceOperation";
76     private static final String CREATE_INTERFACE_OPERATION = "createInterfaceOperation";
77     private static final String UPDATE_INTERFACE_OPERATION = "updateInterfaceOperation";
78
79     @Autowired
80     private ArtifactCassandraDao artifactCassandraDao;
81
82     @Autowired
83     private InterfaceOperationValidation interfaceOperationValidation;
84
85     public Either<List<InterfaceDefinition>, ResponseFormat> deleteInterfaceOperation(String componentId,
86             String interfaceId, List<String> operationsToDelete, User user, boolean lock) {
87         validateUserExists(user.getUserId(), DELETE_INTERFACE_OPERATION, true);
88
89         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
90         if (componentEither.isRight()) {
91             return Either.right(componentEither.right().value());
92         }
93         org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
94
95         Either<Boolean, ResponseFormat> lockResult =
96                 lockComponentResult(lock, storedComponent, DELETE_INTERFACE_OPERATION);
97         if (lockResult.isRight()) {
98             return Either.right(lockResult.right().value());
99         }
100
101         try {
102             Optional<InterfaceDefinition> optionalInterface = getInterfaceDefinitionFromComponentByInterfaceId(
103                     storedComponent, interfaceId);
104             if (!optionalInterface.isPresent()) {
105                 return Either.right(
106                         componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceId));
107             }
108             InterfaceDefinition interfaceDefinition = optionalInterface.get();
109
110             Map<String, Operation> operationsCollection = new HashMap<>();
111             for (String operationId : operationsToDelete) {
112                 Optional<Map.Entry<String, Operation>> optionalOperation =
113                         getOperationFromInterfaceDefinition(interfaceDefinition, operationId);
114                 if (!optionalOperation.isPresent()) {
115                     return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND,
116                             storedComponent.getUniqueId()));
117                 }
118
119                 Operation storedOperation = optionalOperation.get().getValue();
120                 Either<Boolean, ResponseFormat> validateDeleteOperationContainsNoMappedOutputResponse =
121                         interfaceOperationValidation.validateDeleteOperationContainsNoMappedOutput(storedOperation,
122                                 storedComponent, interfaceDefinition);
123                 if (validateDeleteOperationContainsNoMappedOutputResponse.isRight()) {
124                     return Either.right(validateDeleteOperationContainsNoMappedOutputResponse.right().value());
125                 }
126
127                 String artifactUniqueId = storedOperation.getImplementation().getUniqueId();
128                 if(!InterfaceOperationUtils.isArtifactInUse(storedComponent, operationId, artifactUniqueId)){
129                     Either<ArtifactDefinition, StorageOperationStatus> getArtifactEither =
130                             artifactToscaOperation.getArtifactById(storedComponent.getUniqueId(), artifactUniqueId);
131                     if(getArtifactEither.isLeft()){
132                         Either<ArtifactDefinition, StorageOperationStatus> removeArifactFromComponent =
133                                 artifactToscaOperation.removeArifactFromResource(componentId, artifactUniqueId,
134                                         NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()),
135                                         true);
136                         if(removeArifactFromComponent.isRight()){
137                             titanDao.rollback();
138                             ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId(
139                                     componentsUtils.convertFromStorageResponse(removeArifactFromComponent.right().value()),
140                                     storedOperation.getImplementation().getArtifactDisplayName());
141                             return Either.right(responseFormatByArtifactId);
142                         }
143
144                         CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUniqueId);
145                         if (cassandraStatus != CassandraOperationStatus.OK) {
146                             titanDao.rollback();
147                             ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId(
148                                     componentsUtils.convertFromStorageResponse(
149                                             componentsUtils.convertToStorageOperationStatus(cassandraStatus)),
150                                     storedOperation.getImplementation().getArtifactDisplayName());
151                             return Either.right(responseFormatByArtifactId);
152                         }
153                     }
154                 }
155
156                 operationsCollection.put(operationId, interfaceDefinition.getOperationsMap().get(operationId));
157                 interfaceDefinition.getOperations().remove(operationId);
158             }
159
160             Either<List<InterfaceDefinition>, StorageOperationStatus> deleteOperationEither =
161                     interfaceOperation.updateInterfaces(storedComponent.getUniqueId(),
162                             Collections.singletonList(interfaceDefinition));
163             if (deleteOperationEither.isRight()) {
164                 titanDao.rollback();
165                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
166                         deleteOperationEither.right().value(), storedComponent.getComponentType())));
167             }
168
169             if (interfaceDefinition.getOperations().isEmpty()) {
170                 Either<String, StorageOperationStatus> deleteInterfaceEither = interfaceOperation.deleteInterface(
171                         storedComponent.getUniqueId(), interfaceDefinition.getUniqueId());
172                 if (deleteInterfaceEither.isRight()) {
173                     titanDao.rollback();
174                     return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
175                             deleteInterfaceEither.right().value(), storedComponent.getComponentType())));
176                 }
177             }
178
179             titanDao.commit();
180             interfaceDefinition.getOperations().putAll(operationsCollection);
181             interfaceDefinition.getOperations().keySet().removeIf(key -> !(operationsToDelete.contains(key)));
182             return Either.left(Collections.singletonList(interfaceDefinition));
183         } catch (Exception e) {
184             LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "delete", e);
185             titanDao.rollback();
186             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_DELETED));
187         } finally {
188             if (lockResult.isLeft() && lockResult.left().value()) {
189                 graphLockOperation.unlockComponent(storedComponent.getUniqueId(),
190                         NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
191             }
192         }
193     }
194
195     private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> getComponentDetails(String componentId) {
196         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> componentStorageOperationStatusEither =
197                 toscaOperationFacade.getToscaElement(componentId);
198         if (componentStorageOperationStatusEither.isRight()) {
199             return Either.right(componentsUtils.getResponseFormat(
200                     componentsUtils.convertFromStorageResponse(componentStorageOperationStatusEither.right().value())));
201         }
202         return Either.left(componentStorageOperationStatusEither.left().value());
203     }
204
205     private Either<Boolean, ResponseFormat> lockComponentResult(boolean lock,
206             org.openecomp.sdc.be.model.Component component, String action) {
207         if (lock) {
208             Either<Boolean, ResponseFormat> lockResult = lockComponent(component.getUniqueId(), component, action);
209             if (lockResult.isRight()) {
210                 titanDao.rollback();
211                 return Either.right(lockResult.right().value());
212             }
213         }
214         return Either.left(true);
215     }
216
217     public Either<List<InterfaceDefinition>, ResponseFormat> getInterfaceOperation(String componentId,
218             String interfaceId, List<String> operationsToGet, User user, boolean lock) {
219         validateUserExists(user.getUserId(), GET_INTERFACE_OPERATION, true);
220
221         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
222         if (componentEither.isRight()) {
223             return Either.right(componentEither.right().value());
224         }
225         org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
226
227         Either<Boolean, ResponseFormat> lockResult =
228                 lockComponentResult(lock, storedComponent, GET_INTERFACE_OPERATION);
229         if (lockResult.isRight()) {
230             return Either.right(lockResult.right().value());
231         }
232
233         try {
234             Optional<InterfaceDefinition> optionalInterface = getInterfaceDefinitionFromComponentByInterfaceId(
235                     storedComponent, interfaceId);
236             if (!optionalInterface.isPresent()) {
237                 return Either.right(
238                         componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceId));
239             }
240             InterfaceDefinition interfaceDefinition = optionalInterface.get();
241
242             for (String operationId : operationsToGet) {
243                 Optional<Map.Entry<String, Operation>> optionalOperation =
244                         getOperationFromInterfaceDefinition(interfaceDefinition, operationId);
245                 if (!optionalOperation.isPresent()) {
246                     return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND,
247                             storedComponent.getUniqueId()));
248                 }
249             }
250
251             titanDao.commit();
252             interfaceDefinition.getOperations().keySet().removeIf(key -> !(operationsToGet.contains(key)));
253             return Either.left(Collections.singletonList(interfaceDefinition));
254         } catch (Exception e) {
255             LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "get", e);
256             titanDao.rollback();
257             return Either.right(
258                     componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, componentId));
259         } finally {
260             if (lockResult.isLeft() && lockResult.left().value()) {
261                 graphLockOperation.unlockComponent(storedComponent.getUniqueId(),
262                         NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
263             }
264         }
265     }
266
267     public Either<List<InterfaceDefinition>, ResponseFormat> createInterfaceOperation(String componentId,
268             List<InterfaceDefinition> interfaceDefinitions, User user, boolean lock) {
269         return createOrUpdateInterfaceOperation(componentId, interfaceDefinitions, user, false,
270                 CREATE_INTERFACE_OPERATION, lock);
271     }
272
273     private Either<List<InterfaceDefinition>, ResponseFormat> createOrUpdateInterfaceOperation(String componentId,
274             List<InterfaceDefinition> interfaceDefinitions, User user, boolean isUpdate, String errorContext,
275             boolean lock) {
276         validateUserExists(user.getUserId(), errorContext, true);
277
278         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
279         if (componentEither.isRight()) {
280             return Either.right(componentEither.right().value());
281         }
282         org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
283
284         Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, errorContext);
285         if (lockResult.isRight()) {
286             return Either.right(lockResult.right().value());
287         }
288
289         Either<Map<String, InterfaceDefinition>, ResponseFormat> interfaceLifecycleTypes =
290                 getAllInterfaceLifecycleTypes();
291         if (interfaceLifecycleTypes.isRight()) {
292             return Either.right(interfaceLifecycleTypes.right().value());
293         }
294
295         try {
296             List<InterfaceDefinition> interfacesCollection = new ArrayList<>();
297             Map<String, Operation> operationsCollection = new HashMap<>();
298             for (InterfaceDefinition inputInterfaceDefinition : interfaceDefinitions) {
299                 Optional<InterfaceDefinition> optionalInterface =
300                         getInterfaceDefinitionFromComponentByInterfaceType(
301                                 storedComponent, inputInterfaceDefinition.getType());
302                 Either<Boolean, ResponseFormat> interfaceOperationValidationResponseEither =
303                         interfaceOperationValidation
304                                 .validateInterfaceOperations(inputInterfaceDefinition, storedComponent,
305                                         optionalInterface.orElse(null), interfaceLifecycleTypes.left().value(),
306                                         isUpdate);
307                 if (interfaceOperationValidationResponseEither.isRight()) {
308                     return Either.right(interfaceOperationValidationResponseEither.right().value());
309                 }
310
311                 Map<String, Operation> operationsToAddOrUpdate = inputInterfaceDefinition.getOperationsMap();
312                 operationsCollection.putAll(operationsToAddOrUpdate);
313                 inputInterfaceDefinition.getOperations().clear();
314
315                 Either<InterfaceDefinition, ResponseFormat> getInterfaceEither =
316                         getOrCreateInterfaceDefinition(storedComponent, inputInterfaceDefinition,
317                                 optionalInterface.orElse(null));
318                 if (getInterfaceEither.isRight()) {
319                     return Either.right(getInterfaceEither.right().value());
320                 }
321                 InterfaceDefinition interfaceDef = getInterfaceEither.left().value();
322
323                 updateOperationInputDefs(storedComponent, operationsToAddOrUpdate.values());
324
325                 for (Operation operation : operationsToAddOrUpdate.values()) {
326                     if (!isUpdate) {
327                         addOperationToInterface(interfaceDef, operation);
328                     } else {
329                         Optional<Map.Entry<String, Operation>> optionalOperation =
330                                 getOperationFromInterfaceDefinition(interfaceDef,
331                                         operation.getUniqueId());
332                         if (!optionalOperation.isPresent()) {
333                             titanDao.rollback();
334                             return Either.right(componentsUtils
335                                                         .getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND,
336                                                                 storedComponent.getUniqueId()));
337                         }
338
339                         Operation storedOperation = optionalOperation.get().getValue();
340                         String artifactUuId = storedOperation.getImplementation().getArtifactUUID();
341                         String artifactUniqueId = storedOperation.getImplementation().getUniqueId();
342
343                         if(!InterfaceOperationUtils.isArtifactInUse(storedComponent, storedOperation.getUniqueId(), artifactUniqueId)){
344                             Either<ArtifactDefinition, StorageOperationStatus> getArtifactEither =
345                                     artifactToscaOperation.getArtifactById(storedComponent.getUniqueId(), artifactUniqueId);
346                             if(getArtifactEither.isLeft()){
347                                 Either<ArtifactDefinition, StorageOperationStatus> removeArifactFromComponent =
348                                         artifactToscaOperation.removeArifactFromResource(componentId, artifactUniqueId,
349                                                 NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()),
350                                                 true);
351                                 if(removeArifactFromComponent.isRight()){
352                                     titanDao.rollback();
353                                     ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId(
354                                             componentsUtils.convertFromStorageResponse(removeArifactFromComponent.right().value()),
355                                             storedOperation.getImplementation().getArtifactDisplayName());
356                                     return Either.right(responseFormatByArtifactId);
357                                 }
358
359                                 CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUniqueId);
360                                 if (cassandraStatus != CassandraOperationStatus.OK) {
361                                     titanDao.rollback();
362                                     ResponseFormat responseFormatByArtifactId =
363                                             componentsUtils.getResponseFormatByArtifactId(
364                                                     componentsUtils.convertFromStorageResponse(
365                                                             componentsUtils.convertToStorageOperationStatus(
366                                                                     cassandraStatus)),
367                                                     storedOperation.getImplementation().getArtifactDisplayName());
368                                     return Either.right(responseFormatByArtifactId);
369                                 }
370                             }
371                         }
372                         updateOperationOnInterface(interfaceDef, operation, artifactUuId);
373                     }
374                 }
375                 interfacesCollection.add(interfaceDef);
376             }
377
378             Either<List<InterfaceDefinition>, StorageOperationStatus> addCreateOperationEither =
379                     interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), interfacesCollection);
380             if (addCreateOperationEither.isRight()) {
381                 titanDao.rollback();
382                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
383                         addCreateOperationEither.right().value(), storedComponent.getComponentType())));
384             }
385
386             titanDao.commit();
387             interfacesCollection.forEach(interfaceDefinition -> interfaceDefinition.getOperations().entrySet().removeIf(
388                     entry -> !operationsCollection.values().stream().map(OperationDataDefinition::getName)
389                                       .collect(Collectors.toList()).contains(entry.getValue().getName())));
390             return Either.left(interfacesCollection);
391         } catch (Exception e) {
392             titanDao.rollback();
393             LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "addOrUpdate", e);
394             return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
395         } finally {
396             if (lockResult.isLeft() && lockResult.left().value()) {
397                 graphLockOperation.unlockComponent(storedComponent.getUniqueId(),
398                         NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
399             }
400         }
401     }
402
403     public Either<Map<String, InterfaceDefinition>, ResponseFormat> getAllInterfaceLifecycleTypes() {
404
405         Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypes =
406                 interfaceLifecycleTypeOperation.getAllInterfaceLifecycleTypes();
407         if (interfaceLifecycleTypes.isRight()) {
408             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_LIFECYCLE_TYPES_NOT_FOUND));
409         }
410         interfaceLifecycleTypes.left().value().values().forEach(id -> id.setOperations(
411                 id.getOperations().keySet().stream().collect(Collectors.toMap(key -> key.replaceFirst(
412                         id.getUniqueId() + ".", ""), i -> id.getOperations().get(i)))));
413
414         return Either.left(interfaceLifecycleTypes.left().value());
415     }
416
417     private Either<InterfaceDefinition, ResponseFormat> getOrCreateInterfaceDefinition(
418             org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition,
419             InterfaceDefinition storedInterfaceDef) {
420         if (storedInterfaceDef != null) {
421             return Either.left(storedInterfaceDef);
422         }
423         interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
424         interfaceDefinition.setToscaResourceName(interfaceDefinition.getType());
425         Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceCreateEither =
426                 interfaceOperation.addInterfaces(component.getUniqueId(),
427                         Collections.singletonList(interfaceDefinition));
428         if (interfaceCreateEither.isRight()) {
429             titanDao.rollback();
430             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
431                     interfaceCreateEither.right().value(), component.getComponentType())));
432         }
433         return Either.left(interfaceCreateEither.left().value().get(0));
434     }
435
436     private void updateOperationInputDefs(org.openecomp.sdc.be.model.Component component,
437                                           Collection<Operation> interfaceOperations) {
438         interfaceOperations.stream().filter(operation -> Objects.nonNull(operation.getInputs())).forEach(
439                 operation -> operation.getInputs().getListToscaDataDefinition().forEach(
440                         inp -> component.getInputs()
441                                 .forEach(in -> updateOperationInputDefinition(component, inp, in))));
442     }
443
444     private void updateOperationInputDefinition(org.openecomp.sdc.be.model.Component component,
445                                                 OperationInputDefinition operationInput,
446                                                 InputDefinition componentInput) {
447         if (operationInput.getInputId().equals(componentInput.getUniqueId())) {
448             //Set the default value, value and schema only for inputs mapped to component inputs
449             operationInput.setDefaultValue(componentInput.getDefaultValue());
450             operationInput.setToscaDefaultValue(getInputToscaDefaultValue(operationInput, component));
451             operationInput.setValue(componentInput.getValue());
452             operationInput.setSchema(componentInput.getSchema());
453         }
454         //Set the tosca default value for inputs mapped to component inputs as well as other outputs
455         operationInput.setToscaDefaultValue(getInputToscaDefaultValue(operationInput, component));
456     }
457
458     private String getInputToscaDefaultValue(OperationInputDefinition input,
459                                              org.openecomp.sdc.be.model.Component component) {
460         Map<String, List<String>> defaultInputValue;
461         if (isOperationInputMappedToComponentInput(input, component.getInputs())) {
462             String propertyName = input.getInputId().substring(input.getInputId().indexOf('.') + 1);
463             defaultInputValue = createMappedInputPropertyDefaultValue(propertyName);
464         } else {
465             //Currently inputs can only be mapped to a declared input or an other operation outputs
466             defaultInputValue = createMappedOutputDefaultValue(SELF, input.getInputId());
467         }
468         return new Gson().toJson(defaultInputValue);
469     }
470
471     private void addOperationToInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation) {
472         interfaceOperation.setUniqueId(UUID.randomUUID().toString());
473         interfaceOperation.setImplementation(createArtifactDefinition(UUID.randomUUID().toString()));
474         interfaceDefinition.getOperations()
475                 .put(interfaceOperation.getUniqueId(), new OperationDataDefinition(interfaceOperation));
476     }
477
478     private void updateOperationOnInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation,
479             String artifactUuId) {
480         interfaceOperation.setImplementation(createArtifactDefinition(artifactUuId));
481         interfaceDefinition.getOperations()
482                 .put(interfaceOperation.getUniqueId(), new OperationDataDefinition(interfaceOperation));
483     }
484
485     private ArtifactDefinition createArtifactDefinition(String artifactUuId) {
486         ArtifactDefinition artifactDefinition = new ArtifactDefinition();
487         artifactDefinition.setArtifactUUID(artifactUuId);
488         artifactDefinition.setUniqueId(artifactUuId);
489         artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType());
490         artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
491         return artifactDefinition;
492     }
493
494     public Either<List<InterfaceDefinition>, ResponseFormat> updateInterfaceOperation(String componentId,
495             List<InterfaceDefinition> interfaceDefinitions, User user, boolean lock) {
496         return createOrUpdateInterfaceOperation(componentId, interfaceDefinitions, user, true,
497                 UPDATE_INTERFACE_OPERATION, lock);
498     }
499
500     public Either<List<OperationInputDefinition>, ResponseFormat> getInputsListForOperation(String componentId,
501                                                                                             String componentInstanceId, String interfaceId, String operationId, User user) {
502         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
503         if (componentEither.isRight()){
504             return Either.right(componentEither.right().value());
505         }
506
507         org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
508         validateUserExists(user.getUserId(), GET_INTERFACE_OPERATION, true);
509
510         Either<Boolean, ResponseFormat> lockResult = lockComponentResult(true, storedComponent, GET_INTERFACE_OPERATION);
511         if (lockResult.isRight()) {
512             return Either.right(lockResult.right().value());
513         }
514
515         try{
516             org.openecomp.sdc.be.model.Component parentComponent = componentEither.left().value();
517             Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces =
518                     parentComponent.getComponentInstancesInterfaces();
519             if(MapUtils.isEmpty(componentInstanceInterfaces)) {
520                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND,
521                         componentInstanceId));
522             }
523
524             List<ComponentInstanceInterface> componentInstanceInterfaceList =
525                     componentInstanceInterfaces.get(componentInstanceId);
526             for(ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaceList) {
527                 if(componentInstanceInterface.getInterfaceId().equals(interfaceId)){
528                     Map<String, OperationDataDefinition> operations = componentInstanceInterface.getOperations();
529                     if(MapUtils.isNotEmpty(operations) && operations.containsKey(operationId)) {
530                         ListDataDefinition<OperationInputDefinition> inputs = operations.get(operationId).getInputs();
531                         return Either.left(CollectionUtils.isEmpty(inputs.getListToscaDataDefinition())
532                                 ? new ArrayList<>() : inputs.getListToscaDataDefinition());
533                     }
534                 }
535             }
536             return Either.left(new ArrayList<>());
537         }
538         catch (Exception e) {
539             LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "get", e);
540             titanDao.rollback();
541             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND));
542         }
543         finally {
544             if (lockResult.isLeft() && lockResult.left().value()) {
545                 graphLockOperation.unlockComponent(storedComponent.getUniqueId(),
546                         NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
547             }
548         }
549     }
550
551 }