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 static java.util.stream.Collectors.toList;
24 import static org.apache.commons.collections.CollectionUtils.isEmpty;
25 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.Optional;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
38 import org.openecomp.sdc.be.model.GroupDefinition;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.GroupOperation;
42 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
45 @org.springframework.stereotype.Component
46 public class GroupPropertyDeclarator extends DefaultPropertyDeclarator<GroupDefinition, PropertyDataDefinition> {
48 private static final Logger log = Logger.getLogger(GroupPropertyDeclarator.class);
49 private GroupOperation groupOperation;
51 public GroupPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, GroupOperation groupOperation) {
52 super(componentsUtils, propertyOperation);
53 this.groupOperation = groupOperation;
57 public PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
58 return new PropertyDataDefinition(prop);
62 public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String groupId, List<PropertyDataDefinition> properties) {
63 log.debug("#updatePropertiesValues - updating group properties for group {} on component {}", groupId, component.getUniqueId());
64 StorageOperationStatus updateStatus = groupOperation.updateGroupProperties(component, groupId, properties);
65 return updateStatus == StorageOperationStatus.OK ? Either.left(updateStatus) : Either.right(updateStatus);
69 public Optional<GroupDefinition> resolvePropertiesOwner(Component component, String groupId) {
70 log.debug("#resolvePropertiesOwner - fetching group {} of component {}", groupId, component.getUniqueId());
71 return component.getGroupById(groupId);
75 public void addPropertiesListToInput(PropertyDataDefinition declaredProp, InputDefinition input) {
76 List<ComponentInstanceProperty> propertiesList = input.getProperties();
77 if(propertiesList == null) {
78 propertiesList = new ArrayList<>(); // adding the property with the new value for UI
80 propertiesList.add(new ComponentInstanceProperty(declaredProp));
81 input.setProperties(propertiesList);
86 public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition inputForDelete) {
87 return getGroupPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
88 .map(groupProperties -> unDeclareGroupProperties(component, inputForDelete, groupProperties))
89 .orElse(StorageOperationStatus.OK);
93 public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition inputForDelete) {
94 return getGroupPropertiesDeclaredAsInput(component, inputForDelete.getUniqueId())
95 .map(groupProperties -> unDeclareGroupProperties(component, inputForDelete, groupProperties))
96 .orElse(StorageOperationStatus.OK);
99 private StorageOperationStatus unDeclareGroupProperties(Component container, InputDefinition input, GroupProperties groupProperties) {
100 String groupId = groupProperties.getGroupId();
101 List<PropertyDataDefinition> propsDeclaredAsInput = groupProperties.getProperties();
102 propsDeclaredAsInput.forEach(groupProp -> prepareValueBeforeDelete(input, groupProp, Collections.emptyList()));
103 return groupOperation.updateGroupProperties(container, groupId, propsDeclaredAsInput);
106 private Optional<GroupProperties> getGroupPropertiesDeclaredAsInput(Component container, String inputId) {
107 if (container.getGroups() == null) {
108 return Optional.empty();
110 return container.getGroups()
112 .filter(group -> Objects.nonNull(group.getProperties()))
113 .map(grp -> getGroupPropertiesDeclaredAsInput(grp, inputId))
114 .filter(GroupProperties::isNotEmpty)
119 private GroupProperties getGroupPropertiesDeclaredAsInput(GroupDefinition group, String inputId) {
120 List<PropertyDataDefinition> propertyDataDefinitions = group.getProperties()
122 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
124 return new GroupProperties(group.getUniqueId(), propertyDataDefinitions);
127 private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
128 if (isEmpty(property.getGetInputValues())) {
131 return property.getGetInputValues().stream()
132 .filter(Objects::nonNull)
133 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
137 private class GroupProperties {
138 private String groupId;
139 private List<PropertyDataDefinition> properties;
141 GroupProperties(String groupId, List<PropertyDataDefinition> properties) {
142 this.groupId = groupId;
143 this.properties = (properties == null)? null :new ArrayList<>(properties);
146 String getGroupId() {
150 public List<PropertyDataDefinition> getProperties() {
151 return new ArrayList<>(properties);
154 boolean isNotEmpty() {
155 return CollectionUtils.isNotEmpty(properties);