Catalog alignment
[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.impl.exceptions.ByActionStatusComponentException;
25 import org.openecomp.sdc.be.components.impl.lock.LockingTransactional;
26 import org.openecomp.sdc.be.components.validation.AccessValidations;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
30 import org.openecomp.sdc.be.dto.ExternalRefDTO;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 /**
42  * Created by yavivi on 04/02/2018.
43  */
44 @org.springframework.stereotype.Component
45 public class ExternalRefsBusinessLogic {
46
47     private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
48
49     private final ExternalReferencesOperation externalReferencesOperation;
50     private final ToscaOperationFacade toscaOperationFacade;
51     private final AccessValidations accessValidations;
52     private final ComponentLocker componentLocker;
53
54     public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, AccessValidations accessValidations, ComponentLocker componentLocker) {
55         this.externalReferencesOperation = externalReferencesOperation;
56         this.toscaOperationFacade = toscaOperationFacade;
57         this.accessValidations = accessValidations;
58         this.componentLocker = componentLocker;
59     }
60
61     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName, String objectType){
62         Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
63         if (componentsResult == null || componentsResult.isRight()) {
64             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
65         }
66         Component component = componentsResult.left().value();
67         return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
68     }
69
70     public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String version, String objectType){
71         Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
72         if (componentsResult == null || componentsResult.isRight()) {
73             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
74         }
75
76         Component component = componentsResult.left().value();
77
78         Either<Map<String, List<String>>, ActionStatus> externalReferencesResult = this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), objectType);
79         if (externalReferencesResult.isRight()){
80             return Either.right(externalReferencesResult.right().value());
81         } else {
82             return Either.left(externalReferencesResult.left().value());
83         }
84     }
85
86     @LockingTransactional
87     public Either<String, ActionStatus> addExternalReference(String componentId, ComponentTypeEnum componentType, String userId, String componentInstanceName, String objectType, ExternalRefDTO ref) {
88         return this.doAction(componentId, componentType, userId, "POST", componentId, componentInstanceName, objectType, ref.getReferenceUUID(), "");
89     }
90
91     @LockingTransactional
92     public Either<String, ActionStatus> deleteExternalReference(String componentId, ComponentTypeEnum componentType, String userId, String componentInstanceName, String objectType, String reference) {
93         return this.doAction(componentId, componentType, userId, "DELETE", componentId, componentInstanceName, objectType, reference, "");
94     }
95
96     @LockingTransactional
97     public Either<String, ActionStatus> updateExternalReference(String componentId, ComponentTypeEnum componentType, String userId, String componentInstanceName, String objectType, String oldRefValue, String newRefValue) {
98         return this.doAction(componentId, componentType, userId, "PUT", componentId, componentInstanceName, objectType, oldRefValue, newRefValue);
99     }
100
101     public String fetchComponentUniqueIdByUuid(String uuid, ComponentTypeEnum componentType){
102         Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade.getLatestComponentByUuid(uuid, createPropsToMatch(componentType));
103         if (latestServiceByUuid == null || latestServiceByUuid.isRight()){
104             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, uuid);
105         }
106
107         //Get Component Unique ID
108         Component component = latestServiceByUuid.left().value();
109         return component.getUniqueId();
110     }
111
112
113     public Either<String, ActionStatus> doAction(String componentId, ComponentTypeEnum componentType, String userId, String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
114
115         accessValidations.validateUserCanWorkOnComponent(componentId, componentType, userId, action + " EXTERNAL REF");
116
117         switch (action) {
118             case "POST":
119                 return this.externalReferencesOperation.addExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
120             case "PUT":
121                 return this.externalReferencesOperation.updateExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1, ref2);
122             case "DELETE":
123                 return this.externalReferencesOperation.deleteExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
124             default:
125                 return Either.right(ActionStatus.GENERAL_ERROR);
126         }
127
128     }
129
130     private Map<GraphPropertyEnum, Object> createPropsToMatch(ComponentTypeEnum componentType) {
131         Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
132         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
133         return propertiesToMatch;
134     }
135
136 }