aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/rest/SessionData.java
blob: 4cea257fc3d12884d577fb5b8baa7a74c0fd2700 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*-
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021-2022 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.
 * ============LICENSE_END=========================================================
 */

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

import com.google.re2j.Pattern;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
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.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter.ToscaTypedEntityFilterBuilder;
import org.onap.policy.pap.main.notification.DeploymentStatus;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Data used during a single REST call when updating PDP policies.
 */
public class SessionData {
    private static final Logger logger = LoggerFactory.getLogger(SessionData.class);

    /**
     * If a version string matches this, then it is just a prefix (i.e., major or major.minor).
     */
    private static final Pattern VERSION_PREFIX_PAT = Pattern.compile("[^.]+(?:[.][^.]+)?");

    /**
     * Maps a group name to its group data. This accumulates the set of groups to be created and updated when the REST
     * call completes.
     */
    private final Map<String, GroupData> groupCache = new HashMap<>();

    /**
     * Maps a policy type to the list of matching groups. Every group appearing within this map has a corresponding
     * entry in {@link #groupCache}.
     */
    private final Map<ToscaConceptIdentifier, List<GroupData>> type2groups = new HashMap<>();

    /**
     * Maps a PDP name to its most recently generated update and state-change requests.
     */
    private final Map<String, Pair<PdpUpdate, PdpStateChange>> pdpRequests = new HashMap<>();

    /**
     * Maps a policy's identifier to the policy.
     */
    private final Map<ToscaConceptIdentifierOptVersion, ToscaPolicy> policyCache = new HashMap<>();

    /**
     * Maps a policy type's identifier to the policy.
     */
    private final Map<ToscaConceptIdentifier, ToscaPolicyType> typeCache = new HashMap<>();

    /**
     * Map's a policy's identifier to the policies for deployment.
     */
    private final Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployed = new HashMap<>();

    /**
     * Set of policies to be undeployed.
     */
    private final Set<ToscaConceptIdentifier> policiesToBeUndeployed = new HashSet<>();

    /**
     * User starting requests.
     */
    @Getter
    private final String user;

    /**
     * Tracks policy deployment status so notifications can be generated.
     */
    private final DeploymentStatus deployStatus;

    private final PolicyAuditManager auditManager;

    private final ToscaServiceTemplateService toscaService;

    private final PdpGroupService pdpGroupService;

    /**
     * Constructs the object.
     *
     * @param user user triggering the request
     * @param policyAuditService the policyAuditService
     * @param policyStatusService the policyStatusService
     * @param pdpGroupService the pdpGroupService
     * @param toscaService the toscaService
     */
    public SessionData(String user, ToscaServiceTemplateService toscaService, PdpGroupService pdpGroupService,
        PolicyStatusService policyStatusService, PolicyAuditService policyAuditService) {
        this.toscaService = toscaService;
        this.pdpGroupService = pdpGroupService;
        this.deployStatus = makeDeploymentStatus(policyStatusService);
        this.auditManager = makePolicyAuditManager(policyAuditService);
        this.user = user;
    }

    /**
     * Gets the policy type, referenced by an identifier. Loads it from the cache, if possible. Otherwise, gets it from
     * the DB.
     *
     * @param desiredType policy type identifier
     * @return the specified policy type
     * @throws PfModelException if an error occurred
     */
    public ToscaPolicyType getPolicyType(ToscaConceptIdentifier desiredType) throws PfModelException {

        ToscaPolicyType type = typeCache.get(desiredType);
        if (type == null) {

            List<ToscaPolicyType> lst = toscaService.getPolicyTypeList(desiredType.getName(), desiredType.getVersion());
            if (lst.isEmpty()) {
                return null;
            }

            type = lst.get(0);
            typeCache.put(desiredType, type);
        }

        return type;
    }

