acd19e4beee5ac9cfd1424a2e900d1bad2c9541b
[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 static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
46     private final ExternalReferencesOperation externalReferencesOperation;
47     private final ToscaOperationFacade toscaOperationFacade;
48     private final AccessValidations accessValidations;
49     private final ComponentLocker componentLocker;
50
51     public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade,
52                                      AccessValidations accessValidations, ComponentLocker componentLocker) {
53         this.externalReferencesOperation = externalReferencesOperation;
54         this.toscaOperationFacade = toscaOperationFacade;
55         this.accessValidations = accessValidations;
56         this.componentLocker = componentLocker;
57     }
58
59     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName,
60                                                                     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         Component component = componentsResult.left().value();
75         Either<Map<String, List<String>>, ActionStatus> externalReferencesResult = this.externalReferencesOperation
76             .getExternalReferences(component.getUniqueId(), objectType);
77         if (externalReferencesResult.isRight()) {
78             return Either.right(externalReferencesResult.right().value());
79         } else {
80             return Either.left(externalReferencesResult.left().value());
81         }
82     }
83
84     @LockingTransactional
85     public Either<String, ActionStatus> addExternalReference(String componentId, ComponentTypeEnum componentType, String userId,
86                                                              String componentInstanceName, String objectType, ExternalRefDTO ref) {
87         return this.doAction(componentId, componentType, userId, "POST", componentId, componentInstanceName, objectType, ref.getReferenceUUID(), "");
88     }
89
90     @LockingTransactional
91     public Either<String, ActionStatus> deleteExternalReference(String componentId, ComponentTypeEnum componentType, String userId,
92                                                                 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,
98                                                                 String componentInstanceName, String objectType, String oldRefValue,
99                                                                 String newRefValue) {
100         return this.doAction(componentId, componentType, userId, "PUT", componentId, componentInstanceName, objectType, oldRefValue, newRefValue);
101     }
102
103     public String fetchComponentUniqueIdByUuid(String uuid, ComponentTypeEnum componentType) {
104         Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade
105             .getLatestComponentByUuid(uuid, createPropsToMatch(componentType));
106         if (latestServiceByUuid == null || latestServiceByUuid.isRight()) {
107             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, uuid);
108         }
109         //Get Component Unique ID
110         Component component = latestServiceByUuid.left().value();
111         return component.getUniqueId();
112     }
113
114     public Either<String, ActionStatus> doAction(String componentId, ComponentTypeEnum componentType, String userId, String action, String uuid,
115                                                  String componentInstanceName, String objectType, String ref1, String ref2) {
116         accessValidations.validateUserCanWorkOnComponent(componentId, componentType, userId, action + " EXTERNAL REF");
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     private Map<GraphPropertyEnum, Object> createPropsToMatch(ComponentTypeEnum componentType) {
130         Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
131         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
132         return propertiesToMatch;
133     }
134 }