1 package org.openecomp.sdc.be.components.impl;
4 import org.openecomp.sdc.be.dao.api.ActionStatus;
5 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
6 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
7 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
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.jsontitan.operations.ExternalReferencesOperation;
12 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
13 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
14 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import java.util.HashMap;
19 import java.util.List;
23 * Created by yavivi on 04/02/2018.
25 @org.springframework.stereotype.Component
26 public class ExternalRefsBusinessLogic {
28 private static final Logger log = LoggerFactory.getLogger(ExternalRefsBusinessLogic.class);
30 private ExternalReferencesOperation externalReferencesOperation;
31 private ToscaOperationFacade toscaOperationFacade;
32 private GraphLockOperation graphLockOperation;
34 public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, GraphLockOperation graphLockOperation){
35 this.externalReferencesOperation = externalReferencesOperation;
36 this.toscaOperationFacade = toscaOperationFacade;
37 this.graphLockOperation = graphLockOperation;
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(String uuid, String componentInstanceName, String objectType, ExternalRefDTO ref) {
66 return this.doAction("POST", uuid, componentInstanceName, objectType, ref.getReferenceUUID(), "");
70 public Either<String, ActionStatus> deleteExternalReference(String uuid, String componentInstanceName, String objectType, String reference) {
71 return this.doAction("DELETE", uuid, componentInstanceName, objectType, reference, "");
74 public Either<String, ActionStatus> updateExternalReference(String uuid, String componentInstanceName, String objectType, String oldRefValue, String newRefValue) {
75 return this.doAction("PUT", uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
78 private Either<String, ActionStatus> doAction(String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
79 Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade.getLatestComponentByUuid(uuid, createPropsToMatch());
80 if (latestServiceByUuid == null || latestServiceByUuid.isRight()){
81 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
84 //Get Component Unique ID
85 Component component = latestServiceByUuid.left().value();
86 String uniqueId = component.getUniqueId();
89 StorageOperationStatus lockStatus = this.graphLockOperation.lockComponent(uniqueId, NodeTypeEnum.Service);
90 if (lockStatus != StorageOperationStatus.OK){
91 return Either.right(ActionStatus.GENERAL_ERROR);
94 Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
98 opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
101 opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
104 opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
109 } catch (Exception e) {
110 opResult = Either.right(ActionStatus.GENERAL_ERROR);
111 log.error("Failed to execute external ref action:{} on asset:{} component:{} objectType:{}", action, uuid, componentInstanceName, objectType);
112 log.error("Cause is:" , e);
115 this.graphLockOperation.unlockComponent(uniqueId, NodeTypeEnum.Service);
120 private Map<GraphPropertyEnum, Object> createPropsToMatch() {
121 Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
122 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
123 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
124 return propertiesToMatch;