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