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 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;
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;
59 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName,
61 Either<Component, StorageOperationStatus> componentsResult = toscaOperationFacade.getComponentByUuidAndVersion(assetUuid, version);
62 if (componentsResult == null || componentsResult.isRight()) {
63 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
65 Component component = componentsResult.left().value();
66 return this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), componentInstanceName, objectType);
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);
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());
80 return Either.left(externalReferencesResult.left().value());
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(), "");
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, "");
97 public Either<String, ActionStatus> updateExternalReference(String componentId, ComponentTypeEnum componentType, String userId,
98 String componentInstanceName, String objectType, String oldRefValue,
100 return this.doAction(componentId, componentType, userId, "PUT", componentId, componentInstanceName, objectType, oldRefValue, newRefValue);
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);
109 //Get Component Unique ID
110 Component component = latestServiceByUuid.left().value();
111 return component.getUniqueId();
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");
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);
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;