Fix 'Unable to delete declared outputs'
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / attribute / ComponentAttributeDeclarator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.components.attribute;
21
22 import fj.data.Either;
23 import java.util.List;
24 import java.util.Optional;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
28 import org.openecomp.sdc.be.model.AttributeDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.OutputDefinition;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33
34 @org.springframework.stereotype.Component
35 public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {
36
37     private final ToscaOperationFacade toscaOperationFacade;
38
39     public ComponentAttributeDeclarator(final ToscaOperationFacade toscaOperationFacade) {
40         this.toscaOperationFacade = toscaOperationFacade;
41     }
42
43     @Override
44     public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
45         return new AttributeDataDefinition(attributeDataDefinition);
46     }
47
48     @Override
49     public Either<?, StorageOperationStatus> updateAttributesValues(final Component component, final String propertiesOwnerId,
50                                                                     final List<AttributeDataDefinition> attributetypeList) {
51         if (CollectionUtils.isNotEmpty(attributetypeList)) {
52             for (AttributeDataDefinition attribute : attributetypeList) {
53                 Either<AttributeDefinition, StorageOperationStatus> storageStatus = toscaOperationFacade
54                     .updateAttributeOfComponent(component, new AttributeDefinition(attribute));
55                 if (storageStatus.isRight()) {
56                     return Either.right(storageStatus.right().value());
57                 }
58             }
59         }
60         return Either.left(attributetypeList);
61     }
62
63     @Override
64     public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
65         return Optional.of(component);
66     }
67
68     @Override
69     public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
70         final Optional<AttributeDefinition> attributeToUpdateCandidate = getDeclaredAttributeByOutputId(component, output.getUniqueId());
71         if (attributeToUpdateCandidate.isPresent()) {
72             AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
73             return unDeclareOutput(component, output, attributeToUpdate);
74         }
75         return StorageOperationStatus.OK;
76     }
77
78     private StorageOperationStatus unDeclareOutput(final Component component, final OutputDefinition output,
79                                                    final AttributeDefinition attributeToUpdate) {
80         attributeToUpdate.setValue(output.getDefaultValue());
81         Either<AttributeDefinition, StorageOperationStatus> status = toscaOperationFacade.updateAttributeOfComponent(component, attributeToUpdate);
82         if (status.isRight()) {
83             return status.right().value();
84         }
85         return StorageOperationStatus.OK;
86     }
87
88     private Optional<AttributeDefinition> getDeclaredAttributeByOutputId(final Component component, final String outputId) {
89         List<AttributeDefinition> attributes = component.getAttributes();
90         if (CollectionUtils.isEmpty(attributes)) {
91             return Optional.empty();
92         }
93         for (AttributeDefinition attributeDefinition : attributes) {
94             List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
95             if (CollectionUtils.isEmpty(getOutputValues)) {
96                 continue;
97             }
98             Optional<GetOutputValueDataDefinition> getOutputCandidate = getOutputValues.stream()
99                 .filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();
100             if (getOutputCandidate.isPresent()) {
101                 return Optional.of(attributeDefinition);
102             }
103         }
104         return Optional.empty();
105     }
106 }