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=========================================================
20 package org.openecomp.sdc.be.components.impl;
22 import fj.data.Either;
23 import java.util.HashMap;
24 import java.util.List;
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;
40 * Created by yavivi on 04/02/2018.
42 @org.springframework.stereotype.Component
43 public class ExternalRefsBusinessLogic {
45 private final ExternalReferencesOperation externalReferencesOperation;
46 private final ToscaOperationFacade toscaOperationFacade;
47 private final AccessValidations accessValidations;
48 private final ComponentLocker componentLocker;
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;
58 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName,
60 Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
61 if (componentsResult == null || componentsResult.isRight()) {
62 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
64 Component component = componentsResult.left().value();
65 return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
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);
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());
79 return Either.left(externalReferencesResult.left().value());
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(), "");
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, "");
96 public Either<String, ActionStatus> updateExternalReference(String componentId, ComponentTypeEnum componentType, String userId,
97 String componentInstanceName, String objectType, String oldRefValue,
99 return this.doAction(componentId, componentType, userId, "PUT", componentId, componentInstanceName, objectType, oldRefValue, newRefValue);
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);
108 //Get Component Unique ID
109 Component component = latestServiceByUuid.left().value();
110 return component.getUniqueId();
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");
118 return this.externalReferencesOperation.addExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
120 return this.externalReferencesOperation.updateExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1, ref2);
122 return this.externalReferencesOperation.deleteExternalReferenceWithCommit(componentId, componentInstanceName, objectType, ref1);
124 return Either.right(ActionStatus.GENERAL_ERROR);
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;