    /**
     * Gets the policy, referenced by an identifier. Loads it from the cache, if possible. Otherwise, gets it from the
     * DB.
     *
     * @param desiredPolicy policy identifier
     * @return the specified policy
     * @throws PfModelException if an error occurred
     */
    public ToscaPolicy getPolicy(ToscaConceptIdentifierOptVersion desiredPolicy) throws PfModelException {

        ToscaPolicy policy = policyCache.get(desiredPolicy);
        if (policy == null) {
            ToscaTypedEntityFilterBuilder<ToscaPolicy> filterBuilder =
                ToscaTypedEntityFilter.<ToscaPolicy>builder().name(desiredPolicy.getName());
            setPolicyFilterVersion(filterBuilder, desiredPolicy.getVersion());

            List<ToscaPolicy> lst = toscaService.getFilteredPolicyList(filterBuilder.build());
            if (lst.isEmpty()) {
                return null;
            }

            policy = lst.get(0);
            policyCache.put(desiredPolicy, policy);
        }

        // desired version may have only been a prefix - cache with full identifier, too
        policyCache.putIfAbsent(new ToscaConceptIdentifierOptVersion(policy.getIdentifier()), policy);

        return policy;
    }

    /**
     * Sets the "version" in a policy filter.
     *
     * @param filterBuilder filter builder whose version should be set
     * @param desiredVersion desired version
     */
    private void setPolicyFilterVersion(ToscaTypedEntityFilterBuilder<ToscaPolicy> filterBuilder,
            String desiredVersion) {

        if (desiredVersion == null) {
            // no version specified - get the latest
            filterBuilder.version(ToscaTypedEntityFilter.LATEST_VERSION);

        } else if (isVersionPrefix(desiredVersion)) {
            // version prefix provided - match the prefix and then pick the latest
            filterBuilder.versionPrefix(desiredVersion + ".").version(ToscaTypedEntityFilter.LATEST_VERSION);

        } else {
            // must be an exact match
            filterBuilder.version(desiredVersion);
        }
    }

    /**
     * Determines if a version contains only a prefix.
     *
     * @param version version to inspect
     * @return {@code true} if the version contains only a prefix, {@code false} if it is fully qualified
     */
    public static boolean isVersionPrefix(String version) {
        return VERSION_PREFIX_PAT.matcher(version).matches();
    }

    /**
     * Adds an update and state-change to the sets, replacing any previous entries for the given PDP.
     *
     * @param update the update to be added
     * @param change the state-change to be added
     */
    public void addRequests(PdpUpdate update, PdpStateChange change) {
        if (!update.getName().equals(change.getName())) {
            throw new IllegalArgumentException("PDP name mismatch " + update.getName() + ", " + change.getName());
        }

        logger.info("add update and state-change {} {} {} policies={}", update.getName(), update.getPdpGroup(),
                update.getPdpSubgroup(), update.getPoliciesToBeDeployed().size());
        pdpRequests.put(update.getName(), Pair.of(update, change));
    }

    /**
     * Adds an update to the set of updates, replacing any previous entry for the given PDP.
     *
     * @param update the update to be added
     */
    public void addUpdate(PdpUpdate update) {
        logger.info("add update {} {} {} policies={}", update.getName(), update.getPdpGroup(), update.getPdpSubgroup(),
                update.getPoliciesToBeDeployed().size());
        pdpRequests.compute(update.getName(), (name, data) -> Pair.of(update, (data == null ? null : data.getRight())));
    }

    /**
     * Adds a state-change to the set of state-change requests, replacing any previous entry for the given PDP.
     *
     * @param change the state-change to be added
     */
    public void addStateChange(PdpStateChange change) {
        logger.info("add state-change {}", change.getName());
        pdpRequests.compute(change.getName(), (name, data) -> Pair.of((data == null ? null : data.getLeft()), change));
    }

    /**
     * Determines if any changes were made due to the REST call.
     *
     * @return {@code true} if nothing was changed, {@code false} if something was changed
     */
    public boolean isUnchanged() {
        return groupCache.values().stream().allMatch(GroupData::isUnchanged);
    }

    /**
     * Gets the accumulated PDP requests.
     *
     * @return the PDP requests
     */
    public Collection<Pair<PdpUpdate, PdpStateChange>> getPdpRequests() {
        return pdpRequests.values();
    }

