/
DirectAlarmFeedbackDatasource.java
162 lines (142 loc) · 5.63 KB
/
DirectAlarmFeedbackDatasource.java
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
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2018 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2018 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.alec.datasource.opennms.jvm;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import org.opennms.integration.api.v1.dao.AlarmFeedbackDao;
import org.opennms.integration.api.v1.feedback.AlarmFeedbackListener;
import org.opennms.alec.datasource.api.AlarmFeedback;
import org.opennms.alec.datasource.api.AlarmFeedbackDatasource;
import org.opennms.alec.datasource.api.AlarmFeedbackHandler;
import org.opennms.alec.datasource.common.HandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
/**
* A datasource that provides {@link AlarmFeedback} via the integration API.
*/
public class DirectAlarmFeedbackDatasource implements AlarmFeedbackListener, AlarmFeedbackDatasource {
private static final Logger LOG = LoggerFactory.getLogger(DirectAlarmFeedbackDatasource.class);
/**
* Provided via the integration api and used to retrieve the current feedback on {@link #init}.
*/
private final AlarmFeedbackDao feedbackDao;
/**
* A lock that prevents callbacks from being processed before we have finished {@link #init}.
*/
private final CountDownLatch initLock = new CountDownLatch(1);
/**
* A fair {@link ReadWriteLock} to ensure order of execution is preserved during calls to {@link #onFeedback}.
*/
private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
/**
* The registry of all currently registered handlers interested in feedback.
*/
private final HandlerRegistry<AlarmFeedbackHandler> feedbackHandlers = new HandlerRegistry<>();
/**
* A set of all feedback populated initially on init by retrieving via the {@link AlarmFeedbackDao} and
* subsequently kept up to date via calls to {@link #onFeedback}.
*/
private final Set<AlarmFeedback> feedback = new LinkedHashSet<>();
private final ApiMapper mapper;
/**
* @param alarmFeedbackDao used to retrieve the current feedback
* @param mapper used to Map between API and ALEC types
*/
public DirectAlarmFeedbackDatasource(AlarmFeedbackDao alarmFeedbackDao, ApiMapper mapper) {
this.feedbackDao = Objects.requireNonNull(alarmFeedbackDao);
this.mapper = Objects.requireNonNull(mapper);
}
/**
* On init we will populate the feedback by retrieving all current feedback from the {@link AlarmFeedbackDao}.
*/
public void init() {
feedback.addAll(feedbackDao.getFeedback()
.stream()
.map(a -> mapper.toAlarmFeedback(a))
.collect(Collectors.toList()));
initLock.countDown();
}
/**
* Wait for {@link #init the init} to finish to delay callbacks from being processed and clients from polling for
* feedback.
*/
private void waitForInit() {
try {
initLock.await();
} catch (InterruptedException ignore) {
LOG.debug("Interrupted while waiting for init lock.");
Thread.currentThread().interrupt();
}
}
@Override
public void onFeedback(org.opennms.integration.api.v1.model.AlarmFeedback feedback) {
waitForInit();
rwLock.writeLock().lock();
try {
AlarmFeedback newFeedback = mapper.toAlarmFeedback(feedback);
this.feedback.add(newFeedback);
feedbackHandlers.forEach(handler -> handler.handleAlarmFeedback(newFeedback));
} finally {
rwLock.writeLock().unlock();
}
}
@Override
public List<AlarmFeedback> getAlarmFeedback() {
waitForInit();
rwLock.readLock().lock();
try {
return ImmutableList.copyOf(feedback);
} finally {
rwLock.readLock().unlock();
}
}
@Override
public List<AlarmFeedback> getAlarmFeedbackAndRegisterHandler(AlarmFeedbackHandler handler) {
registerHandler(handler);
return getAlarmFeedback();
}
@Override
public void registerHandler(AlarmFeedbackHandler handler) {
feedbackHandlers.register(handler);
}
@Override
public void unregisterHandler(AlarmFeedbackHandler handler) {
feedbackHandlers.unregister(handler);
}
@Override
public void waitUntilReady() {
waitForInit();
}
}