aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-PDP-REST/src/main/java/org/openecomp/policy/pdp/rest/notifications/NotificationController.java
blob: 1dfd074229d3f6e6951684e7057edd64024e9383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-PDP-REST
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.pdp.rest.notifications;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.openecomp.policy.pdp.rest.PapUrlResolver;
import org.openecomp.policy.rest.XACMLRestProperties;
import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;

import org.openecomp.policy.xacml.api.XACMLErrorConstants;
import com.att.research.xacml.api.pap.PDPPolicy;
import com.att.research.xacml.api.pap.PDPStatus;
import com.att.research.xacml.util.XACMLProperties;
import com.att.research.xacmlatt.pdp.policy.AllOf;
import com.att.research.xacmlatt.pdp.policy.AnyOf;
import com.att.research.xacmlatt.pdp.policy.Match;
import com.att.research.xacmlatt.pdp.policy.PolicyDef;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

/**
 * NotificationController Checks for the Updated and Removed policies. It
 * notifies the Server to send Notifications to the Client.
 * 
 * @version 0.2
 *
 */
public class NotificationController {
	private static final Logger logger = FlexLogger.getLogger(NotificationController.class);
	private static Notification record = new Notification();
	private PDPStatus oldStatus = null;
	private Removed removed = null;
	private Updated updated = null;
	private ManualNotificationUpdateThread registerMaunualNotificationRunnable = null;
	private Thread manualNotificationThread = null;
	private boolean manualThreadStarted = false;
	
	private static String notificationJSON = null;
	private static String propNotificationType = null;
	private static String pdpURL = null;
	private static Boolean notificationFlag = false; 
	
