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.validation.AccessValidations;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
28 import org.openecomp.sdc.be.dto.ExternalRefDTO;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.LifecycleStateEnum;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import java.util.HashMap;
37 import java.util.List;
41 * Created by yavivi on 04/02/2018.
43 @org.springframework.stereotype.Component
44 public class ExternalRefsBusinessLogic {
46 private static final Logger log = Logger.getLogger(ExternalRefsBusinessLogic.class);
48 private final ExternalReferencesOperation externalReferencesOperation;
49 private final ToscaOperationFacade toscaOperationFacade;
50 private final AccessValidations accessValidations;
51 private final ComponentLocker componentLocker;
53 public ExternalRefsBusinessLogic(ExternalReferencesOperation externalReferencesOperation, ToscaOperationFacade toscaOperationFacade, AccessValidations accessValidations, ComponentLocker componentLocker) {
54 this.externalReferencesOperation = externalReferencesOperation;
55 this.toscaOperationFacade = toscaOperationFacade;
56 this.accessValidations = accessValidations;
57 this.componentLocker = componentLocker;
60 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String version, String componentInstanceName, 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);
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);
75 Component component = componentsResult.left().value();
77 Either<Map<String, List<String>>, ActionStatus> externalReferencesResult = this.externalReferencesOperation.getExternalReferences(component.getUniqueId(), objectType);
78 if (externalReferencesResult.isRight()){
79 return Either.right(externalReferencesResult.right().value());
81 return Either.left(externalReferencesResult.left().value());
85 public Either<String, ActionStatus> addExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, ExternalRefDTO ref) {
86 return this.doAction(componentType, userId, "POST", uuid, componentInstanceName, objectType, ref.getReferenceUUID(), "");
89 public Either<String, ActionStatus> deleteExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String reference) {
90 return this.doAction(componentType, userId, "DELETE", uuid, componentInstanceName, objectType, reference, "");
93 public Either<String, ActionStatus> updateExternalReference(ComponentTypeEnum componentType, String userId, String uuid, String componentInstanceName, String objectType, String oldRefValue, String newRefValue) {
94 return this.doAction(componentType, userId, "PUT", uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
97 private Either<String, ActionStatus> doAction(ComponentTypeEnum componentType, String userId, String action, String uuid, String componentInstanceName, String objectType, String ref1, String ref2){
98 Either<Component, StorageOperationStatus> latestServiceByUuid = toscaOperationFacade.getLatestComponentByUuid(uuid, createPropsToMatch(componentType));
99 if (latestServiceByUuid == null || latestServiceByUuid.isRight()){
100 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
103 //Get Component Unique ID
104 Component component = latestServiceByUuid.left().value();
105 String uniqueId = component.getUniqueId();
108 this.componentLocker.lock(component);
109 this.accessValidations.validateUserCanWorkOnComponent(component, userId, action + " EXTERNAL REF");
111 Either<String, ActionStatus> opResult = Either.right(ActionStatus.GENERAL_ERROR);
115 opResult = this.externalReferencesOperation.addExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
118 opResult = this.externalReferencesOperation.updateExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1, ref2);
121 opResult = this.externalReferencesOperation.deleteExternalReferenceWithCommit(uniqueId, componentInstanceName, objectType, ref1);
126 } catch (Exception e) {
127 opResult = Either.right(ActionStatus.GENERAL_ERROR);
128 log.error("Failed to execute external ref action:{} on asset:{} component:{} objectType:{}", action, uuid, componentInstanceName, objectType);
129 log.error("Cause is:" , e);
132 this.componentLocker.unlock(uniqueId, componentType);
137 private Map<GraphPropertyEnum, Object> createPropsToMatch(ComponentTypeEnum componentType) {
138 Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<>();
139 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
140 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
141 return propertiesToMatch;