aboutsummaryrefslogtreecommitdiffstats
path: root/PolicyEngineUtils/src/main/java/org/openecomp/policy/utils/BackUpMonitor.java
blob: 1b35dcc578b7c689774c1a8062ab7e7280d9a9ed (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
/*-
 * ============LICENSE_START=======================================================
 * PolicyEngineUtils
 * ================================================================================
 * 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.utils;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.openecomp.policy.api.PDPNotification;
import org.openecomp.policy.jpa.BackUpMonitorEntity;
import org.openecomp.policy.std.NotificationStore;
import org.openecomp.policy.std.StdPDPNotification;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.diff.JsonDiff;

/**
 * BackUp Monitor checks Backup Status with the Database and maintains Redundancy for Gateway Applications. 
 * 
 */
public class BackUpMonitor {
	private static final Logger logger = Logger.getLogger(BackUpMonitor.class.getName());
	private static final int DEFAULT_PING = 60000; // Value is in milliseconds. 
	
	private static BackUpMonitor instance = null;
	private static String resourceName = null;
	private static String resourceNodeName = null;
	private static String notificationRecord = null;
	private static String lastMasterNotification= null;
	private static int pingInterval = DEFAULT_PING;
	private static Boolean masterFlag = false;
	private static Object lock = new Object();
	private static Object notificationLock = new Object();
	private static BackUpHandler handler= null;
	private EntityManager em;
	private EntityManagerFactory emf;
	
	/*
	 * Enumeration for the Resource Node Naming. Add here if required. 
	 */
	public enum ResourceNode{
		BRMS,
		ASTRA
	}
	
	private BackUpMonitor(String resourceNodeName, String resourceName, Properties properties, BackUpHandler handler) throws Exception{
		if(instance == null){
			instance = this;
		}
		BackUpMonitor.resourceNodeName = resourceNodeName;
		BackUpMonitor.resourceName = resourceName;
		BackUpMonitor.handler = handler; 
		// Create Persistence Entity
		properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePU.xml");
		emf = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties);
		if(emf==null){
			logger.error("Unable to create Entity Manger Factory ");
			throw new Exception("Unable to create Entity Manger Factory");
		}
		em = emf.createEntityManager();
		
		// Check Database if this is Master or Slave.
		checkDataBase();
		
		// Start thread.
		Thread t = new Thread(new BMonitor());
		t.start();
	}

	/**
	 * Gets the BackUpMonitor Instance if given proper resourceName and properties. Else returns null. 
	 * 
	 * @param resourceNodeName String format of the Resource Node to which the resource Belongs to. 
	 * @param resourceName String format of the ResourceName. This needs to be Unique. 
	 * @param properties Properties format of the properties file. 
	 * @return BackUpMonitor instance. 
	 */
	public static synchronized BackUpMonitor getInstance(String resourceNodeName, String resourceName, Properties properties, BackUpHandler handler) throws Exception {
		if(resourceNodeName==null || resourceNodeName.trim().equals("") ||resourceName==null|| resourceName.trim().equals("") || properties == null || handler==null){
			logger.error("Error while getting Instance. Please check resourceName and/or properties file");
			return null;
		}else if((resourceNodeName.equals(ResourceNode.ASTRA.toString()) || resourceNodeName.equals(ResourceNode.BRMS.toString())) && validate(properties) && instance==null){
			logger.info("Creating Instance of BackUpMonitor");
			instance = new BackUpMonitor(resourceNodeName, resourceName, properties, handler);
		}
		return instance;
	}
	
	// This is to validate given Properties with required values. 
	private static Boolean validate(Properties properties){
		if(properties.getProperty("javax.persistence.jdbc.driver")==null ||properties.getProperty("javax.persistence.jdbc.driver").trim().equals("")){
			logger.error("javax.persistence.jdbc.driver property is empty");
			return false;
		}
		if(properties.getProperty("javax.persistence.jdbc.url")==null || properties.getProperty("javax.persistence.jdbc.url").trim().equals("")){
			logger.error("javax.persistence.jdbc.url property is empty");
			return false;
		}
		if(properties.getProperty("javax.persistence.jdbc.user")==null || properties.getProperty("javax.persistence.jdbc.user").trim().equals("")){
			logger.error("javax.persistence.jdbc.user property is empty");
			return false;
		}
		if(properties.getProperty("javax.persistence.jdbc.password")==null || properties.getProperty("javax.persistence.jdbc.password").trim().equals("")){
			logger.error("javax.persistence.jdbc.password property is empty");
			return false;
		}
		if(properties.getProperty("ping_interval")==null || properties.getProperty("ping_interval").trim().equals("")){
			logger.info("ping_interval property not specified. Taking default value");
		}else{
			try{
				pingInterval = Integer.parseInt(properties.getProperty("ping_interval").trim());
			}catch(NumberFormatException e){
				logger.warn("Ignored invalid proeprty ping_interval. Taking default value.");
				pingInterval = DEFAULT_PING;
			}
		}
		return true;
	}
	
	// Sets the Flag for masterFlag to either True or False. 
	private static void setFlag(Boolean flag){
		synchronized (lock) {
			masterFlag = flag;
		}
	}
	
	/**
	 * Gets the Boolean value of Master(True) or Slave mode (False)
	 * 
	 * @return Boolean flag which if True means that the operation needs to be performed(Master mode) or if false the operation is in slave mode. 
	 */
	public synchronized Boolean getFlag(){
		synchronized (lock) {
			return masterFlag;
		}
	}
	
	// BackUpMonitor Thread 
	private class BMonitor implements Runnable{
		@Override
		public void run() {
			logger.info("Starting BackUpMonitor Thread.. ");
			while(true){
				try {
					TimeUnit.MILLISECONDS.sleep(pingInterval);
					checkDataBase();
				} catch (Exception e) {
					logger.error("Error during Thread execution " + e.getMessage());
				}
			}
		}
	}
	
	// Set Master 
	private static BackUpMonitorEntity setMaster(BackUpMonitorEntity bMEntity){
		bMEntity.setFlag("MASTER");
		setFlag(true);
		return bMEntity;
	}
	
	// Set Slave 
	private static BackUpMonitorEntity setSlave(BackUpMonitorEntity bMEntity){
		bMEntity.setFlag("SLAVE");
		setFlag(false);
		return bMEntity;
	}
	
	// Check Database and set the Flag. 
	private void checkDataBase() throws Exception {
		EntityTransaction et = em.getTransaction();
		notificationRecord = PolicyUtils.objectToJsonString(NotificationStore.getNotificationRecord());
		// Clear Cache. 
		logger.info("Clearing Cache");
		em.getEntityManagerFactory().getCache().evictAll();
		try{
			logger.info("Checking Datatbase for BackUpMonitor.. ");
			et.begin();
			Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn");
			if(resourceNodeName.equals(ResourceNode.ASTRA.toString())){
				query.setParameter("nn", ResourceNode.ASTRA.toString());
			}else if(resourceNodeName.equals(ResourceNode.BRMS.toString())){
				query.setParameter("nn", ResourceNode.BRMS.toString());
			}
			List<?> bMList = query.getResultList();
			if(bMList.isEmpty()){
				// This is New. create an entry as Master. 
				logger.info("Adding resource " + resourceName + " to Database");
				BackUpMonitorEntity bMEntity = new BackUpMonitorEntity();
				bMEntity.setResoruceNodeName(resourceNodeName);
				bMEntity.setResourceName(resourceName);
				bMEntity = setMaster(bMEntity);
				bMEntity.setTimeStamp(new Date());
				em.persist(bMEntity);
				em.flush();
			}else{
				// Check if resourceName already exists and if there is a Master in Node.
				Boolean masterFlag = false;
				Boolean alreadyMaster = false;
				Date currentTime;
				BackUpMonitorEntity masterEntity = null;
				BackUpMonitorEntity slaveEntity = null;
				long timeDiff = 0;
				for(int i=0; i< bMList.size(); i++){
					BackUpMonitorEntity bMEntity = (BackUpMonitorEntity) bMList.get(i);
					logger.info("Refreshing Entity. ");
					em.refresh(bMEntity);
					if(bMEntity.getResourceName().equals(resourceName)){
						logger.info("Resource Name already Exists. " + resourceName);
						if(bMEntity.getFlag().equalsIgnoreCase("MASTER")){
							// Master mode
							setFlag(true);
							logger.info(resourceName + " is on Master Mode");
							bMEntity.setTimeStamp(new Date());
							bMEntity.setNotificationRecord(notificationRecord);
							em.persist(bMEntity);
							em.flush();
							setLastNotification(null);
							alreadyMaster = true;
							break;
						}else{
							// Slave mode. 
							setFlag(false);
							slaveEntity = bMEntity;
							logger.info(resourceName + " is on Slave Mode");
						}
					}else{
						if(bMEntity.getFlag().equalsIgnoreCase("MASTER")){
							// check if its time stamp is old.
							currentTime = new Date();
							timeDiff = currentTime.getTime()-bMEntity.getTimeStamp().getTime();
							masterEntity = bMEntity;
							masterFlag = true;
						}
					}
				}
				// If there is master and no slave entry then add the slave entry to database. 
				// If we are slave and there is a masterFlag then check timeStamp to find if master is down. 
				if(!alreadyMaster){
					BackUpMonitorEntity bMEntity;
					if(slaveEntity==null){
						bMEntity = new BackUpMonitorEntity();
					}else{
						bMEntity = slaveEntity;
					}
					if(masterFlag && !getFlag()){
						if(timeDiff > pingInterval){
							// This is down or has an issue and we need to become Master while turning the Master to slave. 
							masterEntity = setSlave(masterEntity);
							String lastNotification = null;
							if(masterEntity.getNotificationRecord()!=null){
								lastNotification = calculatePatch(masterEntity.getNotificationRecord());
							}
							setLastNotification(lastNotification);
							em.persist(masterEntity);
							em.flush();
							bMEntity = setMaster(bMEntity);
							logger.info(resourceName + " changed to Master Mode");
						}else{
							bMEntity = setSlave(bMEntity);
							setLastNotification(null);
							logger.info(resourceName + " is on Slave Mode");
						}
					}else{
						// If there is no Master. we need to become Master. 
						bMEntity = setMaster(bMEntity);
						logger.info(resourceName + " is on Master Mode");
						setLastNotification(null);
					}
					bMEntity.setNotificationRecord(notificationRecord);
					bMEntity.setResoruceNodeName(resourceNodeName);
					bMEntity.setResourceName(resourceName);
					bMEntity.setTimeStamp(new Date());
					em.persist(bMEntity);
					em.flush();
				}
			}
			et.commit();
		}catch(Exception e){
			logger.error("failed Database Operation " + e.getMessage());
			if(et.isActive()){
				et.rollback();
			}
			throw new Exception(e);
		}
	}
	
	// Calculate Patch and return String JsonPatch of the notification Delta. 
	private synchronized String calculatePatch(String oldNotificationRecord) {
		try{
			JsonNode notification = JsonLoader.fromString(notificationRecord);
			JsonNode oldNotification = JsonLoader.fromString(oldNotificationRecord);
			JsonNode patchNode = JsonDiff.asJson(oldNotification, notification);
			logger.info("Generated JSON Patch is " + patchNode.toString());
			JsonPatch patch = JsonPatch.fromJson(patchNode);
			try {
				JsonNode patched = patch.apply(oldNotification);
				logger.info("Generated New Notification is : " + patched.toString());
				return patched.toString();
			} catch (JsonPatchException e) {
				logger.error("Error generating Patched "  +e.getMessage());
				return null;
			}
		}catch(IOException e){
			logger.error("Error generating Patched "  +e.getMessage());
			return null;
		}
	}

	/**
	 * Updates Notification in the Database while Performing the health check. 
	 * 
	 * @param notification String format of notification record to store in the Database. 
	 * @throws Exception 
	 */
	public synchronized void updateNotification() throws Exception{
		checkDataBase();
	}
	
	// Take in string notification and send the record delta to Handler. 
	private static void callHandler(String notification){
		if(handler!=null){
			try {
				PDPNotification notificationObject = PolicyUtils.jsonStringToObject(notification, StdPDPNotification.class);
				if(notificationObject.getNotificationType()!=null){
					logger.info("Performing Patched notification ");
					try{
						handler.runOnNotification(notificationObject);
					}catch (Exception e){
						logger.error("Error in Clients Handler Object : " + e.getMessage());
					}
				}
			} catch (IOException e) {
				logger.info("Error while notification Conversion " + e.getMessage());
			}
		}
	}
	
	// Used to set LastMasterNotification Record. 
	private static void setLastNotification(String notification){
		synchronized(notificationLock){
			lastMasterNotification = notification;
			if(lastMasterNotification!=null && !lastMasterNotification.equals("\"notificationType\":null")){
				callHandler(notification);
			}
		}
	}
}