2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
30 import java.util.Objects;
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;
56 * Provider for PAP component to create or update PDP groups. The following items must be in the
59 * <li>PDP Modification Lock</li>
60 * <li>PDP Modify Request Map</li>
61 * <li>PAP DAO Factory</li>
65 public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
66 private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateProvider.class);
69 * Creates or updates PDP groups.
71 * @param groups PDP group configurations to be created or updated
72 * @throws PfModelException if an error occurred
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();
80 throw new PfModelException(Status.BAD_REQUEST, msg);
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()) {
90 "Policies cannot be deployed during PdpGroup Create/Update operation. Ignoring the list of policies");
91 subGroupsWithPolicies.forEach(subGroup -> subGroup.setPolicies(Collections.emptyList()));
93 process(groups, this::createOrUpdate);
97 * Creates or updates PDP groups. This is the method that does the actual work.
99 * @param data session data
100 * @param groups PDP group configurations
101 * @throws PfModelException if an error occurred
103 private void createOrUpdate(SessionData data, PdpGroups groups) throws PfModelException {
104 var result = new BeanValidationResult("groups", groups);
106 for (PdpGroup group : groups.getGroups()) {
107 PdpGroup dbgroup = data.getGroup(group.getName());
108 ValidationResult groupValidationResult;
109 if (dbgroup == null) {
111 groupValidationResult = group.validatePapRest(false);
112 if (groupValidationResult.isValid()) {
113 result.addResult(addGroup(data, group));
115 result.addResult(groupValidationResult);
120 groupValidationResult = group.validatePapRest(true);
121 if (groupValidationResult.isValid()) {
122 result.addResult(updateGroup(data, dbgroup, group));
124 result.addResult(groupValidationResult);
129 if (!result.isValid()) {
130 throw new PfModelException(Status.BAD_REQUEST, result.getResult().trim());
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
142 private ValidationResult addGroup(SessionData data, PdpGroup group) throws PfModelException {
143 var result = new BeanValidationResult(group.getName(), group);
145 validateGroupOnly(group, result);
146 if (!result.isValid()) {
151 if (group.getPdpGroupState() == null) {
152 group.setPdpGroupState(PdpState.ACTIVE);
155 for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
156 result.addResult(addSubGroup(data, subgrp));
159 if (result.isValid()) {
167 * Performs additional validations of a group, but does not examine the subgroups.
169 * @param group the group to be validated
170 * @param result the validation result
172 private void validateGroupOnly(PdpGroup group, BeanValidationResult result) {
173 if (group.getPdpGroupState() == null) {
177 switch (group.getPdpGroupState()) {
183 result.addResult("pdpGroupState", group.getPdpGroupState(),
184 ValidationStatus.INVALID, "must be null, ACTIVE, or PASSIVE");
190 * Updates an existing group.
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
198 private ValidationResult updateGroup(SessionData data, PdpGroup dbgroup, PdpGroup group) throws PfModelException {
199 var result = new BeanValidationResult(group.getName(), group);
201 if (!Objects.equals(dbgroup.getProperties(), group.getProperties())) {
202 result.addResult("properties", "", ValidationStatus.INVALID, "cannot change properties");
205 boolean updated = updateField(dbgroup.getDescription(), group.getDescription(), dbgroup::setDescription);
207 updateField(dbgroup.getPdpGroupState(), group.getPdpGroupState(), dbgroup::setPdpGroupState) || updated;
208 updated = notifyPdpsDelSubGroups(data, dbgroup, group) || updated;
209 updated = addOrUpdateSubGroups(data, dbgroup, group, result) || updated;
211 if (result.isValid() && updated) {
219 * Updates a field, if the new value is different than the old value.
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
227 private <T> boolean updateField(T oldValue, T newValue, Consumer<T> setter) {
228 if (oldValue == newValue) {
232 if (oldValue != null && oldValue.equals(newValue)) {
236 setter.accept(newValue);
241 * Adds or updates subgroups within the group.
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
250 private boolean addOrUpdateSubGroups(SessionData data, PdpGroup dbgroup, PdpGroup group,
251 BeanValidationResult result) throws PfModelException {
253 // create a map of existing subgroups
254 Map<String, PdpSubGroup> type2sub = new HashMap<>();
255 dbgroup.getPdpSubgroups().forEach(subgrp -> type2sub.put(subgrp.getPdpType(), subgrp));
259 for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
260 PdpSubGroup dbsub = type2sub.get(subgrp.getPdpType());
261 var subResult = new BeanValidationResult(subgrp.getPdpType(), subgrp);
265 subResult.addResult(addSubGroup(data, subgrp));
266 dbgroup.getPdpSubgroups().add(subgrp);
269 updated = updateSubGroup(dbsub, subgrp, subResult) || updated;
272 result.addResult(subResult);
279 * Notifies any PDPs whose subgroups are being removed.
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
287 private boolean notifyPdpsDelSubGroups(SessionData data, PdpGroup dbgroup, PdpGroup group) throws PfModelException {
290 // subgroups, as they appear within the updated group
291 Set<String> subgroups = new HashSet<>();
292 group.getPdpSubgroups().forEach(subgrp -> subgroups.add(subgrp.getPdpType()));
294 // loop through subgroups as they appear within the DB
295 for (PdpSubGroup subgrp : dbgroup.getPdpSubgroups()) {
297 if (!subgroups.contains(subgrp.getPdpType())) {
298 // this subgroup no longer appears - notify its PDPs
300 notifyPdpsDelSubGroup(data, subgrp);
301 trackPdpsDelSubGroup(data, dbgroup.getName(), subgrp);
309 * Notifies the PDPs that their subgroup is being removed.
311 * @param data session data
312 * @param subgrp subgroup that is being removed
314 private void notifyPdpsDelSubGroup(SessionData data, PdpSubGroup subgrp) {
315 for (Pdp pdp : subgrp.getPdpInstances()) {
316 String name = pdp.getInstanceId();
319 var change = new PdpStateChange();
320 change.setSource(PapConstants.PAP_NAME);
321 change.setName(name);
322 change.setState(PdpState.PASSIVE);
324 // remove it from subgroup and undeploy all policies
325 var update = new PdpUpdate();
326 update.setSource(PapConstants.PAP_NAME);
327 update.setName(name);
329 data.addRequests(update, change);
334 * Tracks PDP responses when their subgroup is removed.
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
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());
344 for (ToscaConceptIdentifier policyId : subgrp.getPolicies()) {
345 data.trackUndeploy(policyId, pdps, pdpGroup, subgrp.getPdpType());
350 * Adds a new subgroup.
352 * @param data session data
353 * @param subgrp the subgroup to be added, updated to fully qualified versions upon
355 * @return the validation result
356 * @throws PfModelException if an error occurred
358 private ValidationResult addSubGroup(SessionData data, PdpSubGroup subgrp) throws PfModelException {
359 subgrp.setCurrentInstanceCount(0);
360 subgrp.setPdpInstances(Collections.emptyList());
362 var result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
364 result.addResult(validateSupportedTypes(data, subgrp));
369 * Updates an existing subgroup.
371 * @param dbsub the subgroup, from the DB
372 * @param subgrp the subgroup to be updated, updated to fully qualified versions upon
374 * @param container container for additional validation results
375 * @return {@code true} if the subgroup content was changed, {@code false} if there
378 private boolean updateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
380 // perform additional validations first
381 if (!validateSubGroup(dbsub, subgrp, container)) {
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.");
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);
400 * Performs additional validations of a subgroup.
402 * @param dbsub the subgroup, from the DB
403 * @param subgrp the subgroup to be validated, updated to fully qualified versions
405 * @param container container for additional validation results
406 * @return {@code true} if the subgroup is valid, {@code false} otherwise
408 private boolean validateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
410 var result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
412 if (!Objects.equals(dbsub.getProperties(), subgrp.getProperties())) {
413 result.addResult("properties", "", ValidationStatus.INVALID, "cannot change properties");
416 container.addResult(result);
418 return result.isValid();
422 * Performs validations of the supported policy types within a subgroup.
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
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");
441 protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
442 ToscaConceptIdentifierOptVersion desiredPolicy) {
443 throw new UnsupportedOperationException("makeUpdater should not be invoked");