aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java
blob: 437d7a1117762c7c646e6d2a9ff7802c2c6b1734 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019 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.
 * ============LICENSE_END=========================================================
 */

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

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.base.PfModelException;
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.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.ToscaPolicyFilterBuilder;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
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("[^.]+(?:[.][^.]+)?");

    /**
     * DB provider.
     */
    private final PolicyModelsProvider dao;

    /**
     * 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<ToscaPolicyTypeIdentifier, 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<ToscaPolicyIdentifierOptVersion, ToscaPolicy> policyCache = new HashMap<>();

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


    /**
     * Constructs the object.
     *
     * @param dao DAO provider
     */
    public SessionData(PolicyModelsProvider dao) {
        this.dao = dao;
    }

    /**
     * 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(ToscaPolicyTypeIdentifier desiredType) throws PfModelException {

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

            List<ToscaPolicyType> lst = dao.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(ToscaPolicyIdentifierOptVersion desiredPolicy) throws PfModelException {

        ToscaPolicy policy = policyCache.get(desiredPolicy);
        if (policy == null) {
            ToscaPolicyFilterBuilder filterBuilder = ToscaPolicyFilter.builder().name(desiredPolicy.getName());
            setPolicyFilterVersion(filterBuilder, desiredPolicy.getVersion());

            List<ToscaPolicy> lst = dao.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 ToscaPolicyIdentifierOptVersion(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(ToscaPolicyFilterBuilder filterBuilder, String desiredVersion) {

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

        } else if (isVersionPrefix(desiredVersion)) {
            // version prefix provided - match the prefix and then pick the latest
            filterBuilder.versionPrefix(desiredVersion + ".").version(ToscaPolicyFilter.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.getPolicies().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.getPolicies().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 = dao.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);

        } 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
     * @throws PfModelException if an error occurred
     */
    public List<PdpGroup> getActivePdpGroupsByPolicyType(ToscaPolicyTypeIdentifier type) throws PfModelException {
        List<GroupData> data = type2groups.get(type);
        if (data == null) {
            PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(Collections.singletonList(type))
                            .groupState(PdpState.ACTIVE).build();

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

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

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

    /**
     * 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.
     *
     * @throws PfModelException if an error occurred
     */
    public void updateDb() throws PfModelException {
        // 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()));
            }
            dao.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()));
            }
            dao.updatePdpGroups(updated.stream().map(GroupData::getGroup).collect(Collectors.toList()));
        }
    }

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