    /**
     * Gets the accumulated PDP update requests.
     *
     * @return the PDP requests
     */
    public List<PdpUpdate> getPdpUpdates() {
        return pdpRequests.values().stream().filter(req -> req.getLeft() != null).map(Pair::getLeft)
                .collect(Collectors.toList());
    }

    /**
     * Gets the accumulated PDP state-change requests.
     *
     * @return the PDP requests
     */
    public List<PdpStateChange> getPdpStateChanges() {
        return pdpRequests.values().stream().filter(req -> req.getRight() != null).map(Pair::getRight)
                .collect(Collectors.toList());
    }

    /**
     * Creates a group.
     *
     * @param newGroup the new group
     */
    public void create(PdpGroup newGroup) {
        String name = newGroup.getName();

        if (groupCache.put(name, new GroupData(newGroup, true)) != null) {
            throw new IllegalStateException("group already cached: " + name);
        }

        logger.info("create cached group {}", newGroup.getName());
    }

    /**
     * Updates a group.
     *
     * @param newGroup the updated group
     */
    public void update(PdpGroup newGroup) {
        String name = newGroup.getName();
        GroupData data = groupCache.get(name);
        if (data == null) {
            throw new IllegalStateException("group not cached: " + name);
        }

        logger.info("update cached group {}", newGroup.getName());
        data.update(newGroup);
    }

    /**
     * Gets the group by the given name.
     *
     * @param name name of the group to get
     * @return the group, or {@code null} if it does not exist
     * @throws PfModelException if an error occurred
     */
    public PdpGroup getGroup(String name) throws PfModelException {

        GroupData data = groupCache.get(name);
        if (data == null) {
            List<PdpGroup> lst = pdpGroupService.getPdpGroups(name);
            if (lst.isEmpty()) {
                logger.info("unknown group {}", name);
                return null;
            }

            logger.info("cache group {}", name);
            data = new GroupData(lst.get(0));
            groupCache.put(name, data);

            deployStatus.loadByGroup(name);

        } else {
            logger.info("use cached group {}", name);
        }

        return data.getGroup();
    }

    /**
     * Gets the active groups supporting the given policy.
     *
     * @param type desired policy type
     * @return the active groups supporting the given policy
     */
    public List<PdpGroup> getActivePdpGroupsByPolicyType(ToscaConceptIdentifier type) {
        /*
         * Cannot use computeIfAbsent() because the enclosed code throws an unchecked exception and handling that would
         * obfuscate the code too much, thus disabling the sonar.
         */
        List<GroupData> data = type2groups.get(type); // NOSONAR
        if (data == null) {
            PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(Collections.singletonList(type))
                    .groupState(PdpState.ACTIVE).build();

            List<PdpGroup> groups = pdpGroupService.getFilteredPdpGroups(filter);

            data = groups.stream().map(this::addGroup).collect(Collectors.toList());
            type2groups.put(type, data);
        }

        return data.stream().map(GroupData::getGroup).collect(Collectors.toList());
    }

    /**
     * Gets the list of policies to be deployed to the PDPs.
     *
     * @return a list of policies to be deployed
     */
    public List<ToscaPolicy> getPoliciesToBeDeployed() {
        return new LinkedList<>(this.policiesToBeDeployed.values());
    }

    /**
     * Gets the list of policies to be undeployed from the PDPs.
     *
     * @return a list of policies to be undeployed
     */
    public List<ToscaConceptIdentifier> getPoliciesToBeUndeployed() {
        return new LinkedList<>(this.policiesToBeUndeployed);
    }

    /**
     * Adds a group to the group cache, if it isn't already in the cache.
     *
     * @param group the group to be added
     * @return the cache entry
     */
    private GroupData addGroup(PdpGroup group) {
        GroupData data = groupCache.get(group.getName());
        if (data == null) {
            logger.info("cache group {}", group.getName());
            data = new GroupData(group);
            groupCache.put(group.getName(), data);

        } else {
            logger.info("use cached group {}", group.getName());
        }

        return data;
    }

