Support tosca functions for group instances in composition view
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / GroupBusinessLogicNew.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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components.impl;
24
25 import static org.openecomp.sdc.be.components.impl.BaseBusinessLogic.enumHasValueFilter;
26
27 import java.util.ArrayList;
28 import java.util.EnumMap;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.stream.Collectors;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.collections.MapUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.apache.commons.lang3.math.NumberUtils;
37 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
38 import org.openecomp.sdc.be.components.impl.lock.LockingTransactional;
39 import org.openecomp.sdc.be.components.validation.AccessValidations;
40 import org.openecomp.sdc.be.components.validation.ComponentValidations;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.ComponentInstance;
48 import org.openecomp.sdc.be.model.GroupDefinition;
49 import org.openecomp.sdc.be.model.GroupProperty;
50 import org.openecomp.sdc.be.model.PropertyDefinition;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
52 import org.openecomp.sdc.be.model.operations.StorageException;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 import org.openecomp.sdc.be.model.operations.impl.GroupOperation;
55 import org.openecomp.sdc.common.util.ValidationUtils;
56 import org.springframework.transaction.annotation.Transactional;
57
58 @org.springframework.stereotype.Component
59 public class GroupBusinessLogicNew {
60
61     private final AccessValidations accessValidations;
62     private final ComponentValidations componentValidations;
63     private final GroupsOperation groupsOperation;
64     private final GroupOperation groupOperation;
65
66     public GroupBusinessLogicNew(AccessValidations accessValidations, ComponentValidations componentValidations, GroupsOperation groupsOperation,
67                                  GroupOperation groupOperation) {
68         this.accessValidations = accessValidations;
69         this.componentValidations = componentValidations;
70         this.groupsOperation = groupsOperation;
71         this.groupOperation = groupOperation;
72     }
73
74     @LockingTransactional
75     public List<String> updateMembers(String componentId, ComponentTypeEnum componentType, String userId, String groupUniqueId,
76                                       List<String> members) {
77         Component component = accessValidations.validateUserCanWorkOnComponent(componentId, componentType, userId, "UPDATE GROUP MEMBERS");
78         GroupDefinition groupDefinition = getGroup(component, groupUniqueId);
79         groupDefinition.setMembers(buildMembersMap(component, members));
80         groupsOperation.updateGroupOnComponent(componentId, groupDefinition, PromoteVersionEnum.MINOR);
81         return new ArrayList<>(groupDefinition.getMembers().values());
82     }
83
84     @LockingTransactional
85     public List<GroupProperty> updateProperties(String componentId, ComponentTypeEnum componentType, String userId, String groupUniqueId,
86                                                 List<GroupProperty> newProperties) {
87         Component component = accessValidations.validateUserCanWorkOnComponent(componentId, componentType, userId, "UPDATE GROUP PROPERTIES");
88         GroupDefinition currentGroup = getGroup(component, groupUniqueId);
89         validateUpdatedPropertiesAndSetEmptyValues(currentGroup, newProperties);
90         return groupsOperation.updateGroupPropertiesOnComponent(componentId, currentGroup, newProperties, PromoteVersionEnum.MINOR).left()
91             .on(this::onUpdatePropertyError);
92     }
93
94     @Transactional
95     public List<PropertyDataDefinition> getProperties(String componentType, String userId, String componentId, String groupUniqueId) {
96         Component component = accessValidations.validateUserCanRetrieveComponentData(componentId, componentType, userId, "GET GROUP PROPERTIES");
97         GroupDefinition currentGroup = getGroup(component, groupUniqueId);
98         return currentGroup.getProperties();
99     }
100
101     private List<GroupProperty> onUpdatePropertyError(StorageOperationStatus storageOperationStatus) {
102         throw new StorageException(storageOperationStatus);
103     }
104
105     private Map<String, String> buildMembersMap(Component component, List<String> newMemberUniqueIds) {
106         Map<String, String> nameToUniqueId = new HashMap<>();
107         for (String memberUniqueId : newMemberUniqueIds) {
108             ComponentInstance componentInstance = getComponentInstance(component, memberUniqueId);
109             nameToUniqueId.put(componentInstance.getName(), componentInstance.getUniqueId());
110         }
111         return nameToUniqueId;
112     }
113
114     private ComponentInstance getComponentInstance(Component component, String memberUniqueId) {
115         return componentValidations.getComponentInstance(component, memberUniqueId).orElseThrow(
116             () -> new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, memberUniqueId, "",
117                 component.getActualComponentType(), component.getSystemName()));
118     }
119
120     private GroupDefinition getGroup(Component component, String groupUniqueId) {
121         return component.getGroupById(groupUniqueId).orElseThrow(
122             () -> new ByActionStatusComponentException(ActionStatus.GROUP_IS_MISSING, component.getSystemName(), component.getActualComponentType()));
123     }
124
125     private void validateUpdatedPropertiesAndSetEmptyValues(GroupDefinition originalGroup, List<GroupProperty> groupPropertiesToUpdate) {
126         if (CollectionUtils.isEmpty(groupPropertiesToUpdate)) {
127             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, StringUtils.EMPTY);
128         }
129         if (CollectionUtils.isEmpty(originalGroup.getProperties())) {
130             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND,
131                 groupPropertiesToUpdate.get(NumberUtils.INTEGER_ZERO).getName());
132         }
133         Map<String, GroupProperty> originalProperties = originalGroup.convertToGroupProperties().stream()
134             .collect(Collectors.toMap(PropertyDataDefinition::getName, p -> p));
135         for (GroupProperty gp : groupPropertiesToUpdate) {
136             String updatedPropertyName = gp.getName();
137             if (!originalProperties.containsKey(updatedPropertyName)) {
138                 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, updatedPropertyName);
139             }
140             if (!isOnlyGroupPropertyValueChanged(gp, originalProperties.get(updatedPropertyName))) {
141                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY, updatedPropertyName);
142             }
143             if (gp.hasGetFunction()) {
144                 gp.setValue(gp.getToscaGetFunction().generatePropertyValue());
145             }
146             if (StringUtils.isEmpty(gp.getValue())) {
147                 gp.setValue(originalProperties.get(updatedPropertyName).getDefaultValue());
148             }
149             StorageOperationStatus sos = groupOperation.validateAndUpdatePropertyValue(gp);
150             if (StorageOperationStatus.OK != sos) {
151                 throw new StorageException(sos, updatedPropertyName);
152             }
153         }
154         validatePropertyBusinessLogic(groupPropertiesToUpdate, originalGroup);
155     }
156
157     private void validatePropertyBusinessLogic(List<GroupProperty> groupPropertiesToUpdate, GroupDefinition originalGroup) {
158         Map<PropertyDefinition.PropertyNames, String> enumValueMap = new EnumMap<>(PropertyDefinition.PropertyNames.class);
159         for (GroupProperty gp : groupPropertiesToUpdate) {
160             // Filter out non special properties which does not have Enum
161             final PropertyDefinition.PropertyNames gpEnum = PropertyDefinition.PropertyNames.findName(gp.getName());
162             if (gpEnum != null) {
163                 enumValueMap.put(gpEnum, gp.getValue());
164             }
165         }
166         if (MapUtils.isEmpty(enumValueMap)) {
167             return;
168         }
169         validateVFInstancesLogic(enumValueMap, prepareMapWithOriginalProperties(originalGroup));
170         if (enumValueMap.containsKey(PropertyDefinition.PropertyNames.VF_MODULE_DESCRIPTION) || enumValueMap
171             .containsKey(PropertyDefinition.PropertyNames.VF_MODULE_LABEL)) {
172             groupPropertiesToUpdate.stream().filter(e -> enumHasValueFilter(e.getName(), PropertyDefinition.PropertyNames::findName,
173                 PropertyDefinition.PropertyNames.VF_MODULE_DESCRIPTION, PropertyDefinition.PropertyNames.VF_MODULE_LABEL))
174                 .forEach(this::validateFreeText);
175         }
176     }
177
178     private Map<PropertyDefinition.PropertyNames, String> prepareMapWithOriginalProperties(GroupDefinition originalGroup) {
179         Map<PropertyDefinition.PropertyNames, String> oldValueMap = new EnumMap<>(PropertyDefinition.PropertyNames.class);
180         PropertyDefinition.PropertyNames[] propertiesToCheck = new PropertyDefinition.PropertyNames[]{PropertyDefinition.PropertyNames.INITIAL_COUNT,
181             PropertyDefinition.PropertyNames.MAX_INSTANCES, PropertyDefinition.PropertyNames.MIN_INSTANCES};
182         for (GroupProperty gp : originalGroup.convertToGroupProperties()) {
183             if (enumHasValueFilter(gp.getName(), PropertyDefinition.PropertyNames::findName, propertiesToCheck)) {
184                 oldValueMap.put(PropertyDefinition.PropertyNames.findName(gp.getName()), gp.getValue());
185             }
186         }
187         if (StringUtils.isEmpty(oldValueMap.get(PropertyDefinition.PropertyNames.MAX_INSTANCES))) {
188             oldValueMap.put(PropertyDefinition.PropertyNames.MAX_INSTANCES, String.valueOf(Integer.MAX_VALUE));
189         }
190         return oldValueMap;
191     }
192
193     private void validateVFInstancesLogic(Map<PropertyDefinition.PropertyNames, String> newValues,
194                                           Map<PropertyDefinition.PropertyNames, String> parentValues) {
195         if (!newValues.containsKey(PropertyDefinition.PropertyNames.INITIAL_COUNT) && !newValues
196             .containsKey(PropertyDefinition.PropertyNames.MAX_INSTANCES) && !newValues.containsKey(PropertyDefinition.PropertyNames.MIN_INSTANCES)) {
197             return;
198         }
199         int latestMaxInstances = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.MAX_INSTANCES);
200         int latestInitialCount = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.INITIAL_COUNT);
201         int latestMinInstances = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.MIN_INSTANCES);
202         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.INITIAL_COUNT) && (latestInitialCount > latestMaxInstances
203             || latestInitialCount < latestMinInstances)) {
204             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_INITIAL_COUNT_PROPERTY_VALUE,
205                 PropertyDefinition.PropertyNames.INITIAL_COUNT.getPropertyName(), String.valueOf(latestMinInstances),
206                 String.valueOf(latestMaxInstances));
207         }
208         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.MAX_INSTANCES) && latestMaxInstances < latestInitialCount) {
209             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER,
210                 PropertyDefinition.PropertyNames.MAX_INSTANCES.getPropertyName(), "higher", String.valueOf(latestInitialCount));
211         }
212         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.MIN_INSTANCES) && latestMinInstances > latestInitialCount) {
213             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER,
214                 PropertyDefinition.PropertyNames.MIN_INSTANCES.getPropertyName(), "lower", String.valueOf(latestInitialCount));
215         }
216     }
217
218     private boolean isPropertyChanged(Map<PropertyDefinition.PropertyNames, String> newValues,
219                                       Map<PropertyDefinition.PropertyNames, String> parentValues,
220                                       final PropertyDefinition.PropertyNames minInstances) {
221         return newValues.containsKey(minInstances) && !newValues.get(minInstances).equals(parentValues.get(minInstances));
222     }
223
224     private int getLatestIntProperty(Map<PropertyDefinition.PropertyNames, String> newValues,
225                                      Map<PropertyDefinition.PropertyNames, String> parentValues, PropertyDefinition.PropertyNames propertyKey) {
226         String value;
227         if (newValues.containsKey(propertyKey)) {
228             value = newValues.get(propertyKey);
229         } else {
230             value = parentValues.get(propertyKey);
231         }
232         return Integer.parseInt(value);
233     }
234
235     private boolean isOnlyGroupPropertyValueChanged(GroupProperty groupProperty1, GroupProperty groupProperty2) {
236         GroupProperty groupProperty1Duplicate = new GroupProperty(groupProperty1);
237         groupProperty1Duplicate.setValue(null);
238         groupProperty1Duplicate.setSchema(null);
239         groupProperty1Duplicate.setParentUniqueId(null);
240         groupProperty1Duplicate.setToscaGetFunction(null);
241         groupProperty1Duplicate.setToscaGetFunctionType(null);
242         GroupProperty groupProperty2Duplicate = new GroupProperty(groupProperty2);
243         groupProperty2Duplicate.setValue(null);
244         groupProperty2Duplicate.setSchema(null);
245         groupProperty2Duplicate.setParentUniqueId(null);
246         groupProperty2Duplicate.setToscaGetFunction(null);
247         groupProperty2Duplicate.setToscaGetFunctionType(null);
248         return StringUtils.equals(groupProperty1Duplicate.getValueUniqueUid(), groupProperty2Duplicate.getValueUniqueUid()) && groupProperty1Duplicate
249             .equals(groupProperty2Duplicate);
250     }
251
252     private void validateFreeText(GroupProperty groupPropertyToUpdate) {
253         final String groupTypeValue = groupPropertyToUpdate.getValue();
254         if (!org.apache.commons.lang3.StringUtils.isEmpty(groupTypeValue)) {
255             if (!ValidationUtils.validateDescriptionLength(groupTypeValue)) {
256                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, NodeTypeEnum.Property.getName(),
257                     String.valueOf(ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH));
258             } else if (!ValidationUtils.validateIsEnglish(groupTypeValue)) {
259                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_DESCRIPTION, NodeTypeEnum.Property.getName());
260             }
261         }
262     }
263 }