2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.pypdp.notifications;
23 import java.util.HashSet;
24 import java.util.Iterator;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.openecomp.policy.api.LoadedPolicy;
29 import org.openecomp.policy.api.NotificationHandler;
30 import org.openecomp.policy.api.PDPNotification;
31 import org.openecomp.policy.api.RemovedPolicy;
32 import org.openecomp.policy.common.logging.eelf.MessageCodes;
33 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
35 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.ObjectWriter;
40 public class NotificationController implements NotificationHandler{
41 private static final Log logger = LogFactory.getLog(NotificationController.class);
42 private static Notification record = new Notification();
43 //private static CountDownLatch latch;
46 public void notificationReceived(PDPNotification notification) {
47 //latch = new CountDownLatch(1);
48 if(notification!=null){
49 // Take this into our Record holder for polling requests.
50 NotificationServer.setUpdate(record(notification));
51 // Send the Update as is for AUTO clients.
52 ObjectWriter ow = new ObjectMapper().writer();
54 String json = ow.writeValueAsString(notification);
55 System.out.println("\n Notification: "+json);
57 NotificationServer.sendNotification(json);
59 } catch (JsonProcessingException e) {
60 logger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + e.getMessage());
61 // TODO:EELF Cleanup - Remove logger
62 PolicyLogger.error(MessageCodes.ERROR_SCHEMA_INVALID, e, "");
68 public static String record(PDPNotification notification) {
69 // Initialization with updates.
70 if(record.getRemovedPolicies()== null){
71 record.setRemovedPolicies(notification.getRemovedPolicies());
73 if(record.getLoadedPolicies()== null){
74 record.setLoadedPolicies(notification.getLoadedPolicies());
76 // Check if there is anything new and update the record..
77 if(record.getLoadedPolicies()!= null || record.getRemovedPolicies()!=null) {
78 HashSet<RemovedPolicy> removedPolicies = (HashSet<RemovedPolicy>) record.getRemovedPolicies();
79 HashSet<LoadedPolicy> updatedPolicies = (HashSet<LoadedPolicy>) record.getLoadedPolicies();
80 // Checking with New updated policies.
81 if(notification.getLoadedPolicies()!= null && !notification.getLoadedPolicies().isEmpty()) {
82 for( LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) {
83 // If it was removed earlier then we need to remove from our record
84 Iterator<RemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
85 while(oldRemovedPolicy.hasNext()){
86 RemovedPolicy policy = oldRemovedPolicy.next();
87 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
88 if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
89 oldRemovedPolicy.remove();
93 // If it was previously updated need to Overwrite it to the record.
94 Iterator<LoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
95 while(oldUpdatedPolicy.hasNext()){
96 LoadedPolicy policy = oldUpdatedPolicy.next();
97 if(newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
98 if(newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
99 oldUpdatedPolicy.remove();
103 updatedPolicies.add(newUpdatedPolicy);
106 // Checking with New Removed policies.
107 if(notification.getRemovedPolicies()!= null && !notification.getRemovedPolicies().isEmpty()) {
108 for( RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) {
109 // If it was removed earlier then we need to remove from our record
110 Iterator<RemovedPolicy> oldRemovedPolicy = removedPolicies.iterator();
111 while(oldRemovedPolicy.hasNext()){
112 RemovedPolicy policy = oldRemovedPolicy.next();
113 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
114 if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
115 oldRemovedPolicy.remove();
119 // If it was previously updated need to Overwrite it to the record.
120 Iterator<LoadedPolicy> oldUpdatedPolicy = updatedPolicies.iterator();
121 while(oldUpdatedPolicy.hasNext()){
122 LoadedPolicy policy = oldUpdatedPolicy.next();
123 if(newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
124 if(newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
125 oldUpdatedPolicy.remove();
129 removedPolicies.add(newRemovedPolicy);
132 record.setRemovedPolicies(removedPolicies);
133 record.setLoadedPolicies(updatedPolicies);
135 // Send the Result to the caller.
136 ObjectWriter om = new ObjectMapper().writer();
139 json = om.writeValueAsString(record);
140 } catch (JsonProcessingException e) {
141 logger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + e.getMessage());
142 // TODO:EELF Cleanup - Remove logger
143 PolicyLogger.error(MessageCodes.ERROR_SCHEMA_INVALID, e, "");