Support TOSCA functions in Node Filters
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / PropertyConstraintsUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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 package org.openecomp.sdc.be.components.property;
21
22 import static java.util.Objects.nonNull;
23 import static java.util.stream.Collectors.toMap;
24
25 import java.util.List;
26 import java.util.Map;
27 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
28 import org.openecomp.sdc.be.model.PropertyConstraint;
29 import org.openecomp.sdc.be.model.PropertyDefinition;
30 import org.openecomp.sdc.be.model.Resource;
31 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
33
34 /**
35  * Provides specific functionality for property constraints
36  */
37 public class PropertyConstraintsUtils {
38
39     private PropertyConstraintsUtils() {
40     }
41
42     public static void validatePropertiesConstraints(Resource newResource, Resource oldResource) {
43         if (oldResource.getProperties() != null && newResource.getProperties() != null) {
44             Map<String, PropertyDefinition> oldPropWithConstraints = oldResource.getProperties().stream().filter(p -> p.getConstraints() != null)
45                 .collect(toMap(PropertyDefinition::getName, p -> p));
46             newResource.getProperties().stream().filter(p -> p.getConstraints() != null && oldPropWithConstraints.containsKey(p.getName()))
47                 .forEach(p -> validatePropertyConstraints(p.getConstraints(), oldPropWithConstraints.get(p.getName()).getConstraints()));
48         }
49     }
50
51     private static void validatePropertyConstraints(List<PropertyConstraint> newConstraints, List<PropertyConstraint> oldConstraints) {
52         Map<ConstraintType, PropertyConstraint> oldConstraintsByType = oldConstraints.stream()
53             .filter(c -> nonNull(c) && nonNull(c.getConstraintType())).collect(toMap(PropertyConstraint::getConstraintType, c -> c));
54         newConstraints.stream().filter(c -> nonNull(c) && oldConstraintsByType.containsKey(c.getConstraintType()))
55             .forEach(c -> validatePropertyConstraint(c, oldConstraintsByType.get(c.getConstraintType())));
56     }
57
58     private static void validatePropertyConstraint(PropertyConstraint newConstraint, PropertyConstraint currConstraint) {
59         try {
60             currConstraint.validateValueOnUpdate(newConstraint);
61         } catch (PropertyConstraintException e) {
62             throw new ByActionStatusComponentException(e.getActionStatus(), e.getParams());
63         }
64     }
65 }