Add merge utility for service templates
[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
26 import javax.ws.rs.core.Response;
27
28 import lombok.NonNull;
29
30 import org.onap.policy.models.base.PfConceptContainer;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.base.PfValidationMessage;
34 import org.onap.policy.models.base.PfValidationResult;
35 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This utility class provides methods to manage service templates.
45  */
46 public class ToscaServiceTemplateUtils {
47     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateUtils.class);
48
49     /**
50      * Private constructor to prevent subclassing.
51      */
52     private ToscaServiceTemplateUtils() {
53         // Private constructor to prevent subclassing
54     }
55
56     /**
57      * Add a service template fragment to a service template. All entities in the service template fragment must either
58      * a) not exist on the original service template or b) be identical to entities on the original service template.
59      *
60      * @param originalTemplate the original service template
61      * @param fragmentTemplate the fragment being added to the original service template
62      * @return
63      */
64     public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate,
65             @NonNull final JpaToscaServiceTemplate fragmentTemplate) {
66         PfValidationResult result = new PfValidationResult();
67
68         if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) {
69             result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(),
70                     ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
71                     "service template in incoming fragment does not equal existing service template"));
72         }
73
74         JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate);
75
76         compositeTemplate.setDataTypes(
77                 addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result));
78         compositeTemplate.setPolicyTypes(
79                 addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result));
80
81         if (originalTemplate.getTopologyTemplate() != null) {
82             if (originalTemplate.getTopologyTemplate()
83                     .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) {
84                 compositeTemplate.getTopologyTemplate()
85                         .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(),
86                                 fragmentTemplate.getTopologyTemplate().getPolicies(), result));
87             } else {
88                 result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(),
89                         ToscaServiceTemplateUtils.class, ValidationResult.INVALID,
90                         "topology template in incoming fragment does not equal existing topology template"));
91             }
92         } else if (fragmentTemplate.getTopologyTemplate() != null) {
93             compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate()));
94         }
95
96         if (result.isValid()) {
97             result = compositeTemplate.validate(result);
98         }
99
100         if (!result.isValid()) {
101             String message = result.toString();
102             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
103         }
104
105         return compositeTemplate;
106     }
107
108     /**
109      * Check entities from a fragment container can be added to an original container.
110      *
111      * @param <S> The type of container
112      *
113      * @param compositeContainer the original container
114      * @param fragmentContainer the fragment being added to the original container
115      * @return the composite container with the result
116      */
117     @SuppressWarnings("unchecked")
118     // @formatter:off
119     private static
120         <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>>
121             S addFragmentEntitites(final S compositeContainer, final S fragmentContainer,
122                     final PfValidationResult result) {
123
124         if (compositeContainer == null) {
125             return fragmentContainer;
126         }
127
128         if (fragmentContainer == null) {
129             return compositeContainer;
130         }
131
132         for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer
133                 .getConceptMap().entrySet()) {
134             JpaToscaEntityType<? extends ToscaEntity> containerEntry =
135                     compositeContainer.getConceptMap().get(fragmentEntry.getKey());
136             if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) {
137                 result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(),
138                         ToscaServiceTemplateUtils.class,
139                         ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity"));
140             }
141         }
142
143         // This use of a generic map is required to get around typing errors in directly adding the fragment map to the
144         // original map
145         @SuppressWarnings("rawtypes")
146         Map originalContainerMap = compositeContainer.getConceptMap();
147         originalContainerMap.putAll(fragmentContainer.getConceptMap());
148
149         return compositeContainer;
150     }
151     // @formatter:on
152 }