aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java
blob: 314c7728d3eb93c702664d4422e79d8915065a13 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 *  Copyright (C) 2020 AT&T Intellectual Property. 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.models.tosca.legacy.provider;

import java.util.List;

import javax.ws.rs.core.Response;

import lombok.NonNull;

import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
 *
 * @author Liam Fallon (liam.fallon@est.tech)
 */
public class LegacyProvider {
    private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);

    public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";

    // Recurring constants
    private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";

    /**
     * Get legacy operational policy.
     *
     * @param dao the DAO to use to access the database
     * @param policyId ID of the policy.
     * @param policyVersion version of the policy.
     * @return the policies found
     * @throws PfModelException on errors getting policies
     */
    public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
            final String policyVersion) throws PfModelException {

        LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);

        LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
                .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));

        LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
                policyVersion, legacyOperationalPolicy);
        return legacyOperationalPolicy;
    }

    /**
     * Create legacy operational policy.
     *
     * @param dao the DAO to use to access the database
     * @param legacyOperationalPolicy the definition of the policy to be created.
     * @return the created policy
     * @throws PfModelException on errors creating policies
     */
    public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {

        LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);

        JpaToscaServiceTemplate legacyOperationalServiceTemplate =
                new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);

        new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);

        LegacyOperationalPolicy createdLegacyOperationalPolicy =
                new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);

        LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
        return createdLegacyOperationalPolicy;
    }

    /**
     * Update legacy operational policy.
     *
     * @param dao the DAO to use to access the database
     * @param legacyOperationalPolicy the definition of the policy to be updated
     * @return the updated policy
     * @throws PfModelException on errors updating policies
     */
    public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {

        LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
        JpaToscaServiceTemplate incomingServiceTemplate =
                new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
        JpaToscaServiceTemplate outgoingingServiceTemplate =
                new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);

        LegacyOperationalPolicy updatedLegacyOperationalPolicy =
                new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);

        LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
        return updatedLegacyOperationalPolicy;
    }

    /**
     * Delete legacy operational policy.
     *
     * @param dao the DAO to use to access the database
     * @param policyId ID of the policy.
     * @param policyVersion version of the policy.
     * @return the deleted policy
     * @throws PfModelException on errors deleting policies
     */
    public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
            @NonNull final String policyVersion) throws PfModelException {

        LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);

        LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
                .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));

        LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
                policyVersion, legacyOperationalPolicy);
        return legacyOperationalPolicy;
    }

    /**
     * Get the JPA Policy for a policy ID and version.
     *
     * @param dao The DAO to search
     * @param policyId the policy ID to search for
     * @param policyVersion the policy version to search for
     * @return the JPA policy found
     * @throws PfModelRuntimeException if a policy is not found
     */
    private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
            final String policyVersion) {
        JpaToscaPolicy foundPolicy = null;
        if (policyVersion == null) {
            foundPolicy = getLatestPolicy(dao, policyId);
        } else {
            foundPolicy = dao.get(JpaToscaPolicy.class,
                    new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
        }

        if (foundPolicy == null) {
            String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
            LOGGER.warn(errorMessage);
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
        }

        // Create the structure of the TOSCA service template to contain the policy type
        JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
        serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
        serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);

        return serviceTemplate;
    }

    /**
     * Delete a legacy policy.
     *
     * @param dao the DAO to use for the deletion
     * @param policyId the policy ID
     * @param policyVersion the policy version
     * @return a service template containing the policy that has been deleted
     */
    private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
            final String policyVersion) {

        final JpaToscaPolicy deletePolicy =
                dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));

        if (deletePolicy == null) {
            String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
            LOGGER.warn(errorMessage);
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
        }

        // Delete the policy
        dao.delete(deletePolicy);

        // Create the structure of the TOSCA service template to contain the policy type
        JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
        serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
        serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);

        return serviceTemplate;
    }

    /**
     * Get the latest policy for a policy ID.
     *
     * @param dao The DAO to read from
     * @param policyId the ID of the policy
     * @return the policy
     */
    private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
        // Get all the policies in the database and check the policy ID against the policies returned
        List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);

        // Find the latest policy that matches the ID
        JpaToscaPolicy newestPolicy = null;

        for (JpaToscaPolicy policy : policyList) {
            if (!policyId.equals(policy.getKey().getName())) {
                continue;
            }

            // We found a matching policy
            if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
                // First policy found
                newestPolicy = policy;
            }
        }
        return newestPolicy;
    }

}