Changed identifiers to concept identifiers
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupCreateOrUpdateProvider.java
index aed0517..00db7cc 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP PAP
  * ================================================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.pap.main.rest;
 
-import com.att.aft.dme2.internal.apache.commons.lang.ObjectUtils;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
@@ -44,10 +44,9 @@ import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
 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.ToscaPolicyIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,8 +76,8 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
      * @throws PfModelException if an error occurred
      */
     public void createOrUpdateGroups(PdpGroups groups) throws PfModelException {
-        ValidationResult result = groups.validatePapRest();
-
+        BeanValidationResult result = new BeanValidationResult("groups", groups);
+        groups.checkForDuplicateGroups(result);
         if (!result.isValid()) {
             String msg = result.getResult().trim();
             logger.warn(msg);
@@ -88,7 +87,8 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
         // There is a separate API for this.
         List<PdpSubGroup> subGroupsWithPolicies =
             groups.getGroups().parallelStream().flatMap(group -> group.getPdpSubgroups().parallelStream())
-                .filter(subGroup -> !subGroup.getPolicies().isEmpty()).collect(Collectors.toList());
+                .filter(subGroup -> null != subGroup.getPolicies() && !subGroup.getPolicies().isEmpty())
+                .collect(Collectors.toList());
         if (!subGroupsWithPolicies.isEmpty()) {
             logger.warn(
                 "Policies cannot be deployed during PdpGroup Create/Update operation. Ignoring the list of policies");
@@ -109,12 +109,24 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
 
         for (PdpGroup group : groups.getGroups()) {
             PdpGroup dbgroup = data.getGroup(group.getName());
-
+            ValidationResult groupValidationResult;
             if (dbgroup == null) {
-                result.addResult(addGroup(data, group));
+                // create group flow
+                groupValidationResult = group.validatePapRest(false);
+                if (groupValidationResult.isValid()) {
+                    result.addResult(addGroup(data, group));
+                } else {
+                    result.addResult(groupValidationResult);
+                }
 
             } else {
-                result.addResult(updateGroup(data, dbgroup, group));
+                // update group flow
+                groupValidationResult = group.validatePapRest(true);
+                if (groupValidationResult.isValid()) {
+                    result.addResult(updateGroup(data, dbgroup, group));
+                } else {
+                    result.addResult(groupValidationResult);
+                }
             }
         }
 
@@ -190,7 +202,7 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
     private ValidationResult updateGroup(SessionData data, PdpGroup dbgroup, PdpGroup group) throws PfModelException {
         BeanValidationResult result = new BeanValidationResult(group.getName(), group);
 
-        if (!ObjectUtils.equals(dbgroup.getProperties(), group.getProperties())) {
+        if (!Objects.equals(dbgroup.getProperties(), group.getProperties())) {
             result.addResult(
                 new ObjectValidationResult("properties", "", ValidationStatus.INVALID, "cannot change properties"));
         }
@@ -259,7 +271,7 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
                 dbgroup.getPdpSubgroups().add(subgrp);
 
             } else {
-                updated = updateSubGroup(data, dbsub, subgrp, subResult) || updated;
+                updated = updateSubGroup(dbsub, subgrp, subResult) || updated;
             }
 
             result.addResult(subResult);
@@ -331,7 +343,7 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
     private void trackPdpsDelSubGroup(SessionData data, PdpSubGroup subgrp) throws PfModelException {
         Set<String> pdps = subgrp.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
 
-        for (ToscaPolicyIdentifier policyId : subgrp.getPolicies()) {
+        for (ToscaConceptIdentifier policyId : subgrp.getPolicies()) {
             data.trackUndeploy(policyId, pdps);
         }
     }
@@ -358,81 +370,59 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
     /**
      * Updates an existing subgroup.
      *
-     * @param data session data
      * @param dbsub the subgroup, from the DB
      * @param subgrp the subgroup to be updated, updated to fully qualified versions upon
      *        return
      * @param container container for additional validation results
      * @return {@code true} if the subgroup content was changed, {@code false} if there
      *         were no changes
-     * @throws PfModelException if an error occurred
      */
-    private boolean updateSubGroup(SessionData data, PdpSubGroup dbsub, PdpSubGroup subgrp,
-        BeanValidationResult container) throws PfModelException {
+    private boolean updateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
 
         // perform additional validations first
-        if (!validateSubGroup(data, dbsub, subgrp, container)) {
+        if (!validateSubGroup(dbsub, subgrp, container)) {
             return false;
         }
 
-        boolean updated = updateList(dbsub.getSupportedPolicyTypes(), subgrp.getSupportedPolicyTypes(),
-            dbsub::setSupportedPolicyTypes);
+        if (null != subgrp.getSupportedPolicyTypes() && !new HashSet<>(dbsub.getSupportedPolicyTypes())
+            .equals(new HashSet<>(subgrp.getSupportedPolicyTypes()))) {
+            logger.warn("Supported policy types cannot be updated while updating PdpGroup. "
+                + "Hence, ignoring the new set of supported policy types.");
+        }
 
+        // while updating PdpGroup, list of policies (already deployed ones) and supported policies (the ones provided
+        // during PdpGroup creation) has to be retained
+        subgrp.setSupportedPolicyTypes(dbsub.getSupportedPolicyTypes());
+        subgrp.setPolicies(dbsub.getPolicies());
         return updateField(dbsub.getDesiredInstanceCount(), subgrp.getDesiredInstanceCount(),
-            dbsub::setDesiredInstanceCount) || updated;
+            dbsub::setDesiredInstanceCount);
     }
 
     /**
      * Performs additional validations of a subgroup.
      *
-     * @param data session data
      * @param dbsub the subgroup, from the DB
      * @param subgrp the subgroup to be validated, updated to fully qualified versions
      *        upon return
      * @param container container for additional validation results
      * @return {@code true} if the subgroup is valid, {@code false} otherwise
-     * @throws PfModelException if an error occurred
      */
-    private boolean validateSubGroup(SessionData data, PdpSubGroup dbsub, PdpSubGroup subgrp,
-        BeanValidationResult container) throws PfModelException {
+    private boolean validateSubGroup(PdpSubGroup dbsub, PdpSubGroup subgrp, BeanValidationResult container) {
 
         BeanValidationResult result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
 
-        if (!ObjectUtils.equals(dbsub.getProperties(), subgrp.getProperties())) {
+        if (!Objects.equals(dbsub.getProperties(), subgrp.getProperties())) {
             result.addResult(
                 new ObjectValidationResult("properties", "", ValidationStatus.INVALID, "cannot change properties"));
         }
 
-        result.addResult(validateSupportedTypes(data, subgrp));
         container.addResult(result);
 
         return result.isValid();
     }
 
     /**
-     * Updates a DB list with items from a new list.
-     *
-     * @param dblist the list from the DB
-     * @param newList the new list
-     * @param setter function to set the new list
-     * @return {@code true} if the list changed, {@code false} if the lists were the same
-     */
-    private <T> boolean updateList(List<T> dblist, List<T> newList, Consumer<List<T>> setter) {
-
-        Set<T> dbTypes = new HashSet<>(dblist);
-        Set<T> newTypes = new HashSet<>(newList);
-
-        if (dbTypes.equals(newTypes)) {
-            return false;
-        }
-
-        setter.accept(new ArrayList<>(newTypes));
-
-        return true;
-    }
-
-    /**
-     * Performs additional validations of the supported policy types within a subgroup.
+     * Performs validations of the supported policy types within a subgroup.
      *
      * @param data session data
      * @param subgrp the subgroup to be validated
@@ -441,8 +431,7 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
      */
     private ValidationResult validateSupportedTypes(SessionData data, PdpSubGroup subgrp) throws PfModelException {
         BeanValidationResult result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
-
-        for (ToscaPolicyTypeIdentifier type : subgrp.getSupportedPolicyTypes()) {
+        for (ToscaConceptIdentifier type : subgrp.getSupportedPolicyTypes()) {
             if (!type.getName().endsWith(".*") && data.getPolicyType(type) == null) {
                 result.addResult(
                     new ObjectValidationResult("policy type", type, ValidationStatus.INVALID, "unknown policy type"));
@@ -453,7 +442,8 @@ public class PdpGroupCreateOrUpdateProvider extends ProviderBase {
     }
 
     @Override
-    protected Updater makeUpdater(SessionData data, ToscaPolicy policy, ToscaPolicyIdentifierOptVersion desiredPolicy) {
+    protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
+            ToscaConceptIdentifierOptVersion desiredPolicy) {
         throw new UnsupportedOperationException("makeUpdater should not be invoked");
     }
 }