aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/rest/ProviderBase.java
blob: c1c63c7f4f5d3db5f8c662dc141fcb4d03678d71 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
 * Modifications Copyright (C) 2020,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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.rest;

import java.util.Collection;
import javax.ws.rs.core.Response.Status;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
import org.onap.policy.pap.main.notification.PolicyNotifier;
import org.onap.policy.pap.main.service.PdpGroupService;
import org.onap.policy.pap.main.service.PolicyAuditService;
import org.onap.policy.pap.main.service.PolicyStatusService;
import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

/**
 * Super class of providers that deploy and undeploy PDP groups. The following items must
 * be in the {@link Registry}:
 * <ul>
 * <li>PDP Modification Lock</li>
 * <li>PDP Modify Request Map</li>
 * <li>PAP DAO Factory</li>
 * </ul>
 */
public abstract class ProviderBase {
    public static final String DB_ERROR_MSG = "DB error";
    public static final String DEFAULT_USER = "PAP";

    /**
     * Lock used when updating PDPs.
     */
    private Object updateLock;

    /**
     * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
     */
    private PdpModifyRequestMap requestMap;

    /**
     * Generates policy notifications based on responses from PDPs.
     */
    private PolicyNotifier notifier;

    private ToscaServiceTemplateService toscaService;

    private PdpGroupService pdpGroupService;

    private PolicyStatusService policyStatusService;

    private PolicyAuditService policyAuditService;

    /**
     * The setter method for injecting into Spring context.
     *
     * @param toscaService the toscaService to set
     */
    @Autowired
    public final void setToscaService(ToscaServiceTemplateService toscaService) {
        this.toscaService = toscaService;
    }

    /**
     * The setter method for injecting into Spring context.
     *
     * @param pdpGroupService the pdpGroupService to set
     */
    @Autowired
    public final void setPdpGroupService(PdpGroupService pdpGroupService) {
        this.pdpGroupService = pdpGroupService;
    }

    /**
     * The setter method for injecting into Spring context.
     *
     * @param policyStatusService the policyStatusService to set
     */
    @Autowired
    public final void setPolicyStatusService(PolicyStatusService policyStatusService) {
        this.policyStatusService = policyStatusService;
    }

    /**
     * The setter method for injecting into Spring context.
     *
     * @param policyAuditService the policyAuditService to set
     */
    @Autowired
    public final void setPolicyAuditService(PolicyAuditService policyAuditService) {
        this.policyAuditService = policyAuditService;
    }

    /**
     * The setter method for injecting into Spring context.
     *
     * @param policyNotifier the policyNotifier to set
     */
    @Autowired
    public final void setPolicyNotifier(PolicyNotifier policyNotifier) {
        this.notifier = policyNotifier;
    }

    /**
     * Initializes the parameters..
     */
    @EventListener(ApplicationReadyEvent.class)
    public void initialize() {
        this.updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
        this.requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
    }

    /**
     * Processes a policy request.
     *
     * @param user user triggering request
     * @param request PDP policy request
     * @param processor function that processes the request
     * @throws PfModelException if an error occurred
     */
    protected <T> void process(String user, T request, BiConsumerWithEx<SessionData, T> processor)
            throws PfModelException {

        synchronized (updateLock) {
            SessionData data;
            var notif = new PolicyNotification();

            try {

                data = new SessionData(user, toscaService, pdpGroupService, policyStatusService, policyAuditService);
                processor.accept(data, request);

                // make all of the DB updates
                data.updateDb(notif);

            } catch (PfModelRuntimeException e) {
                throw e;

            } catch (RuntimeException e) {
                throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
            }

            // publish the requests
            data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));

