56dec5d93aeac0fa5c8f4577fd10f03b0f1fa9b0
[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
21 package org.openecomp.sdc.be.components.attribute;
22
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;
38
39 @org.springframework.stereotype.Component
40 public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {
41
42   private final ToscaOperationFacade toscaOperationFacade;
43   private final AttributeBusinessLogic attributeBusinessLogic;
44
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;
52   }
53
54   @Override
55   public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
56     return new AttributeDataDefinition(attributeDataDefinition);
57   }
58
59   @Override
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());
70         }
71       }
72     }
73     return Either.left(attributetypeList);
74   }
75
76   @Override
77   public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
78     return Optional.of(component);
79   }
80
81   @Override
82   public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component,
83                                                              final OutputDefinition output) {
84     AttributeDefinition attributeDefinition = new AttributeDefinition(output);
85
86     // TODO - do we need this one
87     if (attributeBusinessLogic.isAttributeUsedByOperation(component, attributeDefinition)) {
88       return StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION;
89     }
90
91     Optional<AttributeDefinition> attributeToUpdateCandidate =
92         getDeclaredAttributeByOutputId(component, output.getUniqueId());
93
94     if (attributeToUpdateCandidate.isPresent()) {
95       AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
96       return unDeclareOutput(component, output, attributeToUpdate);
97     }
98
99     return StorageOperationStatus.OK;
100   }
101
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();
111     }
112
113     return StorageOperationStatus.OK;
114   }
115
116   private Optional<AttributeDefinition> getDeclaredAttributeByOutputId(final Component component, final String outputId) {
117     List<AttributeDefinition> attributes = component.getAttributes();
118
119     if (CollectionUtils.isEmpty(attributes)) {
120       return Optional.empty();
121     }
122
123     for (AttributeDefinition attributeDefinition : attributes) {
124       List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
125       if (CollectionUtils.isEmpty(getOutputValues)) {
126         continue;
127       }
128
129       Optional<GetOutputValueDataDefinition> getOutputCandidate =
130           getOutputValues.stream().filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();
131
132       if (getOutputCandidate.isPresent()) {
133         return Optional.of(attributeDefinition);
134       }
135     }
136
137     return Optional.empty();
138   }
139
140 }