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=========================================================
21 package org.openecomp.sdc.be.components.attribute;
23 import fj.data.Either;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
29 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.AttributeDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.OutputDefinition;
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.be.model.operations.impl.AttributeOperation;
39 @org.springframework.stereotype.Component
40 public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {
42 private final ToscaOperationFacade toscaOperationFacade;
43 private final AttributeBusinessLogic attributeBusinessLogic;
45 public ComponentAttributeDeclarator(final ComponentsUtils componentsUtils,
46 final AttributeOperation attributeOperation,
47 final ToscaOperationFacade toscaOperationFacade,
48 final AttributeBusinessLogic attributeBusinessLogic) {
49 // super(componentsUtils, attributeOperation);
50 this.toscaOperationFacade = toscaOperationFacade;
51 this.attributeBusinessLogic = attributeBusinessLogic;
55 public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
56 return new AttributeDataDefinition(attributeDataDefinition);
60 public Either<?, StorageOperationStatus> updateAttributesValues(final Component component,
61 final String propertiesOwnerId,
62 final List<AttributeDataDefinition> attributetypeList) {
63 if (CollectionUtils.isNotEmpty(attributetypeList)) {
64 for (AttributeDataDefinition attribute : attributetypeList) {
65 Either<AttributeDefinition, StorageOperationStatus>
66 storageStatus = toscaOperationFacade
67 .updateAttributeOfComponent(component, new AttributeDefinition(attribute));
68 if (storageStatus.isRight()) {
69 return Either.right(storageStatus.right().value());
73 return Either.left(attributetypeList);
77 public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
78 return Optional.of(component);
82 public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component,
83 final OutputDefinition output) {
84 AttributeDefinition attributeDefinition = new AttributeDefinition(output);
86 // TODO - do we need this one
87 if (attributeBusinessLogic.isAttributeUsedByOperation(component, attributeDefinition)) {
88 return StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION;
91 Optional<AttributeDefinition> attributeToUpdateCandidate =
92 getDeclaredAttributeByOutputId(component, output.getUniqueId());
94 if (attributeToUpdateCandidate.isPresent()) {
95 AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
96 return unDeclareOutput(component, output, attributeToUpdate);
99 return StorageOperationStatus.OK;
102 private StorageOperationStatus unDeclareOutput(final Component component,
103 final OutputDefinition output,
104 final AttributeDefinition attributeToUpdate) {
105 prepareValueBeforeDelete(output, attributeToUpdate, Collections.emptyList());
106 attributeToUpdate.setValue(output.getDefaultValue());
107 Either<AttributeDefinition, StorageOperationStatus> status = toscaOperationFacade
108 .updateAttributeOfComponent(component, attributeToUpdate);
109 if (status.isRight()) {
110 return status.right().value();
113 return StorageOperationStatus.OK;
116 private Optional<AttributeDefinition> getDeclaredAttributeByOutputId(final Component component, final String outputId) {
117 List<AttributeDefinition> attributes = component.getAttributes();
119 if (CollectionUtils.isEmpty(attributes)) {
120 return Optional.empty();
123 for (AttributeDefinition attributeDefinition : attributes) {
124 List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
125 if (CollectionUtils.isEmpty(getOutputValues)) {
129 Optional<GetOutputValueDataDefinition> getOutputCandidate =
130 getOutputValues.stream().filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();
132 if (getOutputCandidate.isPresent()) {
133 return Optional.of(attributeDefinition);
137 return Optional.empty();