Changes for Checkstyle 8.32
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.utils;
22
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import javax.ws.rs.core.Response;
26 import lombok.NonNull;
27 import org.onap.policy.models.base.PfConceptContainer;
28 import org.onap.policy.models.base.PfConceptKey;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.base.PfValidationMessage;
31 import org.onap.policy.models.base.PfValidationResult;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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
56      */
57     public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
58             @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
59         PfValidationResult result = new PfValidationResult();
60
61         if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
62             result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(),
63                     ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
64                     "service template in incoming fragment does not equal existing service template"));
65         }
66
67         JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
68
69         compositeTemplate.setDataTypes(
70                 addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
71         compositeTemplate.setPolicyTypes(
72                 addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
73
74         if (originalTemplate.getTopologyTemplate() != null && fragmentTemplate.getTopologyTemplate() != null) {
75             if (originalTemplate.getTopologyTemplate()
76                     .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) {
77                 compositeTemplate.getTopologyTemplate()
78                         .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(),
79                                 fragmentTemplate.getTopologyTemplate().getPolicies(), result));
80             } else {
81                 result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
82                         ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
83                         "topology template in incoming fragment does not equal existing topology template"));
84             }
85         } else if (fragmentTemplate.getTopologyTemplate() != null) {
86             compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
87         }
88
89         if (result.isValid()) {
90             result = compositeTemplate.validate(result);
91         }
92
93         if (!result.isValid()) {
94             String message = result.toString();
95             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, message);
96         }
97
98         return compositeTemplate;
99     }
100
101     /**
102      * Check entities from a fragment container can be added to an original container.
103      *
104      * @param <S> The type of container
105      *
106      * @param compositeContainer the original container
107      * @param fragmentContainer the fragment being added to the original container
108      * @return the composite container with the result
109      */
110     @SuppressWarnings("unchecked")
111     // @formatter:off
112     private static
113         <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
114             S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
115                     final PfValidationResult result) {
116
117         if (compositeContainer == null) {
118             return fragmentContainer;
119         }
120
121         if (fragmentContainer == null) {
122             return compositeContainer;
123         }
124
125         for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
126                 .getConceptMap().entrySet()) {
127             JpaToscaEntityType<? extends ToscaEntity> containerEntry =
128                     compositeContainer.getConceptMap().get(fragmentEntry.getKey());
129             if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) {
130                 result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(),
131                         ToscaServiceTemplateUtils.class,
132                         ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity"));
133             }
134         }
135
136         // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
137         // original map
138         @SuppressWarnings("rawtypes")
139         Map originalContainerMap = compositeContainer.getConceptMap();
140         originalContainerMap.putAll(fragmentContainer.getConceptMap());
141
142         return compositeContainer;
143     }
144     // @formatter:on
145 }