Server Stubs PAP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupStateChangeProvider.java
index 0c90ae4..b1f7f66 100644 (file)
@@ -1,7 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
- *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
+ *  Modifications Copyright (C) 2021-2022 Bell Canada. 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.
@@ -28,24 +29,30 @@ import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
-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.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.pap.main.comm.PdpMessageGenerator;
+import org.onap.policy.pap.main.service.PdpGroupService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Service;
 
 /**
  * Provider for PAP component to change state of PDP group.
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
+@Service
 public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
 
+    @Autowired
+    private PdpGroupService pdpGroupService;
+
     /**
      * Constructs the object.
      */
@@ -61,7 +68,7 @@ public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
      * @return a pair containing the status and the response
      * @throws PfModelException in case of errors
      */
-    public Pair<Response.Status, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
+    public Pair<HttpStatus, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
             final PdpState pdpGroupState) throws PfModelException {
         synchronized (updateLock) {
             switch (pdpGroupState) {
@@ -75,52 +82,51 @@ public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
                     throw new PfModelException(Response.Status.BAD_REQUEST,
                             "Only ACTIVE or PASSIVE state changes are allowed");
             }
-            return Pair.of(Response.Status.OK, new PdpGroupStateChangeResponse());
+            return Pair.of(HttpStatus.OK, new PdpGroupStateChangeResponse());
         }
     }
 
     private void handleActiveState(final String groupName) throws PfModelException {
-        try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
-            final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
-            if (!pdpGroups.isEmpty() && !PdpState.ACTIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
-                updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.ACTIVE);
-                sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE, databaseProvider);
-            }
+        final List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups(groupName);
+        if (!pdpGroups.isEmpty() && !PdpState.ACTIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
+            updatePdpGroupAndPdp(pdpGroups, PdpState.ACTIVE);
+            sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE);
         }
     }
 
     private void handlePassiveState(final String groupName) throws PfModelException {
-        try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
-            final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
-            if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
-                updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.PASSIVE);
-                sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE, databaseProvider);
-            }
+        final List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups(groupName);
+        if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
+            updatePdpGroupAndPdp(pdpGroups, PdpState.PASSIVE);
+            sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE);
         }
     }
 
-    private void updatePdpGroupAndPdp(final PolicyModelsProvider databaseProvider, final List<PdpGroup> pdpGroups,
-            final PdpState pdpState) throws PfModelException {
+    private void updatePdpGroupAndPdp(final List<PdpGroup> pdpGroups, final PdpState pdpState) {
         pdpGroups.get(0).setPdpGroupState(pdpState);
         for (final PdpSubGroup subGroup : pdpGroups.get(0).getPdpSubgroups()) {
             for (final Pdp pdp : subGroup.getPdpInstances()) {
                 pdp.setPdpState(pdpState);
             }
         }
-        databaseProvider.updatePdpGroups(pdpGroups);
+        pdpGroupService.updatePdpGroups(pdpGroups);
 
         LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
     }
 
-    private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState,
-            final PolicyModelsProvider databaseProvider) throws PfModelException {
-
+    private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState) throws PfModelException {
+        String pdpGroupName = pdpGroup.getName();
         for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
+            List<ToscaPolicy> policies = getToscaPolicies(subGroup);
             for (final Pdp pdp : subGroup.getPdpInstances()) {
-                final PdpUpdate pdpUpdatemessage =
-                        createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(), databaseProvider);
-                final PdpStateChange pdpStateChangeMessage =
-                        createPdpStateChangeMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(), pdpState);
+                String pdpInstanceId = pdp.getInstanceId();
+                final var pdpUpdatemessage =
+                    createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(),
+                                policies, null);
+                final var pdpStateChangeMessage =
+                    createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
+                updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId,
+                    pdpStateChangeMessage.getState(), pdpUpdatemessage.getPoliciesToBeDeployed());
                 requestMap.addRequest(pdpUpdatemessage, pdpStateChangeMessage);
                 LOGGER.debug("Sent PdpUpdate message - {}", pdpUpdatemessage);
                 LOGGER.debug("Sent PdpStateChange message - {}", pdpStateChangeMessage);