Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / notifications / NotificationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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.pdp.rest.notifications;
22
23 import java.io.File;
24 import java.io.FileFilter;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.net.URLConnection;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
39
40 import org.apache.commons.io.IOUtils;
41 import org.apache.commons.io.filefilter.WildcardFileFilter;
42 import org.openecomp.policy.api.NotificationType;
43 import org.openecomp.policy.api.RemovedPolicy;
44 import org.openecomp.policy.api.UpdateType;
45 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
46 import org.openecomp.policy.common.logging.flexlogger.Logger;
47 import org.openecomp.policy.pdp.rest.PapUrlResolver;
48 import org.openecomp.policy.rest.XACMLRestProperties;
49 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
50
51 import com.att.research.xacml.api.pap.PDPPolicy;
52 import com.att.research.xacml.api.pap.PDPStatus;
53 import com.att.research.xacml.util.XACMLProperties;
54 import com.att.research.xacmlatt.pdp.policy.AllOf;
55 import com.att.research.xacmlatt.pdp.policy.AnyOf;
56 import com.att.research.xacmlatt.pdp.policy.Match;
57 import com.att.research.xacmlatt.pdp.policy.PolicyDef;
58 import com.fasterxml.jackson.core.JsonProcessingException;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.fasterxml.jackson.databind.ObjectWriter;
61
62 /**
63  * NotificationController Checks for the Updated and Removed policies. It
64  * notifies the Server to send Notifications to the Client.
65  * 
66  * @version 0.2
67  *
68  */
69 public class NotificationController {
70         private static final Logger LOGGER = FlexLogger.getLogger(NotificationController.class);
71         private static Notification record = new Notification();
72         private PDPStatus oldStatus = null;
73         private Removed removed = null;
74         private Updated updated = null;
75         private ManualNotificationUpdateThread registerMaunualNotificationRunnable = null;
76         private Thread manualNotificationThread = null;
77         private boolean manualThreadStarted = false;
78         
79         private static String notificationJSON = null;
80         private static String propNotificationType = null;
81         private static String pdpURL = null;
82         private static Boolean notificationFlag = false; 
83         
84         public void check(PDPStatus newStatus,HashMap<String, PolicyDef> policyContainer) {
85                 boolean updated = false;
86                 boolean removed = false;
87                 Notification notification = new Notification();
88                 HashSet<Removed> removedPolicies = new HashSet<Removed>();
89                 HashSet<Updated> updatedPolicies = new HashSet<Updated>();
90
91                 if (oldStatus == null) {
92                         oldStatus = newStatus;
93                 }
94                 // Debugging purpose only.
95                 LOGGER.debug("old config Status :" + oldStatus.getStatus());
96                 LOGGER.debug("new config Status :" + newStatus.getStatus());
97
98                 // Depending on the above condition taking the Change as an Update.
99                 if (oldStatus.getStatus().toString() != newStatus.getStatus().toString()) {
100                         LOGGER.info("There is an Update to the PDP");
101                         LOGGER.debug(oldStatus.getLoadedPolicies());
102                         LOGGER.debug(newStatus.getLoadedPolicies());
103                         // Check if there is an Update/additions in the policy.
104                         for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
105                                 boolean change = true;
106                                 for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
107                                         // Check if there are same policies.
108                                         if (oldPolicy.getId().equals(newPolicy.getId())) {
109                                                 // Check if they have same version.
110                                                 if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
111                                                         change = false;
112                                                 }
113                                         }
114                                 }
115                                 // if there is a change Send the notifications to the Client.
116                                 if (change) {
117                                         sendUpdate(newPolicy, policyContainer);
118                                         updated = true;
119                                         updatedPolicies.add(this.updated);
120                                 }
121                         }
122                         // Check if there is any removal of policy.
123                         for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
124                                 boolean change = true;
125                                 for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
126                                         // Check if there are same policies.
127                                         if (oldPolicy.getId().equals(newPolicy.getId())) {
128                                                 // Check if they have same version.
129                                                 if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
130                                                         change = false;
131                                                 }
132                                         }
133                                 }
134                                 // if there is a change Send the notifications to the Client.
135                                 if (change) {
136                                         sendremove(oldPolicy);
137                                         removed = true;
138                                         removedPolicies.add(this.removed);
139                                 }
140                         }
141                 }
142                 // At the end the oldStatus must be updated with the newStatus.
143                 oldStatus = newStatus;
144                 // Sending Notification to the Server to pass over to the clients
145                 if (updated || removed) {
146                         // Call the Notification Server..
147                         notification.setRemovedPolicies(removedPolicies);
148                         notification.setLoadedPolicies(updatedPolicies);
149                         notification = setUpdateTypes(updated, removed, notification);
150                         ObjectWriter om = new ObjectMapper().writer();
151                         try {
152                                 notificationJSON = om.writeValueAsString(notification);
153                                 LOGGER.info(notificationJSON);
154                                 // NotificationServer Method here.
155                                 propNotificationType = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE);
156                                 pdpURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID);
157                                 if (("ueb".equals(propNotificationType)||"dmaap".equals(propNotificationType)) && !manualThreadStarted) {
158                                         LOGGER.debug("Starting  Thread to accept UEB or DMAAP notfications.");
159                                         this.registerMaunualNotificationRunnable = new ManualNotificationUpdateThread();
160                                         this.manualNotificationThread = new Thread(this.registerMaunualNotificationRunnable);
161                                         this.manualNotificationThread.start();
162                                         manualThreadStarted = true;
163                                 }
164                                 String notificationJSON= null;
165                                 notificationFlag = true;
166                                 try{
167                                         notificationJSON= record(notification);
168                                 }catch(Exception e){
169                                         LOGGER.error(e);
170                                         // TODO:EELF Cleanup - Remove LOGGER
171                                         //PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
172                                 }
173                                 NotificationServer.setUpdate(notificationJSON);
174                                 ManualNotificationUpdateThread.setUpdate(notificationJSON);
175                         } catch (JsonProcessingException e) {
176                                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage());
177                                 // TODO:EELF Cleanup - Remove LOGGER
178                                 //PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
179                         }
180                 }
181         }
182         
183         public static void sendNotification(){
184                 if(notificationFlag){
185                         try {
186                                 NotificationServer.sendNotification(notificationJSON, propNotificationType, pdpURL);
187                         } catch (Exception e) {
188                                 LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
189                                 e.printStackTrace();
190                         }
191                         notificationFlag = false;
192                 }
193         }
194         
195         private void sendremove(PDPPolicy oldPolicy) {
196                 removed = new Removed();
197                 // Want to know what is removed ?
198                 // LOGGER.info("The Policy removed is: " + oldPolicy.getId());
199                 // LOGGER.info("The version no. is: " + oldPolicy.getVersion());
200                 LOGGER.info("Policy removed: " + oldPolicy.getId()+ " with version number: " + oldPolicy.getVersion());
201                 removed.setPolicyName(oldPolicy.getId());
202                 removed.setVersionNo(oldPolicy.getVersion());
203                 removeFile(oldPolicy);
204         }
205
206         private void sendUpdate(PDPPolicy newPolicy,HashMap<String, PolicyDef> policyContainer) {
207                 updated = new Updated();
208                 // Want to know what is new ?
209                 LOGGER.info("The new Policy is: " + newPolicy.getId());
210                 LOGGER.info("The version no. is: " + newPolicy.getVersion());
211                 updated.setPolicyName(newPolicy.getId());
212                 updated.setVersionNo(newPolicy.getVersion());
213                 updated.setUpdateType(UpdateType.NEW);
214                 // If the policy is of Config type then retrieve its matches.
215                 if (newPolicy.getName().contains(".Config_")) {
216                         // Take a Configuration copy to PDP webapps. 
217                         final String urlStart = "attributeId=URLID,expression";
218                         final String urlEnd = "}}},{";
219                         String policy = policyContainer.get(newPolicy.getId()).toString(); 
220                         if(policy.contains(urlStart)){
221                                 String urlFinePartOne = policy.substring(policy.indexOf(urlStart)+urlStart.length());
222                                 String urlFinePart = urlFinePartOne.substring(0,urlFinePartOne.indexOf(urlEnd));
223                                 String urlString = urlFinePart.substring(urlFinePart.indexOf("value=$URL")+6); 
224                                 callPap(urlString, "Config");
225                         }
226                         Iterator<AnyOf> anyOfs = policyContainer.get(newPolicy.getId()).getTarget().getAnyOfs();
227                         while (anyOfs.hasNext()) {
228                                 AnyOf anyOf = anyOfs.next();
229                                 Iterator<AllOf> allOfs = anyOf.getAllOfs();
230                                 while (allOfs.hasNext()) {
231                                         AllOf allOf = allOfs.next();
232                                         Iterator<Match> matches = allOf.getMatches();
233                                         HashMap<String, String> matchValues = new HashMap<String, String>();
234                                         while (matches.hasNext()) {
235                                                 Match match = matches.next();
236                                                 LOGGER.info("Attribute Value is: "+ match.getAttributeValue().getValue().toString());
237                                                 String[] result = match.getAttributeRetrievalBase().toString().split("attributeId=");
238                                                 result[1] = result[1].replaceAll("}", "");
239                                                 if (!result[1].equals("urn:oasis:names:tc:xacml:1.0:subject:subject-id")) {
240                                                         LOGGER.info("Attribute id is: " + result[1]);
241                                                 }
242                                                 matchValues.put(result[1], match.getAttributeValue().getValue().toString());
243                                                 LOGGER.info("Match is : "+ result[1]+ " , "     + match.getAttributeValue().getValue().toString());
244                                         }
245                                         updated.setMatches(matchValues);
246                                 }
247                         }
248                 }else if(newPolicy.getName().contains(".Action_")){
249                         // Take Configuration copy to PDP Webapps.
250                         // Action policies have .json as extension. 
251                         String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf(".")) + ".json";
252                         callPap(urlString, "Action");
253                 }
254         }
255
256         // Adding this for Recording the changes to serve Polling requests..
257         private static String record(Notification notification) throws Exception {
258                 // Initialization with updates.
259                 if (record.getRemovedPolicies() == null || record.getLoadedPolicies() == null) {
260                         record.setRemovedPolicies(notification.getRemovedPolicies());
261                         record.setLoadedPolicies(notification.getLoadedPolicies());
262                 } else {
263                         // Check if there is anything new and update the record..
264                         if (record.getLoadedPolicies() != null  || record.getRemovedPolicies() != null) {
265                                 HashSet<Removed> removedPolicies = (HashSet<Removed>) record.getRemovedPolicies();
266                                 HashSet<Updated> updatedPolicies = (HashSet<Updated>) record.getLoadedPolicies();
267
268                                 // Checking with New updated policies.
269                                 if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
270                                         for (Updated newUpdatedPolicy : notification.getLoadedPolicies()) {
271                                                 // If it was removed earlier then we need to remove from our record
272                                                 Iterator<Removed> oldRemovedPolicy = removedPolicies.iterator();
273                                                 while (oldRemovedPolicy.hasNext()) {
274                                                         Removed policy = oldRemovedPolicy.next();
275                                                         if (newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
276                                                                 if (newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
277                                                                         oldRemovedPolicy.remove();
278                                                                 }
279                                                         }
280                                                 }
281                                                 // If it was previously updated need to Overwrite it to the record.
282                                                 Iterator<Updated> oldUpdatedPolicy = updatedPolicies.iterator();
283                                                 while (oldUpdatedPolicy.hasNext()) {
284                                                         Updated policy = oldUpdatedPolicy.next();
285                                                         if (newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
286                                                                 if (newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
287                                                                         oldUpdatedPolicy.remove();
288                                                                 }
289                                                         }
290                                                 }
291                                                 updatedPolicies.add(newUpdatedPolicy);
292                                         }
293                                 }
294                                 // Checking with New Removed policies.
295                                 if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
296                                         for (Removed newRemovedPolicy : notification.getRemovedPolicies()) {
297                                                 // If it was previously removed Overwrite it to the record.
298                                                 Iterator<Removed> oldRemovedPolicy = removedPolicies.iterator();
299                                                 while (oldRemovedPolicy.hasNext()) {
300                                                         Removed policy = oldRemovedPolicy.next();
301                                                         if (newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
302                                                                 if (newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
303                                                                         oldRemovedPolicy.remove();
304                                                                 }
305                                                         }
306                                                 }
307                                                 // If it was added earlier then we need to remove from our record.
308                                                 Iterator<Updated> oldUpdatedPolicy = updatedPolicies.iterator();
309                                                 while (oldUpdatedPolicy.hasNext()) {
310                                                         Updated policy = oldUpdatedPolicy.next();
311                                                         if (newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
312                                                                 if (newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
313                                                                         oldUpdatedPolicy.remove();
314                                                                 }
315                                                         }
316                                                 }
317                                                 removedPolicies.add(newRemovedPolicy);
318                                         }
319                                 }
320                                 record.setRemovedPolicies(removedPolicies);
321                                 record.setLoadedPolicies(updatedPolicies);
322                         }
323                 }
324                 // Send the Result to the caller.
325                 ObjectWriter om = new ObjectMapper().writer();
326                 String json = null;
327                 try {
328                         json = om.writeValueAsString(record);
329                 } catch (JsonProcessingException e) {
330                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage());
331                         // TODO:EELF Cleanup - Remove LOGGER
332                         //PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
333                 }
334                 LOGGER.info(json);
335                 return json;
336         }
337         
338         private static Notification setUpdateTypes(boolean updated, boolean removed, Notification notification) {
339         if(notification!=null){
340             if(updated && removed){
341                 notification.setNotificationType(NotificationType.BOTH);
342                 if(notification.getLoadedPolicies()!=null){
343                     HashSet<Updated> updatedPolicies = new HashSet<Updated>(); 
344                     for(Updated oldUpdatedPolicy: notification.getLoadedPolicies()){
345                         Updated updatePolicy = oldUpdatedPolicy;
346                         if(notification.getRemovedPolicies()!=null){
347                             for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
348                                 String regex = ".(\\d)*.xml";
349                                 if(removedPolicy.getPolicyName().replaceAll(regex, "").equals(oldUpdatedPolicy.getPolicyName().replaceAll(regex, ""))){
350                                     updatePolicy.setUpdateType(UpdateType.UPDATE);
351                                     break;
352                                 }
353                             }
354                         }
355                         updatedPolicies.add(updatePolicy);
356                     }
357                     notification.setLoadedPolicies(updatedPolicies);
358                 }
359             }else if(updated){
360                 notification.setNotificationType(NotificationType.UPDATE);
361             }else if(removed){
362                 notification.setNotificationType(NotificationType.REMOVE);
363             }
364         }
365         return notification;
366     }
367         
368         private void removeFile(PDPPolicy oldPolicy) {
369                 try{
370                         Path removedPolicyFile = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_CONFIG)+File.separator+oldPolicy.getId());
371                         Files.deleteIfExists(removedPolicyFile);
372                         boolean delete=false;
373                         File dir= null;
374                         if(oldPolicy.getName().startsWith("Config")){
375                                 delete = true;
376                                 dir = new File(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+"Config");
377                         }else if(oldPolicy.getName().startsWith("Action")){
378                                 delete = true;
379                                 dir = new File(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+"Action");
380                         }
381                         if(delete && dir!=null){
382                                 FileFilter fileFilter = new WildcardFileFilter(oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf("."))+".*");
383                                 File[] configFile = dir.listFiles(fileFilter);
384                                 if(configFile.length==1){
385                                         Files.deleteIfExists(configFile[0].toPath());
386                                 }
387                         }
388                 }catch(Exception e){
389                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Couldn't remove the policy/config file " + oldPolicy.getName());
390                         // TODO:EELF Cleanup - Remove LOGGER
391                         //PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Couldn't remove the policy file " + oldPolicy.getName());
392                 }
393         }
394         
395         private void callPap(String urlString, String type) {
396                 Path configLocation = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+type);
397                 if(Files.notExists(configLocation)){
398                         try {
399                                 Files.createDirectories(configLocation);
400                         } catch (IOException e) {
401                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +"Failed to create config directory: " + configLocation.toAbsolutePath().toString(), e);
402                         }
403                 }
404                 PapUrlResolver papUrls = PapUrlResolver.getInstance();
405                 while(papUrls.hasMoreUrls()){
406                         String papPath = papUrls.getUrl();
407                         papPath = papPath.substring(0, papPath.lastIndexOf("/pap"));
408                         String papAddress= urlString.replace("$URL", papPath);
409                         String fileName = papAddress.substring(papAddress.lastIndexOf("/")+1);
410                         String fileLocation = configLocation.toString() + File.separator + fileName;
411                         try {
412                                 URL papURL = new URL(papAddress);
413                                 LOGGER.info("Calling " +papAddress + " for Configuration Copy.");
414                                 URLConnection urlConnection = papURL.openConnection();
415                                 File file= new File(fileLocation);
416                                 try (InputStream is = urlConnection.getInputStream();
417                                                 OutputStream os = new FileOutputStream(file)) {
418                                         IOUtils.copy(is, os);
419                                         break;
420                                 }
421                         } catch (MalformedURLException e) {
422                                 LOGGER.error(e + e.getMessage());
423                         } catch(FileNotFoundException e){
424                                 LOGGER.error(e + e.getMessage());
425                         } catch (IOException e) {
426                                 LOGGER.error(e + e.getMessage());
427                         }
428                         papUrls.getNext();
429                 }
430         }
431 }