2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.utils;
25 import java.util.Map.Entry;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.models.base.PfConceptContainer;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfModelRuntimeException;
31 import org.onap.policy.models.base.PfValidationMessage;
32 import org.onap.policy.models.base.PfValidationResult;
33 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
40 * This utility class provides methods to manage service templates.
42 public class ToscaServiceTemplateUtils {
44 * Private constructor to prevent subclassing.
46 private ToscaServiceTemplateUtils() {
47 // Private constructor to prevent subclassing
51 * Add a service template fragment to a service template. All entities in the service template fragment must either
52 * a) not exist on the original service template or b) be identical to entities on the original service template.
54 * @param originalTemplate the original service template
55 * @param fragmentTemplate the fragment being added to the original service template
56 * @return JpaToscaServiceTemplate
58 public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
59 @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
60 PfValidationResult result = new PfValidationResult();
62 if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
63 result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(),
64 ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
65 "service template in incoming fragment does not equal existing service template"));
68 JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
70 compositeTemplate.setDataTypes(
71 addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
72 compositeTemplate.setPolicyTypes(
73 addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
75 if (originalTemplate.getTopologyTemplate() != null && fragmentTemplate.getTopologyTemplate() != null) {
76 if (originalTemplate.getTopologyTemplate()
77 .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) {
78 compositeTemplate.getTopologyTemplate()
79 .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(),
80 fragmentTemplate.getTopologyTemplate().getPolicies(), result));
82 result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
83 ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
84 "topology template in incoming fragment does not equal existing topology template"));
86 } else if (fragmentTemplate.getTopologyTemplate() != null) {
87 compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
90 if (result.isValid()) {
91 result = compositeTemplate.validate(result);
94 if (!result.isValid()) {
95 String message = result.toString();
96 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, message);
99 return compositeTemplate;
103 * Check entities from a fragment container can be added to an original container.
105 * @param <S> The type of container
107 * @param compositeContainer the original container
108 * @param fragmentContainer the fragment being added to the original container
109 * @return the composite container with the result
111 @SuppressWarnings("unchecked")
114 <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
115 S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
116 final PfValidationResult result) {
118 if (compositeContainer == null) {
119 return fragmentContainer;
122 if (fragmentContainer == null) {
123 return compositeContainer;
126 for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
127 .getConceptMap().entrySet()) {
128 JpaToscaEntityType<? extends ToscaEntity> containerEntry =
129 compositeContainer.getConceptMap().get(fragmentEntry.getKey());
130 if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) {
131 result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(),
132 ToscaServiceTemplateUtils.class,
133 ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity"));
137 // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
139 @SuppressWarnings("rawtypes")
140 Map originalContainerMap = compositeContainer.getConceptMap();
141 originalContainerMap.putAll(fragmentContainer.getConceptMap());
143 return compositeContainer;