2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.openecomp.sdc.be.components.impl;
20 import fj.data.Either;
21 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
22 import org.openecomp.sdc.be.dao.api.ActionStatus;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.model.ArtifactDefinition;
26 import org.openecomp.sdc.be.model.InterfaceDefinition;
27 import org.openecomp.sdc.be.model.Operation;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils;
30 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
31 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
32 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
39 import java.util.Collection;
40 import java.util.Collections;
42 import java.util.Optional;
43 import java.util.UUID;
44 import java.util.stream.Collectors;
46 @Component("interfaceOperationBusinessLogic")
47 public class InterfaceOperationBusinessLogic extends BaseBusinessLogic {
49 private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationBusinessLogic.class);
50 private static final String FAILED_TO_LOCK_COMPONENT_RESPONSE_IS = "Failed to lock component {}. Response is {}";
51 private static final String EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION = "Exception occurred during {}. Response is {}";
52 private static final String DELETE_INTERFACE_OPERATION = "deleteInterfaceOperation";
53 private static final String GET_INTERFACE_OPERATION = "getInterfaceOperation";
54 private static final String CREATE_INTERFACE_OPERATION = "createInterfaceOperation";
55 private static final String UPDATE_INTERFACE_OPERATION = "updateInterfaceOperation";
58 private InterfaceOperationValidation interfaceOperationValidation;
60 public void setInterfaceOperationValidation(InterfaceOperationValidation interfaceOperationValidation) {
61 this.interfaceOperationValidation = interfaceOperationValidation;
64 public Either<Operation, ResponseFormat> deleteInterfaceOperation(String componentId, String interfaceOperationToDelete, User user, boolean lock) {
65 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
66 if (componentEither.isRight()){
67 return Either.right(componentEither.right().value());
69 org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
70 validateUserExists(user.getUserId(), DELETE_INTERFACE_OPERATION, true);
72 Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, DELETE_INTERFACE_OPERATION);
73 if (lockResult.isRight()) {
74 return Either.right(lockResult.right().value());
78 Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName());
79 Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null));
80 if (getInterfaceEither.isRight()) {
81 return Either.right(getInterfaceEither.right().value());
83 InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value();
85 Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, interfaceOperationToDelete);
86 if (getOperationEither.isRight()){
87 return Either.right(getOperationEither.right().value());
90 Either<Operation, StorageOperationStatus> deleteEither = interfaceOperation.deleteInterfaceOperation(componentId, interfaceDefinition, interfaceOperationToDelete);
91 if (deleteEither.isRight()){
92 LOGGER.error("Failed to delete interface operation from component {}. Response is {}", storedComponent.getName(), deleteEither.right().value());
93 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteEither.right().value(), storedComponent.getComponentType())));
97 return Either.left(deleteEither.left().value());
100 LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "delete", e);
102 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_DELETED));
105 if (lockResult.isLeft() && lockResult.left().value()) {
106 graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
111 public Either<Operation, ResponseFormat> getInterfaceOperation(String componentId, String interfaceOperationToGet, User user, boolean lock) {
112 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
113 if (componentEither.isRight()){
114 return Either.right(componentEither.right().value());
116 org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
117 validateUserExists(user.getUserId(), GET_INTERFACE_OPERATION, true);
119 Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, GET_INTERFACE_OPERATION);
120 if (lockResult.isRight()) {
121 return Either.right(lockResult.right().value());
125 Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName());
126 Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null));
127 if (getInterfaceEither.isRight()) {
128 return Either.right(getInterfaceEither.right().value());
130 InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value();
132 Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, interfaceOperationToGet);
133 if (getOperationEither.isRight()){
134 return Either.right(getOperationEither.right().value());
138 return Either.left(getOperationEither.left().value());
141 LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "get", e);
143 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, componentId));
146 if (lockResult.isLeft() && lockResult.left().value()) {
147 graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
152 private Either<InterfaceDefinition, ResponseFormat> getInterfaceDefinition(org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDef) {
153 if (interfaceDef != null){
154 return Either.left(interfaceDef);
156 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
157 interfaceDefinition.setToscaResourceName(InterfaceUtils.createInterfaceToscaResourceName(component.getName()));
158 Either<InterfaceDefinition, StorageOperationStatus> interfaceCreateEither = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition);
159 if (interfaceCreateEither.isRight()){
160 StorageOperationStatus sValue = interfaceCreateEither.right().value();
161 LOGGER.error("Failed to get interface from component {}. Response is {}", component.getName(), sValue);
162 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(sValue, component.getComponentType()), ""));
164 return Either.left(interfaceCreateEither.left().value());
168 public Either<Operation, ResponseFormat> createInterfaceOperation(String componentId, Operation operation, User user, boolean lock) {
169 return createOrUpdateInterfaceOperation(componentId, operation, user, false, CREATE_INTERFACE_OPERATION, lock);
172 public Either<Operation, ResponseFormat> updateInterfaceOperation(String componentId, Operation operation, User user, boolean lock) {
173 return createOrUpdateInterfaceOperation(componentId, operation, user, true, UPDATE_INTERFACE_OPERATION, lock);
176 private Either<Operation, ResponseFormat> createOrUpdateInterfaceOperation(String componentId, Operation operation, User user, boolean isUpdate, String errorContext, boolean lock) {
177 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
178 if (componentEither.isRight()){
179 return Either.right(componentEither.right().value());
181 org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
182 validateUserExists(user.getUserId(), errorContext, true);
183 Either<Boolean, ResponseFormat> interfaceOperationValidationResponseEither = interfaceOperationValidation
184 .validateInterfaceOperations(Collections.singletonList(operation), storedComponent, isUpdate);
185 if(interfaceOperationValidationResponseEither.isRight()) {
186 return Either.right(interfaceOperationValidationResponseEither.right().value());
189 Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, errorContext);
190 if (lockResult.isRight()) {
191 return Either.right(lockResult.right().value());
195 Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName());
196 Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null));
197 if (getInterfaceEither.isRight()) {
198 return Either.right(getInterfaceEither.right().value());
200 InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value();
202 Either<Operation, StorageOperationStatus> result;
204 initNewOperation(operation);
205 result = interfaceOperation.addInterfaceOperation(componentId, interfaceDefinition, operation);
208 Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, operation.getUniqueId());
209 if (getOperationEither.isRight()){
210 return Either.right(getOperationEither.right().value());
212 updateExistingOperation(operation, getOperationEither.left().value().getImplementation().getArtifactUUID());
213 result = interfaceOperation.updateInterfaceOperation(componentId, interfaceDefinition, operation);
216 if (result.isRight()) {
218 LOGGER.debug("Failed to addOrUpdate interface operation on component {}. Response is {}", storedComponent.getName(), result.right().value());
219 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), storedComponent.getComponentType())));
223 return Either.left(result.left().value());
225 catch (Exception e) {
227 LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "addOrUpdate", e);
228 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
231 if (lockResult.isLeft() && lockResult.left().value()) {
232 graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
237 private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> getComponentDetails(String componentId){
238 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
239 if (componentStorageOperationStatusEither.isRight()) {
240 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
241 LOGGER.error("Failed to fetch component information by component id {}, Response is {}", componentId, errorStatus);
242 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
244 return Either.left(componentStorageOperationStatusEither.left().value());
247 private Either<Operation, ResponseFormat> getOperationFromInterfaceDef(
248 org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition, String operationToFetch) {
249 Optional<Map.Entry<String, Operation>> operationMap = interfaceDefinition.getOperationsMap().entrySet().stream()
250 .filter(entry -> entry.getValue().getUniqueId().equals(operationToFetch)).findAny();
251 if (!operationMap.isPresent()) {
252 LOGGER.error("Failed to get interface operation from component {}. Response is {}", component.getUniqueId(), ActionStatus.INTERFACE_OPERATION_NOT_FOUND);
253 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, component.getUniqueId()));
255 return Either.left(operationMap.get().getValue());
258 private void initNewOperation(Operation operation){
259 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
260 String artifactUUID = UUID.randomUUID().toString();
261 artifactDefinition.setArtifactUUID(artifactUUID);
262 artifactDefinition.setUniqueId(artifactUUID);
263 artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType());
264 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
265 operation.setUniqueId(UUID.randomUUID().toString());
266 operation.setImplementation(artifactDefinition);
269 private void updateExistingOperation(Operation operation, String artifactUUID){
270 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
271 artifactDefinition.setArtifactUUID(artifactUUID);
272 artifactDefinition.setUniqueId(artifactUUID);
273 artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType());
274 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
275 operation.setImplementation(artifactDefinition);
278 private Either<Boolean, ResponseFormat> lockComponentResult(boolean lock, org.openecomp.sdc.be.model.Component component, String action){
280 Either<Boolean, ResponseFormat> lockResult = lockComponent(component.getUniqueId(), component, action);
281 if (lockResult.isRight()) {
282 LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, component.getName(), lockResult.right().value().getFormattedMessage());
284 return Either.right(lockResult.right().value());
287 return Either.left(true);
290 public Either<Boolean, ResponseFormat> validateComponentNameAndUpdateInterfaces(org.openecomp.sdc.be.model.Component oldComponent,
291 org.openecomp.sdc.be.model.Component newComponent) {
292 if(!oldComponent.getName().equals(newComponent.getName()) ) {
293 Collection<InterfaceDefinition> interfaceDefinitionListFromToscaName = InterfaceUtils
294 .getInterfaceDefinitionListFromToscaName(oldComponent.getInterfaces().values(),
295 oldComponent.getName());
296 for (InterfaceDefinition interfaceDefinition : interfaceDefinitionListFromToscaName) {
298 Either<InterfaceDefinition, ResponseFormat> interfaceDefinitionResponseEither = updateInterfaceDefinition(oldComponent,
299 newComponent, interfaceDefinition);
300 if(interfaceDefinitionResponseEither.isRight()) {
301 return Either.right(interfaceDefinitionResponseEither.right().value());
305 return Either.left(Boolean.TRUE);
307 private Either<InterfaceDefinition, ResponseFormat > updateInterfaceDefinition(org.openecomp.sdc.be.model.Component oldComponent,
308 org.openecomp.sdc.be.model.Component newComponent,
309 InterfaceDefinition interfaceDefinition) {
310 InterfaceUtils.createInterfaceToscaResourceName(newComponent.getName());
311 interfaceDefinition.setToscaResourceName(InterfaceUtils
312 .createInterfaceToscaResourceName(newComponent.getName()));
314 Either<InterfaceDefinition, StorageOperationStatus> interfaceUpdate = interfaceOperation
315 .updateInterface(oldComponent.getUniqueId(), interfaceDefinition);
316 if (interfaceUpdate.isRight()) {
317 LOGGER.error("Failed to Update interface {}. Response is {}. ", newComponent.getName(), interfaceUpdate.right().value());
319 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(interfaceUpdate.right().value(), ComponentTypeEnum.RESOURCE)));
321 } catch (Exception e) {
322 LOGGER.error("Exception occurred during update interface toscaResourceName : {}", e);
324 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
326 return Either.left( interfaceDefinition);
329 public Either<Map<String, InterfaceDefinition>, ResponseFormat> getAllInterfaceLifecycleTypes() {
331 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypes = interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
332 if(interfaceLifecycleTypes.isRight()) {
333 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_LIFECYCLE_TYPES_NOT_FOUND));
335 interfaceLifecycleTypes.left().value().values().stream().forEach(
336 id -> id.setOperations(id.getOperations().keySet().stream().collect(Collectors.toMap(key -> key.replaceFirst(id.getUniqueId()+".",""), i -> id.getOperations().get(i)))));
338 return Either.left(interfaceLifecycleTypes.left().value());