ce7fdaa7ade61cea71525e1ca147a5f5c5f15d57
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupCreateOrUpdateProvider.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 Nordix Foundation.
6  * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.Set;
32 import java.util.function.Consumer;
33 import java.util.stream.Collectors;
34 import javax.ws.rs.core.Response.Status;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.parameters.ValidationResult;
37 import org.onap.policy.common.parameters.ValidationStatus;
38 import org.onap.policy.common.utils.services.Registry;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.pdp.concepts.Pdp;
41 import org.onap.policy.models.pdp.concepts.PdpGroup;
42 import org.onap.policy.models.pdp.concepts.PdpGroups;
43 import org.onap.policy.models.pdp.concepts.PdpStateChange;
44 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
45 import org.onap.policy.models.pdp.concepts.PdpUpdate;
46 import org.onap.policy.models.pdp.enums.PdpState;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.pap.main.PapConstants;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.stereotype.Service;
54
55 /**
56  * Provider for PAP component to create or update PDP groups. The following items must be in the
57  * {@link Registry}:
58  * <ul>
59  * <li>PDP Modification Lock</li>
60  * <li>PDP Modify Request Map</li>
61  * <li>PAP DAO Factory</li>
62  * </ul>
63  */
64 @Service
65 public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
66     private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateProvider.class);
67
68     /**
69      * Creates or updates PDP groups.
70      *
71      * @param groups PDP group configurations to be created or updated
72      * @throws PfModelException if an error occurred
73      */
74     public void createOrUpdateGroups(PdpGroups groups) throws PfModelException {
75         var result = new BeanValidationResult("groups", groups);
76         groups.checkForDuplicateGroups(result);
77         if (!result.isValid()) {
78             String msg = result.getResult().trim();
79             logger.warn(msg);
80             throw new PfModelException(Status.BAD_REQUEST, msg);
81         }
82         // During PdpGroup create/update, policies are not supposed to be deployed/undeployed into the group.
83         // There is a separate API for this.
84         List<PdpSubGroup> subGroupsWithPolicies =
85             groups.getGroups().parallelStream().flatMap(group -> group.getPdpSubgroups().parallelStream())
86                 .filter(subGroup -> null != subGroup.getPolicies() && !subGroup.getPolicies().isEmpty())
87                 .collect(Collectors.toList());
88         if (!subGroupsWithPolicies.isEmpty()) {
89             logger.warn(
90                 "Policies cannot be deployed during PdpGroup Create/Update operation. Ignoring the list of policies");
91             subGroupsWithPolicies.forEach(subGroup -> subGroup.setPolicies(Collections.emptyList()));
92         }
93         process(groups, this::createOrUpdate);
94     }
95
96     /**
97      * Creates or updates PDP groups. This is the method that does the actual work.
98      *
99      * @param data session data
100      * @param groups PDP group configurations
101      * @throws PfModelException if an error occurred
102      */
103     private void createOrUpdate(SessionData data, PdpGroups groups) throws PfModelException {
104         var result = new BeanValidationResult("groups", groups);
105
106         for (PdpGroup group : groups.getGroups()) {
107             PdpGroup dbgroup = data.getGroup(group.getName());
108             ValidationResult groupValidationResult;
109             if (dbgroup == null) {
110                 // create group flow
111                 groupValidationResult = group.validatePapRest(false);
112                 if (groupValidationResult.isValid()) {
113                     result.addResult(addGroup(data, group));
114                 } else {
115                     result.addResult(groupValidationResult);
116                 }
117
118             } else {
119                 // update group flow
120                 groupValidationResult = group.validatePapRest(true);
121                 if (groupValidationResult.isValid()) {
122                     result.addResult(updateGroup(data, dbgroup, group));
123                 } else {
124                     result.addResult(groupValidationResult);
125                 }
126             }
127         }
128
129         if (!result.isValid()) {
130             throw new PfModelException(Status.BAD_REQUEST, result.getResult().trim());
131         }
132     }
133
134     /**
135      * Adds a new group.
136      *
137      * @param data session data
138      * @param group the group to be added
139      * @return the validation result
140      * @throws PfModelException if an error occurred
141      */
142     private ValidationResult addGroup(SessionData data, PdpGroup group) throws PfModelException {
143         var result = new BeanValidationResult(group.getName(), group);
144
145         validateGroupOnly(group, result);
146         if (!result.isValid()) {
147             return result;
148         }
149
150         // default to active
151         if (group.getPdpGroupState() == null) {
152             group.setPdpGroupState(PdpState.ACTIVE);
153         }
154
155         for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
156             result.addResult(addSubGroup(data, subgrp));
157         }
158
159         if (result.isValid()) {
160             data.create(group);
161         }
162
163         return result;
164     }
165
166     /**
167      * Performs additional validations of a group, but does not examine the subgroups.
168      *
169      * @param group the group to be validated
170      * @param result the validation result
171      */
172     private void validateGroupOnly(PdpGroup group, BeanValidationResult result) {
173         if (group.getPdpGroupState() == null) {
174             return;
175         }
176
177         switch (group.getPdpGroupState()) {
178             case ACTIVE:
179             case PASSIVE:
180                 break;
181
182             default:
183                 result.addResult("pdpGroupState", group.getPdpGroupState(),
184                                 ValidationStatus.INVALID, "must be null, ACTIVE, or PASSIVE");
185                 break;
186         }
187     }
188
189     /**
190      * Updates an existing group.
191      *
192      * @param data session data
193      * @param dbgroup the group, as it appears within the DB
194      * @param group the group to be added
195      * @return the validation result
196      * @throws PfModelException if an error occurred
197      */
198     private ValidationResult updateGroup(SessionData data, PdpGroup dbgroup, PdpGroup group) throws PfModelException {
199         var result = new BeanValidationResult(group.getName(), group);
200
201         if (!Objects.equals(dbgroup.getProperties(), group.getProperties())) {
202             result.addResult("properties", "", ValidationStatus.INVALID, "cannot change properties");
203         }
204
205         boolean updated = updateField(dbgroup.getDescription(), group.getDescription(), dbgroup::setDescription);
206         updated =
207             updateField(dbgroup.getPdpGroupState(), group.getPdpGroupState(), dbgroup::setPdpGroupState) || updated;
208         updated = notifyPdpsDelSubGroups(data, dbgroup, group) || updated;
209         updated = addOrUpdateSubGroups(data, dbgroup, group, result) || updated;
210
211         if (result.isValid() && updated) {
212             data.update(group);
213         }
214
215         return result;
216     }
217
218     /**
219      * Updates a field, if the new value is different than the old value.
220      *
221      * @param oldValue old value
222      * @param newValue new value
223      * @param setter function to set the field to the new value
224      * @return {@code true} if the field was updated, {@code false} if it already matched
225      *         the new value
226      */
227     private <T> boolean updateField(T oldValue, T newValue, Consumer<T> setter) {
228         if (oldValue == newValue) {
229             return false;
230         }
231
232         if (oldValue != null && oldValue.equals(newValue)) {
233             return false;
234         }
235
236         setter.accept(newValue);
237         return true;
238     }
239
240     /**
241      * Adds or updates subgroups within the group.
242      *
243      * @param data session data
244      * @param dbgroup the group, as it appears within the DB
245      * @param group the group to be added
246      * @param result the validation result
247      * @return {@code true} if the DB group was modified, {@code false} otherwise
248      * @throws PfModelException if an error occurred
249      */
250     private boolean addOrUpdateSubGroups(SessionData data, PdpGroup dbgroup, PdpGroup group,
251         BeanValidationResult result) throws PfModelException {
252
253         // create a map of existing subgroups
254         Map<String, PdpSubGroup> type2sub = new HashMap<>();
255         dbgroup.getPdpSubgroups().forEach(subgrp -> type2sub.put(subgrp.getPdpType(), subgrp));
256
257         var updated = false;
258
259         for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
260             PdpSubGroup dbsub = type2sub.get(subgrp.getPdpType());
261             var subResult = new BeanValidationResult(subgrp.getPdpType(), subgrp);
262
263             if (dbsub == null) {
264                 updated = true;
265                 subResult.addResult(addSubGroup(data, subgrp));
266                 dbgroup.getPdpSubgroups().add(subgrp);
267
268             } else {
269                 updated = updateSubGroup(dbsub, subgrp, subResult) || updated;
270             }
271
272             result.addResult(subResult);
273         }
274
275         return updated;
276     }
277
278     /**
279      * Notifies any PDPs whose subgroups are being removed.
280      *
281      * @param data session data
282      * @param dbgroup the group, as it appears within the DB
283      * @param group the group being updated
284      * @return {@code true} if a subgroup was removed, {@code false} otherwise
285      * @throws PfModelException if an error occurred
286      */
287     private boolean notifyPdpsDelSubGroups(SessionData data, PdpGroup dbgroup, PdpGroup group) throws PfModelException {
288         var updated = false;
289
290         // subgroups, as they appear within the updated group
291         Set<String> subgroups = new HashSet<>();
292         group.getPdpSubgroups().forEach(subgrp -> subgroups.add(subgrp.getPdpType()));
293
294         // loop through subgroups as they appear within the DB
295         for (PdpSubGroup subgrp : dbgroup.getPdpSubgroups()) {
296
297             if (!subgroups.contains(subgrp.getPdpType())) {
298                 // this subgroup no longer appears - notify its PDPs
299                 updated = true;
300                 notifyPdpsDelSubGroup(data, subgrp);
301                 trackPdpsDelSubGroup(data, dbgroup.getName(), subgrp);
302             }
303         }
304
305         return updated;
306     }
307
308     /**
309      * Notifies the PDPs that their subgroup is being removed.
310      *
311      * @param data session data
312      * @param subgrp subgroup that is being removed
313      */
314     private void notifyPdpsDelSubGroup(SessionData data, PdpSubGroup subgrp) {
315         for (Pdp pdp : subgrp.getPdpInstances()) {
316             String name = pdp.getInstanceId();
317
318             // make it passive
319             var change = new PdpStateChange();
320             change.setSource(PapConstants.PAP_NAME);
321             change.setName(name);
322             change.setState(PdpState.PASSIVE);
323
324             // remove it from subgroup and undeploy all policies
325             var update = new PdpUpdate();
326             update.setSource(PapConstants.PAP_NAME);
327             update.setName(name);
328
329             data.addRequests(update, change);
330         }
331     }
332
333     /**
334      * Tracks PDP responses when their subgroup is removed.
335      *
336      * @param data session data
337      * @param pdpGroup PdpGroup name
338      * @param subgrp subgroup that is being removed
339      * @throws PfModelException if an error occurred
340      */
341     private void trackPdpsDelSubGroup(SessionData data, String pdpGroup, PdpSubGroup subgrp) throws PfModelException {
342         Set<String> pdps = subgrp.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
343
344         for (ToscaConceptIdentifier policyId : subgrp.getPolicies()) {
345             data.trackUndeploy(policyId, pdps, pdpGroup, subgrp.getPdpType());
346         }
347     }
348
349     /**
350      * Adds a new subgroup.
351      *
352      * @param data session data
353      * @param subgrp the subgroup to be added, updated to fully qualified versions upon
354      *        return
355      * @return the validation result
356      * @throws PfModelException if an error occurred
357      */
358     private ValidationResult addSubGroup(SessionData data, PdpSubGroup subgrp) throws PfModelException {
359         subgrp.setCurrentInstanceCount(0);
360         subgrp.setPdpInstances(Collections.emptyList());
361
362         var result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
363
364         result.addResult(validateSupportedTypes(data, subgrp));
365         return result;
366     }
367
368     /**
369      * Updates an existing subgroup.
370      *
371      * @param dbsub the subgroup, from the DB
372      * @param subgrp the subgroup to be updated, updated to fully qualified versions upon
373      *        return
374      * @param container container for additional validation results
375      * @return {@code true} if the subgroup content was changed, {@code false} if there
376      *         were no changes
377      */
378     private boolean updateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
379
380         // perform additional validations first
381         if (!validateSubGroup(dbsub, subgrp, container)) {
382             return false;
383         }
384
385         if (null != subgrp.getSupportedPolicyTypes() && !new HashSet<>(dbsub.getSupportedPolicyTypes())
386             .equals(new HashSet<>(subgrp.getSupportedPolicyTypes()))) {
387             logger.warn("Supported policy types cannot be updated while updating PdpGroup. "
388                 + "Hence, ignoring the new set of supported policy types.");
389         }
390
391         // while updating PdpGroup, list of policies (already deployed ones) and supported policies (the ones provided
392         // during PdpGroup creation) has to be retained
393         subgrp.setSupportedPolicyTypes(dbsub.getSupportedPolicyTypes());
394         subgrp.setPolicies(dbsub.getPolicies());
395         return updateField(dbsub.getDesiredInstanceCount(), subgrp.getDesiredInstanceCount(),
396             dbsub::setDesiredInstanceCount);
397     }
398
399     /**
400      * Performs additional validations of a subgroup.
401      *
402      * @param dbsub the subgroup, from the DB
403      * @param subgrp the subgroup to be validated, updated to fully qualified versions
404      *        upon return
405      * @param container container for additional validation results
406      * @return {@code true} if the subgroup is valid, {@code false} otherwise
407      */
408     private boolean validateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
409
410         var result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
411
412         if (!Objects.equals(dbsub.getProperties(), subgrp.getProperties())) {
413             result.addResult("properties", "", ValidationStatus.INVALID, "cannot change properties");
414         }
415
416         container.addResult(result);
417
418         return result.isValid();
419     }
420
421     /**
422      * Performs validations of the supported policy types within a subgroup.
423      *
424      * @param data session data
425      * @param subgrp the subgroup to be validated
426      * @return the validation result
427      * @throws PfModelException if an error occurred
428      */
429     private ValidationResult validateSupportedTypes(SessionData data, PdpSubGroup subgrp) throws PfModelException {
430         var result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
431         for (ToscaConceptIdentifier type : subgrp.getSupportedPolicyTypes()) {
432             if (!type.getName().endsWith(".*") && data.getPolicyType(type) == null) {
433                 result.addResult("policy type", type, ValidationStatus.INVALID, "unknown policy type");
434             }
435         }
436
437         return result;
438     }
439
440     @Override
441     protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
442             ToscaConceptIdentifierOptVersion desiredPolicy) {
443         throw new UnsupportedOperationException("makeUpdater should not be invoked");
444     }
445 }