Subversion Repositories SmartDukaan

Rev

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