Subversion Repositories SmartDukaan

Rev

Rev 5679 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5519 amar.kumar 1
package in.shop2020.alert.handler;
2
 
3
import in.shop2020.alert.AlertMapper;
4
import in.shop2020.alert.AlertServiceException;
5
import in.shop2020.alert.AlertedEntity;
5536 amar.kumar 6
import in.shop2020.alert.EntityMonitoringStatus;
5519 amar.kumar 7
import in.shop2020.alert.EntityProcessedState;
8
import in.shop2020.alert.EntityType;
9
import in.shop2020.alert.MonitoredEntity;
10
import in.shop2020.alert.SearchFilter;
11
import in.shop2020.alert.persistence.EntityMapper;
12
import in.shop2020.alert.util.Converter;
13
 
5674 amar.kumar 14
import java.util.ArrayList;
15
import java.util.List;
16
 
5519 amar.kumar 17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
5674 amar.kumar 19
import org.apache.log4j.Logger;
5519 amar.kumar 20
import org.apache.thrift.TException;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Service;
23
 
24
@Service
25
public class EntityHandler {
26
 
27
	@Autowired
28
	private EntityMapper entityMapper;
29
 
30
	private static final Log log = LogFactory.getLog(EntityHandler.class);
5674 amar.kumar 31
	private static final Logger alertLogger= Logger.getLogger("AppLogging");
5519 amar.kumar 32
 
33
	public List<MonitoredEntity> getEntities(SearchFilter searchFilter) {
34
		List<in.shop2020.alert.domain.MonitoredEntity> entities = 
35
			entityMapper.getEntities(Converter.toDomainSearchFilter
36
					(searchFilter));
37
		if(entities!=null) {
38
			List<MonitoredEntity> tEntities = new ArrayList<MonitoredEntity>();
39
			for(in.shop2020.alert.domain.MonitoredEntity entity : entities) {
40
				tEntities.add(Converter.toThriftMonitoredEntity(entity));
41
			}
42
		}
43
		return null;
44
	}
45
 
46
	public List<AlertedEntity> getAlertedEntities(SearchFilter searchFilter) {
47
		List<in.shop2020.alert.domain.AlertedEntity> alertedEntities = 
48
			entityMapper.getAlertedEntities(Converter.toDomainSearchFilter
49
					(searchFilter));
50
		if(alertedEntities!=null) {
51
			List<AlertedEntity> tAlertedEntities = new ArrayList<AlertedEntity>();
52
			for(in.shop2020.alert.domain.AlertedEntity alertedEntity : alertedEntities) {
53
				tAlertedEntities.add(Converter.toThriftAlertedEntity(alertedEntity));
54
			}
55
		}
56
		return null;
57
	}
58
 
59
	public void endMonitoring(EntityType entityType, String key) {
60
		in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(entityType, key);
61
		if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
5563 amar.kumar 62
			in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
63
			alertedEntity.setEntityType(entity.getEntityType());
64
			alertedEntity.setEventType(entity.getEventType());
65
			alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
66
			alertedEntity.setEntityProperties(entity.getEntityProperties());
5519 amar.kumar 67
			if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
5563 amar.kumar 68
				alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
69
				alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
70
				entityMapper.insertAlertedEntity(alertedEntity);
5674 amar.kumar 71
 
72
				AlertMapper alertMap;
73
		    	String 	delayedEventDescription = "UNKNOWN";
74
	    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
75
	    		if(alertMap!=null) {
76
	    			delayedEventDescription = alertMap.getEventdesciption();
77
	    		}
5680 amar.kumar 78
	    		alertLogger.warn("\tEntityType=" + 
5679 amar.kumar 79
						entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
80
						"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " + 
81
						"ExpiredTime=" + entity.getWarnExpiryTime());
5519 amar.kumar 82
			}
83
			else {
5563 amar.kumar 84
				alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
85
				alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
86
				entityMapper.insertAlertedEntity(alertedEntity);
5674 amar.kumar 87
 
88
				AlertMapper alertMap;
89
		    	String 	delayedEventDescription = "UNKNOWN";
90
	    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
91
	    		if(alertMap!=null) {
92
	    			delayedEventDescription = alertMap.getEventdesciption();
93
	    		}
5680 amar.kumar 94
	    		alertLogger.error("\tEntityType=" + 
5679 amar.kumar 95
						entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
96
						"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
97
						"ExpiredTime=" + entity.getCriticalExpiryTime());
5519 amar.kumar 98
			}
99
		}
100
		entityMapper.removeMonitoredEntity(entityType, key);
101
	}
102
 
5674 amar.kumar 103
	public void updateMonitoredObject(MonitoredEntity monitoredEntity) 
