1 package org.openecomp.sdc.be.components.impl;
4 import org.openecomp.sdc.be.components.validation.AccessValidations;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
7 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
8 import org.openecomp.sdc.be.dto.ExternalRefDTO;
9 import org.openecomp.sdc.be.model.Component;
10 import org.openecomp.sdc.be.model.LifecycleStateEnum;
11 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
12 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
13 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
14 import org.openecomp.sdc.common.log.wrappers.Logger;
16 import java.util.HashMap;
17 import java.util.List;
21 * Created by yavivi on 04/02/2018.
23 @org.springframework.stereotype.Component
24 public class ExternalRefsBusinessLogic {
26 private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
28 private final ExternalReferencesOperation externalReferencesOperation;
29 private final ToscaOperationFacade toscaOperationFacade;
30 private final AccessValidations accessValidations;
31 private final ComponentLocker componentLocker;
33 public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, AccessValidations accessValidations, ComponentLocker componentLocker) {
34 this.externalReferencesOperation = externalReferencesOperation;
35 this.toscaOperationFacade = toscaOperationFacade;
36 this.accessValidations = accessValidations;
37 this.componentLocker = componentLocker;
40 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName, String objectType){
41 Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
42 if (componentsResult == null || componentsResult.isRight()) {
43 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
45 Component component = componentsResult.left().value();
46 return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
49 public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String version, String objectType){
50 Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
51 if (componentsResult == null || componentsResult.isRight()) {
52 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
55 Component component = componentsResult.left().value();
57 Either<Map<String, List<String>>, ActionStatus> externalReferencesResult = this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), objectType);
58 if (externalReferencesResult.isRight()){
59 return Either.right(externalReferencesResult.right().value());
61 return Either.left(externalReferencesResult.left().value());
65 public Either<String, ActionStatus> addExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, ExternalRefDTO ref) {
66 return this.doAction(componentType, userId, "POST", uuid, componentInstanceName, objectType, ref.getReferenceUUID(), "");
69 public Either<String, ActionStatus> deleteExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String reference) {
70 return this.doAction(componentType, userId, "DELETE", uuid, componentInstanceName, objectType, reference, "");
73 public Either<String, ActionStatus> updateExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String oldRefValue, String newRefValue) {
74 return this.doAction(componentType, userId, "PUT", uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
77 private Either<String, ActionStatus> doAction(ComponentTypeEnum componentType, String userId, String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
78 Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade.getLatestComponentByUuid(uuid, createPropsToMatch(componentType));
79 if (latestServiceByUuid == null || latestServiceByUuid.isRight()){
80 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
83 //Get Component Unique ID
84 Component component = latestServiceByUuid.left().value();
85 String uniqueId = component.getUniqueId();
88 this.componentLocker.lock(component);
89 this.accessValidations.validateUserCanWorkOnComponent(component, userId, action + " EXTERNAL REF");
91 Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
95 opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
98 opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
101 opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
106 } catch (Exception e) {
107 opResult = Either.right(ActionStatus.GENERAL_ERROR);
108 log.error("Failed to execute external ref action:{} on asset:{} component:{} objectType:{}", action, uuid, componentInstanceName, objectType);
109 log.error("Cause is:" , e);
112 this.componentLocker.unlock(uniqueId, componentType);
117 private Map<GraphPropertyEnum, Object> createPropsToMatch(ComponentTypeEnum componentType) {
118 Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
119 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
120 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
121 return propertiesToMatch;