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.onap.policy.std;
23 import java.util.List;
25 import org.json.JSONObject;
26 import org.onap.policy.api.NotificationScheme;
27 import org.onap.policy.api.NotificationType;
28 import org.onap.policy.api.PDPNotification;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.utils.BusConsumer;
32 import org.onap.policy.utils.BusPublisher;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
35 public class ManualClientEndDMAAP {
36 private static StdPDPNotification notification = null;
37 private static String resultJson = null;
38 private static Logger logger = FlexLogger.getLogger(ManualClientEndDMAAP.class.getName());
39 private static BusConsumer dmaapConsumer = null;
40 private static String uniquID = null;
41 private static String topic = null;
43 private ManualClientEndDMAAP() {
48 public static PDPNotification result(NotificationScheme scheme) {
49 if (resultJson == null || notification == null) {
50 logger.debug("No Result" );
53 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
54 boolean removed = false;
55 boolean updated = false;
56 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
59 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
62 if(removed && updated) {
63 notification.setNotificationType(NotificationType.BOTH);
65 notification.setNotificationType(NotificationType.REMOVE);
67 notification.setNotificationType(NotificationType.UPDATE);
70 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
71 return MatchStore.checkMatch(notification);
76 private static void publishMessage(String pubTopic, String uniqueID, List<String> dmaapList, String aafLogin, String aafPassword) {
77 BusPublisher pub = null;
79 pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword);
80 final JSONObject msg1 = new JSONObject ();
81 msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID);
82 pub.send ( "MyPartitionKey", msg1.toString () );
83 } catch (Exception e) {
84 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e);
91 //NOTE: should be able to remove this for DMAAP since we will not be creating topics dynamically
92 public static void createTopic (String topic, String uniquID, List<String> dmaapList, String aafLogin, String aafPassword){
93 ManualClientEndDMAAP.topic = topic;
94 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
98 public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
100 ManualClientEndDMAAP.uniquID = uniqueID;
101 ManualClientEndDMAAP.topic = topic;
106 dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000);
107 } catch (Exception e) {
108 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
113 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
115 for ( String msg : dmaapConsumer.fetch () )
117 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
119 if (!msg.contains("DMaaP Update")){
120 notification = NotificationUnMarshal.notificationJSON(msg);
124 }catch (Exception e) {
125 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);