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