4c75624ec1174bfa9818c672f5b55b6aff33f49c
[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.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
33 import org.openecomp.sdc.be.model.OutputDefinition;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37
38 @org.springframework.stereotype.Component
39 public class ComponentInstanceAttributeDeclarator extends DefaultAttributeDeclarator<ComponentInstance, ComponentInstanceAttribute> {
40
41     private static final Logger log = Logger.getLogger(ComponentInstanceAttributeDeclarator.class);
42     private ToscaOperationFacade toscaOperationFacade;
43     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
44
45     public ComponentInstanceAttributeDeclarator(final ToscaOperationFacade toscaOperationFacade,
46                                                 final ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
47         this.toscaOperationFacade = toscaOperationFacade;
48         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
49     }
50
51     @Override
52     public ComponentInstanceAttribute createDeclaredAttribute(AttributeDataDefinition attributeDataDefinition) {
53         return new ComponentInstanceAttribute(attributeDataDefinition);
54     }
55
56     @Override
57     public Either<?, StorageOperationStatus> updateAttributesValues(final Component component, final String cmptInstanceId,
58                                                                     final List<ComponentInstanceAttribute> attributetypeList) {
59         log.debug("#updateAttributesValues - updating component instance attributes for instance {} on component {}", cmptInstanceId,
60             component.getUniqueId());
61         Map<String, List<ComponentInstanceAttribute>> instAttributes = Collections.singletonMap(cmptInstanceId, attributetypeList);
62         return toscaOperationFacade.addComponentInstanceAttributesToComponent(component, instAttributes);
63     }
64
65     @Override
66     public Optional<ComponentInstance> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
67         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
68         return component.getComponentInstanceById(propertiesOwnerId);
69     }
70
71     @Override
72     public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component, final OutputDefinition output) {
73         final List<ComponentInstanceAttribute> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic
74             .getComponentInstanceAttributesByOutputId(component, output.getUniqueId());
75         if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
76             return StorageOperationStatus.OK;
77         }
78         return toscaOperationFacade
79             .updateComponentInstanceAttributes(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(),
80                 componentInstancePropertiesDeclaredAsInput);
81     }
82 }