    /**
     * Update the DB with the changes.
     *
     * @param notification notification to which to add policy status
     */
    public void updateDb(PolicyNotification notification) {
        // create new groups
        List<GroupData> created = groupCache.values().stream().filter(GroupData::isNew).collect(Collectors.toList());
        if (!created.isEmpty()) {
            if (logger.isInfoEnabled()) {
                created.forEach(group -> logger.info("creating DB group {}", group.getGroup().getName()));
            }
            pdpGroupService.createPdpGroups(created.stream().map(GroupData::getGroup).collect(Collectors.toList()));
        }

        // update existing groups
        List<GroupData> updated =
                groupCache.values().stream().filter(GroupData::isUpdated).collect(Collectors.toList());
        if (!updated.isEmpty()) {
            if (logger.isInfoEnabled()) {
                updated.forEach(group -> logger.info("updating DB group {}", group.getGroup().getName()));
            }
            pdpGroupService.updatePdpGroups(updated.stream().map(GroupData::getGroup).collect(Collectors.toList()));
        }

        // send audits records to DB
        auditManager.saveRecordsToDb();

        // flush deployment status records to the DB
        deployStatus.flush(notification);
    }

    /**
     * Deletes a group from the DB, immediately (i.e., without caching the request to be executed later).
     *
     * @param group the group to be deleted
     */
    public void deleteGroupFromDb(PdpGroup group) {
        logger.info("deleting DB group {}", group.getName());
        pdpGroupService.deletePdpGroup(group.getName());
    }

    /**
     * Adds policy deployment data.
     *
     * @param policy policy being deployed
     * @param pdps PDPs to which the policy is being deployed
     * @param pdpGroup PdpGroup containing the PDP of interest
     * @param pdpType PDP type (i.e., PdpSubGroup) containing the PDP of interest
     * @throws PfModelException if an error occurred
     */
    protected void trackDeploy(ToscaPolicy policy, Collection<String> pdps, String pdpGroup, String pdpType)
            throws PfModelException {
        ToscaConceptIdentifier policyId = policy.getIdentifier();
        policiesToBeDeployed.put(policyId, policy);

        addData(policyId, pdps, pdpGroup, pdpType, true);
        auditManager.addDeploymentAudit(policyId, pdpGroup, pdpType, user);
    }

    /**
     * Adds policy undeployment data.
     *
     * @param policyId ID of the policy being undeployed
     * @param pdps PDPs to which the policy is being undeployed
     * @param pdpGroup PdpGroup containing the PDP of interest
     * @param pdpType PDP type (i.e., PdpSubGroup) containing the PDP of interest
     * @throws PfModelException if an error occurred
     */
    protected void trackUndeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup,
            String pdpType) throws PfModelException {
        policiesToBeUndeployed.add(policyId);

        addData(policyId, pdps, pdpGroup, pdpType, false);
        auditManager.addUndeploymentAudit(policyId, pdpGroup, pdpType, user);
    }

    /**
     * Adds policy deployment/undeployment data.
     *
     * @param policyId ID of the policy being deployed/undeployed
     * @param pdps PDPs to which the policy is being deployed/undeployed
     * @param deploy {@code true} if the policy is being deployed, {@code false} if undeployed
     * @param pdpGroup PdpGroup containing the PDP of interest
     * @param pdpType PDP type (i.e., PdpSubGroup) containing the PDP of interest
     * @throws PfModelException if an error occurred
     */
    private void addData(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup, String pdpType,
            boolean deploy) throws PfModelException {

        // delete all records whose "deploy" flag is the opposite of what we want
        deployStatus.deleteDeployment(policyId, !deploy);

        var optid = new ToscaConceptIdentifierOptVersion(policyId);
        ToscaConceptIdentifier policyType = getPolicy(optid).getTypeIdentifier();

        for (String pdp : pdps) {
            deployStatus.deploy(pdp, policyId, policyType, pdpGroup, pdpType, deploy);
        }
    }

    // these may be overridden by junit tests

    protected DeploymentStatus makeDeploymentStatus(PolicyStatusService policyStatusService) {
        return new DeploymentStatus(policyStatusService);
    }

    protected PolicyAuditManager makePolicyAuditManager(PolicyAuditService policyAuditService) {
        return new PolicyAuditManager(policyAuditService);
    }
}