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 fj.data.Either;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.Optional;
29 import org.apache.commons.collections.CollectionUtils;
30 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
31 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.CapabilityDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentInstance;
37 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
38 import org.openecomp.sdc.be.model.InputDefinition;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
44 @org.springframework.stereotype.Component
45 public class ComponentInstancePropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceProperty> {
47 private static final Logger log = Logger.getLogger(ComponentInstancePropertyDeclarator.class);
48 private ToscaOperationFacade toscaOperationFacade;
49 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
51 public ComponentInstancePropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
52 super(componentsUtils, propertyOperation);
53 this.toscaOperationFacade = toscaOperationFacade;
54 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
58 public ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
59 return new ComponentInstanceProperty(prop);
63 public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceProperty> properties) {
64 log.debug("#updatePropertiesValues - updating component instance properties for instance {} on component {}", cmptInstanceId, component.getUniqueId());
65 Map<String, List<ComponentInstanceProperty>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
66 return toscaOperationFacade.addComponentInstancePropertiesToComponent(component, instProperties);
70 public Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
71 log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
72 return component.getComponentInstanceById(propertiesOwnerId);
76 public void addPropertiesListToInput(ComponentInstanceProperty declaredProp, InputDefinition input) {
77 List<ComponentInstanceProperty> propertiesList = input.getProperties();
78 if(propertiesList == null) {
79 propertiesList = new ArrayList<>(); // adding the property with the new value for UI
81 propertiesList.add(declaredProp);
82 input.setProperties(propertiesList);
86 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
88 Optional<ComponentInstanceProperty> propertyByInputId = PropertiesUtils.getPropertyByInputId(component,
90 if(propertyByInputId.isPresent()) {
91 List<ComponentInstanceProperty> capabilityPropertyDeclaredAsInput =
92 PropertiesUtils.getCapabilityProperty(propertyByInputId.get(), input.getUniqueId());
93 capabilityPropertyDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDeleteOfCapProp(input,
94 cmptInstanceProperty));
96 Optional<CapabilityDefinition> propertyCapabilityOptional = PropertiesUtils.getPropertyCapabilityOfChildInstance(
97 capabilityPropertyDeclaredAsInput.get(0).getParentUniqueId(), component.getCapabilities());
98 if(!propertyCapabilityOptional.isPresent()) {
99 return StorageOperationStatus.OK;
102 return toscaOperationFacade.updateInstanceCapabilityProperty(component, input.getInstanceUniqueId(),
103 capabilityPropertyDeclaredAsInput.get(0), propertyCapabilityOptional.get() );
105 List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic
106 .getComponentInstancePropertiesByInputId(component, input.getUniqueId());
107 if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
108 return StorageOperationStatus.OK;
110 componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input,
111 cmptInstanceProperty, cmptInstanceProperty.getPath()));
112 return toscaOperationFacade.updateComponentInstanceProperties(component,
113 componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(),
114 componentInstancePropertiesDeclaredAsInput);
119 public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
120 List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, input.getUniqueId());
121 if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
122 return StorageOperationStatus.OK;
124 componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input, cmptInstanceProperty, cmptInstanceProperty.getPath()));
125 return toscaOperationFacade.updateComponentInstanceProperties(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(), componentInstancePropertiesDeclaredAsInput);