re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ExternalRefsBusinessLogic.java
1 package org.openecomp.sdc.be.components.impl;
2
3 import fj.data.Either;
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.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.common.log.wrappers.Logger;
15
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 /**
21  * Created by yavivi on 04/02/2018.
22  */
23 @org.springframework.stereotype.Component
24 public class ExternalRefsBusinessLogic {
25
26     private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
27
28     private final ExternalReferencesOperation externalReferencesOperation;
29     private final ToscaOperationFacade toscaOperationFacade;
30     private final AccessValidations accessValidations;
31     private final ComponentLocker componentLocker;
32
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;
38     }
39
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);
44         }
45         Component component = componentsResult.left().value();
46         return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
47     }
48
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);
53         }
54
55         Component component = componentsResult.left().value();
56
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());
60         } else {
61             return Either.left(externalReferencesResult.left().value());
62         }
63     }
64
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(), "");
67     }
68
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, "");
71     }
72
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);
75     }
76
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);
81         }
82
83         //Get Component Unique ID
84         Component component = latestServiceByUuid.left().value();
85         String uniqueId = component.getUniqueId();
86
87         //Lock Asset
88         this.componentLocker.lock(component);
89         this.accessValidations.validateUserCanWorkOnComponent(component, userId, action + " EXTERNAL REF");
90
91         Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
92         try {
93             switch (action) {
94                 case "POST":
95                     opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
96                     break;
97                 case "PUT":
98                     opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
99                     break;
100                 case "DELETE":
101                     opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
102                     break;
103                 default:
104                     break;
105             }
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);
110         } finally {
111             //Unlock Asset
112             this.componentLocker.unlock(uniqueId, componentType);
113         }
114         return opResult;
115     }
116
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;
122     }
123
124 }