Catalog alignment
[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
21 package org.openecomp.sdc.be.components.property;
22
23 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
24 import org.openecomp.sdc.be.model.PropertyConstraint;
25 import org.openecomp.sdc.be.model.PropertyDefinition;
26 import org.openecomp.sdc.be.model.Resource;
27 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
28 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
29
30 import java.util.List;
31 import java.util.Map;
32
33 import static java.util.Objects.nonNull;
34 import static java.util.stream.Collectors.toMap;
35
36 /**
37  * Provides specific functionality for property constraints
38  */
39 public class PropertyConstraintsUtils {
40
41     private PropertyConstraintsUtils(){}
42
43     public static void validatePropertiesConstraints(Resource newResource, Resource oldResource) {
44         if(oldResource.getProperties() != null && newResource.getProperties() != null){
45             Map<String, PropertyDefinition> oldPropWithConstraints = oldResource.getProperties()
46                     .stream()
47                     .filter(p -> p.getConstraints() != null)
48                     .collect(toMap(PropertyDefinition::getName,p -> p));
49
50             newResource.getProperties()
51                     .stream()
52                     .filter(p -> p.getConstraints() != null && oldPropWithConstraints.containsKey(p.getName()))
53                     .forEach(p -> validatePropertyConstraints(p.getConstraints(), oldPropWithConstraints.get(p.getName()).getConstraints()));
54         }
55     }
56
57     private static void validatePropertyConstraints(List<PropertyConstraint> newConstraints, List<PropertyConstraint> oldConstraints) {
58         Map <ConstraintType, PropertyConstraint> oldConstraintsByType = oldConstraints.stream()
59                 .filter(c -> nonNull(c) && nonNull(c.getConstraintType()))
60                 .collect(toMap(PropertyConstraint::getConstraintType, c -> c));
61
62         newConstraints.stream()
63                 .filter(c -> nonNull(c) && oldConstraintsByType.containsKey(c.getConstraintType()))
64                 .forEach(c -> validatePropertyConstraint(c, oldConstraintsByType.get(c.getConstraintType())));
65     }
66
67     private static void validatePropertyConstraint(PropertyConstraint newConstraint, PropertyConstraint currConstraint) {
68         try {
69             currConstraint.validateValueOnUpdate(newConstraint);
70         } catch (PropertyConstraintException e) {
71             throw new ByActionStatusComponentException(e.getActionStatus(), e.getParams());
72         }
73     }
74
75 }