Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / ComponentInstancePropertyDeclarator.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.apache.commons.collections.CollectionUtils;
25 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
26 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.CapabilityDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
33 import org.openecomp.sdc.be.model.InputDefinition;
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.be.model.operations.impl.PropertyOperation;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Optional;
44
45 @org.springframework.stereotype.Component
46 public class ComponentInstancePropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceProperty> {
47
48     private static final Logger log = Logger.getLogger(ComponentInstancePropertyDeclarator.class);
49     private ToscaOperationFacade toscaOperationFacade;
50     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
51
52     public ComponentInstancePropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
53         super(componentsUtils, propertyOperation);
54         this.toscaOperationFacade = toscaOperationFacade;
55         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
56     }
57
58     @Override
59     public ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
60         return new ComponentInstanceProperty(prop);
61     }
62
63     @Override
64     public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String cmptInstanceId, List<ComponentInstanceProperty> properties) {
65         log.debug("#updatePropertiesValues - updating component instance properties for instance {} on component {}", cmptInstanceId, component.getUniqueId());
66         Map<String, List<ComponentInstanceProperty>> instProperties = Collections.singletonMap(cmptInstanceId, properties);
67         return toscaOperationFacade.addComponentInstancePropertiesToComponent(component, instProperties);
68     }
69
70     @Override
71     public Optional<ComponentInstance> resolvePropertiesOwner(Component component, String propertiesOwnerId) {
72         log.debug("#resolvePropertiesOwner - fetching component instance {} of component {}", propertiesOwnerId, component.getUniqueId());
73         return component.getComponentInstanceById(propertiesOwnerId);
74     }
75
76     @Override
77     public void addPropertiesListToInput(ComponentInstanceProperty declaredProp, InputDefinition input) {
78         List<ComponentInstanceProperty> propertiesList = input.getProperties();
79         if(propertiesList == null) {
80             propertiesList = new ArrayList<>(); // adding the property with the new value for UI
81         }
82         propertiesList.add(declaredProp);
83         input.setProperties(propertiesList);
84     }
85
86     @Override
87     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
88
89         Optional<ComponentInstanceProperty> propertyByInputId = PropertiesUtils.getPropertyByInputId(component,
90                 input.getUniqueId());
91         if(propertyByInputId.isPresent()) {
92             List<ComponentInstanceProperty> capabilityPropertyDeclaredAsInput =
93                    PropertiesUtils.getCapabilityProperty(propertyByInputId.get(), input.getUniqueId());
94             capabilityPropertyDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDeleteOfCapProp(input,
95                     cmptInstanceProperty));
96
97             Optional<CapabilityDefinition> propertyCapabilityOptional = PropertiesUtils.getPropertyCapabilityOfChildInstance(
98                     capabilityPropertyDeclaredAsInput.get(0).getParentUniqueId(), component.getCapabilities());
99             if(!propertyCapabilityOptional.isPresent()) {
100                 return StorageOperationStatus.OK;
101             }
102
103             return toscaOperationFacade.updateInstanceCapabilityProperty(component, input.getInstanceUniqueId(),
104                     capabilityPropertyDeclaredAsInput.get(0), propertyCapabilityOptional.get() );
105         } else {
106             List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic
107                     .getComponentInstancePropertiesByInputId(component, input.getUniqueId());
108             if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
109                 return StorageOperationStatus.OK;
110             }
111             componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input,
112                     cmptInstanceProperty, cmptInstanceProperty.getPath()));
113             return toscaOperationFacade.updateComponentInstanceProperties(component,
114                     componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(),
115                     componentInstancePropertiesDeclaredAsInput);
116         }
117     }
118
119     @Override
120     public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
121         List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, input.getUniqueId());
122         if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
123             return StorageOperationStatus.OK;
124         }
125         componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input, cmptInstanceProperty, cmptInstanceProperty.getPath()));
126         return toscaOperationFacade.updateComponentInstanceProperties(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(), componentInstancePropertiesDeclaredAsInput);
127     }
128 }