9b3487dbc920008f41ad61f633bba11307c7eb6a
[sdc.git] /
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.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;
37
38 @org.springframework.stereotype.Component
39 public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {
40
41     private final ToscaOperationFacade toscaOperationFacade;
42     private final AttributeBusinessLogic attributeBusinessLogic;
43
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;
49     }
50
51     @Override
52     public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
53         return new AttributeDataDefinition(attributeDataDefinition);
54     }
55
56     @Override
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());
65                 }
66             }
67         }
68         return Either.left(attributetypeList);
69     }
70
71     @Override
72     public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
73         return Optional.of(component);
74     }
75
76     @Override
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;
82         }
83         Optional<AttributeDefinition> attributeToUpdateCandidate = getDeclaredAttributeByOutputId(component, output.getUniqueId());
84         if (attributeToUpdateCandidate.isPresent()) {
85             AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
86             return unDeclareOutput(component, output, attributeToUpdate);
87         }
88         return StorageOperationStatus.OK;
89     }
90
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();
98         }
99         return StorageOperationStatus.OK;
100     }
101
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();
106         }
107         for (AttributeDefinition attributeDefinition : attributes) {
108             List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
109             if (CollectionUtils.isEmpty(getOutputValues)) {
110                 continue;
111             }
112             Optional<GetOutputValueDataDefinition> getOutputCandidate = getOutputValues.stream()
113                 .filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();
114             if (getOutputCandidate.isPresent()) {
115                 return Optional.of(attributeDefinition);
116             }
117         }
118         return Optional.empty();
119     }
120 }