7ded7c1209c8e8d18bd9b1ae69500b56738a771e
[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
21 package org.openecomp.sdc.be.components.attribute;
22
23 import static org.apache.commons.collections4.CollectionUtils.isEmpty;
24
25 import fj.data.Either;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
31 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
36 import org.openecomp.sdc.be.model.OutputDefinition;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.model.operations.impl.AttributeOperation;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41
42 @org.springframework.stereotype.Component
43 public class ComponentInstanceOutputAttributeDeclarator extends DefaultAttributeDeclarator<ComponentInstance, ComponentInstanceOutput> {
44
45     private static final Logger log = Logger.getLogger(ComponentInstanceOutputAttributeDeclarator.class);
46     private final ToscaOperationFacade toscaOperationFacade;
47     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
48
49     public ComponentInstanceOutputAttributeDeclarator(final ComponentsUtils componentsUtils,
50                                                       final AttributeOperation attributeOperation,
51                                                       final ToscaOperationFacade toscaOperationFacade,
52                                                       final ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
53 //        super(componentsUtils, attributeOperation);
54         this.toscaOperationFacade = toscaOperationFacade;
55         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
56     }
57
58     @Override
59     public ComponentInstanceOutput createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
60         return new ComponentInstanceOutput(attributeDataDefinition);
61     }
62
63     @Override
64     public Either<?, StorageOperationStatus> updateAttributesValues(final Component component,
65                                                                     final String cmptInstanceId,
66                                                                     final List<ComponentInstanceOutput> attributetypeList) {
67         log.debug("#updateAttributesValues - updating component instance outputs for instance {} on component {}", cmptInstanceId,
68             component.getUniqueId());
69         Map<String, List<ComponentInstanceOutput>> instAttributes = Collections.singletonMap(cmptInstanceId, attributetypeList);
70         return toscaOperationFacade.addComponentInstanceOutputsToComponent(component, instAttributes);
71     }
72
73     @Override
74     public Optional<ComponentInstance> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
75         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
76         return component.getComponentInstanceById(propertiesOwnerId);
77     }
78
79     public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
80         List<ComponentInstanceOutput> componentInstanceInputsByInputId = componentInstanceBusinessLogic
81             .getComponentInstanceOutputsByOutputId(component, output
82                 .getUniqueId());
83         if (isEmpty(componentInstanceInputsByInputId)) {
84             return StorageOperationStatus.OK;
85         }
86         componentInstanceInputsByInputId
87             .forEach(cmptInstanceInput -> prepareValueBeforeDelete(output, cmptInstanceInput, cmptInstanceInput.getPath()));
88         return toscaOperationFacade.updateComponentInstanceOutputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(),
89             componentInstanceInputsByInputId);
90     }
91
92 }