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