Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ExternalRefsBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.validation.AccessValidations;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
28 import org.openecomp.sdc.be.dto.ExternalRefDTO;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.LifecycleStateEnum;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 /**
41  * Created by yavivi on 04/02/2018.
42  */
43 @org.springframework.stereotype.Component
44 public class ExternalRefsBusinessLogic {
45
46     private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
47
48     private final ExternalReferencesOperation externalReferencesOperation;
49     private final ToscaOperationFacade toscaOperationFacade;
50     private final AccessValidations accessValidations;
51     private final ComponentLocker componentLocker;
52
53     public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, AccessValidations accessValidations, ComponentLocker componentLocker) {
54         this.externalReferencesOperation = externalReferencesOperation;
55         this.toscaOperationFacade = toscaOperationFacade;
56         this.accessValidations = accessValidations;
57         this.componentLocker = componentLocker;
58     }
59
60     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName, String objectType){
61         Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
62         if (componentsResult == null || componentsResult.isRight()) {
63             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
64         }
65         Component component = componentsResult.left().value();
66         return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
67     }
68
69     public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String version, String objectType){
70         Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
71         if (componentsResult == null || componentsResult.isRight()) {
72             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
73         }
74
75         Component component = componentsResult.left().value();
76
77         Either<Map<String, List<String>>, ActionStatus> externalReferencesResult = this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), objectType);
78         if (externalReferencesResult.isRight()){
79             return Either.right(externalReferencesResult.right().value());
80         } else {
81             return Either.left(externalReferencesResult.left().value());
82         }
83     }
84
85     public Either<String, ActionStatus> addExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, ExternalRefDTO ref) {
86         return this.doAction(componentType, userId, "POST", uuid, componentInstanceName, objectType, ref.getReferenceUUID(), "");
87     }
88
89     public Either<String, ActionStatus> deleteExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String reference) {
90         return this.doAction(componentType, userId, "DELETE", uuid, componentInstanceName, objectType, reference, "");
91     }
92
93     public Either<String, ActionStatus> updateExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String oldRefValue, String newRefValue) {
94         return this.doAction(componentType, userId, "PUT", uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
95     }
96
97     private Either<String, ActionStatus> doAction(ComponentTypeEnum componentType, String userId, String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
98         Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade.getLatestComponentByUuid(uuid, createPropsToMatch(componentType));
99         if (latestServiceByUuid == null || latestServiceByUuid.isRight()){
100             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
101         }
102
103         //Get Component Unique ID
104         Component component = latestServiceByUuid.left().value();
105         String uniqueId = component.getUniqueId();
106
107         //Lock Asset
108         this.componentLocker.lock(component);
109         this.accessValidations.validateUserCanWorkOnComponent(component, userId, action + " EXTERNAL REF");
110
111         Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
112         try {
113             switch (action) {
114                 case "POST":
115                     opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
116                     break;
117                 case "PUT":
118                     opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
119                     break;
120                 case "DELETE":
121                     opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
122                     break;
123                 default:
124                     break;
125             }
126         } catch (Exception e) {
127             opResult = Either.right(ActionStatus.GENERAL_ERROR);
128             log.error("Failed to execute external ref action:{} on asset:{} component:{} objectType:{}", action, uuid, componentInstanceName, objectType);
129             log.error("Cause is:" , e);
130         } finally {
131             //Unlock Asset
132             this.componentLocker.unlock(uniqueId, componentType);
133         }
134         return opResult;
135     }
136
137     private Map<GraphPropertyEnum, Object> createPropsToMatch(ComponentTypeEnum componentType) {
138         Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
139         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
140         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
141         return propertiesToMatch;
142     }
143
144 }