2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.property;
23 import static org.apache.commons.collections.CollectionUtils.isEmpty;
24 import static org.openecomp.sdc.be.model.utils.ComponentUtilities.getInputAnnotations;
26 import fj.data.Either;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.Optional;
32 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
34 import org.openecomp.sdc.be.datatypes.elements.Annotation;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.ComponentInstanceInput;
40 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
41 import org.openecomp.sdc.be.model.ComponentParametersView;
42 import org.openecomp.sdc.be.model.InputDefinition;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
46 import org.openecomp.sdc.common.log.wrappers.Logger;
48 @org.springframework.stereotype.Component
49 public class ComponentInstanceInputPropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceInput> {
51 private static final Logger log = Logger.getLogger(ComponentInstanceInputPropertyDeclarator.class);
52 private final ToscaOperationFacade toscaOperationFacade;
53 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
54 private final ExceptionUtils exceptionUtils;
56 public ComponentInstanceInputPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, ExceptionUtils exceptionUtils) {
57 super(componentsUtils, propertyOperation);
58 this.toscaOperationFacade = toscaOperationFacade;
59 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
60 this.exceptionUtils = exceptionUtils;
64 public ComponentInstanceInput createDeclaredProperty(PropertyDataDefinition prop) {
65 return new ComponentInstanceInput(prop);
69 public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceInput> properties) {
70 log.debug("#updatePropertiesValues - updating component instance inputs for instance {} on component {}", cmptInstanceId, component.getUniqueId());
71 Map<String, List<ComponentInstanceInput>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
72 return toscaOperationFacade.addComponentInstanceInputsToComponent(component, instProperties);
76 public Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
77 log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
78 return component.getComponentInstanceById(propertiesOwnerId);
82 public void addPropertiesListToInput(ComponentInstanceInput declaredProp, InputDefinition input) {
83 List<ComponentInstanceInput> inputsValueList = input.getInputs();
84 if(inputsValueList == null) {
85 inputsValueList = new ArrayList<>(); // adding the property with the new value for UI
87 inputsValueList.add(declaredProp);
88 input.setInputs(inputsValueList);
92 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
93 List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
94 if (isEmpty(componentInstanceInputsByInputId)) {
95 return StorageOperationStatus.OK;
97 componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
98 return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
102 public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
103 List<ComponentInstanceInput> componentInstanceInputsByInputId = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, input.getUniqueId());
104 if (isEmpty(componentInstanceInputsByInputId)) {
105 return StorageOperationStatus.OK;
107 componentInstanceInputsByInputId.forEach(cmptInstanceInput -> prepareValueBeforeDelete(input, cmptInstanceInput, cmptInstanceInput.getPath()));
108 return toscaOperationFacade.updateComponentInstanceInputs(component, componentInstanceInputsByInputId.get(0).getComponentInstanceId(), componentInstanceInputsByInputId);
112 InputDefinition createInputFromProperty(String componentId, ComponentInstance propertiesOwner, String inputName, ComponentInstancePropInput propInput, PropertyDataDefinition prop) {
113 InputDefinition inputFromProperty = super.createInputFromProperty(componentId, propertiesOwner, inputName, propInput, prop);
114 Component propertiesOwnerNodeType = getInstanceOriginType(propertiesOwner);
115 enrichInputWithAnnotations(prop, inputFromProperty, propertiesOwnerNodeType);
116 return inputFromProperty;
119 private void enrichInputWithAnnotations(PropertyDataDefinition prop, InputDefinition inputFromProperty, Component propertiesOwnerNodeType) {
120 List<Annotation> inputAnnotations = getInputAnnotations(propertiesOwnerNodeType, prop.getName());
121 if(!isEmpty(inputAnnotations)){
122 inputFromProperty.setAnnotations(inputAnnotations);
126 private Component getInstanceOriginType(ComponentInstance propertiesOwner) {
127 return toscaOperationFacade.getToscaElement(propertiesOwner.getActualComponentUid(), getFilterComponentInputs())
129 .on(err -> exceptionUtils.rollBackAndThrow(err, propertiesOwner.getActualComponentUid()));
132 private ComponentParametersView getFilterComponentInputs() {
133 ComponentParametersView filterInputs = new ComponentParametersView(true);
134 filterInputs.setIgnoreInputs(false);