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