Sync Integ to Master
[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.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;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 /**
23  * Created by yavivi on 04/02/2018.
24  */
25 @org.springframework.stereotype.Component
26 public class ExternalRefsBusinessLogic {
27
28     private static final Logger log = LoggerFactory.getLogger(ExternalRefsBusinessLogic.class);
29
30     private ExternalReferencesOperation externalReferencesOperation;
31     private ToscaOperationFacade toscaOperationFacade;
32     private GraphLockOperation graphLockOperation;
33
34     public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, GraphLockOperation graphLockOperation){
35         this.externalReferencesOperation = externalReferencesOperation;
36         this.toscaOperationFacade = toscaOperationFacade;
37         this.graphLockOperation = graphLockOperation;
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(String uuid, String componentInstanceName, String objectType, ExternalRefDTO ref) {
66         return this.doAction("POST", uuid, componentInstanceName, objectType, ref.getReferenceUUID(), "");
67     }
68
69
70     public Either<String, ActionStatus> deleteExternalReference(String uuid, String componentInstanceName, String objectType, String reference) {
71         return this.doAction("DELETE", uuid, componentInstanceName, objectType, reference, "");
72     }
73
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);
76     }
77
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);
82         }
83
84         //Get Component Unique ID
85         Component component = latestServiceByUuid.left().value();
86         String uniqueId = component.getUniqueId();
87
88         //Lock Asset
89         StorageOperationStatus lockStatus = this.graphLockOperation.lockComponent(uniqueId, NodeTypeEnum.Service);
90         if (lockStatus != StorageOperationStatus.OK){
91             return Either.right(ActionStatus.GENERAL_ERROR);
92         }
93
94         Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
95         try {
96             switch (action) {
97                 case "POST":
98                     opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
99                     break;
100                 case "PUT":
101                     opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
102                     break;
103                 case "DELETE":
104                     opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
105                     break;
106                 default:
107                     break;
108             }
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);
113         } finally {
114             //Unlock Asset
115             this.graphLockOperation.unlockComponent(uniqueId, NodeTypeEnum.Service);
116         }
117         return opResult;
118     }
119
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;
125     }
126
127 }