8981515ea4e2c1e3e0d723a8e618d7f9507a5faa
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / attribute / ComponentInstanceAttributeDeclarator.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.Map;
27 import java.util.Optional;
28 import org.apache.commons.collections4.CollectionUtils;
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.ComponentInstanceAttribute;
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 ComponentInstanceAttributeDeclarator extends
43     DefaultAttributeDeclarator<ComponentInstance, ComponentInstanceAttribute> {
44
45     private static final Logger log = Logger.getLogger(ComponentInstanceAttributeDeclarator.class);
46     private ToscaOperationFacade toscaOperationFacade;
47     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
48
49     public ComponentInstanceAttributeDeclarator(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 ComponentInstanceAttribute createDeclaredAttribute(AttributeDataDefinition attributeDataDefinition) {
60         return new ComponentInstanceAttribute(attributeDataDefinition);
61     }
62
63     @Override
64     public Either<?, StorageOperationStatus> updateAttributesValues(final Component component,
65                                                                     final String cmptInstanceId,
66                                                                     final List<ComponentInstanceAttribute> attributetypeList) {
67         log.debug("#updateAttributesValues - updating component instance attributes for instance {} on component {}", cmptInstanceId,
68             component.getUniqueId());
69         Map<String, List<ComponentInstanceAttribute>> instAttributes = Collections.singletonMap(cmptInstanceId, attributetypeList);
70         return toscaOperationFacade.addComponentInstanceAttributesToComponent(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     @Override
80     public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
81
82         final List<ComponentInstanceAttribute> componentInstancePropertiesDeclaredAsInput
83             = componentInstanceBusinessLogic.getComponentInstanceAttributesByOutputId(component, output.getUniqueId());
84         if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
85             return StorageOperationStatus.OK;
86         }
87         componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(output,
88             cmptInstanceProperty, cmptInstanceProperty.getPath()));
89         return toscaOperationFacade.updateComponentInstanceAttributes(component,
90             componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(),
91             componentInstancePropertiesDeclaredAsInput);
92     }
93
94 }