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;
44 public static PDPNotification result(NotificationScheme scheme) {
45 if (resultJson == null || notification == null) {
46 logger.debug("No Result" );
49 if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
50 boolean removed = false, updated = false;
51 if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
54 if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
57 if(removed && updated) {
58 notification.setNotificationType(NotificationType.BOTH);
60 notification.setNotificationType(NotificationType.REMOVE);
62 notification.setNotificationType(NotificationType.UPDATE);
65 }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
66 return MatchStore.checkMatch(notification);
73 private static void publishMessage(String pubTopic, String uniqueID, List<String> dmaapList, String aafLogin, String aafPassword) {
74 BusPublisher pub = null;
76 pub = new BusPublisher.DmaapPublisherWrapper(dmaapList, topic, aafLogin, aafPassword);
77 final JSONObject msg1 = new JSONObject ();
78 msg1.put ( "JSON", "DMaaP Update Request UID=" + uniqueID);
79 pub.send ( "MyPartitionKey", msg1.toString () );
80 } catch (Exception e) {
81 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Publisher: ", e);
88 //NOTE: should be able to remove this for DMAAP since we will not be creating topics dynamically
89 public static void createTopic (String topic, String uniquID, List<String> dmaapList, String aafLogin, String aafPassword){
90 ManualClientEndDMAAP.topic = topic;
91 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
95 public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
97 ManualClientEndDMAAP.uniquID = uniqueID;
98 ManualClientEndDMAAP.topic = topic;
103 dmaapConsumer = new BusConsumer.DmaapConsumerWrapper(dmaapList, topic, aafLogin, aafPassword, "clientGroup", id, 15*1000, 1000);
104 } catch (Exception e) {
105 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to create DMaaP Consumer: ", e);
110 publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
112 for ( String msg : dmaapConsumer.fetch () )
114 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
116 if (!msg.contains("DMaaP Update")){
117 notification = NotificationUnMarshal.notificationJSON(msg);
121 }catch (Exception e) {
122 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);