- private void sendUpdate(PDPPolicy newPolicy,Map<String, PolicyDef> policyContainer) {
- updated = new Updated();
- // Want to know what is new ?
- LOGGER.info("The new Policy is: " + newPolicy.getId());
- LOGGER.info("The version no. is: " + newPolicy.getVersion());
- updated.setPolicyName(newPolicy.getId());
- updated.setVersionNo(newPolicy.getVersion());
- updated.setUpdateType(UpdateType.NEW);
- // If the policy is of Config type then retrieve its matches.
- if (newPolicy.getName().contains(".Config_")) {
- // Take a Configuration copy to PDP webapps.
- final String urlStart = "attributeId=URLID,expression";
- final String urlEnd = "}}},{";
- String policy = policyContainer.get(newPolicy.getId()).toString();
- if(policy.contains(urlStart)){
- String urlFinePartOne = policy.substring(policy.indexOf(urlStart)+urlStart.length());
- String urlFinePart = urlFinePartOne.substring(0,urlFinePartOne.indexOf(urlEnd));
- String urlString = urlFinePart.substring(urlFinePart.indexOf("value=$URL")+6);
- callPap(urlString, "Config");
- }
- Iterator<AnyOf> anyOfs = policyContainer.get(newPolicy.getId()).getTarget().getAnyOfs();
- while (anyOfs.hasNext()) {
- AnyOf anyOf = anyOfs.next();
- Iterator<AllOf> allOfs = anyOf.getAllOfs();
- while (allOfs.hasNext()) {
- AllOf allOf = allOfs.next();
- Iterator<Match> matches = allOf.getMatches();
- HashMap<String, String> matchValues = new HashMap<>();
- while (matches.hasNext()) {
- Match match = matches.next();
- LOGGER.info("Attribute Value is: "+ match.getAttributeValue().getValue().toString());
- String[] result = match.getAttributeRetrievalBase().toString().split("attributeId=");
- result[1] = result[1].replaceAll("}", "");
- if (!result[1].equals("urn:oasis:names:tc:xacml:1.0:subject:subject-id")) {
- LOGGER.info("Attribute id is: " + result[1]);
- }
- matchValues.put(result[1], match.getAttributeValue().getValue().toString());
- LOGGER.info("Match is : "+ result[1]+ " , " + match.getAttributeValue().getValue().toString());
- }
- updated.setMatches(matchValues);
- }
- }
- }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(".")) + ".json";
- callPap(urlString, "Action");
- }
- }
+ if (oldStatus == null) {
+ oldStatus = newStatus;
+ }
+ // Debugging purpose only.
+ LOGGER.debug("old config Status :" + oldStatus.getStatus());
+ LOGGER.debug("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.
+ for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
+ boolean change = true;
+ for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
+ // Check if there are same policies.
+ if (oldPolicy.getId().equals(newPolicy.getId())) {
+ // Check if they have same version.
+ if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
+ change = false;
+ }
+ }
+ }
+ // if there is a change Send the notifications to the Client.
+ if (change) {
+ sendUpdate(newPolicy, policyContainer);
+ updated = true;
+ updatedPolicies.add(this.updated);
+ }
+ }
+ // Check if there is any removal of policy.
+ for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
+ boolean change = true;
+ for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
+ // Check if there are same policies.
+ if (oldPolicy.getId().equals(newPolicy.getId())) {
+ // Check if they have same version.
+ if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
+ change = false;
+ }
+ }
+ }
+ // if there is a change Send the notifications to the Client.
+ if (change) {
+ sendremove(oldPolicy);
+ removed = true;
+ removedPolicies.add(this.removed);
+ }
+ }
+ }
+ // 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) {
+ // Call the Notification Server..
+ notification.setRemovedPolicies(removedPolicies);
+ notification.setLoadedPolicies(updatedPolicies);
+ notification = setUpdateTypes(updated, removed, notification);
+ ObjectWriter om = new ObjectMapper().writer();
+ try {
+ setNotificationJSON(om.writeValueAsString(notification));
+ LOGGER.info(notificationJSON);
+ // NotificationServer Method here.
+ setPropNotification();
+ if (("ueb".equals(propNotificationType) || "dmaap".equals(propNotificationType))
+ && !manualThreadStarted) {
+ LOGGER.debug("Starting Thread to accept UEB or DMAAP notfications.");
+ this.registerMaunualNotificationRunnable = new ManualNotificationUpdateThread();
+ this.manualNotificationThread = new Thread(this.registerMaunualNotificationRunnable);
+ this.manualNotificationThread.start();
+ manualThreadStarted = true;
+ }
+ String notificationJSONString = null;
+ setNotificationFlag(true);
+ try {
+ notificationJSONString = record(notification);
+ } catch (Exception e) {
+ LOGGER.error(e);
+ }
+ NotificationServer.setUpdate(notificationJSONString);
+ ManualNotificationUpdateThread.setUpdate(notificationJSONString);
+ } catch (JsonProcessingException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage() + e);
+ }
+ }
+ }
+
+ private void setNotificationFlag(boolean value) {
+ notificationFlag = value;
+ }
+
+ private static void setNotificationJSON(String message) {
+ notificationJSON = message;
+ }
+
+ private static void setPropNotification() {
+ propNotificationType = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE);
+ pdpURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID);
+ }
+
+ public static void sendNotification() {
+ if (notificationFlag) {
+ try {
+ NotificationServer.sendNotification(notificationJSON, propNotificationType, pdpURL);
+ } catch (Exception e) {
+ LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "
+ + e.getMessage() + e);
+ }
+ notificationFlag = false;
+ }
+ }
+
+ private void sendremove(PDPPolicy oldPolicy) {
+ removed = new Removed();
+ // Want to know what is removed ?
+ LOGGER.info("Policy removed: " + oldPolicy.getId() + " with version number: " + oldPolicy.getVersion());
+ removed.setPolicyName(oldPolicy.getId());
+ removed.setVersionNo(oldPolicy.getVersion());
+ removeFile(oldPolicy);
+ }
+
+ private void sendUpdate(PDPPolicy newPolicy, Map<String, PolicyDef> policyContainer) {
+ updated = new Updated();
+ // Want to know what is new ?
+ LOGGER.info("The new Policy is: " + newPolicy.getId());
+ LOGGER.info("The version no. is: " + newPolicy.getVersion());
+ updated.setPolicyName(newPolicy.getId());
+ updated.setVersionNo(newPolicy.getVersion());
+ updated.setUpdateType(UpdateType.NEW);
+ // If the policy is of Config type then retrieve its matches.
+ if (newPolicy.getName().contains(".Config_")) {
+ // Take a Configuration copy to PDP webapps.
+ final String urlStart = "attributeId=URLID,expression";
+ final String urlEnd = "}}},{";
+ String policy = policyContainer.get(newPolicy.getId()).toString();
+ if (policy.contains(urlStart)) {
+ String urlFinePartOne = policy.substring(policy.indexOf(urlStart) + urlStart.length());
+ String urlFinePart = urlFinePartOne.substring(0, urlFinePartOne.indexOf(urlEnd));
+ String urlString = urlFinePart.substring(urlFinePart.indexOf("value=$URL") + 6);
+ callPap(urlString, "Config");
+ }
+ Iterator<AnyOf> anyOfs = policyContainer.get(newPolicy.getId()).getTarget().getAnyOfs();
+ while (anyOfs.hasNext()) {
+ AnyOf anyOf = anyOfs.next();
+ Iterator<AllOf> allOfs = anyOf.getAllOfs();
+ while (allOfs.hasNext()) {
+ AllOf allOf = allOfs.next();
+ Iterator<Match> matches = allOf.getMatches();
+ HashMap<String, String> matchValues = new HashMap<>();
+ while (matches.hasNext()) {
+ Match match = matches.next();
+ LOGGER.info("Attribute Value is: " + match.getAttributeValue().getValue().toString());
+ String[] result = match.getAttributeRetrievalBase().toString().split("attributeId=");
+ result[1] = result[1].replaceAll("}", "");
+ if (!result[1].equals("urn:oasis:names:tc:xacml:1.0:subject:subject-id")) {
+ LOGGER.info("Attribute id is: " + result[1]);
+ }
+ matchValues.put(result[1], match.getAttributeValue().getValue().toString());
+ LOGGER.info(
+ "Match is : " + result[1] + " , " + match.getAttributeValue().getValue().toString());
+ }
+ updated.setMatches(matchValues);
+ }
+ }
+ } 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("."))
+ + ".json";
+ callPap(urlString, "Action");
+ }
+ }
+
+ // Adding this for Recording the changes to serve Polling requests..
+ private static String record(Notification notification) {
+ // Initialization with updates.
+ if (record.getRemovedPolicies() == null || record.getLoadedPolicies() == null) {
+ record.setRemovedPolicies(notification.getRemovedPolicies());
+ record.setLoadedPolicies(notification.getLoadedPolicies());
+ } else {
+ // Check if there is anything new and update the record..
+ if (record.getLoadedPolicies() != null || record.getRemovedPolicies() != null) {
+ HashSet<Removed> removedPolicies = (HashSet<Removed>) record.getRemovedPolicies();
+ HashSet<Updated> updatedPolicies = (HashSet<Updated>) record.getLoadedPolicies();