This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
DiscoveryServiceRegistryImpl.java
511 lines (431 loc) · 19.4 KB
/
DiscoveryServiceRegistryImpl.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.config.discovery.internal;
import static org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
import org.eclipse.smarthome.config.discovery.DiscoveryListener;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultFlag;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryServiceCallback;
import org.eclipse.smarthome.config.discovery.DiscoveryServiceRegistry;
import org.eclipse.smarthome.config.discovery.ExtendedDiscoveryService;
import org.eclipse.smarthome.config.discovery.ScanListener;
import org.eclipse.smarthome.config.discovery.inbox.Inbox;
import org.eclipse.smarthome.core.common.SafeMethodCaller;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.HashMultimap;
/**
* The {@link DiscoveryServiceRegistryImpl} is a concrete implementation of the {@link DiscoveryServiceRegistry}.
* <p>
* This implementation tracks any existing {@link DiscoveryService} and registers itself as {@link DiscoveryListener} on
* it.
* <p>
* This implementation does neither handle memory leaks (orphaned listener instances) nor blocked listeners. No
* performance optimizations have been done (synchronization).
*
* @author Michael Grammling - Initial Contribution
* @author Kai Kreuzer - Refactored API
* @author Andre Fuechsel - Added removeOlderResults
* @author Ivaylo Ivanov - Added getMaxScanTimeout
*
* @see DiscoveryServiceRegistry
* @see DiscoveryListener
*/
@Component(immediate = true, service = org.eclipse.smarthome.config.discovery.DiscoveryServiceRegistry.class)
public final class DiscoveryServiceRegistryImpl implements DiscoveryServiceRegistry, DiscoveryListener {
private HashMultimap<DiscoveryService, DiscoveryResult> cachedResults = HashMultimap.create();
private final class AggregatingScanListener implements ScanListener {
private final ScanListener listener;
private int finishedDiscoveryServices = 0;
private boolean errorOccurred = false;
private int numberOfDiscoveryServices;
private AggregatingScanListener(int numberOfDiscoveryServices, ScanListener listener) {
this.numberOfDiscoveryServices = numberOfDiscoveryServices;
this.listener = listener;
}
@Override
public synchronized void onFinished() {
synchronized (this) {
finishedDiscoveryServices++;
logger.debug("Finished {} of {} discovery services.", finishedDiscoveryServices,
numberOfDiscoveryServices);
if (!errorOccurred && finishedDiscoveryServices == numberOfDiscoveryServices) {
if (listener != null) {
listener.onFinished();
}
}
}
}
@Override
public void onErrorOccurred(Exception exception) {
synchronized (this) {
if (!errorOccurred) {
if (listener != null) {
listener.onErrorOccurred(exception);
}
errorOccurred = true;
} else {
// Skip error logging for aborted scans
if (!(exception instanceof CancellationException)) {
logger.warn("Error occurred while executing discovery service: {}", exception.getMessage(),
exception);
}
}
}
}
public void reduceNumberOfDiscoveryServices() {
synchronized (this) {
numberOfDiscoveryServices--;
if (!errorOccurred && finishedDiscoveryServices == numberOfDiscoveryServices) {
if (listener != null) {
listener.onFinished();
}
}
}
}
}
private List<DiscoveryService> discoveryServices = new CopyOnWriteArrayList<>();
private Set<DiscoveryListener> listeners = new CopyOnWriteArraySet<>();
private final Logger logger = LoggerFactory.getLogger(DiscoveryServiceRegistryImpl.class);
private Inbox inbox;
private ThingRegistry thingRegistry;
private DiscoveryServiceCallback discoveryServiceCallback = new DiscoveryServiceCallback() {
@Override
public Thing getExistingThing(ThingUID thingUID) {
ThingRegistry thingRegistryReference = thingRegistry;
if (thingRegistryReference == null) {
logger.warn("ThingRegistry not set");
return null;
}
return thingRegistryReference.get(thingUID);
}
@Override
public DiscoveryResult getExistingDiscoveryResult(ThingUID thingUID) {
Inbox inboxReference = inbox;
if (inboxReference == null) {
logger.warn("Inbox not set");
return null;
}
List<DiscoveryResult> ret = new ArrayList<>();
ret = inboxReference.stream().filter(withFlag(DiscoveryResultFlag.NEW).and(forThingUID(thingUID)))
.collect(Collectors.toList());
if (ret.size() > 0) {
return ret.get(0);
} else {
return null;
}
}
};
@Override
public boolean abortScan(ThingTypeUID thingTypeUID) throws IllegalStateException {
Set<DiscoveryService> discoveryServicesForThingType = getDiscoveryServices(thingTypeUID);
if (discoveryServicesForThingType.isEmpty()) {
logger.warn("No discovery service for thing type '{}' found!", thingTypeUID);
return false;
}
return abortScans(discoveryServicesForThingType);
}
@Override
public boolean abortScan(String bindingId) throws IllegalStateException {
Set<DiscoveryService> discoveryServicesForBinding = getDiscoveryServices(bindingId);
if (discoveryServicesForBinding.isEmpty()) {
logger.warn("No discovery service for binding '{}' found!", bindingId);
return false;
}
return abortScans(discoveryServicesForBinding);
}
@Override
public void addDiscoveryListener(DiscoveryListener listener) throws IllegalStateException {
synchronized (cachedResults) {
Set<Entry<DiscoveryService, DiscoveryResult>> entries = cachedResults.entries();
for (Entry<DiscoveryService, DiscoveryResult> entry : entries) {
listener.thingDiscovered(entry.getKey(), entry.getValue());
}
}
if (listener != null) {
this.listeners.add(listener);
}
}
@Override
public boolean startScan(ThingTypeUID thingTypeUID, ScanListener listener) throws IllegalStateException {
Set<DiscoveryService> discoveryServicesForThingType = getDiscoveryServices(thingTypeUID);
if (discoveryServicesForThingType.isEmpty()) {
logger.warn("No discovery service for thing type '{}' found!", thingTypeUID);
return false;
}
return startScans(discoveryServicesForThingType, listener);
}
@Override
public boolean startScan(String bindingId, final ScanListener listener) throws IllegalStateException {
final Set<DiscoveryService> discoveryServicesForBinding = getDiscoveryServices(bindingId);
if (discoveryServicesForBinding.isEmpty()) {
logger.warn("No discovery service for binding id '{}' found!", bindingId);
return false;
}
return startScans(discoveryServicesForBinding, listener);
}
@Override
public boolean supportsDiscovery(ThingTypeUID thingTypeUID) {
return !getDiscoveryServices(thingTypeUID).isEmpty();
}
@Override
public boolean supportsDiscovery(String bindingId) {
return !getDiscoveryServices(bindingId).isEmpty();
}
@Override
public List<ThingTypeUID> getSupportedThingTypes() {
List<ThingTypeUID> thingTypeUIDs = new ArrayList<>();
for (DiscoveryService discoveryService : this.discoveryServices) {
thingTypeUIDs.addAll(discoveryService.getSupportedThingTypes());
}
return thingTypeUIDs;
}
@Override
public List<String> getSupportedBindings() {
List<String> bindings = new ArrayList<>();
for (DiscoveryService discoveryService : this.discoveryServices) {
Collection<ThingTypeUID> supportedThingTypes = discoveryService.getSupportedThingTypes();
for (ThingTypeUID thingTypeUID : supportedThingTypes) {
bindings.add(thingTypeUID.getBindingId());
}
}
return bindings;
}
@Override
public synchronized void removeDiscoveryListener(DiscoveryListener listener) throws IllegalStateException {
if (listener != null) {
this.listeners.remove(listener);
}
}
@Override
public synchronized void thingDiscovered(final DiscoveryService source, final DiscoveryResult result) {
synchronized (cachedResults) {
cachedResults.remove(source, result);
cachedResults.put(source, result);
}
for (final DiscoveryListener listener : this.listeners) {
try {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
listener.thingDiscovered(source, result);
return null;
}
});
} catch (Exception ex) {
logger.error("Cannot notify the DiscoveryListener {} on Thing discovered event!",
listener.getClass().getName(), ex);
}
}
}
@Override
public synchronized void thingRemoved(final DiscoveryService source, final ThingUID thingUID) {
synchronized (cachedResults) {
Iterator<DiscoveryResult> it = cachedResults.get(source).iterator();
while (it.hasNext()) {
if (it.next().getThingUID().equals(thingUID)) {
it.remove();
}
}
}
for (final DiscoveryListener listener : this.listeners) {
try {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
listener.thingRemoved(source, thingUID);
return null;
}
});
} catch (Exception ex) {
logger.error("Cannot notify the DiscoveryListener '{}' on Thing removed event!",
listener.getClass().getName(), ex);
}
}
}
@Override
public Collection<ThingUID> removeOlderResults(final DiscoveryService source, final long timestamp,
final Collection<ThingTypeUID> thingTypeUIDs) {
HashSet<ThingUID> removedResults = new HashSet<>();
for (final DiscoveryListener listener : this.listeners) {
try {
Collection<ThingUID> olderResults = AccessController
.doPrivileged(new PrivilegedAction<Collection<ThingUID>>() {
@Override
public Collection<ThingUID> run() {
return listener.removeOlderResults(source, timestamp, thingTypeUIDs);
}
});
if (olderResults != null) {
removedResults.addAll(olderResults);
}
} catch (Exception ex) {
logger.error("Cannot notify the DiscoveryListener '{}' on all things removed event!",
listener.getClass().getName(), ex);
}
}
return removedResults;
}
private boolean abortScans(Set<DiscoveryService> discoveryServices) {
boolean allServicesAborted = true;
for (DiscoveryService discoveryService : discoveryServices) {
Collection<ThingTypeUID> supportedThingTypes = discoveryService.getSupportedThingTypes();
try {
logger.debug("Abort scan for thing types '{}' on '{}'...", supportedThingTypes,
discoveryService.getClass().getName());
discoveryService.abortScan();
logger.debug("Scan for thing types '{}' aborted on '{}'.", supportedThingTypes,
discoveryService.getClass().getName());
} catch (Exception ex) {
logger.error("Cannot abort scan for thing types '{}' on '{}'!", supportedThingTypes,
discoveryService.getClass().getName(), ex);
allServicesAborted = false;
}
}
return allServicesAborted;
}
private boolean startScans(Set<DiscoveryService> discoveryServices, ScanListener listener) {
boolean atLeastOneDiscoveryServiceHasBeenStarted = false;
if (discoveryServices.size() > 1) {
logger.debug("Trying to start {} scans with an aggregating listener.", discoveryServices.size());
AggregatingScanListener aggregatingScanListener = new AggregatingScanListener(discoveryServices.size(),
listener);
for (DiscoveryService discoveryService : discoveryServices) {
if (startScan(discoveryService, aggregatingScanListener)) {
atLeastOneDiscoveryServiceHasBeenStarted = true;
} else {
logger.debug(
"Reducing number of discovery services in aggregating listener, because discovery service failed to start scan.");
aggregatingScanListener.reduceNumberOfDiscoveryServices();
}
}
} else {
if (startScan(discoveryServices.iterator().next(), listener)) {
atLeastOneDiscoveryServiceHasBeenStarted = true;
}
}
return atLeastOneDiscoveryServiceHasBeenStarted;
}
private boolean startScan(DiscoveryService discoveryService, ScanListener listener) {
Collection<ThingTypeUID> supportedThingTypes = discoveryService.getSupportedThingTypes();
try {
logger.debug("Triggering scan for thing types '{}' on '{}'...", supportedThingTypes,
discoveryService.getClass().getSimpleName());
discoveryService.startScan(listener);
return true;
} catch (Exception ex) {
logger.error("Cannot trigger scan for thing types '{}' on '{}'!", supportedThingTypes,
discoveryService.getClass().getSimpleName(), ex);
return false;
}
}
private synchronized Set<DiscoveryService> getDiscoveryServices(ThingTypeUID thingTypeUID)
throws IllegalStateException {
Set<DiscoveryService> discoveryServices = new HashSet<>();
if (thingTypeUID != null) {
for (DiscoveryService discoveryService : this.discoveryServices) {
Collection<ThingTypeUID> discoveryThingTypes = discoveryService.getSupportedThingTypes();
if (discoveryThingTypes.contains(thingTypeUID)) {
discoveryServices.add(discoveryService);
}
}
}
return discoveryServices;
}
private synchronized Set<DiscoveryService> getDiscoveryServices(String bindingId) throws IllegalStateException {
Set<DiscoveryService> discoveryServices = new HashSet<>();
for (DiscoveryService discoveryService : this.discoveryServices) {
Collection<ThingTypeUID> discoveryThingTypes = discoveryService.getSupportedThingTypes();
for (ThingTypeUID thingTypeUID : discoveryThingTypes) {
if (thingTypeUID.getBindingId().equals(bindingId)) {
discoveryServices.add(discoveryService);
}
}
}
return discoveryServices;
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addDiscoveryService(final DiscoveryService discoveryService) {
discoveryService.addDiscoveryListener(this);
if (discoveryService instanceof ExtendedDiscoveryService) {
SafeMethodCaller.call(new SafeMethodCaller.Action<Void>() {
@Override
public Void call() throws Exception {
((ExtendedDiscoveryService) discoveryService).setDiscoveryServiceCallback(discoveryServiceCallback);
return null;
}
});
}
this.discoveryServices.add(discoveryService);
}
protected void removeDiscoveryService(DiscoveryService discoveryService) {
this.discoveryServices.remove(discoveryService);
discoveryService.removeDiscoveryListener(this);
synchronized (cachedResults) {
this.cachedResults.removeAll(discoveryService);
}
}
protected void deactivate() {
this.discoveryServices.clear();
this.listeners.clear();
this.cachedResults.clear();
}
private int getMaxScanTimeout(Set<DiscoveryService> discoveryServices) {
int result = 0;
for (DiscoveryService discoveryService : discoveryServices) {
if (discoveryService.getScanTimeout() > result) {
result = discoveryService.getScanTimeout();
}
}
return result;
}
@Override
public int getMaxScanTimeout(ThingTypeUID thingTypeUID) {
return getMaxScanTimeout(getDiscoveryServices(thingTypeUID));
}
@Override
public int getMaxScanTimeout(String bindingId) {
return getMaxScanTimeout(getDiscoveryServices(bindingId));
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
protected void setInbox(Inbox inbox) {
this.inbox = inbox;
}
protected void unsetInbox(Inbox inbox) {
this.inbox = null;
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
protected void setThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}
protected void unsetThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = null;
}
}