Added oparent to sdc main
[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 java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
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;
43
44 @org.springframework.stereotype.Component
45 public class ComponentInstancePropertyDeclarator extends DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceProperty> {
46
47     private static final Logger log = Logger.getLogger(ComponentInstancePropertyDeclarator.class);
48     private ToscaOperationFacade toscaOperationFacade;
49     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
50
51     public ComponentInstancePropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade, ComponentInstanceBusinessLogic componentInstanceBusinessLogic) {
52         super(componentsUtils, propertyOperation);
53         this.toscaOperationFacade = toscaOperationFacade;
54         this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
55     }
56
57     @Override
58     public ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
59         return new ComponentInstanceProperty(prop);
60     }
61
62     @Override
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);
67     }
68
69     @Override
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);
73     }
74
75     @Override
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
80         }
81         propertiesList.add(declaredProp);
82         input.setProperties(propertiesList);
83     }
84
85     @Override
86     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
87
88         Optional<ComponentInstanceProperty> propertyByInputId = PropertiesUtils.getPropertyByInputId(component,
89                 input.getUniqueId());
90         if(propertyByInputId.isPresent()) {
91             List<ComponentInstanceProperty> capabilityPropertyDeclaredAsInput =
92                    PropertiesUtils.getCapabilityProperty(propertyByInputId.get(), input.getUniqueId());
93             capabilityPropertyDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDeleteOfCapProp(input,
94                     cmptInstanceProperty));
95
96             Optional<CapabilityDefinition> propertyCapabilityOptional = PropertiesUtils.getPropertyCapabilityOfChildInstance(
97                     capabilityPropertyDeclaredAsInput.get(0).getParentUniqueId(), component.getCapabilities());
98             if(!propertyCapabilityOptional.isPresent()) {
99                 return StorageOperationStatus.OK;
100             }
101
102             return toscaOperationFacade.updateInstanceCapabilityProperty(component, input.getInstanceUniqueId(),
103                     capabilityPropertyDeclaredAsInput.get(0), propertyCapabilityOptional.get() );
104         } else {
105             List<ComponentInstanceProperty> componentInstancePropertiesDeclaredAsInput = componentInstanceBusinessLogic
106                     .getComponentInstancePropertiesByInputId(component, input.getUniqueId());
107             if (CollectionUtils.isEmpty(componentInstancePropertiesDeclaredAsInput)) {
108                 return StorageOperationStatus.OK;
109             }
110             componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input,
111                     cmptInstanceProperty, cmptInstanceProperty.getPath()));
112             return toscaOperationFacade.updateComponentInstanceProperties(component,
113                     componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(),
114                     componentInstancePropertiesDeclaredAsInput);
115         }
116     }
117
118     @Override
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;
123         }
124         componentInstancePropertiesDeclaredAsInput.forEach(cmptInstanceProperty -> prepareValueBeforeDelete(input, cmptInstanceProperty, cmptInstanceProperty.getPath()));
125         return toscaOperationFacade.updateComponentInstanceProperties(component, componentInstancePropertiesDeclaredAsInput.get(0).getComponentInstanceId(), componentInstancePropertiesDeclaredAsInput);
126     }
127 }