re base code
[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 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;
35
36 import java.util.*;
37
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;
41
42 @org.springframework.stereotype.Component
43 public class GroupPropertyDeclarator extends DefaultPropertyDeclarator<GroupDefinition, PropertyDataDefinition> {
44
45     private static final Logger log = Logger.getLogger(GroupPropertyDeclarator.class);
46     private GroupOperation groupOperation;
47
48     public GroupPropertyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation, GroupOperation groupOperation) {
49         super(componentsUtils, propertyOperation);
50         this.groupOperation = groupOperation;
51     }
52
53     @Override
54     PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
55         return new PropertyDataDefinition(prop);
56     }
57
58     @Override
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);
63     }
64
65     @Override
66     Optional<GroupDefinition> resolvePropertiesOwner(Component component, String groupId) {
67         log.debug("#resolvePropertiesOwner - fetching group {} of component {}", groupId, component.getUniqueId());
68         return component.getGroupById(groupId);
69     }
70
71     @Override
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
76         }
77         propertiesList.add(new ComponentInstanceProperty(declaredProp));
78         input.setProperties(propertiesList);
79
80     }
81
82     @Override
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);
87     }
88
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);
94     }
95
96     private Optional<GroupProperties> getGroupPropertiesDeclaredAsInput(Component container, String inputId) {
97         if (container.getGroups() == null) {
98             return Optional.empty();
99         }
100         return container.getGroups()
101                 .stream()
102                 .filter(group -> Objects.nonNull(group.getProperties()))
103                 .map(grp -> getGroupPropertiesDeclaredAsInput(grp, inputId))
104                 .filter(GroupProperties::isNotEmpty)
105                 .findFirst();
106     }
107
108
109     private GroupProperties getGroupPropertiesDeclaredAsInput(GroupDefinition group, String inputId) {
110         List<PropertyDataDefinition> propertyDataDefinitions = group.getProperties()
111                 .stream()
112                 .filter(prop -> isPropertyDeclaredAsInputByInputId(prop, inputId))
113                 .collect(toList());
114         return new GroupProperties(group.getUniqueId(), propertyDataDefinitions);
115     }
116
117     private boolean isPropertyDeclaredAsInputByInputId(PropertyDataDefinition property, String inputId) {
118         if (isEmpty(property.getGetInputValues())) {
119             return false;
120         }
121         return property.getGetInputValues().stream()
122                 .filter(Objects::nonNull)
123                 .anyMatch(getInputVal -> isGetInputValueForInput(getInputVal, inputId));
124     }
125
126
127     private class GroupProperties {
128         private String groupId;
129         private List<PropertyDataDefinition> properties;
130
131         GroupProperties(String groupId, List<PropertyDataDefinition> properties) {
132             this.groupId = groupId;
133             this.properties = (properties == null)? null :new ArrayList<>(properties);
134         }
135
136         String getGroupId() {
137             return groupId;
138         }
139
140         public List<PropertyDataDefinition> getProperties() {
141             return new ArrayList<>(properties);
142         }
143
144         boolean isNotEmpty() {
145             return CollectionUtils.isNotEmpty(properties);
146         }
147     }
148 }