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