Refactoring Consolidation Service
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / PolicyPropertyDeclarator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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 fj.data.Either;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.Component;
27 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
28 import org.openecomp.sdc.be.model.InputDefinition;
29 import org.openecomp.sdc.be.model.PolicyDefinition;
30 import org.openecomp.sdc.be.model.jsontitan.operations.PolicyOperation;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34
35 import java.util.*;
36 import java.util.stream.Collectors;
37
38 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
39
40 @org.springframework.stereotype.Component
41 public class PolicyPropertyDeclarator extends DefaultPropertyDeclarator<PolicyDefinition, PropertyDataDefinition> {
42
43     private static final Logger log = Logger.getLogger(PolicyPropertyDeclarator.class);
44     private PolicyOperation policyOperation;
45
46     public PolicyPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, PolicyOperation policyOperation) {
47         super(componentsUtils, propertyOperation);
48         this.policyOperation = policyOperation;
49     }
50
51     @Override
52     PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
53         return new PropertyDataDefinition(prop);
54     }
55
56     @Override
57     Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String policyId, List<PropertyDataDefinition> properties) {
58         log.debug("#updatePropertiesValues - updating policies properties for policy {} on component {}", policyId, component.getUniqueId());
59         StorageOperationStatus updateStatus = policyOperation.updatePolicyProperties(component, policyId, properties);
60         return updateStatus == StorageOperationStatus.OK ? Either.left(updateStatus) : Either.right(updateStatus);
61     }
62
63     @Override
64     Optional<PolicyDefinition> resolvePropertiesOwner(Component component, String policyId) {
65         log.debug("#resolvePropertiesOwner - fetching policy {} of component {}", policyId, component.getUniqueId());
66         return Optional.ofNullable(component.getPolicyById(policyId));
67     }
68
69     @Override
70     void addPropertiesListToInput(PropertyDataDefinition declaredProp, InputDefinition input) {
71         List<ComponentInstanceProperty> propertiesList = input.getProperties();
72         if(propertiesList == null) {
73             propertiesList = new ArrayList<>(); // adding the property with the new value for UI
74         }
75         propertiesList.add(new ComponentInstanceProperty(declaredProp));
76         input.setProperties(propertiesList);
77
78     }
79
80     @Override
81     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition inputForDelete) {
82         return getPolicyPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
83                 .map(policyProperties -> unDeclarePolicyProperties(component, inputForDelete, policyProperties))
84                 .orElse(StorageOperationStatus.OK);
85     }
86
87     private StorageOperationStatus unDeclarePolicyProperties(Component container, InputDefinition input, PolicyProperties policyProperties) {
88         String policyId = policyProperties.getPolicyId();
89         List<PropertyDataDefinition> propsDeclaredAsInput = policyProperties.getProperties();
90         propsDeclaredAsInput.forEach(policyProp -> prepareValueBeforeDelete(input, policyProp, Collections.emptyList()));
91         return policyOperation.updatePolicyProperties(container, policyId, propsDeclaredAsInput);
92     }
93
94     private Optional<PolicyProperties> getPolicyPropertiesDeclaredAsInput(Component container, String inputId) {
95         if (container.getPolicies() == null) {
96             return Optional.empty();
97         }
98         return container.getPolicies()
99                 .values()
100                 .stream()
101                 .filter(policy -> Objects.nonNull(policy.getProperties()))
102                 .map(policy -> getPolicyPropertiesDeclaredAsInput(policy, inputId))
103                 .filter(PolicyProperties::isNotEmpty)
104                 .findFirst();
105     }
106
107     private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
108         if (CollectionUtils.isEmpty(property.getGetInputValues())) {
109             return false;
110         }
111         return property.getGetInputValues().stream()
112                 .filter(Objects::nonNull)
113                 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
114     }
115
116     private PolicyProperties getPolicyPropertiesDeclaredAsInput(PolicyDefinition policy, String inputId) {
117         List<PropertyDataDefinition> collect = policy.getProperties()
118                 .stream()
119                 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
120                 .collect(Collectors.toList());
121         return new PolicyProperties(policy.getUniqueId(), collect);
122
123     }
124
125     private class PolicyProperties {
126         private String policyId;
127         private List<PropertyDataDefinition> properties;
128
129         PolicyProperties(String policyId, List<PropertyDataDefinition> properties) {
130             this.policyId = policyId;
131             this.properties = (properties == null)? null : new ArrayList<>(properties);
132         }
133
134         String getPolicyId() {
135             return policyId;
136         }
137
138         public List<PropertyDataDefinition> getProperties() {
139             return new ArrayList<>(properties);
140         }
141
142         boolean isNotEmpty() {
143             return CollectionUtils.isNotEmpty(properties);
144         }
145     }
146 }