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