Set "source" name in PAP PdpMessages
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / ProviderBase.java
index 5665e79..4cc5a44 100644 (file)
@@ -2,7 +2,9 @@
  * ============LICENSE_START=======================================================
  * ONAP PAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020-2021 Nordix Foundation.
+ * Modifications Copyright (C) 2020 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.
 package org.onap.policy.pap.main.rest;
 
 import java.util.Collection;
-import java.util.stream.Collectors;
 import javax.ws.rs.core.Response.Status;
 import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.provider.PolicyModelsProvider;
+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.ToscaPolicyIdentifierOptVersion;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
 import org.onap.policy.pap.main.notification.PolicyNotifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Super class of providers that deploy and undeploy PDP groups. The following items must
@@ -51,11 +51,8 @@ import org.slf4j.LoggerFactory;
  * </ul>
  */
 public abstract class ProviderBase {
-    private static final String DEPLOY_FAILED = "failed to deploy/undeploy policies";
     public static final String DB_ERROR_MSG = "DB error";
 
-    private static final Logger logger = LoggerFactory.getLogger(ProviderBase.class);
-
     /**
      * Lock used when updating PDPs.
      */
@@ -76,11 +73,10 @@ public abstract class ProviderBase {
      */
     private final PolicyModelsProviderFactoryWrapper daoFactory;
 
-
     /**
      * Constructs the object.
      */
-    public ProviderBase() {
+    protected ProviderBase() {
         this.updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
         this.requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
         this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
@@ -98,6 +94,7 @@ public abstract class ProviderBase {
 
         synchronized (updateLock) {
             SessionData data;
+            var notif = new PolicyNotification();
 
             try (PolicyModelsProvider dao = daoFactory.create()) {
 
@@ -105,23 +102,20 @@ public abstract class ProviderBase {
                 processor.accept(data, request);
 
                 // make all of the DB updates
-                data.updateDb();
+                data.updateDb(notif);
 
             } catch (PfModelException | PfModelRuntimeException e) {
-                logger.warn(DEPLOY_FAILED, e);
                 throw e;
 
             } catch (RuntimeException e) {
-                logger.warn(DEPLOY_FAILED, e);
                 throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
             }
 
-            // track responses for notification purposes
-            data.getDeployData().forEach(notifier::addDeploymentData);
-            data.getUndeployData().forEach(notifier::addUndeploymentData);
-
             // publish the requests
             data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
+
+            // publish the notifications
+            notifier.publish(notif);
         }
     }
 
@@ -132,7 +126,7 @@ public abstract class ProviderBase {
      * @param desiredPolicy request policy
      * @throws PfModelException if an error occurred
      */
-    protected void processPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy)
+    protected void processPolicy(SessionData data, ToscaConceptIdentifierOptVersion desiredPolicy)
                     throws PfModelException {
 
         ToscaPolicy policy = getPolicy(data, desiredPolicy);
@@ -143,7 +137,7 @@ public abstract class ProviderBase {
                             + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
         }
 
-        Updater updater = makeUpdater(data, policy, desiredPolicy);
+        var updater = makeUpdater(data, policy, desiredPolicy);
 
         for (PdpGroup group : groups) {
             upgradeGroup(data, group, updater);
@@ -161,7 +155,7 @@ public abstract class ProviderBase {
      * @return a function to update a subgroup
      */
     protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
-                    ToscaPolicyIdentifierOptVersion desiredPolicy);
+                    ToscaConceptIdentifierOptVersion desiredPolicy);
 
     /**
      * Finds the active PDP group(s) that supports the given policy type.
@@ -172,7 +166,7 @@ public abstract class ProviderBase {
      *         given PDP types
      * @throws PfModelException if an error occurred
      */
-    private Collection<PdpGroup> getGroups(SessionData data, ToscaPolicyTypeIdentifier policyType)
+    private Collection<PdpGroup> getGroups(SessionData data, ToscaConceptIdentifier policyType)
                     throws PfModelException {
 
         return data.getActivePdpGroupsByPolicyType(policyType);
@@ -186,9 +180,10 @@ public abstract class ProviderBase {
      * @param updater function to update a group
      * @throws PfModelException if an error occurred
      */
-    private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException {
+    private void upgradeGroup(SessionData data, PdpGroup group, Updater updater)
+                    throws PfModelException {
 
-        boolean updated = false;
+        var updated = false;
 
         for (PdpSubGroup subgroup : group.getPdpSubgroups()) {
 
@@ -201,7 +196,6 @@ public abstract class ProviderBase {
             makeUpdates(data, group, subgroup);
         }
 
-
         if (updated) {
             // something changed
             data.update(group);
@@ -232,14 +226,15 @@ public abstract class ProviderBase {
      */
     private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {
 
-        PdpUpdate update = new PdpUpdate();
+        var update = new PdpUpdate();
 
+        update.setSource(PapConstants.PAP_NAME);
         update.setName(pdp.getInstanceId());
         update.setDescription(group.getDescription());
         update.setPdpGroup(group.getName());
         update.setPdpSubgroup(subgroup.getPdpType());
-        update.setPolicies(subgroup.getPolicies().stream().map(ToscaPolicyIdentifierOptVersion::new)
-                        .map(ident -> getPolicy(data, ident)).collect(Collectors.toList()));
+        update.setPoliciesToBeDeployed(data.getPoliciesToBeDeployed());
+        update.setPoliciesToBeUndeployed(data.getPoliciesToBeUndeployed());
 
         return update;
     }
@@ -252,7 +247,7 @@ public abstract class ProviderBase {
      * @return the policy of interest
      * @throws PfModelRuntimeException if an error occurred or the policy was not found
      */
-    private ToscaPolicy getPolicy(SessionData data, ToscaPolicyIdentifierOptVersion ident) {
+    private ToscaPolicy getPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) {
         try {
             ToscaPolicy policy = data.getPolicy(ident);
             if (policy == null) {