aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
blob: 55f49b18ec922bb4f8e2c6a4a340426890cd97d2 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy API
 * ================================================================================
 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
 * ================================================================================
 * 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.api.main.startstop;

import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.onap.policy.api.main.config.PolicyPreloadConfig;
import org.onap.policy.api.main.exception.PolicyApiException;
import org.onap.policy.api.main.service.ToscaServiceTemplateService;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * This class creates initial policy types in the database.
 *
 * @author Chenfei Gao (cgao@research.att.com)
 */
@Component
@ConditionalOnProperty(value = "database.initialize", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
public class ApiDatabaseInitializer {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class);
    private static final StandardYamlCoder coder = new StandardYamlCoder();

    private final ToscaServiceTemplateService toscaServiceTemplateService;
    private final PolicyPreloadConfig policyPreloadConfig;

    @PostConstruct
    public void loadData() throws PolicyApiException {
        initializeApiDatabase(policyPreloadConfig.getPolicyTypes(), policyPreloadConfig.getPolicies());
    }

    /**
     * Initializes database by preloading policy types and policies.
     *
     * @param policyTypes List of policy types to preload.
     * @param policies List of policies to preload.
     * @throws PolicyApiException in case of errors.
     */
    public void initializeApiDatabase(final List<String> policyTypes, final List<String> policies)
        throws PolicyApiException {
        try {
            if (alreadyExists()) {
                LOGGER.warn("DB already contains policy data - skipping preload");
                return;
            }

            var serviceTemplate = new ToscaServiceTemplate();
            serviceTemplate.setDataTypes(new LinkedHashMap<>());
            serviceTemplate.setPolicyTypes(new LinkedHashMap<>());
            serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");

            ToscaServiceTemplate createdPolicyTypes =
                preloadServiceTemplate(serviceTemplate, policyTypes, toscaServiceTemplateService::createPolicyType);
            preloadServiceTemplate(createdPolicyTypes, policies, toscaServiceTemplateService::createPolicies);
        } catch (final PolicyApiException | PfModelException | CoderException exp) {
            throw new PolicyApiException(exp);
        }
    }

    private boolean alreadyExists() throws PfModelException {
        try {
            ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService
                .getFilteredPolicyTypes(ToscaEntityFilter.<ToscaPolicyType>builder().build());
            if (!serviceTemplate.getPolicyTypes().isEmpty()) {
                return true;
            }
        } catch (PfModelRuntimeException e) {
            LOGGER.trace("DB does not yet contain policy types", e);
        }
        return false;
    }

    private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate, List<String> entities,
            FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter)
            throws PolicyApiException, CoderException, PfModelException {

        var multiVersionTemplates = new ArrayList<ToscaServiceTemplate>();

        for (String entity : entities) {
            ToscaServiceTemplate singleEntity = deserializeServiceTemplate(entity);

            if (isMultiVersion(serviceTemplate.getPolicyTypes(), singleEntity.getPolicyTypes())) {
                // if this entity introduces a new policy version of an existing policy type,
                // process it on its own as continuing here will override the existing policy type
                // in a different version

                multiVersionTemplates.add(singleEntity);
                LOGGER.warn("Detected multi-versioned type: {}", entity);
                continue;
            }

            // Consolidate data types and policy types
            if (singleEntity.getDataTypes() != null) {
                serviceTemplate.getDataTypes().putAll(singleEntity.getDataTypes());
            }
            if (singleEntity.getPolicyTypes() != null) {
                serviceTemplate.getPolicyTypes().putAll(singleEntity.getPolicyTypes());
            }

            // Consolidate policies
            var topologyTemplate = singleEntity.getToscaTopologyTemplate();
            if (topologyTemplate != null && topologyTemplate.getPolicies() != null) {
                serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
                serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>());
                serviceTemplate.getToscaTopologyTemplate().getPolicies()
                        .addAll(singleEntity.getToscaTopologyTemplate().getPolicies());
            }
        }
        // Preload the specified entities
        ToscaServiceTemplate createdServiceTemplate = getter.apply(serviceTemplate);
        LOGGER.debug("Created initial tosca service template in DB - {}", createdServiceTemplate);

        multiVersionTemplates
            .forEach(mvServiceTemplate -> {
                try {
                    LOGGER.info("Multi-versioned Service Template {}", mvServiceTemplate.getPolicyTypes().keySet());
                    getter.apply(mvServiceTemplate);
                } catch (PfModelException e) {
                    LOGGER.warn("ToscaServiceTemple cannot be preloaded: {}", mvServiceTemplate, e);
                }
            });
        return createdServiceTemplate;
    }

    private ToscaServiceTemplate deserializeServiceTemplate(String entity) throws PolicyApiException, CoderException {
        var entityAsStringYaml = ResourceUtils.getResourceAsString(entity);
        if (entityAsStringYaml == null) {
            throw new PolicyApiException("Preloaded entity cannot be found " + entity);
        }

        ToscaServiceTemplate singleEntity = coder.decode(entityAsStringYaml, ToscaServiceTemplate.class);
        if (singleEntity == null) {
            throw new PolicyApiException("Error deserializing entity from file: " + entity);
        }
        return singleEntity;
    }

    // This method is templated, so it can be used with other derivations of ToscaEntity in the future,
    // if multi-version are desired.

    protected <T extends ToscaEntity> boolean isMultiVersion(Map<String, T> aggEntity,
                                                             Map<String, T> singleEntity) {
        if (aggEntity == null || singleEntity == null) {
            return false;
        }

        // There is a multi-version entity if both key sets have the same
        // entity name but different version.

        return
            Sets.intersection(aggEntity.keySet(), singleEntity.keySet())
                .stream()
                .anyMatch(e -> !Objects.equals(aggEntity.get(e).getVersion(), singleEntity.get(e).getVersion()));
    }

    @FunctionalInterface
    protected interface FunctionWithEx<T, R> {
        R apply(T value) throws PfModelException;
    }
}