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