Fix 'Unable to delete declared outputs'
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / attribute / ComponentInstanceOutputAttributeDeclarator.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 static org.apache.commons.collections4.CollectionUtils.isEmpty;
23
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
30 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.ComponentInstance;
33 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
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.common.log.wrappers.Logger;
38
39 @org.springframework.stereotype.Component
40 public class ComponentInstanceOutputAttributeDeclarator extends DefaultAttributeDeclarator<ComponentInstance, ComponentInstanceOutput> {
41
42     private static final Logger log = Logger.getLogger(ComponentInstanceOutputAttributeDeclarator.class);
43     private final ToscaOperationFacade toscaOperationFacade;
44     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
45
46     public ComponentInstanceOutputAttributeDeclarator(final ToscaOperationFacade toscaOperationFacade,
47                                                       final ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
48         this.toscaOperationFacade = toscaOperationFacade;
49         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
50     }
51
52     @Override
53     public ComponentInstanceOutput createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
54         return new ComponentInstanceOutput(attributeDataDefinition);
55     }
56
57     @Override
58     public Either<?, StorageOperationStatus> updateAttributesValues(final Component component, final String cmptInstanceId,
59                                                                     final List<ComponentInstanceOutput> attributetypeList) {
60         log.debug("#updateAttributesValues - updating component instance outputs for instance {} on component {}", cmptInstanceId,
61             component.getUniqueId());
62         Map<String, List<ComponentInstanceOutput>> instAttributes = Collections.singletonMap(cmptInstanceId, attributetypeList);
63         return toscaOperationFacade.addComponentInstanceOutputsToComponent(component, instAttributes);
64     }
65
66     @Override
67     public Optional<ComponentInstance> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
68         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
69         return component.getComponentInstanceById(propertiesOwnerId);
70     }
71
72     public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
73         List<ComponentInstanceOutput> componentInstanceInputsByInputId = componentInstanceBusinessLogic
74             .getComponentInstanceOutputsByOutputId(component, output.getUniqueId());
75         if (isEmpty(componentInstanceInputsByInputId)) {
76             return StorageOperationStatus.OK;
77         }
78         return toscaOperationFacade.updateComponentInstanceOutputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(),
79             componentInstanceInputsByInputId);
80     }
81 }