Subversion Repositories SmartDukaan

Rev

Rev 5680 | 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);
5787 amar.kumar 61
			if(entity!=null){
62
			if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
63
				in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
64
				alertedEntity.setEntityType(entity.getEntityType());
65
				alertedEntity.setEventType(entity.getEventType());
66
				alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
67
				alertedEntity.setEntityProperties(entity.getEntityProperties());
68
				if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
69
					alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
70
					alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
71
					entityMapper.insertAlertedEntity(alertedEntity);
72
 
73
					AlertMapper alertMap;
74
			    	String 	delayedEventDescription = "UNKNOWN";
75
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
76
		    		if(alertMap!=null) {
77
		    			delayedEventDescription = alertMap.getEventdesciption();
78
		    		}
79
		    		alertLogger.warn("\tEntityType=" + 
80
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
81
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " + 
82
							"ExpiredTime=" + entity.getWarnExpiryTime());
83
				}
84
				else {
85
					alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
86
					alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
87
					entityMapper.insertAlertedEntity(alertedEntity);
88
 
89
					AlertMapper alertMap;
90
			    	String 	delayedEventDescription = "UNKNOWN";
91
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
92
		    		if(alertMap!=null) {
93
		    			delayedEventDescription = alertMap.getEventdesciption();
94
		    		}
95
		    		alertLogger.error("\tEntityType=" + 
96
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
97
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
98
							"ExpiredTime=" + entity.getCriticalExpiryTime());
99
				}
5519 amar.kumar 100
			}
5787 amar.kumar 101
			entityMapper.removeMonitoredEntity(entityType, key);
5519 amar.kumar 102
		}
103
	}
104
 
5674 amar.kumar 105
	public void updateMonitoredObject(MonitoredEntity monitoredEntity) 
106
		throws AlertServiceException{
107
		in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(monitoredEntity.getEntityType(), 
108
				monitoredEntity.getEntityIdentifier());
109
		if(entity!=null) {
110
			if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
111
				in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
112
				alertedEntity.setEntityType(entity.getEntityType());
113
				alertedEntity.setEventType(entity.getEventType());
114
				alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
115
				alertedEntity.setEntityProperties(entity.getEntityProperties());
116
				if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
117
					alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
118
					alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
119
					entityMapper.insertAlertedEntity(alertedEntity);
120
 
121
					AlertMapper alertMap;
122
			    	String 	delayedEventDescription = "UNKNOWN";
123
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
124
		    		if(alertMap!=null) {
125
		    			delayedEventDescription = alertMap.getEventdesciption();
126
		    		}
5680 amar.kumar 127
		    		alertLogger.warn("\tEntityType=" + 
5679 amar.kumar 128
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
129
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " +
130
							"ExpiredTime=" + entity.getWarnExpiryTime());
5674 amar.kumar 131
				}
132
				else {
133
					alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
134
					alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
135
					entityMapper.insertAlertedEntity(alertedEntity);
136
 
137
					AlertMapper alertMap;
138
			    	String 	delayedEventDescription = "UNKNOWN";
139
		    		alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
140
		    		if(alertMap!=null) {
141
		    			delayedEventDescription = alertMap.getEventdesciption();
142
		    		}
5680 amar.kumar 143
		    		alertLogger.error("\tEntityType=" + 
5679 amar.kumar 144
							entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
145
							"\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
146
							"ExpiredTime=" + entity.getCriticalExpiryTime());
5674 amar.kumar 147
				}
5519 amar.kumar 148
			}
5674 amar.kumar 149
				monitoredEntity.setId(entity.getId());
150
				entityMapper.updateEntity(Converter.toDbEntity(monitoredEntity));
151
		} else {
152
			scheduleAlert(monitoredEntity);
5519 amar.kumar 153
		}
154
	}
155
 
156
	public void scheduleAlert(MonitoredEntity monitoredEntity) 
