2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.openecomp.sdc.be.components.property;
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;
36 import java.util.stream.Collectors;
38 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
40 @org.springframework.stereotype.Component
41 public class PolicyPropertyDeclarator extends DefaultPropertyDeclarator<PolicyDefinition, PropertyDataDefinition> {
43 private static final Logger log = Logger.getLogger(PolicyPropertyDeclarator.class);
44 private PolicyOperation policyOperation;
46 public PolicyPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, PolicyOperation policyOperation) {
47 super(componentsUtils, propertyOperation);
48 this.policyOperation = policyOperation;
52 PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
53 return new PropertyDataDefinition(prop);
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);
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));
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
75 propertiesList.add(new ComponentInstanceProperty(declaredProp));
76 input.setProperties(propertiesList);
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);
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);
94 private Optional<PolicyProperties> getPolicyPropertiesDeclaredAsInput(Component container, String inputId) {
95 if (container.getPolicies() == null) {
96 return Optional.empty();
98 return container.getPolicies()
101 .filter(policy -> Objects.nonNull(policy.getProperties()))
102 .map(policy -> getPolicyPropertiesDeclaredAsInput(policy, inputId))
103 .filter(PolicyProperties::isNotEmpty)
107 private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
108 if (CollectionUtils.isEmpty(property.getGetInputValues())) {
111 return property.getGetInputValues().stream()
112 .filter(Objects::nonNull)
113 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
116 private PolicyProperties getPolicyPropertiesDeclaredAsInput(PolicyDefinition policy, String inputId) {
117 List<PropertyDataDefinition> collect = policy.getProperties()
119 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
120 .collect(Collectors.toList());
121 return new PolicyProperties(policy.getUniqueId(), collect);
125 private class PolicyProperties {
126 private String policyId;
127 private List<PropertyDataDefinition> properties;
129 PolicyProperties(String policyId, List<PropertyDataDefinition> properties) {
130 this.policyId = policyId;
131 this.properties = (properties == null)? null : new ArrayList<>(properties);
134 String getPolicyId() {
138 public List<PropertyDataDefinition> getProperties() {
139 return new ArrayList<>(properties);
142 boolean isNotEmpty() {
143 return CollectionUtils.isNotEmpty(properties);