2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.std;
24 import java.util.List;
26 import org.json.JSONObject;
27 import org.onap.policy.api.NotificationScheme;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.utils.BusConsumer;
33 import org.onap.policy.utils.BusPublisher;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 public class ManualClientEndDMAAP {
37 private static StdPDPNotification notification = null;
38 private static String resultJson = null;
39 private static Logger logger = FlexLogger.getLogger(ManualClientEndDMAAP.class.getName());
40 private static BusConsumer dmaapConsumer = null;
41 private static String uniquID = null;
42 private static String topic = null;
43 private static int RETRY_LIMIT = 4;
44 private ManualClientEndDMAAP() {
49 public static PDPNotification result(NotificationScheme scheme) {
50 if (resultJson == null || notification == null) {
51 logger.debug("No Result" );
54 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
55 boolean removed = false;
56 boolean updated = false;
57 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
60 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
63 if(removed && updated) {
64 notification.setNotificationType(NotificationType.BOTH);
66 notification.setNotificationType(NotificationType.REMOVE);
68 notification.setNotificationType(NotificationType.UPDATE);
71 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
72 return MatchStore.checkMatch(notification);
77 private static void publishMessage(String pubTopic, String uniqueID, List<String> dmaapList, String aafLogin, String aafPassword) {
78 BusPublisher pub = null;
80 pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword);
81 final JSONObject msg1 = new JSONObject ();
82 msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID);
83 pub.send ( "MyPartitionKey", msg1.toString () );
84 } catch (Exception e) {
85 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e);
92 //NOTE: should be able to remove this for DMAAP since we will not be creating topics dynamically
93 public static void createTopic (String topic, String uniquID, List<String> dmaapList, String aafLogin, String aafPassword){
94 ManualClientEndDMAAP.topic = topic;
95 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
99 public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
101 ManualClientEndDMAAP.uniquID = uniqueID;
102 ManualClientEndDMAAP.topic = topic;
107 dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000);
108 } catch (Exception e) {
109 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
113 boolean isSuccess = false;
114 while (retries < RETRY_LIMIT && !isSuccess) {
115 isSuccess = publishMessageAndSetNotification(dmaapList, topic, aafLogin, aafPassword);
120 private static boolean publishMessageAndSetNotification(List<String> dmaapList, String topic, String aafLogin, String aafPassword) {
121 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
123 for ( String msg : dmaapConsumer.fetch () ) {
124 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
126 if (!msg.contains("DMaaP Update")){
127 notification = NotificationUnMarshal.notificationJSON(msg);
131 }catch (Exception e) {
132 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);