Fix upcoming checkstyle issues
[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.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;
38
39 /**
40  * This utility class provides methods to manage service templates.
41  */
42 public class ToscaServiceTemplateUtils {
43     /**
44      * Private constructor to prevent subclassing.
45      */
46     private ToscaServiceTemplateUtils() {
47         // Private constructor to prevent subclassing
48     }
49
50     /**
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.
53      *
54      * @param originalTemplate the original service template
55      * @param fragmentTemplate the fragment being added to the original service template
56      * @return JpaToscaServiceTemplate
57      */
58     public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
59             @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
60         PfValidationResult result = new PfValidationResult();
61
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"));
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                 result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
83                         ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
84                         "topology template in incoming fragment 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 = compositeTemplate.validate(result);
92         }
93
94         if (!result.isValid()) {
95             String message = result.toString();
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 PfValidationResult result) {
117
118         if (compositeContainer == null) {
119             return fragmentContainer;
120         }
121
122         if (fragmentContainer == null) {
123             return compositeContainer;
124         }
125
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"));
134             }
135         }
136
137         // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
138         // original map
139         @SuppressWarnings("rawtypes")
140         Map originalContainerMap = compositeContainer.getConceptMap();
141         originalContainerMap.putAll(fragmentContainer.getConceptMap());
142
143         return compositeContainer;
144     }
145     // @formatter:on
146 }