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