2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.config.BeEcompErrorManager;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
27 import org.openecomp.sdc.be.model.DataTypeDefinition;
28 import org.openecomp.sdc.be.model.PropertyDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
32 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
33 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
34 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
35 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
38 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
44 import java.util.List;
46 import java.util.Objects;
47 import java.util.Optional;
50 * This class holds the business logic relevant for attributes manipulation.
55 @Component("attributeBusinessLogic")
56 public class AttributeBusinessLogic extends BaseBusinessLogic {
58 private static final String CREATE_ATTRIBUTE = "CreateAttribute";
59 private static final String UPDATE_ATTRIBUTE = "UpdateAttribute";
60 private static final String DELETE_ATTRIBUTE = "DeleteAttribute";
62 private static final Logger log = Logger.getLogger(AttributeBusinessLogic.class.getName());
63 private static final String FAILED_TO_LOCK_COMPONENT_ERROR = "Failed to lock component {}. Error - {}";
66 public AttributeBusinessLogic(IElementOperation elementDao,
67 IGroupOperation groupOperation,
68 IGroupInstanceOperation groupInstanceOperation,
69 IGroupTypeOperation groupTypeOperation,
70 InterfaceOperation interfaceOperation,
71 InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
72 ArtifactsOperations artifactToscaOperation) {
73 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
74 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
78 * Created attribute on the resource with resourceId
81 * @param newAttributeDef
83 * @return AttributeDefinition if created successfully Or ResponseFormat
85 public Either<PropertyDefinition, ResponseFormat> createAttribute(String resourceId, PropertyDefinition newAttributeDef, String userId) {
86 Either<PropertyDefinition, ResponseFormat> result = null;
87 validateUserExists(userId);
89 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
90 if (lockResult != StorageOperationStatus.OK) {
91 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(CREATE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
92 log.info(FAILED_TO_LOCK_COMPONENT_ERROR, resourceId, lockResult);
93 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
97 // Get the resource from DB
98 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.getToscaElement(resourceId);
99 if (status.isRight()) {
100 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
102 Resource resource = status.left().value();
104 // verify that resource is checked-out and the user is the last updater
105 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
106 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
109 // verify attribute does not exist in resource
110 if (isAttributeExist(resource.getAttributes(), resourceId, newAttributeDef.getName())) {
111 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_ALREADY_EXIST, newAttributeDef.getName()));
113 Map<String, DataTypeDefinition> eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache);
114 // validate property default values
115 Either<Boolean, ResponseFormat> defaultValuesValidation = validatePropertyDefaultValue(newAttributeDef, eitherAllDataTypes);
116 if (defaultValuesValidation.isRight()) {
117 return Either.right(defaultValuesValidation.right().value());
120 handleDefaultValue(newAttributeDef, eitherAllDataTypes);
122 // add the new attribute to resource on graph
123 // need to get StorageOpaerationStatus and convert to ActionStatus from
125 Either<PropertyDefinition, StorageOperationStatus> either = toscaOperationFacade.addAttributeOfResource(resource, newAttributeDef);
126 if (either.isRight()) {
127 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(either.right().value()), resource.getName()));
130 result = Either.left(either.left().value());
134 commitOrRollback(result);
135 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
140 private boolean isAttributeExist(List<PropertyDefinition> attributes, String resourceUid, String propertyName) {
141 boolean isExist = false;
142 if (attributes != null) {
143 isExist = attributes.stream().anyMatch(p -> Objects.equals(p.getName(), propertyName) && Objects.equals(p.getParentUniqueId(), resourceUid));
155 public Either<PropertyDefinition, ResponseFormat> getAttribute(String resourceId, String attributeId, String userId) {
157 validateUserExists(userId);
159 // Get the resource from DB
160 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.getToscaElement(resourceId);
161 if (status.isRight()) {
162 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
164 Resource resource = status.left().value();
166 List<PropertyDefinition> attributes = resource.getAttributes();
167 if (attributes == null) {
168 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, ""));
170 // verify attribute exist in resource
171 Optional<PropertyDefinition> optionalAtt = attributes.stream().filter(att -> att.getUniqueId().equals(attributeId) && att.getParentUniqueId().equals(resourceId)).findAny();
172 return optionalAtt.<Either<PropertyDefinition, ResponseFormat>>map(Either::left).orElseGet(() -> Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, "")));
178 * Updates Attribute on resource
186 public Either<PropertyDefinition, ResponseFormat> updateAttribute(String resourceId, String attributeId, PropertyDefinition newAttDef, String userId) {
187 Either<PropertyDefinition, ResponseFormat> result = null;
189 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
190 if (lockResult != StorageOperationStatus.OK) {
191 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(UPDATE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
192 log.info(FAILED_TO_LOCK_COMPONENT_ERROR, resourceId, lockResult);
193 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
196 // Get the resource from DB
197 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(resourceId);
198 if (eitherResource.isRight()) {
199 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
201 Resource resource = eitherResource.left().value();
203 // verify that resource is checked-out and the user is the last updater
204 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
205 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
208 // verify attribute exist in resource
209 Either<PropertyDefinition, ResponseFormat> eitherAttribute = getAttribute(resourceId, attributeId, userId);
210 if (eitherAttribute.isRight()) {
211 return Either.right(eitherAttribute.right().value());
213 Map<String, DataTypeDefinition> eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache);
215 // validate attribute default values
216 Either<Boolean, ResponseFormat> defaultValuesValidation = validatePropertyDefaultValue(newAttDef, eitherAllDataTypes);
217 if (defaultValuesValidation.isRight()) {
218 return Either.right(defaultValuesValidation.right().value());
220 // add the new property to resource on graph
222 StorageOperationStatus validateAndUpdateAttribute = propertyOperation.validateAndUpdateProperty(newAttDef, eitherAllDataTypes);
223 if (validateAndUpdateAttribute != StorageOperationStatus.OK) {
224 log.debug("Problem while updating attribute with id {}. Reason - {}", attributeId, validateAndUpdateAttribute);
225 result = Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(validateAndUpdateAttribute), resource.getName()));
229 Either<PropertyDefinition, StorageOperationStatus> eitherAttUpdate = toscaOperationFacade.updateAttributeOfResource(resource, newAttDef);
231 if (eitherAttUpdate.isRight()) {
232 log.debug("Problem while updating attribute with id {}. Reason - {}", attributeId, eitherAttUpdate.right().value());
233 result = Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherAttUpdate.right().value()), resource.getName()));
237 result = Either.left(eitherAttUpdate.left().value());
240 commitOrRollback(result);
241 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
247 * Deletes Attribute on resource
254 public Either<PropertyDefinition, ResponseFormat> deleteAttribute(String resourceId, String attributeId, String userId) {
256 Either<PropertyDefinition, ResponseFormat> result = null;
258 validateUserExists(userId);
260 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
261 if (lockResult != StorageOperationStatus.OK) {
262 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(DELETE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
263 log.info(FAILED_TO_LOCK_COMPONENT_ERROR, resourceId, lockResult);
264 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
268 // Get the resource from DB
269 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(resourceId);
270 if (eitherResource.isRight()) {
271 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
273 Resource resource = eitherResource.left().value();
275 // verify that resource is checked-out and the user is the last updater
276 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
277 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
280 // verify attribute exist in resource
281 Either<PropertyDefinition, ResponseFormat> eitherAttributeExist = getAttribute(resourceId, attributeId, userId);
282 if (eitherAttributeExist.isRight()) {
283 return Either.right(eitherAttributeExist.right().value());
285 String attributeName = eitherAttributeExist.left().value().getName();
287 // delete attribute of resource from graph
288 StorageOperationStatus eitherAttributeDelete = toscaOperationFacade.deleteAttributeOfResource(resource, attributeName);
289 if (eitherAttributeDelete != StorageOperationStatus.OK) {
290 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(eitherAttributeDelete), resource.getName()));
294 result = Either.left(eitherAttributeExist.left().value());
297 commitOrRollback(result);
298 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);