Change to enable SDC list type input
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / GroupPropertyDeclarator.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
21 package org.openecomp.sdc.be.components.property;
22
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;
26
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;
44
45 @org.springframework.stereotype.Component
46 public class GroupPropertyDeclarator extends DefaultPropertyDeclarator<GroupDefinition, PropertyDataDefinition> {
47
48     private static final Logger log = Logger.getLogger(GroupPropertyDeclarator.class);
49     private GroupOperation groupOperation;
50
51     public GroupPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, GroupOperation groupOperation) {
52         super(componentsUtils, propertyOperation);
53         this.groupOperation = groupOperation;
54     }
55
56     @Override
57     public PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
58         return new PropertyDataDefinition(prop);
59     }
60
61     @Override
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);
66     }
67
68     @Override
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);
72     }
73
74     @Override
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
79         }
80         propertiesList.add(new ComponentInstanceProperty(declaredProp));
81         input.setProperties(propertiesList);
82
83     }
84
85     @Override
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);
90     }
91
92     @Override
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);
97     }
98
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);
104     }
105
106     private Optional<GroupProperties> getGroupPropertiesDeclaredAsInput(Component container, String inputId) {
107         if (container.getGroups() == null) {
108             return Optional.empty();
109         }
110         return container.getGroups()
111                 .stream()
112                 .filter(group -> Objects.nonNull(group.getProperties()))
113                 .map(grp -> getGroupPropertiesDeclaredAsInput(grp, inputId))
114                 .filter(GroupProperties::isNotEmpty)
115                 .findFirst();
116     }
117
118
119     private GroupProperties getGroupPropertiesDeclaredAsInput(GroupDefinition group, String inputId) {
120         List<PropertyDataDefinition> propertyDataDefinitions = group.getProperties()
121                 .stream()
122                 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
123                 .collect(toList());
124         return new GroupProperties(group.getUniqueId(), propertyDataDefinitions);
125     }
126
127     private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
128         if (isEmpty(property.getGetInputValues())) {
129             return false;
130         }
131         return property.getGetInputValues().stream()
132                 .filter(Objects::nonNull)
133                 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
134     }
135
136
137     private class GroupProperties {
138         private String groupId;
139         private List<PropertyDataDefinition> properties;
140
141         GroupProperties(String groupId, List<PropertyDataDefinition> properties) {
142             this.groupId = groupId;
143             this.properties = (properties == null)? null :new ArrayList<>(properties);
144         }
145
146         String getGroupId() {
147             return groupId;
148         }
149
150         public List<PropertyDataDefinition> getProperties() {
151             return new ArrayList<>(properties);
152         }
153
154         boolean isNotEmpty() {
155             return CollectionUtils.isNotEmpty(properties);
156         }
157     }
158 }