	public void check(PDPStatus newStatus,HashMap<String, PolicyDef> policyContainer) {
		boolean updated = false;
		boolean removed = false;
		Notification notification = new Notification();
		HashSet<Removed> removedPolicies = new HashSet<Removed>();
		HashSet<Updated> updatedPolicies = new HashSet<Updated>();

		if (oldStatus == null) {
			oldStatus = newStatus;
		}
		// Debugging purpose only.
		logger.debug("old config Status :" + oldStatus.getStatus());
		logger.debug("new config Status :" + newStatus.getStatus());

		// Depending on the above condition taking the Change as an Update.
		if (oldStatus.getStatus().toString() != newStatus.getStatus().toString()) {
			logger.info("There is an Update to the PDP");
			logger.debug(oldStatus.getLoadedPolicies());
			logger.debug(newStatus.getLoadedPolicies());
			// Check if there is an Update/additions in the policy.
			for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
				boolean change = true;
				for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
					// Check if there are same policies.
					if (oldPolicy.getId().equals(newPolicy.getId())) {
						// Check if they have same version.
						if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
							change = false;
						}
					}
				}
				// if there is a change Send the notifications to the Client.
				if (change) {
					sendUpdate(newPolicy, policyContainer);
					updated = true;
					updatedPolicies.add(this.updated);
				}
			}
			// Check if there is any removal of policy.
			for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) {
				boolean change = true;
				for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) {
					// Check if there are same policies.
					if (oldPolicy.getId().equals(newPolicy.getId())) {
						// Check if they have same version.
						if (oldPolicy.getVersion().equals(newPolicy.getVersion())) {
							change = false;
						}
					}
				}
				// if there is a change Send the notifications to the Client.
				if (change) {
					sendremove(oldPolicy);
					removed = true;
					removedPolicies.add(this.removed);
				}
			}
		}
		// At the end the oldStatus must be updated with the newStatus.
		oldStatus = newStatus;
		// Sending Notification to the Server to pass over to the clients
		if (updated || removed) {
			// Call the Notification Server..
			notification.setRemovedPolicies(removedPolicies);
			notification.setLoadedPolicies(updatedPolicies);
			ObjectWriter om = new ObjectMapper().writer();
			try {
				notificationJSON = om.writeValueAsString(notification);
				logger.info(notificationJSON);
				// NotificationServer Method here.
				propNotificationType = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE);
				pdpURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID);
				if (propNotificationType!=null && propNotificationType.equals("ueb") && !manualThreadStarted) {
					logger.debug("Starting  Thread to accept UEB notfications.");
					this.registerMaunualNotificationRunnable = new ManualNotificationUpdateThread();
					this.manualNotificationThread = new Thread(this.registerMaunualNotificationRunnable);
					this.manualNotificationThread.start();
					manualThreadStarted = true;
				}
				String notificationJSON= null;
				notificationFlag = true;
				try{
					notificationJSON= record(notification);
				}catch(Exception e){
					logger.error(e);
					// TODO:EELF Cleanup - Remove logger
					//PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
				}
				NotificationServer.setUpdate(notificationJSON);
				ManualNotificationUpdateThread.setUpdate(notificationJSON);
			} catch (JsonProcessingException e) {
				logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage());
				// TODO:EELF Cleanup - Remove logger
				//PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
			}
		}
	}
	
	public static void sendNotification(){
		if(notificationFlag){
			NotificationServer.sendNotification(notificationJSON, propNotificationType, pdpURL);
			notificationFlag = false;
		}
	}
	
	private void sendremove(PDPPolicy oldPolicy) {
		removed = new Removed();
		// Want to know what is removed ?
		// logger.info("The Policy removed is: " + oldPolicy.getId());
		// logger.info("The version no. is: " + oldPolicy.getVersion());
		logger.info("Policy removed: " + oldPolicy.getId()+ " with version number: " + oldPolicy.getVersion());
		removed.setPolicyName(oldPolicy.getId());
		removed.setVersionNo(oldPolicy.getVersion());
		removeFile(oldPolicy);
	}

	private void sendUpdate(PDPPolicy newPolicy,HashMap<String, PolicyDef> policyContainer) {
		updated = new Updated();
		// Want to know what is new ?
		logger.info("The new Policy is: " + newPolicy.getId());
		logger.info("The version no. is: " + newPolicy.getVersion());
		updated.setPolicyName(newPolicy.getId());
		updated.setVersionNo(newPolicy.getVersion());
		// If the policy is of Config type then retrieve its matches.
		if (newPolicy.getName().startsWith("Config")) {
			// Take a Configuration copy to PDP webapps. 
			final String urlStart = "attributeId=URLID,expression";
			final String urlEnd = "}}},{";
			String policy = policyContainer.get(newPolicy.getId()).toString(); 
			if(policy.contains(urlStart)){
				String urlFinePartOne = policy.substring(policy.indexOf(urlStart)+urlStart.length());
				String urlFinePart = urlFinePartOne.substring(0,urlFinePartOne.indexOf(urlEnd));
				String urlString = urlFinePart.substring(urlFinePart.indexOf("value=$URL")+6); 
				callPap(urlString, "Config");
			}
			Iterator<AnyOf> anyOfs = policyContainer.get(newPolicy.getId()).getTarget().getAnyOfs();
			while (anyOfs.hasNext()) {
				AnyOf anyOf = anyOfs.next();
				Iterator<AllOf> allOfs = anyOf.getAllOfs();
				while (allOfs.hasNext()) {
					AllOf allOf = allOfs.next();
					Iterator<Match> matches = allOf.getMatches();
					HashMap<String, String> matchValues = new HashMap<String, String>();
					while (matches.hasNext()) {
						Match match = matches.next();
						logger.info("Attribute Value is: "+ match.getAttributeValue().getValue().toString());
						String[] result = match.getAttributeRetrievalBase().toString().split("attributeId=");
						result[1] = result[1].replaceAll("}", "");
						if (!result[1].equals("urn:oasis:names:tc:xacml:1.0:subject:subject-id")) {
							logger.info("Attribute id is: " + result[1]);
						}
						matchValues.put(result[1], match.getAttributeValue().getValue().toString());
						logger.info("Match is : "+ result[1]+ " , "	+ match.getAttributeValue().getValue().toString());
					}
					updated.setMatches(matchValues);
				}
			}
		}else if(newPolicy.getName().startsWith("Action")){
			// Take Configuration copy to PDP Webapps.
			// Action policies have .json as extension. 
			String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf(".")) + ".json";
			callPap(urlString, "Action");
		}
	}

	// Adding this for Recording the changes to serve Polling requests..
	public static String record(Notification notification) throws Exception {
		// Initialization with updates.
		if (record.getRemovedPolicies() == null	|| record.getLoadedPolicies() == null) {
			record.setRemovedPolicies(notification.getRemovedPolicies());
			record.setLoadedPolicies(notification.getLoadedPolicies());
		} else {
			// Check if there is anything new and update the record..
			if (record.getLoadedPolicies() != null	|| record.getRemovedPolicies() != null) {
				HashSet<Removed> removedPolicies = (HashSet<Removed>) record.getRemovedPolicies();
				HashSet<Updated> updatedPolicies = (HashSet<Updated>) record.getLoadedPolicies();

				// Checking with New updated policies.
				if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) {
					for (Updated newUpdatedPolicy : notification.getLoadedPolicies()) {
						// If it was removed earlier then we need to remove from our record
						Iterator<Removed> oldRemovedPolicy = removedPolicies.iterator();
						while (oldRemovedPolicy.hasNext()) {
							Removed policy = oldRemovedPolicy.next();
							if (newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
								if (newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
									oldRemovedPolicy.remove();
								}
							}
						}
						// If it was previously updated need to Overwrite it to the record.
						Iterator<Updated> oldUpdatedPolicy = updatedPolicies.iterator();
						while (oldUpdatedPolicy.hasNext()) {
							Updated policy = oldUpdatedPolicy.next();
							if (newUpdatedPolicy.getPolicyName().equals(policy.getPolicyName())) {
								if (newUpdatedPolicy.getVersionNo().equals(policy.getVersionNo())) {
									oldUpdatedPolicy.remove();
								}
							}
						}
						updatedPolicies.add(newUpdatedPolicy);
					}
				}
				// Checking with New Removed policies.
				if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) {
					for (Removed newRemovedPolicy : notification.getRemovedPolicies()) {
						// If it was previously removed Overwrite it to the record.
						Iterator<Removed> oldRemovedPolicy = removedPolicies.iterator();
						while (oldRemovedPolicy.hasNext()) {
							Removed policy = oldRemovedPolicy.next();
							if (newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
								if (newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
									oldRemovedPolicy.remove();
								}
							}
						}
						// If it was added earlier then we need to remove from our record.
						Iterator<Updated> oldUpdatedPolicy = updatedPolicies.iterator();
						while (oldUpdatedPolicy.hasNext()) {
							Updated policy = oldUpdatedPolicy.next();
							if (newRemovedPolicy.getPolicyName().equals(policy.getPolicyName())) {
								if (newRemovedPolicy.getVersionNo().equals(policy.getVersionNo())) {
									oldUpdatedPolicy.remove();
								}
							}
						}
						removedPolicies.add(newRemovedPolicy);
					}
				}
				record.setRemovedPolicies(removedPolicies);
				record.setLoadedPolicies(updatedPolicies);
			}
		}
		// Send the Result to the caller.
		ObjectWriter om = new ObjectMapper().writer();
		String json = null;
		try {
			json = om.writeValueAsString(record);
		} catch (JsonProcessingException e) {
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage());
			// TODO:EELF Cleanup - Remove logger
			//PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "");
		}
		logger.info(json);
		return json;
	}
	
	private void removeFile(PDPPolicy oldPolicy) {
		try{
			Path removedPolicyFile = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_CONFIG)+File.separator+oldPolicy.getId());
			Files.deleteIfExists(removedPolicyFile);
			boolean delete=false;
			File dir= null;
			if(oldPolicy.getName().startsWith("Config")){
				delete = true;
				dir = new File(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+"Config");
			}else if(oldPolicy.getName().startsWith("Action")){
				delete = true;
				dir = new File(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+"Action");
			}
			if(delete && dir!=null){
				FileFilter fileFilter = new WildcardFileFilter(oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf("."))+".*");
				File[] configFile = dir.listFiles(fileFilter);
				if(configFile.length==1){
					Files.deleteIfExists(configFile[0].toPath());
				}
			}
		}catch(Exception e){
			logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Couldn't remove the policy/config file " + oldPolicy.getName());
			// TODO:EELF Cleanup - Remove logger
			//PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Couldn't remove the policy file " + oldPolicy.getName());
		}
	}
	
	private void callPap(String urlString, String type) {
		Path configLocation = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_WEBAPPS)+File.separator+type);
		if(Files.notExists(configLocation)){
			try {
				Files.createDirectories(configLocation);
			} catch (IOException e) {
				logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +"Failed to create config directory: " + configLocation.toAbsolutePath().toString(), e);
			}
		}
		PapUrlResolver papUrls = PapUrlResolver.getInstance();
		while(papUrls.hasMoreUrls()){
			String papPath = papUrls.getUrl();
			papPath = papPath.substring(0, papPath.lastIndexOf("/pap"));
			String papAddress= urlString.replace("$URL", papPath);
			String fileName = papAddress.substring(papAddress.lastIndexOf("/")+1);
			String fileLocation = configLocation.toString() + File.separator + fileName;
			try {
				URL papURL = new URL(papAddress);
				logger.info("Calling " +papAddress + " for Configuration Copy.");
				URLConnection urlConnection = papURL.openConnection();
				File file= new File(fileLocation);
				try (InputStream is = urlConnection.getInputStream();
						OutputStream os = new FileOutputStream(file)) {
					IOUtils.copy(is, os);
					break;
				}
			} catch (MalformedURLException e) {
				logger.error(e + e.getMessage());
			} catch(FileNotFoundException e){
				logger.error(e + e.getMessage());
			} catch (IOException e) {
				logger.error(e + e.getMessage());
			}
			papUrls.getNext();
		}
	}
}