104
		throws AlertServiceException{
105
		in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(monitoredEntity.getEntityType(), 
106
				monitoredEntity.getEntityIdentifier());
107
		if(entity!=null) {
108
			if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
109
				in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
110
				alertedEntity.setEntityType(entity.getEntityType());
111
				alertedEntity.setEventType(entity.getEventType());
112
				alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
113
				alertedEntity.setEntityProperties(entity.getEntityProperties());
114
				if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
115
					alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
116
					alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
117
					entityMapper.insertAlertedEntity(alertedEntity);
118
 
119
					AlertMapper alertMap;
120
			    	String 	delayedEventDescription = "UNKNOWN";
121
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
122
		    		if(alertMap!=null) {
123
		    			delayedEventDescription = alertMap.getEventdesciption();
124
		    		}
5680 amar.kumar 125
		    		alertLogger.warn("\tEntityType=" + 
5679 amar.kumar 126
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
127
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " +
128
							"ExpiredTime=" + entity.getWarnExpiryTime());
5674 amar.kumar 129
				}
130
				else {
131
					alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
132
					alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
133
					entityMapper.insertAlertedEntity(alertedEntity);
134
 
135
					AlertMapper alertMap;
136
			    	String 	delayedEventDescription = "UNKNOWN";
137
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
138
		    		if(alertMap!=null) {
139
		    			delayedEventDescription = alertMap.getEventdesciption();
140
		    		}
5680 amar.kumar 141
		    		alertLogger.error("\tEntityType=" + 
5679 amar.kumar 142
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
143
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
144
							"ExpiredTime=" + entity.getCriticalExpiryTime());
5674 amar.kumar 145
				}
5519 amar.kumar 146
			}
5674 amar.kumar 147
				monitoredEntity.setId(entity.getId());
148
				entityMapper.updateEntity(Converter.toDbEntity(monitoredEntity));
149
		} else {
150
			scheduleAlert(monitoredEntity);
5519 amar.kumar 151
		}
152
	}
153
 
154
	public void scheduleAlert(MonitoredEntity monitoredEntity) 
155
		throws AlertServiceException {
156
		if(isEntityMonitorable(monitoredEntity.getEntityType())) {
157
			entityMapper.insertEntity(Converter.toDbEntity(monitoredEntity));
158
		} else {
159
			log.error("EntityType : " + monitoredEntity.getEntityType() + 
160
					"is not monitorable currently");
161
			throw new AlertServiceException("EntityType : " + monitoredEntity.getEntityType() + 
162
					"is not monitorable currently", 103);
163
		}
164
 
165
	}
166
 
167
	public List<MonitoredEntity> getEntitiesToBeAlerted() {
168
		List<in.shop2020.alert.domain.MonitoredEntity> entitiesTobeAlerted = 
169
			entityMapper.getEntitiesToBeAlerted();
170
		if(entitiesTobeAlerted != null) {
171
			List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
172
			for(in.shop2020.alert.domain.MonitoredEntity entity : entitiesTobeAlerted) {
173
				tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
174
			}
175
			return tEntitiesTobeAlerted;
176
		}
177
		return null;
178
	}
5674 amar.kumar 179
 
180
	public List<MonitoredEntity> getActiveAlertEntities() {
181
		List<in.shop2020.alert.domain.MonitoredEntity> activeAlertEntities = 
182
			entityMapper.getActiveAlertEntities();
183
		if(activeAlertEntities != null) {
184
			List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
185
			for(in.shop2020.alert.domain.MonitoredEntity entity : activeAlertEntities) {
186
				tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
187
			}
188
			return tEntitiesTobeAlerted;
189
		}
190
		return null;
191
	}
5519 amar.kumar 192
 
193
	public void updateEntityProcessedState(long id, EntityProcessedState state) {
194
		entityMapper.updateEntityProcessedState(id, state);
195
	}
196
 
197
 
198
	private boolean isEntityMonitorable(EntityType entityType) {
199
		return entityMapper.isEntityMonitorable(entityType.getValue());
200
	}
201
 
202
	public void registerEventType(AlertMapper alertMap) {
203
		if(entityMapper.getAlertMapper(alertMap.getEntityType(), alertMap.getEventType())==null){
204
			entityMapper.registerEventType(Converter.toDbAlertMap(alertMap));
205
		} {
206
		//TODO update existing alertMap
207
		}
208
	}
209
 
210
	public AlertMapper getAlertMapper(EntityType entityType, int eventType) {
211
		return Converter.toThriftAlertMapper(entityMapper.getAlertMapper(entityType, eventType));
212
	}
213
 
214
	public void activateEntityMonitoring(EntityType entityType, String userIds) throws TException {
215
		if(userIds == null) {
216
			throw new TException("Default Ids for alert cannot be null while " + 
217
					"activating Entity Monitoring");
218
		}
5536 amar.kumar 219
		if(entityMapper.getEntityMonitoringStatus(entityType.getValue())!=null) {
220
			entityMapper.activateEntityMonitoring(entityType, userIds);
221
		} else {
222
			in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
223
				new in.shop2020.alert.domain.EntityMonitoringStatus();
224
			entityMonStatus.setEntityType(entityType);
225
			entityMonStatus.setActive(true);
226
			entityMonStatus.setDefaultAlertGroup(userIds);
227
			entityMapper.addEntityType(entityMonStatus);
228
		}
5519 amar.kumar 229
	}
230
 
231
	public void deActivateEntityMonitoring(EntityType entityType) {
232
		entityMapper.deActivateEntityMonitoring(entityType);
233
	}
5536 amar.kumar 234
 
235
	public EntityMonitoringStatus getEntityMonitoringStatus(
236
			EntityType entityType) {
237
		in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
238
			entityMapper.getEntityMonitoringStatus(entityType.getValue());
239
		EntityMonitoringStatus tEntityMonitoringStatus = new EntityMonitoringStatus();
240
		tEntityMonitoringStatus.setEntityType(entityMonStatus.getEntityType());
241
		tEntityMonitoringStatus.setIsActive(entityMonStatus.isActive());
242
		tEntityMonitoringStatus.setUserIds(entityMonStatus.getDefaultAlertGroup());
243
		return tEntityMonitoringStatus;
244
	}
5519 amar.kumar 245
 
246
}