2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
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;
37 import java.util.HashMap;
38 import java.util.List;
42 * Created by yavivi on 04/02/2018.
44 @org.springframework.stereotype.Component
45 public class ExternalRefsBusinessLogic {
47 private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
49 private final ExternalReferencesOperation externalReferencesOperation;
50 private final ToscaOperationFacade toscaOperationFacade;
51 private final AccessValidations accessValidations;
52 private final ComponentLocker componentLocker;
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;
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);
66 Component component = componentsResult.left().value();
67 return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
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);
76 Component component = componentsResult.left().value();
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());
82 return Either.left(externalReferencesResult.left().value());
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(), "");
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, "");
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);
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);
107 //Get Component Unique ID
108 Component component = latestServiceByUuid.left().value();
109 return component.getUniqueId();
113 public Either<String, ActionStatus> doAction(String componentId, ComponentTypeEnum componentType, String userId, String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
115 accessValidations.validateUserCanWorkOnComponent(componentId, componentType, userId, action + " EXTERNAL REF");
119 return this.externalReferencesOperation.addExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
121 return this.externalReferencesOperation.updateExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1, ref2);
123 return this.externalReferencesOperation.deleteExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
125 return Either.right(ActionStatus.GENERAL_ERROR);
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;