2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021, Nordix Foundation. 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.attribute;
22 import fj.data.Either;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
28 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.AttributeDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.OutputDefinition;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.impl.AttributeOperation;
38 @org.springframework.stereotype.Component
39 public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {
41 private final ToscaOperationFacade toscaOperationFacade;
42 private final AttributeBusinessLogic attributeBusinessLogic;
44 public ComponentAttributeDeclarator(final ComponentsUtils componentsUtils, final AttributeOperation attributeOperation,
45 final ToscaOperationFacade toscaOperationFacade, final AttributeBusinessLogic attributeBusinessLogic) {
46 // super(componentsUtils, attributeOperation);
47 this.toscaOperationFacade = toscaOperationFacade;
48 this.attributeBusinessLogic = attributeBusinessLogic;
52 public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
53 return new AttributeDataDefinition(attributeDataDefinition);
57 public Either<?, StorageOperationStatus> updateAttributesValues(final Component component, final String propertiesOwnerId,
58 final List<AttributeDataDefinition> attributetypeList) {
59 if (CollectionUtils.isNotEmpty(attributetypeList)) {
60 for (AttributeDataDefinition attribute : attributetypeList) {
61 Either<AttributeDefinition, StorageOperationStatus> storageStatus = toscaOperationFacade
62 .updateAttributeOfComponent(component, new AttributeDefinition(attribute));
63 if (storageStatus.isRight()) {
64 return Either.right(storageStatus.right().value());
68 return Either.left(attributetypeList);
72 public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
73 return Optional.of(component);
77 public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
78 AttributeDefinition attributeDefinition = new AttributeDefinition(output);
79 // TODO - do we need this one
80 if (attributeBusinessLogic.isAttributeUsedByOperation(component, attributeDefinition)) {
81 return StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION;
83 Optional<AttributeDefinition> attributeToUpdateCandidate = getDeclaredAttributeByOutputId(component, output.getUniqueId());
84 if (attributeToUpdateCandidate.isPresent()) {
85 AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
86 return unDeclareOutput(component, output, attributeToUpdate);
88 return StorageOperationStatus.OK;
91 private StorageOperationStatus unDeclareOutput(final Component component, final OutputDefinition output,
92 final AttributeDefinition attributeToUpdate) {
93 prepareValueBeforeDelete(output, attributeToUpdate, Collections.emptyList());
94 attributeToUpdate.setValue(output.getDefaultValue());
95 Either<AttributeDefinition, StorageOperationStatus> status = toscaOperationFacade.updateAttributeOfComponent(component, attributeToUpdate);
96 if (status.isRight()) {
97 return status.right().value();
99 return StorageOperationStatus.OK;
102 private Optional<AttributeDefinition> getDeclaredAttributeByOutputId(final Component component, final String outputId) {
103 List<AttributeDefinition> attributes = component.getAttributes();
104 if (CollectionUtils.isEmpty(attributes)) {
105 return Optional.empty();
107 for (AttributeDefinition attributeDefinition : attributes) {
108 List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
109 if (CollectionUtils.isEmpty(getOutputValues)) {
112 Optional<GetOutputValueDataDefinition> getOutputCandidate = getOutputValues.stream()
113 .filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();
114 if (getOutputCandidate.isPresent()) {
115 return Optional.of(attributeDefinition);
118 return Optional.empty();