]> git.basschouten.com Git - openhab-addons.git/blob
fbc3d40e9570e8d055eaffe1b62776197d24738d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.unifi.internal.api.cache;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.function.Function;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
26 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
27 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTuple;
28 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
29 import org.openhab.binding.unifi.internal.api.dto.UniFiSwitchPorts;
30 import org.openhab.binding.unifi.internal.api.dto.UniFiWlan;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Class to manager cache for the controller keeping track of all specific cache objects.
36  *
37  * @author Matthew Bowman - Initial contribution
38  * @author Hilbrand Bouwkamp - Moved cache to this dedicated class.
39  */
40 @NonNullByDefault
41 public class UniFiControllerCache {
42
43     private final Logger logger = LoggerFactory.getLogger(UniFiControllerCache.class);
44
45     private final UniFiSiteCache sitesCache = new UniFiSiteCache();
46     private final UniFiWlanCache wlansCache = new UniFiWlanCache();
47     private final UniFiDeviceCache devicesCache = new UniFiDeviceCache();
48     private final UniFiClientCache clientsCache = new UniFiClientCache();
49     private final UniFiClientCache insightsCache = new UniFiClientCache();
50     private final Map<String, UniFiSwitchPorts> devicesToPortTables = new ConcurrentHashMap<>();
51
52     public void clear() {
53         sitesCache.clear();
54         wlansCache.clear();
55         devicesCache.clear();
56         clientsCache.clear();
57         insightsCache.clear();
58     }
59
60     // Sites Cache
61
62     public List<UniFiSite> setSites(final UniFiSite @Nullable [] sites) {
63         sitesCache.putAll(sites);
64         return List.of(sites);
65     }
66
67     public @Nullable UniFiSite getSite(final @Nullable String id) {
68         return sitesCache.get(id);
69     }
70
71     public Collection<UniFiSite> getSites() {
72         return sitesCache.values();
73     }
74
75     // Wlans Cache
76
77     public void putWlans(final UniFiWlan @Nullable [] wlans) {
78         wlansCache.putAll(wlans);
79     }
80
81     public @Nullable UniFiWlan getWlan(@Nullable final String id) {
82         return wlansCache.get(id);
83     }
84
85     public Collection<UniFiWlan> getWlans() {
86         return wlansCache.values();
87     }
88
89     // Devices Cache
90
91     public void putDevices(final UniFiDevice @Nullable [] devices) {
92         devicesCache.putAll(devices);
93         if (devices != null) {
94             Stream.of(devices).filter(Objects::nonNull).forEach(d -> {
95                 Stream.ofNullable(d.getPortTable()).forEach(pt -> {
96                     final UniFiSwitchPorts switchPorts = devicesToPortTables.computeIfAbsent(d.getMac(),
97                             p -> new UniFiSwitchPorts());
98
99                     Stream.of(pt).forEach(p -> {
100                         @SuppressWarnings("null")
101                         final UniFiPortTuple tuple = switchPorts.computeIfAbsent(p.getPortIdx());
102
103                         tuple.setDevice(d);
104                         tuple.setTable(p);
105                     });
106                 });
107                 Stream.ofNullable(d.getPortOverrides()).forEach(po -> {
108                     final UniFiSwitchPorts tupleTable = devicesToPortTables.get(d.getMac());
109
110                     if (tupleTable != null) {
111                         Stream.of(po).forEach(p -> tupleTable.setOverride(p));
112                     }
113                 });
114             });
115         }
116     }
117
118     public @Nullable UniFiDevice getDevice(@Nullable final String id) {
119         return devicesCache.get(id);
120     }
121
122     public UniFiSwitchPorts getSwitchPorts(@Nullable final String deviceId) {
123         return deviceId == null ? new UniFiSwitchPorts()
124                 : devicesToPortTables.getOrDefault(deviceId, new UniFiSwitchPorts());
125     }
126
127     public Collection<UniFiSwitchPorts> getSwitchPorts() {
128         return devicesToPortTables.values();
129     }
130
131     // Clients Cache
132
133     public void putClients(final UniFiClient @Nullable [] clients) {
134         clientsCache.putAll(clients);
135     }
136
137     public Collection<UniFiClient> getClients() {
138         return clientsCache.values();
139     }
140
141     public long countClients(final UniFiSite site, final Function<UniFiClient, Boolean> filter) {
142         return getClients().stream().filter(c -> site.isSite(c.getSite())).filter(filter::apply).count();
143     }
144
145     public @Nullable UniFiClient getClient(@Nullable final String cid) {
146         UniFiClient client = null;
147         if (cid != null && !cid.isBlank()) {
148             synchronized (this) {
149                 // mgb: first check active clients and fallback to insights if not found
150                 client = clientsCache.get(cid);
151                 if (client == null) {
152                     final String id = clientsCache.getId(cid);
153
154                     client = insightsCache.get(id == null ? cid : id);
155                 }
156             }
157             if (client == null) {
158                 logger.debug("Could not find a matching client for cid = {}", cid);
159             }
160         }
161         return client;
162     }
163
164     public synchronized Stream<UniFiClient> getClientStreamForSite(final UniFiSite site) {
165         return clientsCache.values().stream().filter(client -> client.getSite().equals(site));
166     }
167
168     // Insights Cache
169
170     public void putInsights(final UniFiClient @Nullable [] insights) {
171         insightsCache.putAll(insights);
172     }
173 }