aboutsummaryrefslogtreecommitdiffstats
path: root/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
blob: 8ff0dd102a561f2d2292554f78bc5b265dd15c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.RequiredArgsConstructor;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaServiceTemplateRepository;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class ServiceTemplateProvider {

    private final ToscaServiceTemplateRepository serviceTemplateRepository;

    /**
     * Create service template.
     *
     * @param serviceTemplate the service template to be created
     * @return the created service template
     * @throws PfModelException on errors creating the service template
     */
    public ToscaServiceTemplate createServiceTemplate(final ToscaServiceTemplate serviceTemplate)
            throws PfModelException {
        try {
            var result = serviceTemplateRepository.save(ProviderUtils.getJpaAndValidate(serviceTemplate,
                    JpaToscaServiceTemplate::new, "toscaServiceTemplate"));
            return result.toAuthorative();
        } catch (IllegalArgumentException e) {
            throw new PfModelException(Status.BAD_REQUEST, "Error in save serviceTemplate", e);
        }
    }

    /**
     * Delete service template.
     *
     * @param name the name of the service template to delete.
     * @param version the version of the service template to delete.
     * @return the TOSCA service template that was deleted
     * @throws PfModelException on errors deleting policy types
     */
    public ToscaServiceTemplate deleteServiceTemplate(final String name, final String version) throws PfModelException {
        var serviceTemplateKey = new PfConceptKey(name, version);
        var jpaDelete = serviceTemplateRepository.findById(serviceTemplateKey);
        if (jpaDelete.isEmpty()) {
            String errorMessage = "delete of serviceTemplate \"" + serviceTemplateKey.getId()
                    + "\" failed, serviceTemplate does not exist";
            throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
        }
        serviceTemplateRepository.deleteById(serviceTemplateKey);
        return jpaDelete.get().toAuthorative();
    }

    /**
     * Get the requested control loop definitions.
     *
     * @param name the name of the definition to get, null for all definitions
     * @param version the version of the definition to get, null for all definitions
     * @return the control loop definitions
     * @throws PfModelException on errors getting control loop definitions
     */
    @Transactional(readOnly = true)
    public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
        var serviceTemplateKey = new PfConceptKey(name, version);
        var jpaServiceTemplates = serviceTemplateRepository.findById(serviceTemplateKey);
        if (jpaServiceTemplates.isEmpty()) {
            throw new PfModelException(Status.NOT_FOUND, "Control Loop definitions not found");
        }
        return jpaServiceTemplates.get().toAuthorative();
    }

    /**
     * Get service templates.
     *
     * @return the topology templates found
     * @throws PfModelException on errors getting service templates
     */
    @Transactional(readOnly = true)
    public List<ToscaServiceTemplate> getAllServiceTemplates() throws PfModelException {
        var jpaList = serviceTemplateRepository.findAll();
        return ProviderUtils.asEntityList(jpaList);
    }

    /**
     * Get service templates.
     *
     * @param name the name of the topology template to get, set to null to get all service templates
     * @param version the version of the service template to get, set to null to get all service templates
     * @return the topology templates found
     * @throws PfModelException on errors getting service templates
     */
    @Transactional(readOnly = true)
    public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
            throws PfModelException {
        var jpaList = serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, name, version);
        return ProviderUtils.asEntityList(jpaList);
    }

    /**
     * Get the initial node types with common or instance properties.
     *
     * @param fullNodeTypes map of all the node types in the specified template
     * @param common boolean to indicate whether common or instance properties are required
     * @return node types map that only has common properties
     * @throws PfModelException on errors getting node type with common properties
     */
    private Map<String, ToscaNodeType> getInitialNodeTypesMap(Map<String, ToscaNodeType> fullNodeTypes,
            boolean common) {

        var tempNodeTypesMap = new HashMap<String, ToscaNodeType>();

        fullNodeTypes.forEach((key, nodeType) -> {
            var tempToscaNodeType = new ToscaNodeType();
            tempToscaNodeType.setName(key);

            var resultantPropertyMap = findCommonOrInstancePropsInNodeTypes(nodeType, common);

            if (!resultantPropertyMap.isEmpty()) {
                tempToscaNodeType.setProperties(resultantPropertyMap);
                tempNodeTypesMap.put(key, tempToscaNodeType);
            }
        });
        return tempNodeTypesMap;
    }

    private Map<String, ToscaProperty> findCommonOrInstancePropsInNodeTypes(ToscaNodeType nodeType, boolean common) {

        var tempCommonPropertyMap = new HashMap<String, ToscaProperty>();
        var tempInstancePropertyMap = new HashMap<String, ToscaProperty>();

        nodeType.getProperties().forEach((propKey, prop) -> {

            if (prop.getMetadata() != null) {
                prop.getMetadata().forEach((k, v) -> {
                    if (k.equals("common") && v.equals("true") && common) {
                        tempCommonPropertyMap.put(propKey, prop);
                    } else if (k.equals("common") && v.equals("false") && !common) {
                        tempInstancePropertyMap.put(propKey, prop);
                    }

                });
            } else {
                tempInstancePropertyMap.put(propKey, prop);
            }
        });

        if (tempCommonPropertyMap.isEmpty() && !common) {
            return tempInstancePropertyMap;
        } else {
            return tempCommonPropertyMap;
        }
    }

    /**
     * Get the node types derived from those that have common properties.
     *
     * @param initialNodeTypes map of all the node types in the specified template
     * @param filteredNodeTypes map of all the node types that have common or instance properties
     * @return all node types that have common properties including their children
     * @throws PfModelException on errors getting node type with common properties
     */
    private Map<String, ToscaNodeType> getFinalNodeTypesMap(Map<String, ToscaNodeType> initialNodeTypes,
            Map<String, ToscaNodeType> filteredNodeTypes) {
        for (var i = 0; i < initialNodeTypes.size(); i++) {
            initialNodeTypes.forEach((key, nodeType) -> {
                var tempToscaNodeType = new ToscaNodeType();
                tempToscaNodeType.setName(key);

                if (filteredNodeTypes.get(nodeType.getDerivedFrom()) != null) {
                    tempToscaNodeType.setName(key);

                    var finalProps = new HashMap<String, ToscaProperty>(
                            filteredNodeTypes.get(nodeType.getDerivedFrom()).getProperties());

                    tempToscaNodeType.setProperties(finalProps);
                } else {
                    return;
                }
                filteredNodeTypes.putIfAbsent(key, tempToscaNodeType);

            });
        }
        return filteredNodeTypes;
    }

    /**
     * Get the requested node types with common or instance properties.
     *
     * @param common boolean indicating common or instance properties
     * @param serviceTemplate the ToscaServiceTemplate
     * @return the node types with common or instance properties
     * @throws PfModelException on errors getting node type properties
     */
    public Map<String, ToscaNodeType> getCommonOrInstancePropertiesFromNodeTypes(boolean common,
            ToscaServiceTemplate serviceTemplate) throws PfModelException {
        var tempNodeTypesMap = this.getInitialNodeTypesMap(serviceTemplate.getNodeTypes(), common);

        return this.getFinalNodeTypesMap(serviceTemplate.getNodeTypes(), tempNodeTypesMap);

    }

    /**
     * Get node templates with appropriate common or instance properties added.
     *
     * @param initialNodeTemplates map of all the node templates in the specified template
     * @param nodeTypeProps map of all the node types that have common or instance properties including children
     * @return all node templates with appropriate common or instance properties added
     * @throws PfModelException on errors getting map of node templates with common or instance properties added
     */
    public Map<String, ToscaNodeTemplate> getDerivedCommonOrInstanceNodeTemplates(
            Map<String, ToscaNodeTemplate> initialNodeTemplates, Map<String, ToscaNodeType> nodeTypeProps) {

        var finalNodeTemplatesMap = new HashMap<String, ToscaNodeTemplate>();

        initialNodeTemplates.forEach((templateKey, template) -> {
            if (nodeTypeProps.containsKey(template.getType())) {
                var finalMergedProps = new HashMap<String, Object>();

                nodeTypeProps.get(template.getType()).getProperties().forEach(finalMergedProps::putIfAbsent);

                template.setProperties(finalMergedProps);

                finalNodeTemplatesMap.put(templateKey, template);
            } else {
                return;
            }
        });
        return finalNodeTemplatesMap;
    }
}