2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
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.common.parameters.BeanValidationResult;
29 import org.onap.policy.models.base.PfConceptContainer;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.base.Validated;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
39 * This utility class provides methods to manage service templates.
41 public class ToscaServiceTemplateUtils {
43 * Private constructor to prevent subclassing.
45 private ToscaServiceTemplateUtils() {
46 // Private constructor to prevent subclassing
50 * Add a service template fragment to a service template. All entities in the service template fragment must either
51 * a) not exist on the original service template or b) be identical to entities on the original service template.
53 * @param originalTemplate the original service template
54 * @param fragmentTemplate the fragment being added to the original service template
55 * @return JpaToscaServiceTemplate
57 public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
58 @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
60 var result = new BeanValidationResult("incoming fragment", fragmentTemplate);
62 if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
63 Validated.addResult(result, "service template",
64 originalTemplate.getKey(),
65 "does not equal existing service template");
68 var 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 Validated.addResult(result, "topology template",
83 originalTemplate.getTopologyTemplate().getKey(),
84 "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.addResult(compositeTemplate.validate("composite template"));
94 if (!result.isValid()) {
95 String message = result.getResult();
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 BeanValidationResult result) {
118 if (compositeContainer == null) {
119 return fragmentContainer;
122 if (fragmentContainer == null) {
123 return compositeContainer;
126 var result2 = new BeanValidationResult("incoming fragment", fragmentContainer);
128 for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
129 .getConceptMap().entrySet()) {
130 JpaToscaEntityType<? extends ToscaEntity> containerEntity =
131 compositeContainer.getConceptMap().get(fragmentEntry.getKey());
132 if (containerEntity != null && containerEntity.compareTo(fragmentEntry.getValue()) != 0) {
133 Validated.addResult(result, "entity", fragmentEntry.getKey(),
134 "does not equal existing entity");
138 if (!result2.isClean()) {
139 result.addResult(result2);
142 // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
144 @SuppressWarnings("rawtypes")
145 Map originalContainerMap = compositeContainer.getConceptMap();
146 originalContainerMap.putAll(fragmentContainer.getConceptMap());
148 return compositeContainer;