157
		throws AlertServiceException {
158
		if(isEntityMonitorable(monitoredEntity.getEntityType())) {
159
			entityMapper.insertEntity(Converter.toDbEntity(monitoredEntity));
160
		} else {
161
			log.error("EntityType : " + monitoredEntity.getEntityType() + 
162
					"is not monitorable currently");
163
			throw new AlertServiceException("EntityType : " + monitoredEntity.getEntityType() + 
164
					"is not monitorable currently", 103);
165
		}
166
 
167
	}
168
 
169
	public List<MonitoredEntity> getEntitiesToBeAlerted() {
170
		List<in.shop2020.alert.domain.MonitoredEntity> entitiesTobeAlerted = 
171
			entityMapper.getEntitiesToBeAlerted();
172
		if(entitiesTobeAlerted != null) {
173
			List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
174
			for(in.shop2020.alert.domain.MonitoredEntity entity : entitiesTobeAlerted) {
175
				tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
176
			}
177
			return tEntitiesTobeAlerted;
178
		}
179
		return null;
180
	}
5674 amar.kumar 181
 
182
	public List<MonitoredEntity> getActiveAlertEntities() {
183
		List<in.shop2020.alert.domain.MonitoredEntity> activeAlertEntities = 
184
			entityMapper.getActiveAlertEntities();
185
		if(activeAlertEntities != null) {
186
			List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
187
			for(in.shop2020.alert.domain.MonitoredEntity entity : activeAlertEntities) {
188
				tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
189
			}
190
			return tEntitiesTobeAlerted;
191
		}
192
		return null;
193
	}
5519 amar.kumar 194
 
195
	public void updateEntityProcessedState(long id, EntityProcessedState state) {
196
		entityMapper.updateEntityProcessedState(id, state);
197
	}
198
 
199
 
200
	private boolean isEntityMonitorable(EntityType entityType) {
201
		return entityMapper.isEntityMonitorable(entityType.getValue());
202
	}
203
 
204
	public void registerEventType(AlertMapper alertMap) {
205
		if(entityMapper.getAlertMapper(alertMap.getEntityType(), alertMap.getEventType())==null){
206
			entityMapper.registerEventType(Converter.toDbAlertMap(alertMap));
207
		} {
208
		//TODO update existing alertMap
209
		}
210
	}
211
 
212
	public AlertMapper getAlertMapper(EntityType entityType, int eventType) {
213
		return Converter.toThriftAlertMapper(entityMapper.getAlertMapper(entityType, eventType));
214
	}
215
 
216
	public void activateEntityMonitoring(EntityType entityType, String userIds) throws TException {
217
		if(userIds == null) {
218
			throw new TException("Default Ids for alert cannot be null while " + 
219
					"activating Entity Monitoring");
220
		}
5536 amar.kumar 221
		if(entityMapper.getEntityMonitoringStatus(entityType.getValue())!=null) {
222
			entityMapper.activateEntityMonitoring(entityType, userIds);
223
		} else {
224
			in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
225
				new in.shop2020.alert.domain.EntityMonitoringStatus();
226
			entityMonStatus.setEntityType(entityType);
227
			entityMonStatus.setActive(true);
228
			entityMonStatus.setDefaultAlertGroup(userIds);
229
			entityMapper.addEntityType(entityMonStatus);
230
		}
5519 amar.kumar 231
	}
232
 
233
	public void deActivateEntityMonitoring(EntityType entityType) {
234
		entityMapper.deActivateEntityMonitoring(entityType);
235
	}
5536 amar.kumar 236
 
237
	public EntityMonitoringStatus getEntityMonitoringStatus(
238
			EntityType entityType) {
239
		in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
240
			entityMapper.getEntityMonitoringStatus(entityType.getValue());
241
		EntityMonitoringStatus tEntityMonitoringStatus = new EntityMonitoringStatus();
242
		tEntityMonitoringStatus.setEntityType(entityMonStatus.getEntityType());
243
		tEntityMonitoringStatus.setIsActive(entityMonStatus.isActive());
244
		tEntityMonitoringStatus.setUserIds(entityMonStatus.getDefaultAlertGroup());
245
		return tEntityMonitoringStatus;
246
	}
5519 amar.kumar 247
 
248
}