Centralize TOSCA function validation
[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(component, 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(Component groupOwner, 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 (StringUtils.isEmpty(gp.getValue())) {
144                 gp.setValue(originalProperties.get(updatedPropertyName).getDefaultValue());
145             }
146             StorageOperationStatus sos = groupOperation.validateAndUpdatePropertyValue(groupOwner, gp);
147             if (StorageOperationStatus.OK != sos) {
148                 throw new StorageException(sos, updatedPropertyName);
149             }
150         }
151         validatePropertyBusinessLogic(groupPropertiesToUpdate, originalGroup);
152     }
153
154     private void validatePropertyBusinessLogic(List<GroupProperty> groupPropertiesToUpdate, GroupDefinition originalGroup) {
155         Map<PropertyDefinition.PropertyNames, String> enumValueMap = new EnumMap<>(PropertyDefinition.PropertyNames.class);
156         for (GroupProperty gp : groupPropertiesToUpdate) {
157             // Filter out non special properties which does not have Enum
158             final PropertyDefinition.PropertyNames gpEnum = PropertyDefinition.PropertyNames.findName(gp.getName());
159             if (gpEnum != null) {
160                 enumValueMap.put(gpEnum, gp.getValue());
161             }
162         }
163         if (MapUtils.isEmpty(enumValueMap)) {
164             return;
165         }
166         validateVFInstancesLogic(enumValueMap, prepareMapWithOriginalProperties(originalGroup));
167         if (enumValueMap.containsKey(PropertyDefinition.PropertyNames.VF_MODULE_DESCRIPTION) || enumValueMap
168             .containsKey(PropertyDefinition.PropertyNames.VF_MODULE_LABEL)) {
169             groupPropertiesToUpdate.stream().filter(e -> enumHasValueFilter(e.getName(), PropertyDefinition.PropertyNames::findName,
170                 PropertyDefinition.PropertyNames.VF_MODULE_DESCRIPTION, PropertyDefinition.PropertyNames.VF_MODULE_LABEL))
171                 .forEach(this::validateFreeText);
172         }
173     }
174
175     private Map<PropertyDefinition.PropertyNames, String> prepareMapWithOriginalProperties(GroupDefinition originalGroup) {
176         Map<PropertyDefinition.PropertyNames, String> oldValueMap = new EnumMap<>(PropertyDefinition.PropertyNames.class);
177         PropertyDefinition.PropertyNames[] propertiesToCheck = new PropertyDefinition.PropertyNames[]{PropertyDefinition.PropertyNames.INITIAL_COUNT,
178             PropertyDefinition.PropertyNames.MAX_INSTANCES, PropertyDefinition.PropertyNames.MIN_INSTANCES};
179         for (GroupProperty gp : originalGroup.convertToGroupProperties()) {
180             if (enumHasValueFilter(gp.getName(), PropertyDefinition.PropertyNames::findName, propertiesToCheck)) {
181                 oldValueMap.put(PropertyDefinition.PropertyNames.findName(gp.getName()), gp.getValue());
182             }
183         }
184         if (StringUtils.isEmpty(oldValueMap.get(PropertyDefinition.PropertyNames.MAX_INSTANCES))) {
185             oldValueMap.put(PropertyDefinition.PropertyNames.MAX_INSTANCES, String.valueOf(Integer.MAX_VALUE));
186         }
187         return oldValueMap;
188     }
189
190     private void validateVFInstancesLogic(Map<PropertyDefinition.PropertyNames, String> newValues,
191                                           Map<PropertyDefinition.PropertyNames, String> parentValues) {
192         if (!newValues.containsKey(PropertyDefinition.PropertyNames.INITIAL_COUNT) && !newValues
193             .containsKey(PropertyDefinition.PropertyNames.MAX_INSTANCES) && !newValues.containsKey(PropertyDefinition.PropertyNames.MIN_INSTANCES)) {
194             return;
195         }
196         int latestMaxInstances = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.MAX_INSTANCES);
197         int latestInitialCount = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.INITIAL_COUNT);
198         int latestMinInstances = getLatestIntProperty(newValues, parentValues, PropertyDefinition.PropertyNames.MIN_INSTANCES);
199         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.INITIAL_COUNT) && (latestInitialCount > latestMaxInstances
200             || latestInitialCount < latestMinInstances)) {
201             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_INITIAL_COUNT_PROPERTY_VALUE,
202                 PropertyDefinition.PropertyNames.INITIAL_COUNT.getPropertyName(), String.valueOf(latestMinInstances),
203                 String.valueOf(latestMaxInstances));
204         }
205         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.MAX_INSTANCES) && latestMaxInstances < latestInitialCount) {
206             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER,
207                 PropertyDefinition.PropertyNames.MAX_INSTANCES.getPropertyName(), "higher", String.valueOf(latestInitialCount));
208         }
209         if (isPropertyChanged(newValues, parentValues, PropertyDefinition.PropertyNames.MIN_INSTANCES) && latestMinInstances > latestInitialCount) {
210             throw new ByActionStatusComponentException(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER,
211                 PropertyDefinition.PropertyNames.MIN_INSTANCES.getPropertyName(), "lower", String.valueOf(latestInitialCount));
212         }
213     }
214
215     private boolean isPropertyChanged(Map<PropertyDefinition.PropertyNames, String> newValues,
216                                       Map<PropertyDefinition.PropertyNames, String> parentValues,
217                                       final PropertyDefinition.PropertyNames minInstances) {
218         return newValues.containsKey(minInstances) && !newValues.get(minInstances).equals(parentValues.get(minInstances));
219     }
220
221     private int getLatestIntProperty(Map<PropertyDefinition.PropertyNames, String> newValues,
222                                      Map<PropertyDefinition.PropertyNames, String> parentValues, PropertyDefinition.PropertyNames propertyKey) {
223         String value;
224         if (newValues.containsKey(propertyKey)) {
225             value = newValues.get(propertyKey);
226         } else {
227             value = parentValues.get(propertyKey);
228         }
229         return Integer.parseInt(value);
230     }
231
232     private boolean isOnlyGroupPropertyValueChanged(GroupProperty groupProperty1, GroupProperty groupProperty2) {
233         GroupProperty groupProperty1Duplicate = new GroupProperty(groupProperty1);
234         groupProperty1Duplicate.setValue(null);
235         groupProperty1Duplicate.setSchema(null);
236         groupProperty1Duplicate.setParentUniqueId(null);
237         groupProperty1Duplicate.setToscaFunction(null);
238         groupProperty1Duplicate.setToscaGetFunctionType(null);
239         GroupProperty groupProperty2Duplicate = new GroupProperty(groupProperty2);
240         groupProperty2Duplicate.setValue(null);
241         groupProperty2Duplicate.setSchema(null);
242         groupProperty2Duplicate.setParentUniqueId(null);
243         groupProperty2Duplicate.setToscaFunction(null);
244         groupProperty2Duplicate.setToscaGetFunctionType(null);
245         return StringUtils.equals(groupProperty1Duplicate.getValueUniqueUid(), groupProperty2Duplicate.getValueUniqueUid()) && groupProperty1Duplicate
246             .equals(groupProperty2Duplicate);
247     }
248
249     private void validateFreeText(GroupProperty groupPropertyToUpdate) {
250         final String groupTypeValue = groupPropertyToUpdate.getValue();
251         if (!org.apache.commons.lang3.StringUtils.isEmpty(groupTypeValue)) {
252             if (!ValidationUtils.validateDescriptionLength(groupTypeValue)) {
253                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, NodeTypeEnum.Property.getName(),
254                     String.valueOf(ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH));
255             } else if (!ValidationUtils.validateIsEnglish(groupTypeValue)) {
256                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_DESCRIPTION, NodeTypeEnum.Property.getName());
257             }
258         }
259     }
260 }