            // publish the notifications
            notifier.publish(notif);
        }
    }

    /**
     * Processes a policy request.
     *
     * @param request PDP policy request
     * @param processor function that processes the request
     * @throws PfModelException if an error occurred
     */
    protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
        this.process(DEFAULT_USER, request, processor);
    }

    /**
     * Process a single policy from the request.
     *
     * @param data session data
     * @param desiredPolicy request policy
     * @throws PfModelException if an error occurred
     */
    protected void processPolicy(SessionData data, ToscaConceptIdentifierOptVersion desiredPolicy)
            throws PfModelException {

        ToscaPolicy policy = getPolicy(data, desiredPolicy);

        Collection<PdpGroup> groups = getGroups(data, policy.getTypeIdentifier());
        if (groups.isEmpty()) {
            throw new PfModelException(Status.BAD_REQUEST, "policy not supported by any PDP group: "
                    + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
        }

        var updater = makeUpdater(data, policy, desiredPolicy);

        for (PdpGroup group : groups) {
            upgradeGroup(data, group, updater);
        }
    }

    /**
     * Makes a function to update a subgroup. The function is expected to return
     * {@code true} if the subgroup was updated, {@code false} if no update was
     * necessary/appropriate.
     *
     * @param data session data
     * @param policy policy to be added to or removed from each subgroup
     * @param desiredPolicy request policy
     * @return a function to update a subgroup
     */
    protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
            ToscaConceptIdentifierOptVersion desiredPolicy);

    /**
     * Finds the active PDP group(s) that supports the given policy type.
     *
     * @param data session data
     * @param policyType the policy type of interest
     * @return the matching PDP group, or {@code null} if no active group supports the
     *         given PDP types
     * @throws PfModelException if an error occurred
     */
    private Collection<PdpGroup> getGroups(SessionData data, ToscaConceptIdentifier policyType)
            throws PfModelException {

        return data.getActivePdpGroupsByPolicyType(policyType);
    }

    /**
     * Updates a group, assigning a new version number, if it actually changes.
     *
     * @param data session data
     * @param group the original group, to be updated
     * @param updater function to update a group
     * @throws PfModelException if an error occurred
     */
    private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException {

        var updated = false;

        for (PdpSubGroup subgroup : group.getPdpSubgroups()) {

            if (!updater.apply(group, subgroup)) {
                continue;
            }

            updated = true;

            makeUpdates(data, group, subgroup);
        }

        if (updated) {
            // something changed
            data.update(group);
        }
    }

    /**
     * Makes UPDATE messages for each PDP in a subgroup.
     *
     * @param data session data
     * @param group group containing the subgroup
     * @param subgroup subgroup whose PDPs should receive messages
     */
    protected void makeUpdates(SessionData data, PdpGroup group, PdpSubGroup subgroup) {
        for (Pdp pdp : subgroup.getPdpInstances()) {
            data.addUpdate(makeUpdate(data, group, subgroup, pdp));
        }
    }

    /**
     * Makes an UPDATE message for a particular PDP.
     *
     * @param data session data
     * @param group group to which the PDP should belong
     * @param subgroup subgroup to which the PDP should belong
     * @param pdp the PDP of interest
     * @return a new UPDATE message
     */
    private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {

        var update = new PdpUpdate();

        update.setSource(PapConstants.PAP_NAME);
        update.setName(pdp.getInstanceId());
        update.setDescription(group.getDescription());
        update.setPdpGroup(group.getName());
        update.setPdpSubgroup(subgroup.getPdpType());
        update.setPoliciesToBeDeployed(data.getPoliciesToBeDeployed());
        update.setPoliciesToBeUndeployed(data.getPoliciesToBeUndeployed());

        return update;
    }

    /**
     * Gets the specified policy.
     *
     * @param data session data
     * @param ident policy identifier, with an optional version
     * @return the policy of interest
     * @throws PfModelRuntimeException if an error occurred or the policy was not found
     */
    private ToscaPolicy getPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) {
        try {
            ToscaPolicy policy = data.getPolicy(ident);
            if (policy == null) {
                throw new PfModelRuntimeException(Status.NOT_FOUND,
                        "cannot find policy: " + ident.getName() + " " + ident.getVersion());
            }

            return policy;

        } catch (PfModelException e) {
            throw new PfModelRuntimeException(e.getErrorResponse().getResponseCode(),
                    e.getErrorResponse().getErrorMessage(), e);
        }
    }

    @FunctionalInterface
    public static interface BiConsumerWithEx<F, S> {
        /**
         * Performs this operation on the given arguments.
         *
         * @param firstArg the first input argument
         * @param secondArg the second input argument
         * @throws PfModelException if an error occurred
         */
        void accept(F firstArg, S secondArg) throws PfModelException;
    }

    @FunctionalInterface
    public static interface Updater {
        boolean apply(PdpGroup group, PdpSubGroup subgroup) throws PfModelException;
    }
}