Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / ComponentInstanceInputPropertyDeclarator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. 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.property;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
26 import org.openecomp.sdc.be.datatypes.elements.Annotation;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.ComponentInstance;
31 import org.openecomp.sdc.be.model.ComponentInstanceInput;
32 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
33 import org.openecomp.sdc.be.model.ComponentParametersView;
34 import org.openecomp.sdc.be.model.InputDefinition;
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.PropertyOperation;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Optional;
45
46 import static org.apache.commons.collections.CollectionUtils.isEmpty;
47 import static org.openecomp.sdc.be.model.utils.ComponentUtilities.getInputAnnotations;
48
49 @org.springframework.stereotype.Component
50 public class ComponentInstanceInputPropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceInput> {
51
52     private static final Logger log = Logger.getLogger(ComponentInstanceInputPropertyDeclarator.class);
53     private final ToscaOperationFacade toscaOperationFacade;
54     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
55     private final ExceptionUtils exceptionUtils;
56
57     public ComponentInstanceInputPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, ExceptionUtils exceptionUtils) {
58         super(componentsUtils, propertyOperation);
59         this.toscaOperationFacade = toscaOperationFacade;
60         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
61         this.exceptionUtils = exceptionUtils;
62     }
63
64     @Override
65     public ComponentInstanceInput createDeclaredProperty(PropertyDataDefinition prop) {
66         return new ComponentInstanceInput(prop);
67     }
68
69     @Override
70     public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceInput> properties) {
71         log.debug("#updatePropertiesValues - updating component instance inputs for instance {} on component {}", cmptInstanceId, component.getUniqueId());
72         Map<String, List<ComponentInstanceInput>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
73         return toscaOperationFacade.addComponentInstanceInputsToComponent(component, instProperties);
74     }
75
76     @Override
77     public Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
78         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
79         return component.getComponentInstanceById(propertiesOwnerId);
80     }
81
82     @Override
83     public void addPropertiesListToInput(ComponentInstanceInput declaredProp, InputDefinition input) {
84         List<ComponentInstanceInput> inputsValueList = input.getInputs();
85         if(inputsValueList == null) {
86             inputsValueList = new ArrayList<>(); // adding the property with the new value for UI
87         }
88         inputsValueList.add(declaredProp);
89         input.setInputs(inputsValueList);
90     }
91
92     @Override
93     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
94         List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
95         if (isEmpty(componentInstanceInputsByInputId)) {
96             return StorageOperationStatus.OK;
97         }
98         componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
99         return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
100     }
101
102     @Override
103     public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
104         List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
105         if (isEmpty(componentInstanceInputsByInputId)) {
106             return StorageOperationStatus.OK;
107         }
108         componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
109         return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
110     }
111
112     @Override
113     InputDefinition createInputFromProperty(String componentId, ComponentInstance propertiesOwner, String inputName, ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
114         InputDefinition inputFromProperty = super.createInputFromProperty(componentId, propertiesOwner, inputName, propInput, prop);
115         Component propertiesOwnerNodeType = getInstanceOriginType(propertiesOwner);
116         enrichInputWithAnnotations(prop, inputFromProperty, propertiesOwnerNodeType);
117         return inputFromProperty;
118     }
119
120     private void enrichInputWithAnnotations(PropertyDataDefinition prop, InputDefinition inputFromProperty, Component propertiesOwnerNodeType) {
121         List<Annotation> inputAnnotations = getInputAnnotations(propertiesOwnerNodeType, prop.getName());
122         if(!isEmpty(inputAnnotations)){
123             inputFromProperty.setAnnotations(inputAnnotations);
124         }
125     }
126
127     private Component getInstanceOriginType(ComponentInstance propertiesOwner) {
128         return toscaOperationFacade.getToscaElement(propertiesOwner.getActualComponentUid(), getFilterComponentInputs())
129                     .left()
130                     .on(err -> exceptionUtils.rollBackAndThrow(err, propertiesOwner.getActualComponentUid()));
131     }
132
133     private ComponentParametersView getFilterComponentInputs() {
134         ComponentParametersView filterInputs = new ComponentParametersView(true);
135         filterInputs.setIgnoreInputs(false);
136         return filterInputs;
137     }
138 }