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 java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
38 import org.openecomp.sdc.exception.ResponseFormat;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Component;
43 import fj.data.Either;
46 * This class holds the business logic relevant for attributes manipulation.
51 @Component("attributeBusinessLogic")
52 public class AttributeBusinessLogic extends BaseBusinessLogic {
54 private static final String CREATE_ATTRIBUTE = "CreateAttribute";
55 private static final String UPDATE_ATTRIBUTE = "UpdateAttribute";
56 private static final String DELETE_ATTRIBUTE = "DeleteAttribute";
58 private static Logger log = LoggerFactory.getLogger(AttributeBusinessLogic.class.getName());
61 * Created attribute on the resource with resourceId
64 * @param newAttributeDef
66 * @return AttributeDefinition if created successfully Or ResponseFormat
68 public Either<PropertyDefinition, ResponseFormat> createAttribute(String resourceId, PropertyDefinition newAttributeDef, String userId) {
69 Either<PropertyDefinition, ResponseFormat> result = null;
70 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Attribute", false);
72 return Either.right(resp.right().value());
75 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
76 if (lockResult != StorageOperationStatus.OK) {
77 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(CREATE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
78 log.info("Failed to lock component {}. Error - {}", resourceId, lockResult);
79 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
83 // Get the resource from DB
84 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.getToscaElement(resourceId);
85 if (status.isRight()) {
86 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
88 Resource resource = status.left().value();
90 // verify that resource is checked-out and the user is the last updater
91 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
92 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
95 // verify attribute does not exist in resource
96 if (isAttributeExist(resource.getAttributes(), resourceId, newAttributeDef.getName())) {
97 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_ALREADY_EXIST, newAttributeDef.getName()));
99 Either<Map<String, DataTypeDefinition>, ResponseFormat> eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache);
100 if (eitherAllDataTypes.isRight()) {
101 return Either.right(eitherAllDataTypes.right().value());
103 // validate property default values
104 Either<Boolean, ResponseFormat> defaultValuesValidation = validatePropertyDefaultValue(newAttributeDef, eitherAllDataTypes.left().value());
105 if (defaultValuesValidation.isRight()) {
106 return Either.right(defaultValuesValidation.right().value());
109 handleDefaultValue(newAttributeDef, eitherAllDataTypes.left().value());
111 // add the new attribute to resource on graph
112 // need to get StorageOpaerationStatus and convert to ActionStatus from
114 Either<PropertyDefinition, StorageOperationStatus> either = toscaOperationFacade.addAttributeOfResource(resource, newAttributeDef);
115 if (either.isRight()) {
116 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(either.right().value()), resource.getName()));
119 result = Either.left(either.left().value());
123 commitOrRollback(result);
124 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
129 private boolean isAttributeExist(List<PropertyDefinition> attributes, String resourceUid, String propertyName) {
130 boolean isExist = false;
131 if (attributes != null) {
132 isExist = attributes.stream().filter(p -> Objects.equals(p.getName(), propertyName) && Objects.equals(p.getParentUniqueId(), resourceUid)).findAny().isPresent();
144 public Either<PropertyDefinition, ResponseFormat> getAttribute(String resourceId, String attributeId, String userId) {
146 Either<User, ResponseFormat> resp = validateUserExists(userId, "get Attribute", false);
147 if (resp.isRight()) {
148 return Either.right(resp.right().value());
151 // Get the resource from DB
152 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.getToscaElement(resourceId);
153 if (status.isRight()) {
154 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
156 Resource resource = status.left().value();
158 List<PropertyDefinition> attributes = resource.getAttributes();
159 if (attributes == null) {
160 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, ""));
162 Either<PropertyDefinition, ResponseFormat> result;
163 // verify attribute exist in resource
164 Optional<PropertyDefinition> optionalAtt = attributes.stream().filter(att -> att.getUniqueId().equals(attributeId) && att.getParentUniqueId().equals(resourceId)).findAny();
166 if (optionalAtt.isPresent()) {
167 result = Either.left(optionalAtt.get());
169 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, ""));
177 * Updates Attribute on resource
185 public Either<PropertyDefinition, ResponseFormat> updateAttribute(String resourceId, String attributeId, PropertyDefinition newAttDef, String userId) {
186 Either<PropertyDefinition, ResponseFormat> result = null;
188 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
189 if (lockResult != StorageOperationStatus.OK) {
190 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(UPDATE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
191 log.info("Failed to lock component {}. Error - {}", resourceId, lockResult);
192 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
195 // Get the resource from DB
196 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(resourceId);
197 if (eitherResource.isRight()) {
198 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
200 Resource resource = eitherResource.left().value();
202 // verify that resource is checked-out and the user is the last updater
203 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
204 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
207 // verify attribute exist in resource
208 Either<PropertyDefinition, ResponseFormat> eitherAttribute = getAttribute(resourceId, attributeId, userId);
209 if (eitherAttribute.isRight()) {
210 return Either.right(eitherAttribute.right().value());
212 Either<Map<String, DataTypeDefinition>, ResponseFormat> eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache);
213 if (eitherAllDataTypes.isRight()) {
214 return Either.right(eitherAllDataTypes.right().value());
217 // validate attribute default values
218 Either<Boolean, ResponseFormat> defaultValuesValidation = validatePropertyDefaultValue(newAttDef, eitherAllDataTypes.left().value());
219 if (defaultValuesValidation.isRight()) {
220 return Either.right(defaultValuesValidation.right().value());
222 // add the new property to resource on graph
224 StorageOperationStatus validateAndUpdateAttribute = propertyOperation.validateAndUpdateProperty(newAttDef, eitherAllDataTypes.left().value());
225 if (validateAndUpdateAttribute != StorageOperationStatus.OK) {
226 log.debug("Problem while updating attribute with id {}. Reason - {}", attributeId, validateAndUpdateAttribute);
227 result = Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(validateAndUpdateAttribute), resource.getName()));
231 Either<PropertyDefinition, StorageOperationStatus> eitherAttUpdate = toscaOperationFacade.updateAttributeOfResource(resource, newAttDef);
233 if (eitherAttUpdate.isRight()) {
234 log.debug("Problem while updating attribute with id {}. Reason - {}", attributeId, eitherAttUpdate.right().value());
235 result = Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherAttUpdate.right().value()), resource.getName()));
239 result = Either.left(eitherAttUpdate.left().value());
242 commitOrRollback(result);
243 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
249 * Deletes Attribute on resource
256 public Either<PropertyDefinition, ResponseFormat> deleteAttribute(String resourceId, String attributeId, String userId) {
258 Either<PropertyDefinition, ResponseFormat> result = null;
260 Either<User, ResponseFormat> resp = validateUserExists(userId, "delete Attribute", false);
261 if (resp.isRight()) {
262 return Either.right(resp.right().value());
265 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceId, NodeTypeEnum.Resource);
266 if (lockResult != StorageOperationStatus.OK) {
267 BeEcompErrorManager.getInstance().logBeFailedLockObjectError(DELETE_ATTRIBUTE, NodeTypeEnum.Resource.name().toLowerCase(), resourceId);
268 log.info("Failed to lock component {}. Error - {}", resourceId, lockResult);
269 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
273 // Get the resource from DB
274 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(resourceId);
275 if (eitherResource.isRight()) {
276 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, ""));
278 Resource resource = eitherResource.left().value();
280 // verify that resource is checked-out and the user is the last updater
281 if (!ComponentValidationUtils.canWorkOnResource(resource, userId)) {
282 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
285 // verify attribute exist in resource
286 Either<PropertyDefinition, ResponseFormat> eitherAttributeExist = getAttribute(resourceId, attributeId, userId);
287 if (eitherAttributeExist.isRight()) {
288 return Either.right(eitherAttributeExist.right().value());
290 String attributeName = eitherAttributeExist.left().value().getName();
292 // delete attribute of resource from graph
293 StorageOperationStatus eitherAttributeDelete = toscaOperationFacade.deleteAttributeOfResource(resource, attributeName);
294 if (eitherAttributeDelete != StorageOperationStatus.OK) {
295 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(eitherAttributeDelete), resource.getName()));
299 result = Either.left(eitherAttributeExist.left().value());
302 commitOrRollback(result);
303 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);