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=========================================================
21 package org.openecomp.sdc.be.components.property;
23 import fj.data.Either;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
29 import org.openecomp.sdc.be.model.GroupDefinition;
30 import org.openecomp.sdc.be.model.InputDefinition;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.be.model.operations.impl.GroupOperation;
33 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import static java.util.stream.Collectors.toList;
39 import static org.apache.commons.collections.CollectionUtils.isEmpty;
40 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
42 @org.springframework.stereotype.Component
43 public class GroupPropertyDeclarator extends DefaultPropertyDeclarator<GroupDefinition, PropertyDataDefinition> {
45 private static final Logger log = Logger.getLogger(GroupPropertyDeclarator.class);
46 private GroupOperation groupOperation;
48 public GroupPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, GroupOperation groupOperation) {
49 super(componentsUtils, propertyOperation);
50 this.groupOperation = groupOperation;
54 PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
55 return new PropertyDataDefinition(prop);
59 Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String groupId, List<PropertyDataDefinition> properties) {
60 log.debug("#updatePropertiesValues - updating group properties for group {} on component {}", groupId, component.getUniqueId());
61 StorageOperationStatus updateStatus = groupOperation.updateGroupProperties(component, groupId, properties);
62 return updateStatus == StorageOperationStatus.OK ? Either.left(updateStatus) : Either.right(updateStatus);
66 Optional<GroupDefinition> resolvePropertiesOwner(Component component, String groupId) {
67 log.debug("#resolvePropertiesOwner - fetching group {} of component {}", groupId, component.getUniqueId());
68 return component.getGroupById(groupId);
72 void addPropertiesListToInput(PropertyDataDefinition declaredProp, InputDefinition input) {
73 List<ComponentInstanceProperty> propertiesList = input.getProperties();
74 if(propertiesList == null) {
75 propertiesList = new ArrayList<>(); // adding the property with the new value for UI
77 propertiesList.add(new ComponentInstanceProperty(declaredProp));
78 input.setProperties(propertiesList);
83 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition inputForDelete) {
84 return getGroupPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
85 .map(groupProperties -> unDeclareGroupProperties(component, inputForDelete, groupProperties))
86 .orElse(StorageOperationStatus.OK);
89 private StorageOperationStatus unDeclareGroupProperties(Component container, InputDefinition input, GroupProperties groupProperties) {
90 String groupId = groupProperties.getGroupId();
91 List<PropertyDataDefinition> propsDeclaredAsInput = groupProperties.getProperties();
92 propsDeclaredAsInput.forEach(groupProp -> prepareValueBeforeDelete(input, groupProp, Collections.emptyList()));
93 return groupOperation.updateGroupProperties(container, groupId, propsDeclaredAsInput);
96 private Optional<GroupProperties> getGroupPropertiesDeclaredAsInput(Component container, String inputId) {
97 if (container.getGroups() == null) {
98 return Optional.empty();
100 return container.getGroups()
102 .filter(group -> Objects.nonNull(group.getProperties()))
103 .map(grp -> getGroupPropertiesDeclaredAsInput(grp, inputId))
104 .filter(GroupProperties::isNotEmpty)
109 private GroupProperties getGroupPropertiesDeclaredAsInput(GroupDefinition group, String inputId) {
110 List<PropertyDataDefinition> propertyDataDefinitions = group.getProperties()
112 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
114 return new GroupProperties(group.getUniqueId(), propertyDataDefinitions);
117 private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
118 if (isEmpty(property.getGetInputValues())) {
121 return property.getGetInputValues().stream()
122 .filter(Objects::nonNull)
123 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
127 private class GroupProperties {
128 private String groupId;
129 private List<PropertyDataDefinition> properties;
131 GroupProperties(String groupId, List<PropertyDataDefinition> properties) {
132 this.groupId = groupId;
133 this.properties = (properties == null)? null :new ArrayList<>(properties);
136 String getGroupId() {
140 public List<PropertyDataDefinition> getProperties() {
141 return new ArrayList<>(properties);
144 boolean isNotEmpty() {
145 return CollectionUtils.isNotEmpty(properties);