Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / PdpGroupService.java
index f3b1d6f..09c9640 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2022 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,9 +21,9 @@
 
 package org.onap.policy.pap.main.service;
 
+import jakarta.ws.rs.core.Response;
 import java.util.ArrayList;
 import java.util.List;
-import javax.ws.rs.core.Response;
 import lombok.NonNull;
 import lombok.RequiredArgsConstructor;
 import org.onap.policy.common.parameters.BeanValidationResult;
@@ -33,7 +34,6 @@ import org.onap.policy.models.base.PfReferenceKey;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-import org.onap.policy.models.pdp.concepts.PdpGroups;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
@@ -69,7 +69,7 @@ public class PdpGroupService {
      * @param pdpGroup the name of group
      * @return the PDP groups found
      */
-    public List<PdpGroup> getPdpGroupByName(@NonNull String pdpGroup) {
+    public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup) {
         return asPdpGroups(pdpGroupRepository.findByKeyName(pdpGroup));
     }
 
@@ -79,7 +79,7 @@ public class PdpGroupService {
      * @param pdpState the state of pdpGroup
      * @return the PDP groups found
      */
-    public List<PdpGroup> getPdpGroupByState(@NonNull PdpState pdpState) {
+    public List<PdpGroup> getPdpGroups(@NonNull PdpState pdpState) {
         return asPdpGroups(pdpGroupRepository.findByPdpGroupState(pdpState));
     }
 
@@ -90,7 +90,7 @@ public class PdpGroupService {
      * @param state the state of pdpGroup
      * @return the PDP groups found
      */
-    public List<PdpGroup> getPdpGroupByNameAndState(@NonNull String pdpGroup, @NonNull PdpState state) {
+    public List<PdpGroup> getPdpGroups(@NonNull String pdpGroup, @NonNull PdpState state) {
         return asPdpGroups(pdpGroupRepository.findByKeyNameAndPdpGroupState(pdpGroup, state));
     }
 
@@ -110,9 +110,21 @@ public class PdpGroupService {
      * @param pdpGroups the PDP groups to create
      * @return the PDP groups created
      */
-    public PdpGroups savePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
+    public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
+        return savePdpGroups(pdpGroups);
+    }
+
+    /**
+     * Updates PDP groups.
+     *
+     * @param pdpGroups the PDP groups to create
+     * @return the PDP groups created
+     */
+    public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) {
+        return savePdpGroups(pdpGroups);
+    }
 
-        // Return the created PDP groups
+    private List<PdpGroup> savePdpGroups(final List<PdpGroup> pdpGroups) {
         List<PdpGroup> returnPdpGroupList = new ArrayList<>();
 
         for (PdpGroup pdpGroup : pdpGroups) {
@@ -131,9 +143,7 @@ public class PdpGroupService {
                     "Failed saving PdpGroup. " + exc.getMessage(), exc);
             }
         }
-        PdpGroups returnPdpGroups = new PdpGroups();
-        returnPdpGroups.setGroups(returnPdpGroupList);
-        return returnPdpGroups;
+        return returnPdpGroupList;
     }
 
     /**
@@ -142,11 +152,12 @@ public class PdpGroupService {
      * @param pdpGroup the name of the pdpGroup to delete
      */
     public void deletePdpGroup(String pdpGroup) {
-        try {
-            pdpGroupRepository.deleteById(new PfConceptKey(pdpGroup, "0.0.0"));
-        } catch (Exception exc) {
+        PfConceptKey groupKey = new PfConceptKey(pdpGroup, "0.0.0");
+        if (pdpGroupRepository.existsById(groupKey)) {
+            pdpGroupRepository.deleteById(groupKey);
+        } else {
             String errorMessage = "delete of PDP group \"" + pdpGroup + "\" failed, PDP group does not exist";
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
     }