2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.unifi.internal.api.cache;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Objects;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.function.Function;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.unifi.internal.api.dto.UnfiPortOverrideJsonElement;
27 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
28 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
29 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTable;
30 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTuple;
31 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
32 import org.openhab.binding.unifi.internal.api.dto.UniFiWlan;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class to manager cache for the controller keeping track of all specific cache objects.
39 * @author Matthew Bowman - Initial contribution
40 * @author Hilbrand Bouwkamp - Moved cache to this dedicated class.
43 public class UniFiControllerCache {
45 private final Logger logger = LoggerFactory.getLogger(UniFiControllerCache.class);
47 private final UniFiSiteCache sitesCache = new UniFiSiteCache();
48 private final UniFiWlanCache wlansCache = new UniFiWlanCache();
49 private final UniFiDeviceCache devicesCache = new UniFiDeviceCache();
50 private final UniFiClientCache clientsCache = new UniFiClientCache();
51 private final UniFiClientCache insightsCache = new UniFiClientCache();
52 private final Map<String, Map<Integer, UniFiPortTuple>> devicesToPortTables = new ConcurrentHashMap<>();
59 insightsCache.clear();
64 public List<UniFiSite> setSites(final UniFiSite @Nullable [] sites) {
65 sitesCache.putAll(sites);
66 return List.of(sites);
69 public @Nullable UniFiSite getSite(final @Nullable String id) {
70 return sitesCache.get(id);
73 public Collection<UniFiSite> getSites() {
74 return sitesCache.values();
79 public void putWlans(final UniFiWlan @Nullable [] wlans) {
80 wlansCache.putAll(wlans);
83 public @Nullable UniFiWlan getWlan(@Nullable final String id) {
84 return wlansCache.get(id);
87 public Collection<UniFiWlan> getWlans() {
88 return wlansCache.values();
93 public void putDevices(final UniFiDevice @Nullable [] devices) {
94 devicesCache.putAll(devices);
95 if (devices != null) {
96 Stream.of(devices).filter(Objects::nonNull).forEach(d -> {
97 Stream.ofNullable(d.getPortTable()).flatMap(pt -> Stream.of(pt)).filter(UniFiPortTable::isPortPoe)
99 final Map<Integer, UniFiPortTuple> tupleTable = devicesToPortTables
100 .computeIfAbsent(d.getMac(), tt -> new HashMap<>());
101 final UniFiPortTuple tuple = tupleTable.computeIfAbsent(p.getPortIdx(),
102 t -> new UniFiPortTuple());
107 Stream.ofNullable(d.getPortOverrides()).forEach(po -> {
108 final Map<Integer, UniFiPortTuple> tupleTable = devicesToPortTables.get(d.getMac());
110 if (tupleTable != null) {
111 Stream.of(po).filter(pof -> !pof.getAsJsonObject().entrySet().isEmpty())
112 .map(UnfiPortOverrideJsonElement::new).forEach(p -> tupleTable
113 .computeIfAbsent(p.getPortIdx(), t -> new UniFiPortTuple()).setJsonElement(p));
120 public @Nullable UniFiDevice getDevice(@Nullable final String id) {
121 return devicesCache.get(id);
124 public Map<Integer, UniFiPortTuple> getSwitchPorts(@Nullable final String deviceId) {
125 return deviceId == null ? Map.of() : devicesToPortTables.getOrDefault(deviceId, Map.of());
128 public Collection<Map<Integer, UniFiPortTuple>> getSwitchPorts() {
129 return devicesToPortTables.values();
134 public void putClients(final UniFiClient @Nullable [] clients) {
135 clientsCache.putAll(clients);
138 public Collection<UniFiClient> getClients() {
139 return clientsCache.values();
142 public long countClients(final UniFiSite site, final Function<UniFiClient, Boolean> filter) {
143 return getClients().stream().filter(c -> site.isSite(c.getSite())).filter(filter::apply).count();
146 public @Nullable UniFiClient getClient(@Nullable final String cid) {
147 UniFiClient client = null;
148 if (cid != null && !cid.isBlank()) {
149 synchronized (this) {
150 // mgb: first check active clients and fallback to insights if not found
151 client = clientsCache.get(cid);
152 if (client == null) {
153 final String id = clientsCache.getId(cid);
155 client = insightsCache.get(id == null ? cid : id);
158 if (client == null) {
159 logger.debug("Could not find a matching client for cid = {}", cid);
165 public synchronized Stream<UniFiClient> getClientStreamForSite(final UniFiSite site) {
166 return clientsCache.values().stream().filter(client -> client.getSite().equals(site));
171 public void putInsights(final UniFiClient @Nullable [] insights) {
172 insightsCache.putAll(insights);