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.UniFiPortTuple;
30 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
31 import org.openhab.binding.unifi.internal.api.dto.UniFiWlan;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Class to manager cache for the controller keeping track of all specific cache objects.
38 * @author Matthew Bowman - Initial contribution
39 * @author Hilbrand Bouwkamp - Moved cache to this dedicated class.
42 public class UniFiControllerCache {
44 private final Logger logger = LoggerFactory.getLogger(UniFiControllerCache.class);
46 private final UniFiSiteCache sitesCache = new UniFiSiteCache();
47 private final UniFiWlanCache wlansCache = new UniFiWlanCache();
48 private final UniFiDeviceCache devicesCache = new UniFiDeviceCache();
49 private final UniFiClientCache clientsCache = new UniFiClientCache();
50 private final UniFiClientCache insightsCache = new UniFiClientCache();
51 private final Map<String, Map<Integer, UniFiPortTuple>> devicesToPortTables = new ConcurrentHashMap<>();
58 insightsCache.clear();
63 public List<UniFiSite> setSites(final UniFiSite @Nullable [] sites) {
64 sitesCache.putAll(sites);
65 return List.of(sites);
68 public @Nullable UniFiSite getSite(final @Nullable String id) {
69 return sitesCache.get(id);
72 public Collection<UniFiSite> getSites() {
73 return sitesCache.values();
78 public void putWlans(final UniFiWlan @Nullable [] wlans) {
79 wlansCache.putAll(wlans);
82 public @Nullable UniFiWlan getWlan(@Nullable final String id) {
83 return wlansCache.get(id);
86 public Collection<UniFiWlan> getWlans() {
87 return wlansCache.values();
92 public void putDevices(final UniFiDevice @Nullable [] devices) {
93 devicesCache.putAll(devices);
94 if (devices != null) {
95 Stream.of(devices).filter(Objects::nonNull).forEach(d -> {
96 Stream.ofNullable(d.getPortTable()).filter(ptl -> ptl.length > 0 && ptl[0].isPortPoe()).forEach(pt -> {
97 final Map<Integer, UniFiPortTuple> tupleTable = devicesToPortTables.computeIfAbsent(d.getMac(),
98 p -> new HashMap<>());
100 Stream.of(pt).forEach(p -> {
101 final UniFiPortTuple tuple = tupleTable.computeIfAbsent(p.getPortIdx(),
102 t -> new UniFiPortTuple());
108 Stream.ofNullable(d.getPortOverrides()).filter(ptl -> ptl.length > 0).forEach(po -> {
109 final Map<Integer, UniFiPortTuple> tupleTable = devicesToPortTables.get(d.getMac());
111 if (tupleTable != null) {
112 Stream.of(po).filter(pof -> !pof.getAsJsonObject().entrySet().isEmpty())
113 .map(UnfiPortOverrideJsonElement::new)
114 .forEach(p -> tupleTable.get(p.getPortIdx()).setJsonElement(p));
121 public @Nullable UniFiDevice getDevice(@Nullable final String id) {
122 return devicesCache.get(id);
125 public Map<Integer, UniFiPortTuple> getSwitchPorts(@Nullable final String deviceId) {
126 return deviceId == null ? Map.of() : devicesToPortTables.getOrDefault(deviceId, Map.of());
129 public Collection<Map<Integer, UniFiPortTuple>> getSwitchPorts() {
130 return devicesToPortTables.values();
135 public void putClients(final UniFiClient @Nullable [] clients) {
136 clientsCache.putAll(clients);
139 public Collection<UniFiClient> getClients() {
140 return clientsCache.values();
143 public long countClients(final UniFiSite site, final Function<UniFiClient, Boolean> filter) {
144 return getClients().stream().filter(c -> site.isSite(c.getSite())).filter(filter::apply).count();
147 public @Nullable UniFiClient getClient(@Nullable final String cid) {
148 UniFiClient client = null;
149 if (cid != null && !cid.isBlank()) {
150 synchronized (this) {
151 // mgb: first check active clients and fallback to insights if not found
152 client = clientsCache.get(cid);
153 if (client == null) {
154 final String id = clientsCache.getId(cid);
156 client = insightsCache.get(id == null ? cid : id);
159 if (client == null) {
160 logger.debug("Could not find a matching client for cid = {}", cid);
166 public synchronized Stream<UniFiClient> getClientStreamForSite(final UniFiSite site) {
167 return clientsCache.values().stream().filter(client -> client.getSite().equals(site));
172 public void putInsights(final UniFiClient @Nullable [] insights) {
173 insightsCache.putAll(insights);