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;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
42 import static java.util.stream.Collectors.toList;
43 import static org.apache.commons.collections.CollectionUtils.isEmpty;
44 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
46 @org.springframework.stereotype.Component
47 public class GroupPropertyDeclarator extends DefaultPropertyDeclarator<GroupDefinition, PropertyDataDefinition> {
49 private static final Logger log = Logger.getLogger(GroupPropertyDeclarator.class);
50 private GroupOperation groupOperation;
52 public GroupPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, GroupOperation groupOperation) {
53 super(componentsUtils, propertyOperation);
54 this.groupOperation = groupOperation;
58 public PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
59 return new PropertyDataDefinition(prop);
63 public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String groupId, List<PropertyDataDefinition> properties) {
64 log.debug("#updatePropertiesValues - updating group properties for group {} on component {}", groupId, component.getUniqueId());
65 StorageOperationStatus updateStatus = groupOperation.updateGroupProperties(component, groupId, properties);
66 return updateStatus == StorageOperationStatus.OK ? Either.left(updateStatus) : Either.right(updateStatus);
70 public Optional<GroupDefinition> resolvePropertiesOwner(Component component, String groupId) {
71 log.debug("#resolvePropertiesOwner - fetching group {} of component {}", groupId, component.getUniqueId());
72 return component.getGroupById(groupId);
76 public void addPropertiesListToInput(PropertyDataDefinition 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
81 propertiesList.add(new ComponentInstanceProperty(declaredProp));
82 input.setProperties(propertiesList);
87 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition inputForDelete) {
88 return getGroupPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
89 .map(groupProperties -> unDeclareGroupProperties(component, inputForDelete, groupProperties))
90 .orElse(StorageOperationStatus.OK);
94 public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition inputForDelete) {
95 return getGroupPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
96 .map(groupProperties -> unDeclareGroupProperties(component, inputForDelete, groupProperties))
97 .orElse(StorageOperationStatus.OK);
100 private StorageOperationStatus unDeclareGroupProperties(Component container, InputDefinition input, GroupProperties groupProperties) {
101 String groupId = groupProperties.getGroupId();
102 List<PropertyDataDefinition> propsDeclaredAsInput = groupProperties.getProperties();
103 propsDeclaredAsInput.forEach(groupProp -> prepareValueBeforeDelete(input, groupProp, Collections.emptyList()));
104 return groupOperation.updateGroupProperties(container, groupId, propsDeclaredAsInput);
107 private Optional<GroupProperties> getGroupPropertiesDeclaredAsInput(Component container, String inputId) {
108 if (container.getGroups() == null) {
109 return Optional.empty();
111 return container.getGroups()
113 .filter(group -> Objects.nonNull(group.getProperties()))
114 .map(grp -> getGroupPropertiesDeclaredAsInput(grp, inputId))
115 .filter(GroupProperties::isNotEmpty)
120 private GroupProperties getGroupPropertiesDeclaredAsInput(GroupDefinition group, String inputId) {
121 List<PropertyDataDefinition> propertyDataDefinitions = group.getProperties()
123 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
125 return new GroupProperties(group.getUniqueId(), propertyDataDefinitions);
128 private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
129 if (isEmpty(property.getGetInputValues())) {
132 return property.getGetInputValues().stream()
133 .filter(Objects::nonNull)
134 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
138 private class GroupProperties {
139 private String groupId;
140 private List<PropertyDataDefinition> properties;
142 GroupProperties(String groupId, List<PropertyDataDefinition> properties) {
143 this.groupId = groupId;
144 this.properties = (properties == null)? null :new ArrayList<>(properties);
147 String getGroupId() {
151 public List<PropertyDataDefinition> getProperties() {
152 return new ArrayList<>(properties);
155 boolean isNotEmpty() {
156 return CollectionUtils.isNotEmpty(properties);