Commit includes ControlLoopPolicy API and bugfixes
[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.util.List;
26 import java.util.UUID;
27
28 import org.openecomp.policy.api.NotificationHandler;
29 import org.openecomp.policy.api.NotificationScheme;
30 import org.openecomp.policy.api.NotificationType;
31 import org.openecomp.policy.api.PDPNotification;
32 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
33 import org.openecomp.policy.common.logging.flexlogger.Logger;
34 import org.openecomp.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         public volatile boolean isRunning = 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 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.isRunning;
92         }
93         
94         public synchronized void terminate() {
95                 this.isRunning = false;
96         }
97         
98         @Override
99         public void run() {
100                 synchronized(this) {
101                         this.isRunning = true;
102                 }
103                 String group =  UUID.randomUUID ().toString ();
104                 String id = "0";
105                 //String topic = null;
106                 // Stop and Start needs to be done.
107                 if (scheme != null && handler!=null) {
108                         if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS) || scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
109                                 URL aURL;
110                                 try {
111                                         aURL = new URL(AutoClientUEB.topic);
112                                         topic = aURL.getHost() + aURL.getPort();
113                                 } catch (MalformedURLException e) {
114                                         topic = AutoClientUEB.url.replace("[:/]", "");
115                                 }
116                                         
117                                 try {
118                                         //CConsumer = CambriaClientFactory.createConsumer ( null, uebURLList, topic, group, id, 15*1000, 1000 );
119                                         ConsumerBuilder builder = new CambriaClientBuilders.ConsumerBuilder();
120                                         builder.knownAs(group, id)
121                                         .usingHosts(uebURLList)
122                                         .onTopic(topic)
123                                         .waitAtServer(15*1000)
124                                         .receivingAtMost(1000)
125                                         .authenticatedBy(apiKey, apiSecret);
126                                         
127                                          CConsumer = builder.build();
128                                         
129                                 } catch (Exception e1) {
130                                         logger.error("Exception Occured" + e1);
131                                 } 
132                                 while (this.isRunning() )
133                                 {
134                                         try {
135                                                 for ( String msg : CConsumer.fetch () )
136                                                 {               
137                                                         logger.debug("Auto Notification Recieved Message " + msg + " from UEB cluster : " + uebURLList.toString());
138                                                         notification = NotificationUnMarshal.notificationJSON(msg);
139                                                         callHandler();
140                                                 }
141                                         } catch (Exception e) {
142                                                 logger.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error in processing UEB message" + e.getMessage());
143                                         }
144
145                                 }
146                                 logger.debug("Stopping UEB Consumer loop will not logger fetch messages from the cluster");
147                         }
148                 }
149         }
150
151         private static void callHandler() {
152                 if (handler != null && scheme != null) {
153                         if (scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)) {
154                                 boolean removed = false, updated = false;
155                                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
156                                         removed = true;
157                                 }
158                                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
159                                         updated = true;
160                                 }
161                                 if (removed && updated) {
162                                         notification.setNotificationType(NotificationType.BOTH);
163                                 } else if (removed) {
164                                         notification.setNotificationType(NotificationType.REMOVE);
165                                 } else if (updated) {
166                                         notification.setNotificationType(NotificationType.UPDATE);
167                                 }
168                                 handler.notificationReceived(notification);
169                         } else if (scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
170                                 PDPNotification newNotification = MatchStore.checkMatch(notification);
171                                 if (newNotification.getNotificationType() != null) {
172                                         handler.notificationReceived(newNotification);
173                                 }
174                         }
175                 }
176         }
177
178 }