Use ValidationResult for models v2.0
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / utils / ToscaServiceTemplateUtils.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.utils;
23
24 import java.util.Map;
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;
37
38 /**
39  * This utility class provides methods to manage service templates.
40  */
41 public class ToscaServiceTemplateUtils {
42     /**
43      * Private constructor to prevent subclassing.
44      */
45     private ToscaServiceTemplateUtils() {
46         // Private constructor to prevent subclassing
47     }
48
49     /**
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.
52      *
53      * @param originalTemplate the original service template
54      * @param fragmentTemplate the fragment being added to the original service template
55      * @return JpaToscaServiceTemplate
56      */
57     public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
58             @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
59
60         BeanValidationResult result = new BeanValidationResult("incoming fragment", fragmentTemplate);
61
62         if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
63             Validated.addResult(result, "service template",
64                             originalTemplate.getKey(),
65                             "does not equal existing service template");
66         }
67
68         JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
69
70         compositeTemplate.setDataTypes(
71                 addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
72         compositeTemplate.setPolicyTypes(
73                 addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
74
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));
81             } else {
82                 Validated.addResult(result, "topology template",
83                                 originalTemplate.getTopologyTemplate().getKey(),
84                                 "does not equal existing topology template");
85             }
86         } else if (fragmentTemplate.getTopologyTemplate() != null) {
87             compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
88         }
89
90         if (result.isValid()) {
91             result.addResult(compositeTemplate.validate("composite template"));
92         }
93
94         if (!result.isValid()) {
95             String message = result.getResult();
96             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, message);
97         }
98
99         return compositeTemplate;
100     }
101
102     /**
103      * Check entities from a fragment container can be added to an original container.
104      *
105      * @param <S> The type of container
106      *
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
110      */
111     @SuppressWarnings("unchecked")
112     // @formatter:off
113     private static
114         <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
115             S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
116                     final BeanValidationResult result) {
117
118         if (compositeContainer == null) {
119             return fragmentContainer;
120         }
121
122         if (fragmentContainer == null) {
123             return compositeContainer;
124         }
125
126         BeanValidationResult result2 = new BeanValidationResult("incoming fragment", fragmentContainer);
127
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");
135             }
136         }
137
138         if (!result2.isClean()) {
139             result.addResult(result2);
140         }
141
142         // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
143         // original map
144         @SuppressWarnings("rawtypes")
145         Map originalContainerMap = compositeContainer.getConceptMap();
146         originalContainerMap.putAll(fragmentContainer.getConceptMap());
147
148         return compositeContainer;
149     }
150     // @formatter:on
151 }