X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PDP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpdp%2Frest%2Fnotifications%2FNotificationController.java;h=974ca5c01b15bd6e6db208521794387fb2eba304;hb=refs%2Fchanges%2F96%2F97596%2F1;hp=577d5b3475e4e4c769703be6c1fef2d15cde4f79;hpb=6f2b3f2dd0e80895acd0f77aa5784e717be33696;p=policy%2Fengine.git diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java index 577d5b347..974ca5c01 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP-PDP-REST * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -44,7 +44,7 @@ import org.onap.policy.api.UpdateType; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pdp.rest.PapUrlResolver; -import org.onap.policy.rest.XACMLRestProperties; +import org.onap.policy.rest.XacmlRestProperties; import org.onap.policy.xacml.api.XACMLErrorConstants; import com.att.research.xacml.api.pap.PDPPolicy; @@ -61,7 +61,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; /** * NotificationController Checks for the Updated and Removed policies. It notifies the Server to send Notifications to * the Client. - * + * * @version 0.2 * */ @@ -81,8 +81,10 @@ public class NotificationController { private static Boolean notificationFlag = false; public void check(PDPStatus newStatus, Map policyContainer) { - boolean updated = false; - boolean removed = false; + + LOGGER.info("NotificationController: checking for updated and removed policies."); + boolean isUpdated = false; + boolean isRemoved = false; Notification notification = new Notification(); HashSet removedPolicies = new HashSet<>(); HashSet updatedPolicies = new HashSet<>(); @@ -90,16 +92,18 @@ public class NotificationController { if (oldStatus == null) { oldStatus = newStatus; } - // Debugging purpose only. - LOGGER.debug("old config Status :" + oldStatus.getStatus()); - LOGGER.debug("new config Status :" + newStatus.getStatus()); + + LOGGER.info("old config Status :" + oldStatus.getStatus()); + LOGGER.info("new config Status :" + newStatus.getStatus()); // Depending on the above condition taking the Change as an Update. if (oldStatus.getStatus().toString() != newStatus.getStatus().toString()) { LOGGER.info("There is an Update to the PDP"); LOGGER.debug(oldStatus.getLoadedPolicies()); LOGGER.debug(newStatus.getLoadedPolicies()); + // Check if there is an Update/additions in the policy. + LOGGER.info("NotificationController: check for updated or new policies"); for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) { boolean change = true; for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) { @@ -113,12 +117,14 @@ public class NotificationController { } // if there is a change Send the notifications to the Client. if (change) { + LOGGER.info("NotificationController: sending update/new policy notification"); sendUpdate(newPolicy, policyContainer); - updated = true; + isUpdated = true; updatedPolicies.add(this.updated); } } // Check if there is any removal of policy. + LOGGER.info("NotificationController: check for removed policies"); for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) { boolean change = true; for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) { @@ -132,8 +138,9 @@ public class NotificationController { } // if there is a change Send the notifications to the Client. if (change) { + LOGGER.info("NotificationController: sending removal notification"); sendremove(oldPolicy); - removed = true; + isRemoved = true; removedPolicies.add(this.removed); } } @@ -141,15 +148,15 @@ public class NotificationController { // At the end the oldStatus must be updated with the newStatus. oldStatus = newStatus; // Sending Notification to the Server to pass over to the clients - if (updated || removed) { + if (isUpdated || isRemoved) { // Call the Notification Server.. notification.setRemovedPolicies(removedPolicies); notification.setLoadedPolicies(updatedPolicies); - notification = setUpdateTypes(updated, removed, notification); + notification = setUpdateTypes(isUpdated, isRemoved, notification); ObjectWriter om = new ObjectMapper().writer(); try { setNotificationJSON(om.writeValueAsString(notification)); - LOGGER.info(notificationJSON); + LOGGER.info("NotificationController notificationJson: " + notificationJSON); // NotificationServer Method here. setPropNotification(); if (("ueb".equals(propNotificationType) || "dmaap".equals(propNotificationType)) @@ -175,7 +182,7 @@ public class NotificationController { } } - private void setNotificationFlag(boolean value) { + private static void setNotificationFlag(boolean value) { notificationFlag = value; } @@ -184,13 +191,14 @@ public class NotificationController { } private static void setPropNotification() { - propNotificationType = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE); - pdpURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID); + propNotificationType = XACMLProperties.getProperty(XacmlRestProperties.PROP_NOTIFICATION_TYPE); + pdpURL = XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_ID); } public static void sendNotification() { if (notificationFlag) { try { + LOGGER.info("NotificationController: calling NotificationServer to sendNotification"); NotificationServer.sendNotification(notificationJSON, propNotificationType, pdpURL); } catch (Exception e) { LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: " @@ -255,7 +263,7 @@ public class NotificationController { } else if (newPolicy.getName().contains(".Action_")) { // Take Configuration copy to PDP Webapps. // Action policies have .json as extension. - String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf(".")) + String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf('.')) + ".json"; callPap(urlString, "Action"); } @@ -374,7 +382,7 @@ public class NotificationController { private void removeFile(PDPPolicy oldPolicy) { try { - Path removedPolicyFile = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_CONFIG) + Path removedPolicyFile = Paths.get(XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_CONFIG) + File.separator + oldPolicy.getId()); Files.deleteIfExists(removedPolicyFile); boolean delete = false; @@ -382,15 +390,15 @@ public class NotificationController { if (oldPolicy.getName().contains(".Config_")) { delete = true; dir = new File( - XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS) + File.separator + "Config"); + XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_WEBAPPS) + File.separator + "Config"); } else if (oldPolicy.getName().contains(".Action_")) { delete = true; dir = new File( - XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS) + File.separator + "Action"); + XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_WEBAPPS) + File.separator + "Action"); } if (delete) { FileFilter fileFilter = new WildcardFileFilter( - oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf(".")) + ".*"); + oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf('.')) + ".*"); File[] configFile = dir.listFiles(fileFilter); if (configFile.length == 1) { Files.deleteIfExists(configFile[0].toPath()); @@ -404,7 +412,7 @@ public class NotificationController { private void callPap(String urlString, String type) { Path configLocation = Paths - .get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS) + File.separator + type); + .get(XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_WEBAPPS) + File.separator + type); if (Files.notExists(configLocation)) { try { Files.createDirectories(configLocation); @@ -418,7 +426,7 @@ public class NotificationController { String papPath = papUrls.getUrl(); papPath = papPath.substring(0, papPath.lastIndexOf("/pap")); String papAddress = urlString.replace("$URL", papPath); - String fileName = papAddress.substring(papAddress.lastIndexOf("/") + 1); + String fileName = papAddress.substring(papAddress.lastIndexOf('/') + 1); String fileLocation = configLocation.toString() + File.separator + fileName; try { URL papURL = new URL(papAddress);