Refactor code for nested stmts in policy std
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / ManualClientEndDMAAP.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.policy.std;
23
24 import java.util.List;
25
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;
35
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() {
45         // Empty constructor
46     }
47
48
49     public static PDPNotification result(NotificationScheme scheme) {
50         if (resultJson == null || notification == null) {
51             logger.debug("No Result" );
52             return null;
53         }
54         if(scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
55             boolean removed = false;
56             boolean updated = false;
57             if(notification.getRemovedPolicies()!=null && !notification.getRemovedPolicies().isEmpty()){
58                 removed = true;
59             }
60             if(notification.getLoadedPolicies()!=null && !notification.getLoadedPolicies().isEmpty()){
61                 updated = true;
62             }
63             if(removed && updated) {
64                 notification.setNotificationType(NotificationType.BOTH);
65             }else if(removed){
66                 notification.setNotificationType(NotificationType.REMOVE);
67             }else if(updated){
68                 notification.setNotificationType(NotificationType.UPDATE);
69             }
70             return notification;
71         }else if(scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
72             return MatchStore.checkMatch(notification);
73         }
74         return null;
75     }
76
77     private static void publishMessage(String pubTopic, String uniqueID, List<String> dmaapList, String aafLogin, String aafPassword) {
78         BusPublisher pub = null;
79         try {
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);
86         }
87         if(pub != null){
88             pub.close ();
89         }
90     }
91
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);
96     }
97
98
99     public static void start(List<String> dmaapList, String topic, String aafLogin, String aafPassword, String uniqueID) {
100
101         ManualClientEndDMAAP.uniquID = uniqueID;
102         ManualClientEndDMAAP.topic = topic;
103
104         String id = "0";
105
106         try {
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);
110         }
111
112         int retries = 1;
113         boolean isSuccess = false;
114         while (retries < RETRY_LIMIT && !isSuccess) {
115             isSuccess = publishMessageAndSetNotification(dmaapList, topic, aafLogin, aafPassword);
116             retries++;
117         }
118     }
119
120     private static boolean publishMessageAndSetNotification(List<String> dmaapList, String topic, String aafLogin, String aafPassword) {
121         publishMessage(topic, uniquID, dmaapList, aafLogin, aafPassword);
122         try {
123             for ( String msg : dmaapConsumer.fetch () ) {
124                 logger.debug("Manual Notification Recieved Message " + msg + " from DMaaP server : " + dmaapList.toString());
125                 resultJson = msg;
126                 if (!msg.contains("DMaaP Update")){
127                     notification = NotificationUnMarshal.notificationJSON(msg);
128                     return true;
129                 }
130             }
131         }catch (Exception e) {
132             logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Unable to fetch messages from DMaaP servers: ", e);
133         }
134         return false;
135     }
136 }