2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.property;
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;
30 import java.util.List;
33 import static java.util.Objects.nonNull;
34 import static java.util.stream.Collectors.toMap;
37 * Provides specific functionality for property constraints
39 public class PropertyConstraintsUtils {
41 private PropertyConstraintsUtils(){}
43 public static void validatePropertiesConstraints(Resource newResource, Resource oldResource) {
44 if(oldResource.getProperties() != null && newResource.getProperties() != null){
45 Map<String, PropertyDefinition> oldPropWithConstraints = oldResource.getProperties()
47 .filter(p -> p.getConstraints() != null)
48 .collect(toMap(PropertyDefinition::getName,p -> p));
50 newResource.getProperties()
52 .filter(p -> p.getConstraints() != null && oldPropWithConstraints.containsKey(p.getName()))
53 .forEach(p -> validatePropertyConstraints(p.getConstraints(), oldPropWithConstraints.get(p.getName()).getConstraints()));
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));
62 newConstraints.stream()
63 .filter(c -> nonNull(c) && oldConstraintsByType.containsKey(c.getConstraintType()))
64 .forEach(c -> validatePropertyConstraint(c, oldConstraintsByType.get(c.getConstraintType())));
67 private static void validatePropertyConstraint(PropertyConstraint newConstraint, PropertyConstraint currConstraint) {
69 currConstraint.validateValueOnUpdate(newConstraint);
70 } catch (PropertyConstraintException e) {
71 throw new ByActionStatusComponentException(e.getActionStatus(), e.getParams());