2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.utils;
24 import java.util.Map.Entry;
26 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
30 import org.onap.policy.models.base.PfConceptContainer;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.base.PfValidationMessage;
34 import org.onap.policy.models.base.PfValidationResult;
35 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
42 * This utility class provides methods to manage service templates.
44 public class ToscaServiceTemplateUtils {
46 * Private constructor to prevent subclassing.
48 private ToscaServiceTemplateUtils() {
49 // Private constructor to prevent subclassing
53 * Add a service template fragment to a service template. All entities in the service template fragment must either
54 * a) not exist on the original service template or b) be identical to entities on the original service template.
56 * @param originalTemplate the original service template
57 * @param fragmentTemplate the fragment being added to the original service template
60 public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
61 @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
62 PfValidationResult result = new PfValidationResult();
64 if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
65 result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(),
66 ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
67 "service template in incoming fragment does not equal existing service template"));
70 JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
72 compositeTemplate.setDataTypes(
73 addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
74 compositeTemplate.setPolicyTypes(
75 addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
77 if (originalTemplate.getTopologyTemplate() != null && fragmentTemplate.getTopologyTemplate() != null) {
78 if (originalTemplate.getTopologyTemplate()
79 .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) {
80 compositeTemplate.getTopologyTemplate()
81 .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(),
82 fragmentTemplate.getTopologyTemplate().getPolicies(), result));
84 result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
85 ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
86 "topology template in incoming fragment does not equal existing topology template"));
88 } else if (fragmentTemplate.getTopologyTemplate() != null) {
89 compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
92 if (result.isValid()) {
93 result = compositeTemplate.validate(result);
96 if (!result.isValid()) {
97 String message = result.toString();
98 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
101 return compositeTemplate;
105 * Check entities from a fragment container can be added to an original container.
107 * @param <S> The type of container
109 * @param compositeContainer the original container
110 * @param fragmentContainer the fragment being added to the original container
111 * @return the composite container with the result
113 @SuppressWarnings("unchecked")
116 <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
117 S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
118 final PfValidationResult result) {
120 if (compositeContainer == null) {
121 return fragmentContainer;
124 if (fragmentContainer == null) {
125 return compositeContainer;
128 for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
129 .getConceptMap().entrySet()) {
130 JpaToscaEntityType<? extends ToscaEntity> containerEntry =
131 compositeContainer.getConceptMap().get(fragmentEntry.getKey());
132 if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) {
133 result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(),
134 ToscaServiceTemplateUtils.class,
135 ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity"));
139 // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
141 @SuppressWarnings("rawtypes")
142 Map originalContainerMap = compositeContainer.getConceptMap();
143 originalContainerMap.putAll(fragmentContainer.getConceptMap());
145 return compositeContainer;