Fixes for sonar critical issues
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / AutoClientUEB.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.std;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.List;
26 import java.util.UUID;
27
28 import org.onap.policy.api.NotificationHandler;
29 import org.onap.policy.api.NotificationScheme;
30 import org.onap.policy.api.NotificationType;
31 import org.onap.policy.api.PDPNotification;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35
36 import com.att.nsa.cambria.client.CambriaClientBuilders;
37 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
38 import com.att.nsa.cambria.client.CambriaConsumer;
39 /**
40  * Create a UEB Consumer to receive policy update notification.
41  * 
42  * 
43  *
44  */
45 public class AutoClientUEB implements Runnable  {
46         private static StdPDPNotification notification = null;
47         private static NotificationScheme scheme = null;
48         private static NotificationHandler handler = null;
49         private static String topic = null;
50         private static String url = null;
51         private static boolean status = false; 
52         private static Logger logger = FlexLogger.getLogger(AutoClientUEB.class.getName());
53         private static String notficatioinType = null;
54         private static CambriaConsumer cConsumer = null;
55         private static String apiKey = null;
56         private static String apiSecret = null;
57         private static List<String> uebURLList = null; 
58         private volatile boolean running = false;
59     
60
61         public AutoClientUEB(String url, List<String> uebURLList, String apiKey, String apiSecret) {
62                AutoClientUEB.url = url;
63                AutoClientUEB.uebURLList = uebURLList;
64                AutoClientUEB.apiKey = apiKey;
65                AutoClientUEB.apiKey = apiKey;
66         }
67
68         public static void setAuto(NotificationScheme scheme,
69                         NotificationHandler handler) {
70                 AutoClientUEB.scheme = scheme;
71                 AutoClientUEB.handler = handler;
72         }
73
74         public static void setScheme(NotificationScheme scheme) {
75                 AutoClientUEB.scheme = scheme;
76         }
77         
78         public static boolean getStatus(){
79                 return AutoClientUEB.status;
80         }
81
82         public static String getURL() {
83                 return AutoClientUEB.url;
84         }
85         
86         public static String getNotficationType(){
87                 return AutoClientUEB.notficatioinType;
88         }
89
90         public synchronized boolean isRunning() {
91                 return this.running;
92         }
93         
94         public synchronized void terminate() {
95                 this.running = false;
96         }
97         
98         @Override
99         public void run() {
100                 synchronized(this) {
101                         this.running = true;
102                 }
103                 String group =  UUID.randomUUID ().toString ();
104                 String id = "0";
105                 // Stop and Start needs to be done.
106                 if (scheme != null && handler!=null) {
107                         if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
108                                 URL aURL;
109                                 try {
110                                         aURL = new URL(AutoClientUEB.topic);
111                                         setTopic(aURL.getHost() + aURL.getPort());
112                                 } catch (MalformedURLException e) {
113                                         setTopic(AutoClientUEB.url.replace("[:/]", ""));
114                                 }
115                                 try {
116                                         ConsumerBuilder builder = new CambriaClientBuilders.ConsumerBuilder();
117                                         builder.knownAs(group, id)
118                                         .usingHosts(uebURLList)
119                                         .onTopic(topic)
120                                         .waitAtServer(15*1000)
121                                         .receivingAtMost(1000)
122                                         .authenticatedBy(apiKey, apiSecret);
123                                         setConsumer(builder.build()); 
124                                 } catch (Exception e1) {
125                                         logger.error("Exception Occured" + e1);
126                                 } 
127                 while (this.isRunning()) {
128                     try {
129                         for (String msg : cConsumer.fetch()) {
130                             logger.debug("Auto Notification Recieved Message " + msg + " from UEB cluster : "
131                                     + uebURLList.toString());
132                             setNotification(NotificationUnMarshal.notificationJSON(msg));
133                             callHandler();
134                         }
135                     } catch (Exception e) {
136                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error in processing UEB message"
137                                 + e.getMessage(), e);
138                     }
139
140                 }
141                                 logger.debug("Stopping UEB Consumer loop will not logger fetch messages from the cluster");
142                         }
143                 }
144         }
145
146         private static void setNotification(StdPDPNotification notificationJSON) {
147             notification = notificationJSON;
148     }
149
150     private static void setConsumer(CambriaConsumer build) {
151             cConsumer = build;
152     }
153
154     private static void setTopic(String topic) {
155             AutoClientUEB.topic = topic;
156     }
157
158     private static void callHandler() {
159                 if (handler != null && scheme != null) {
160                         if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
161                                 boolean removed = false;
162                                 boolean updated = false;
163                                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
164                                         removed = true;
165                                 }
166                                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
167                                         updated = true;
168                                 }
169                                 if (removed && updated) {
170                                         notification.setNotificationType(NotificationType.BOTH);
171                                 } else if (removed) {
172                                         notification.setNotificationType(NotificationType.REMOVE);
173                                 } else if (updated) {
174                                         notification.setNotificationType(NotificationType.UPDATE);
175                                 }
176                                 handler.notificationReceived(notification);
177                         } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
178                                 PDPNotification newNotification = MatchStore.checkMatch(notification);
179                                 if (newNotification.getNotificationType() != null) {
180                                         handler.notificationReceived(newNotification);
181                                 }
182                         }
183                 }
184         }
185
186 }